|
| 1 | +#![allow(missing_docs)] |
| 2 | + |
| 3 | +use assert_json_diff::assert_json_eq; |
| 4 | +use kube::{CustomResource, CustomResourceExt}; |
| 5 | +use schemars::JsonSchema; |
| 6 | +use serde::{Deserialize, Serialize}; |
| 7 | +use serde_json::json; |
| 8 | + |
| 9 | +// Enum definitions |
| 10 | + |
| 11 | +/// A very simple enum with unit variants |
| 12 | +#[derive(Serialize, Deserialize, Debug, Clone, JsonSchema)] |
| 13 | +enum NormalEnum { |
| 14 | + /// First variant |
| 15 | + A, |
| 16 | + /// Second variant |
| 17 | + B, |
| 18 | + |
| 19 | + // No doc-comments on these variants |
| 20 | + C, |
| 21 | + D, |
| 22 | +} |
| 23 | + |
| 24 | +#[derive(Serialize, Deserialize, Debug, Clone, JsonSchema)] |
| 25 | +pub enum NormalEnumWithoutDescriptions { |
| 26 | + A, |
| 27 | + B, |
| 28 | + C, |
| 29 | + D, |
| 30 | +} |
| 31 | + |
| 32 | +/// A complex enum with tuple and struct variants |
| 33 | +#[derive(Serialize, Deserialize, Debug, Clone, JsonSchema)] |
| 34 | +enum ComplexEnum { |
| 35 | + /// Override documentation on the Normal variant |
| 36 | + Normal(NormalEnum), |
| 37 | + |
| 38 | + /// Documentation on the Hardcore variant |
| 39 | + Hardcore { |
| 40 | + hard: String, |
| 41 | + core: NormalEnum, |
| 42 | + without_description: NormalEnumWithoutDescriptions, |
| 43 | + }, |
| 44 | +} |
| 45 | + |
| 46 | +// CRD definitions |
| 47 | + |
| 48 | +#[derive(CustomResource, Serialize, Deserialize, Debug, Clone, JsonSchema)] |
| 49 | +#[kube(group = "clux.dev", version = "v1", kind = "NormalEnumTest")] |
| 50 | +struct NormalEnumTestSpec { |
| 51 | + foo: NormalEnum, |
| 52 | +} |
| 53 | + |
| 54 | +#[derive(CustomResource, Serialize, Deserialize, Debug, Clone, JsonSchema)] |
| 55 | +#[kube(group = "clux.dev", version = "v1", kind = "OptionalEnumTest")] |
| 56 | +struct OptionalEnumTestSpec { |
| 57 | + foo: Option<NormalEnum>, |
| 58 | +} |
| 59 | + |
| 60 | +#[derive(CustomResource, Serialize, Deserialize, Debug, Clone, JsonSchema)] |
| 61 | +#[kube( |
| 62 | + group = "clux.dev", |
| 63 | + version = "v1", |
| 64 | + kind = "NormalEnumWithoutDescriptionsTest" |
| 65 | +)] |
| 66 | +struct NormalEnumWithoutDescriptionsTestSpec { |
| 67 | + foo: NormalEnumWithoutDescriptions, |
| 68 | +} |
| 69 | + |
| 70 | +#[derive(CustomResource, Serialize, Deserialize, Debug, Clone, JsonSchema)] |
| 71 | +#[kube(group = "clux.dev", version = "v1", kind = "ComplexEnumTest")] |
| 72 | +struct ComplexEnumTestSpec { |
| 73 | + foo: ComplexEnum, |
| 74 | +} |
| 75 | + |
| 76 | +#[test] |
| 77 | +fn complex_enum() { |
| 78 | + assert_json_eq!( |
| 79 | + ComplexEnumTest::crd(), |
| 80 | + json!( |
| 81 | + { |
| 82 | + "apiVersion": "apiextensions.k8s.io/v1", |
| 83 | + "kind": "CustomResourceDefinition", |
| 84 | + "metadata": { |
| 85 | + "name": "complexenumtests.clux.dev" |
| 86 | + }, |
| 87 | + "spec": { |
| 88 | + "group": "clux.dev", |
| 89 | + "names": { |
| 90 | + "categories": [], |
| 91 | + "kind": "ComplexEnumTest", |
| 92 | + "plural": "complexenumtests", |
| 93 | + "shortNames": [], |
| 94 | + "singular": "complexenumtest" |
| 95 | + }, |
| 96 | + "scope": "Cluster", |
| 97 | + "versions": [ |
| 98 | + { |
| 99 | + "additionalPrinterColumns": [], |
| 100 | + "name": "v1", |
| 101 | + "schema": { |
| 102 | + "openAPIV3Schema": { |
| 103 | + "description": "Auto-generated derived type for ComplexEnumTestSpec via `CustomResource`", |
| 104 | + "properties": { |
| 105 | + "spec": { |
| 106 | + "properties": { |
| 107 | + "foo": { |
| 108 | + "description": "A complex enum with tuple and struct variants", |
| 109 | + "oneOf": [ |
| 110 | + { |
| 111 | + "required": [ |
| 112 | + "Normal" |
| 113 | + ] |
| 114 | + }, |
| 115 | + { |
| 116 | + "required": [ |
| 117 | + "Hardcore" |
| 118 | + ] |
| 119 | + } |
| 120 | + ], |
| 121 | + "properties": { |
| 122 | + "Hardcore": { |
| 123 | + "description": "Documentation on the Hardcore variant", |
| 124 | + "properties": { |
| 125 | + "core": { |
| 126 | + "description": "A very simple enum with unit variants", |
| 127 | + "enum": [ |
| 128 | + "C", |
| 129 | + "D", |
| 130 | + "A", |
| 131 | + "B" |
| 132 | + ], |
| 133 | + "type": "string" |
| 134 | + }, |
| 135 | + "hard": { |
| 136 | + "type": "string" |
| 137 | + }, |
| 138 | + "without_description": { |
| 139 | + "enum": [ |
| 140 | + "A", |
| 141 | + "B", |
| 142 | + "C", |
| 143 | + "D" |
| 144 | + ], |
| 145 | + "type": "string" |
| 146 | + } |
| 147 | + }, |
| 148 | + "required": [ |
| 149 | + "core", |
| 150 | + "hard", |
| 151 | + "without_description" |
| 152 | + ], |
| 153 | + "type": "object" |
| 154 | + }, |
| 155 | + "Normal": { |
| 156 | + "description": "Override documentation on the Normal variant", |
| 157 | + "enum": [ |
| 158 | + "C", |
| 159 | + "D", |
| 160 | + "A", |
| 161 | + "B" |
| 162 | + ], |
| 163 | + "type": "string" |
| 164 | + } |
| 165 | + }, |
| 166 | + "type": "object" |
| 167 | + } |
| 168 | + }, |
| 169 | + "required": [ |
| 170 | + "foo" |
| 171 | + ], |
| 172 | + "type": "object" |
| 173 | + } |
| 174 | + }, |
| 175 | + "required": [ |
| 176 | + "spec" |
| 177 | + ], |
| 178 | + "title": "ComplexEnumTest", |
| 179 | + "type": "object" |
| 180 | + } |
| 181 | + }, |
| 182 | + "served": true, |
| 183 | + "storage": true, |
| 184 | + "subresources": {} |
| 185 | + } |
| 186 | + ] |
| 187 | + } |
| 188 | + } |
| 189 | + ) |
| 190 | + ); |
| 191 | +} |
| 192 | + |
| 193 | +#[test] |
| 194 | +fn normal_enum() { |
| 195 | + assert_json_eq!( |
| 196 | + NormalEnumTest::crd(), |
| 197 | + json!( |
| 198 | + { |
| 199 | + "apiVersion": "apiextensions.k8s.io/v1", |
| 200 | + "kind": "CustomResourceDefinition", |
| 201 | + "metadata": { |
| 202 | + "name": "normalenumtests.clux.dev" |
| 203 | + }, |
| 204 | + "spec": { |
| 205 | + "group": "clux.dev", |
| 206 | + "names": { |
| 207 | + "categories": [], |
| 208 | + "kind": "NormalEnumTest", |
| 209 | + "plural": "normalenumtests", |
| 210 | + "shortNames": [], |
| 211 | + "singular": "normalenumtest" |
| 212 | + }, |
| 213 | + "scope": "Cluster", |
| 214 | + "versions": [ |
| 215 | + { |
| 216 | + "additionalPrinterColumns": [], |
| 217 | + "name": "v1", |
| 218 | + "schema": { |
| 219 | + "openAPIV3Schema": { |
| 220 | + "description": "Auto-generated derived type for NormalEnumTestSpec via `CustomResource`", |
| 221 | + "properties": { |
| 222 | + "spec": { |
| 223 | + "properties": { |
| 224 | + "foo": { |
| 225 | + "description": "A very simple enum with unit variants", |
| 226 | + "enum": [ |
| 227 | + "C", |
| 228 | + "D", |
| 229 | + "A", |
| 230 | + "B" |
| 231 | + ], |
| 232 | + "type": "string" |
| 233 | + } |
| 234 | + }, |
| 235 | + "required": [ |
| 236 | + "foo" |
| 237 | + ], |
| 238 | + "type": "object" |
| 239 | + } |
| 240 | + }, |
| 241 | + "required": [ |
| 242 | + "spec" |
| 243 | + ], |
| 244 | + "title": "NormalEnumTest", |
| 245 | + "type": "object" |
| 246 | + } |
| 247 | + }, |
| 248 | + "served": true, |
| 249 | + "storage": true, |
| 250 | + "subresources": {} |
| 251 | + } |
| 252 | + ] |
| 253 | + } |
| 254 | + } |
| 255 | + ) |
| 256 | + ); |
| 257 | +} |
| 258 | + |
| 259 | +#[test] |
| 260 | +fn optional_enum() { |
| 261 | + assert_json_eq!( |
| 262 | + OptionalEnumTest::crd(), |
| 263 | + json!( |
| 264 | + { |
| 265 | + "apiVersion": "apiextensions.k8s.io/v1", |
| 266 | + "kind": "CustomResourceDefinition", |
| 267 | + "metadata": { |
| 268 | + "name": "optionalenumtests.clux.dev" |
| 269 | + }, |
| 270 | + "spec": { |
| 271 | + "group": "clux.dev", |
| 272 | + "names": { |
| 273 | + "categories": [], |
| 274 | + "kind": "OptionalEnumTest", |
| 275 | + "plural": "optionalenumtests", |
| 276 | + "shortNames": [], |
| 277 | + "singular": "optionalenumtest" |
| 278 | + }, |
| 279 | + "scope": "Cluster", |
| 280 | + "versions": [ |
| 281 | + { |
| 282 | + "additionalPrinterColumns": [], |
| 283 | + "name": "v1", |
| 284 | + "schema": { |
| 285 | + "openAPIV3Schema": { |
| 286 | + "description": "Auto-generated derived type for OptionalEnumTestSpec via `CustomResource`", |
| 287 | + "properties": { |
| 288 | + "spec": { |
| 289 | + "properties": { |
| 290 | + "foo": { |
| 291 | + "description": "A very simple enum with unit variants", |
| 292 | + "enum": [ |
| 293 | + "C", |
| 294 | + "D", |
| 295 | + "A", |
| 296 | + "B" |
| 297 | + ], |
| 298 | + "nullable": true, |
| 299 | + "type": "string" |
| 300 | + } |
| 301 | + }, |
| 302 | + "type": "object" |
| 303 | + } |
| 304 | + }, |
| 305 | + "required": [ |
| 306 | + "spec" |
| 307 | + ], |
| 308 | + "title": "OptionalEnumTest", |
| 309 | + "type": "object" |
| 310 | + } |
| 311 | + }, |
| 312 | + "served": true, |
| 313 | + "storage": true, |
| 314 | + "subresources": {} |
| 315 | + } |
| 316 | + ] |
| 317 | + } |
| 318 | + } |
| 319 | + ) |
| 320 | + ); |
| 321 | +} |
| 322 | + |
| 323 | + |
| 324 | +#[test] |
| 325 | +fn normal_enum_without_descriptions() { |
| 326 | + assert_json_eq!( |
| 327 | + NormalEnumWithoutDescriptionsTest::crd(), |
| 328 | + json!( |
| 329 | + { |
| 330 | + "apiVersion": "apiextensions.k8s.io/v1", |
| 331 | + "kind": "CustomResourceDefinition", |
| 332 | + "metadata": { |
| 333 | + "name": "normalenumwithoutdescriptionstests.clux.dev" |
| 334 | + }, |
| 335 | + "spec": { |
| 336 | + "group": "clux.dev", |
| 337 | + "names": { |
| 338 | + "categories": [], |
| 339 | + "kind": "NormalEnumWithoutDescriptionsTest", |
| 340 | + "plural": "normalenumwithoutdescriptionstests", |
| 341 | + "shortNames": [], |
| 342 | + "singular": "normalenumwithoutdescriptionstest" |
| 343 | + }, |
| 344 | + "scope": "Cluster", |
| 345 | + "versions": [ |
| 346 | + { |
| 347 | + "additionalPrinterColumns": [], |
| 348 | + "name": "v1", |
| 349 | + "schema": { |
| 350 | + "openAPIV3Schema": { |
| 351 | + "description": "Auto-generated derived type for NormalEnumWithoutDescriptionsTestSpec via `CustomResource`", |
| 352 | + "properties": { |
| 353 | + "spec": { |
| 354 | + "properties": { |
| 355 | + "foo": { |
| 356 | + "enum": [ |
| 357 | + "A", |
| 358 | + "B", |
| 359 | + "C", |
| 360 | + "D" |
| 361 | + ], |
| 362 | + "type": "string" |
| 363 | + } |
| 364 | + }, |
| 365 | + "required": [ |
| 366 | + "foo" |
| 367 | + ], |
| 368 | + "type": "object" |
| 369 | + } |
| 370 | + }, |
| 371 | + "required": [ |
| 372 | + "spec" |
| 373 | + ], |
| 374 | + "title": "NormalEnumWithoutDescriptionsTest", |
| 375 | + "type": "object" |
| 376 | + } |
| 377 | + }, |
| 378 | + "served": true, |
| 379 | + "storage": true, |
| 380 | + "subresources": {} |
| 381 | + } |
| 382 | + ] |
| 383 | + } |
| 384 | + } |
| 385 | + ) |
| 386 | + ); |
| 387 | +} |
0 commit comments