|
| 1 | +/* |
| 2 | +Copyright 2024 The Kubernetes Authors. |
| 3 | +
|
| 4 | +Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | +you may not use this file except in compliance with the License. |
| 6 | +You may obtain a copy of the License at |
| 7 | +
|
| 8 | + http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | +
|
| 10 | +Unless required by applicable law or agreed to in writing, software |
| 11 | +distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | +See the License for the specific language governing permissions and |
| 14 | +limitations under the License. |
| 15 | +*/ |
| 16 | + |
| 17 | +package testing |
| 18 | + |
| 19 | +import ( |
| 20 | + "context" |
| 21 | + "testing" |
| 22 | + |
| 23 | + "k8s.io/apimachinery/pkg/runtime" |
| 24 | + "k8s.io/apimachinery/pkg/util/sets" |
| 25 | + "k8s.io/apimachinery/pkg/util/validation/field" |
| 26 | +) |
| 27 | + |
| 28 | +type VersionValidationRunner func(t *testing.T, gv string, versionValidationErrors field.ErrorList) |
| 29 | + |
| 30 | +// RunValidationForEachVersion runs f as a subtest of t for each version of the given unversioned object. |
| 31 | +// Each subtest is named by GroupVersionKind. Each call to f is provided the field.ErrorList results |
| 32 | +// of converting the unversioned object to a version and validating it. |
| 33 | +// |
| 34 | +// Only autogenerated validation is run. To test both handwritten and autogenerated validation: |
| 35 | +// |
| 36 | +// RunValidationForEachVersion(t, testCase.pod, func(t *testing.T, versionValidationErrors field.ErrorList) { |
| 37 | +// errs := ValidatePod(testCase.obj) // hand written validation |
| 38 | +// errs = append(errs, versionValidationErrors...) // generated declarative validation |
| 39 | +// // Validate that the errors are what was expected for this test case. |
| 40 | +// }) |
| 41 | +func RunValidationForEachVersion(t *testing.T, scheme *runtime.Scheme, options sets.Set[string], unversioned runtime.Object, fn VersionValidationRunner) { |
| 42 | + runValidation(t, scheme, options, unversioned, fn) |
| 43 | +} |
| 44 | + |
| 45 | +// RunUpdateValidationForEachVersion is like RunValidationForEachVersion but for update validation. |
| 46 | +func RunUpdateValidationForEachVersion(t *testing.T, scheme *runtime.Scheme, options sets.Set[string], unversioned, unversionedOld runtime.Object, fn VersionValidationRunner) { |
| 47 | + runUpdateValidation(t, scheme, options, unversioned, unversionedOld, fn) |
| 48 | +} |
| 49 | + |
| 50 | +// RunStatusValidationForEachVersion is like RunUpdateValidationForEachVersion but for status validation. |
| 51 | +func RunStatusValidationForEachVersion(t *testing.T, scheme *runtime.Scheme, options sets.Set[string], unversioned, unversionedOld runtime.Object, fn VersionValidationRunner) { |
| 52 | + runUpdateValidation(t, scheme, options, unversioned, unversionedOld, fn, "status") |
| 53 | +} |
| 54 | + |
| 55 | +func runValidation(t *testing.T, scheme *runtime.Scheme, options sets.Set[string], unversioned runtime.Object, fn VersionValidationRunner, subresources ...string) { |
| 56 | + unversionedGVKs, _, err := scheme.ObjectKinds(unversioned) |
| 57 | + if err != nil { |
| 58 | + t.Fatal(err) |
| 59 | + } |
| 60 | + for _, unversionedGVK := range unversionedGVKs { |
| 61 | + gvs := scheme.VersionsForGroupKind(unversionedGVK.GroupKind()) |
| 62 | + for _, gv := range gvs { |
| 63 | + gvk := gv.WithKind(unversionedGVK.Kind) |
| 64 | + t.Run(gvk.String(), func(t *testing.T) { |
| 65 | + if gvk.Version != runtime.APIVersionInternal { // skip internal |
| 66 | + versioned, err := scheme.New(gvk) |
| 67 | + if err != nil { |
| 68 | + t.Fatal(err) |
| 69 | + } |
| 70 | + err = scheme.Convert(unversioned, versioned, nil) |
| 71 | + if err != nil { |
| 72 | + t.Fatal(err) |
| 73 | + } |
| 74 | + fn(t, gv.String(), scheme.Validate(context.Background(), options, versioned, subresources...)) |
| 75 | + } |
| 76 | + }) |
| 77 | + } |
| 78 | + } |
| 79 | +} |
| 80 | + |
| 81 | +func runUpdateValidation(t *testing.T, scheme *runtime.Scheme, options sets.Set[string], unversionedNew, unversionedOld runtime.Object, fn VersionValidationRunner, subresources ...string) { |
| 82 | + unversionedGVKs, _, err := scheme.ObjectKinds(unversionedNew) |
| 83 | + if err != nil { |
| 84 | + t.Fatal(err) |
| 85 | + } |
| 86 | + for _, unversionedGVK := range unversionedGVKs { |
| 87 | + gvs := scheme.VersionsForGroupKind(unversionedGVK.GroupKind()) |
| 88 | + for _, gv := range gvs { |
| 89 | + gvk := gv.WithKind(unversionedGVK.Kind) |
| 90 | + t.Run(gvk.String(), func(t *testing.T) { |
| 91 | + if gvk.Version != runtime.APIVersionInternal { // skip internal |
| 92 | + versionedNew, err := scheme.New(gvk) |
| 93 | + if err != nil { |
| 94 | + t.Fatal(err) |
| 95 | + } |
| 96 | + err = scheme.Convert(unversionedNew, versionedNew, nil) |
| 97 | + if err != nil { |
| 98 | + t.Fatal(err) |
| 99 | + } |
| 100 | + |
| 101 | + var versionedOld runtime.Object |
| 102 | + if unversionedOld != nil { |
| 103 | + versionedOld, err = scheme.New(gvk) |
| 104 | + if err != nil { |
| 105 | + t.Fatal(err) |
| 106 | + } |
| 107 | + |
| 108 | + err = scheme.Convert(unversionedOld, versionedOld, nil) |
| 109 | + if err != nil { |
| 110 | + t.Fatal(err) |
| 111 | + } |
| 112 | + } |
| 113 | + |
| 114 | + fn(t, gv.String(), scheme.ValidateUpdate(context.Background(), options, versionedNew, versionedOld, subresources...)) |
| 115 | + } |
| 116 | + }) |
| 117 | + } |
| 118 | + } |
| 119 | +} |
0 commit comments