Skip to content

Commit 2055029

Browse files
committed
test: Rework tests that check that the CRDs are valid
1 parent 75c7ffa commit 2055029

File tree

3 files changed

+74
-38
lines changed

3 files changed

+74
-38
lines changed

kube-derive/Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,3 +36,4 @@ trybuild.workspace = true
3636
assert-json-diff.workspace = true
3737
runtime-macros.workspace = true
3838
prettyplease.workspace = true
39+
tokio = { workspace = true, features = ["rt", "macros"] }

kube-derive/tests/crd_complex_enum_tests.rs

Lines changed: 42 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,14 @@
11
#![allow(missing_docs)]
2+
use std::time::Duration;
3+
24
use assert_json_diff::assert_json_eq;
3-
use kube::{CustomResource, CustomResourceExt};
5+
use k8s_openapi::apiextensions_apiserver::pkg::apis::apiextensions::v1::{
6+
CustomResourceConversion, CustomResourceDefinition,
7+
};
8+
use kube::{
9+
api::{DeleteParams, PostParams},
10+
Api, Client, CustomResource, CustomResourceExt, ResourceExt,
11+
};
412
use schemars::JsonSchema;
513
use serde::{Deserialize, Serialize};
614
use serde_json::json;
@@ -125,44 +133,41 @@ struct FlattenedUntaggedEnumTestSpec {
125133
foo: FlattenedUntaggedEnum,
126134
}
127135

128-
/// Use `cargo test --package kube-derive print_crds -- --nocapture` to get the CRDs as YAML.
129-
/// Afterwards you can use `kubectl apply -f -` to see if they are valid CRDs.
130-
#[test]
131-
fn print_crds() {
132-
println!("{}", serde_yaml::to_string(&NormalEnumTest::crd()).unwrap());
133-
println!("---");
134-
println!("{}", serde_yaml::to_string(&OptionalEnumTest::crd()).unwrap());
135-
println!("---");
136-
println!(
137-
"{}",
138-
serde_yaml::to_string(&NormalEnumWithoutDescriptionsTest::crd()).unwrap()
139-
);
140-
println!("---");
141-
println!(
142-
"{}",
143-
serde_yaml::to_string(&OptionalEnumWithoutDescriptionsTest::crd()).unwrap()
144-
);
145-
println!("---");
146-
println!("{}", serde_yaml::to_string(&ComplexEnumTest::crd()).unwrap());
147-
println!("---");
148-
println!(
149-
"{}",
150-
serde_yaml::to_string(&OptionalComplexEnumTest::crd()).unwrap()
151-
);
152-
println!("---");
153-
println!("{}", serde_yaml::to_string(&UntaggedEnumTest::crd()).unwrap());
154-
println!("---");
155-
println!(
156-
"{}",
157-
serde_yaml::to_string(&OptionalUntaggedEnumTest::crd()).unwrap()
158-
);
159-
println!("---");
160-
println!(
161-
"{}",
162-
serde_yaml::to_string(&FlattenedUntaggedEnumTest::crd()).unwrap()
163-
);
136+
#[tokio::test]
137+
#[ignore = "needs apiserver to validate CRDs"]
138+
async fn check_are_valid_crds() {
139+
let crds = [
140+
NormalEnumTest::crd(),
141+
OptionalEnumTest::crd(),
142+
NormalEnumWithoutDescriptionsTest::crd(),
143+
OptionalEnumWithoutDescriptionsTest::crd(),
144+
ComplexEnumTest::crd(),
145+
OptionalComplexEnumTest::crd(),
146+
UntaggedEnumTest::crd(),
147+
OptionalUntaggedEnumTest::crd(),
148+
FlattenedUntaggedEnumTest::crd(),
149+
];
150+
151+
let client = Client::try_default()
152+
.await
153+
.expect("failed to create Kubernetes client");
154+
let crd_api: Api<CustomResourceDefinition> = Api::all(client);
155+
for crd in crds {
156+
// Clean up existing CRDs. As these are only test CRDs and this test is not run by default
157+
// this is fine.
158+
let _ = crd_api.delete(&crd.name_any(), &DeleteParams::default()).await;
159+
160+
// Prevent "object is being deleted: customresourcedefinition already exists
161+
tokio::time::sleep(Duration::from_millis(100)).await;
162+
163+
crd_api
164+
.create(&PostParams::default(), &crd)
165+
.await
166+
.expect("failed to create CRD");
167+
}
164168
}
165169

170+
166171
#[test]
167172
fn normal_enum() {
168173
assert_json_eq!(

kube-derive/tests/crd_mixed_enum_test.rs

Lines changed: 31 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,12 @@
11
#![allow(missing_docs)]
2+
use std::time::Duration;
3+
24
use assert_json_diff::assert_json_eq;
3-
use kube::{CustomResource, CustomResourceExt};
5+
use k8s_openapi::apiextensions_apiserver::pkg::apis::apiextensions::v1::CustomResourceDefinition;
6+
use kube::{
7+
api::{DeleteParams, PostParams},
8+
Api, Client, CustomResource, CustomResourceExt, ResourceExt,
9+
};
410
use schemars::JsonSchema;
511
use serde::{Deserialize, Serialize};
612
use serde_json::json;
@@ -86,6 +92,30 @@ enum InvalidEnum7Spec {
8692
},
8793
}
8894

95+
#[tokio::test]
96+
#[ignore = "needs apiserver to validate CRDs"]
97+
async fn check_are_valid_crds() {
98+
let crds = [ValidEnum3::crd(), ValidEnum4::crd(), ValidEnum6::crd()];
99+
100+
let client = Client::try_default()
101+
.await
102+
.expect("failed to create Kubernetes client");
103+
let crd_api: Api<CustomResourceDefinition> = Api::all(client);
104+
for crd in crds {
105+
// Clean up existing CRDs. As these are only test CRDs and this test is not run by default
106+
// this is fine.
107+
let _ = crd_api.delete(&crd.name_any(), &DeleteParams::default()).await;
108+
109+
// Prevent "object is being deleted: customresourcedefinition already exists
110+
tokio::time::sleep(Duration::from_millis(100)).await;
111+
112+
crd_api
113+
.create(&PostParams::default(), &crd)
114+
.await
115+
.expect("failed to create CRD");
116+
}
117+
}
118+
89119
/// Use `cargo test --package kube-derive print_crds -- --nocapture` to get the CRDs as YAML.
90120
/// Afterwards you can use `kubectl apply -f -` to see if they are valid CRDs.
91121
#[test]

0 commit comments

Comments
 (0)