Skip to content

Commit 920d6aa

Browse files
committed
test: Add some tests on untagged enums duplicated fields
1 parent a5d68d8 commit 920d6aa

File tree

1 file changed

+133
-0
lines changed

1 file changed

+133
-0
lines changed

kube-derive/tests/crd_mixed_enum_test.rs

Lines changed: 133 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,49 @@ enum ValidEnum4Spec {
4343
B { inner: u32 },
4444
}
4545

46+
/// This enum is invalid, as the types of the `inner` fields differ.
47+
#[derive(CustomResource, Serialize, Deserialize, Debug, Clone, JsonSchema)]
48+
#[kube(group = "clux.dev", version = "v1", kind = "InvalidEnum5")]
49+
#[serde(untagged)]
50+
enum InvalidEnum5Spec {
51+
A { inner: String },
52+
B { inner: u32 },
53+
}
54+
55+
/// This enum is valid, as the `inner` fields are the same.
56+
#[derive(CustomResource, Serialize, Deserialize, Debug, Clone, JsonSchema)]
57+
#[kube(group = "clux.dev", version = "v1", kind = "ValidEnum6")]
58+
#[serde(untagged)]
59+
enum ValidEnum6Spec {
60+
B {
61+
/// The inner fields need to have the same schema (so also same description)
62+
inner: u32,
63+
/// An additional field
64+
additional: String,
65+
},
66+
A {
67+
/// The inner fields need to have the same schema (so also same description)
68+
inner: u32,
69+
},
70+
}
71+
72+
/// This enum is invalid, as the typed of `inner` fields are the same, *but* the description (which
73+
/// is part of the schema differs).
74+
#[derive(CustomResource, Serialize, Deserialize, Debug, Clone, JsonSchema)]
75+
#[kube(group = "clux.dev", version = "v1", kind = "InvalidEnum7")]
76+
#[serde(untagged)]
77+
enum InvalidEnum7Spec {
78+
B {
79+
/// The inner fields need to have the same schema (so also same description)
80+
inner: u32,
81+
additional: String,
82+
},
83+
A {
84+
/// This description differs!
85+
inner: u32,
86+
},
87+
}
88+
4689
/// Use `cargo test --package kube-derive print_crds -- --nocapture` to get the CRDs as YAML.
4790
/// Afterwards you can use `kubectl apply -f -` to see if they are valid CRDs.
4891
#[test]
@@ -221,6 +264,96 @@ fn valid_enum_4() {
221264
);
222265
}
223266

267+
#[test]
268+
#[should_panic = "Properties for \"inner\" are defined multiple times with different shapes"]
269+
fn invalid_enum_5() {
270+
InvalidEnum5::crd();
271+
}
272+
273+
274+
#[test]
275+
fn valid_enum_6() {
276+
assert_json_eq!(
277+
ValidEnum6::crd(),
278+
json!(
279+
{
280+
"apiVersion": "apiextensions.k8s.io/v1",
281+
"kind": "CustomResourceDefinition",
282+
"metadata": {
283+
"name": "validenum6s.clux.dev"
284+
},
285+
"spec": {
286+
"group": "clux.dev",
287+
"names": {
288+
"categories": [],
289+
"kind": "ValidEnum6",
290+
"plural": "validenum6s",
291+
"shortNames": [],
292+
"singular": "validenum6"
293+
},
294+
"scope": "Cluster",
295+
"versions": [
296+
{
297+
"additionalPrinterColumns": [],
298+
"name": "v1",
299+
"schema": {
300+
"openAPIV3Schema": {
301+
"description": "Auto-generated derived type for ValidEnum6Spec via `CustomResource`",
302+
"properties": {
303+
"spec": {
304+
"anyOf": [
305+
{
306+
"required": [
307+
"additional",
308+
"inner"
309+
]
310+
},
311+
{
312+
"required": [
313+
"inner"
314+
]
315+
}
316+
],
317+
"description": "This enum is valid, as the `inner` fields are the same.",
318+
"properties": {
319+
"additional": {
320+
"description": "An additional field",
321+
"type": "string"
322+
},
323+
"inner": {
324+
"description": "The inner fields need to have the same schema (so also same description)",
325+
"format": "uint32",
326+
"minimum": 0.0,
327+
"type": "integer"
328+
}
329+
},
330+
"type": "object"
331+
}
332+
},
333+
"required": [
334+
"spec"
335+
],
336+
"title": "ValidEnum6",
337+
"type": "object"
338+
}
339+
},
340+
"served": true,
341+
"storage": true,
342+
"subresources": {}
343+
}
344+
]
345+
}
346+
}
347+
)
348+
);
349+
}
350+
351+
#[test]
352+
#[should_panic = "Properties for \"inner\" are defined multiple times with different shapes"]
353+
fn invalid_enum_7() {
354+
InvalidEnum7::crd();
355+
}
356+
224357
#[test]
225358
#[should_panic = "oneOf variants must all have the same type"]
226359
fn struct_with_enum_1() {

0 commit comments

Comments
 (0)