|
1 | 1 | #![allow(missing_docs)] |
| 2 | +use std::time::Duration; |
| 3 | + |
2 | 4 | 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 | +}; |
4 | 12 | use schemars::JsonSchema; |
5 | 13 | use serde::{Deserialize, Serialize}; |
6 | 14 | use serde_json::json; |
@@ -125,44 +133,41 @@ struct FlattenedUntaggedEnumTestSpec { |
125 | 133 | foo: FlattenedUntaggedEnum, |
126 | 134 | } |
127 | 135 |
|
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 | + } |
164 | 168 | } |
165 | 169 |
|
| 170 | + |
166 | 171 | #[test] |
167 | 172 | fn normal_enum() { |
168 | 173 | assert_json_eq!( |
|
0 commit comments