| 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 integration |
| 18 |
|
| 19 |
import ( |
| 20 |
"fmt" |
| 21 |
"testing" |
| 22 |
|
| 23 |
"k8s.io/kube-openapi/pkg/util" |
| 24 |
"k8s.io/kube-openapi/pkg/validation/spec" |
| 25 |
generated "k8s.io/kube-openapi/test/integration/pkg/generated/namedmodels" |
| 26 |
"k8s.io/kube-openapi/test/integration/testdata/namedmodels" |
| 27 |
) |
| 28 |
|
| 29 |
func TestCanonicalTypeNames(t *testing.T) { |
| 30 |
defs := generated.GetOpenAPIDefinitions(func(path string) spec.Ref { |
| 31 |
return spec.MustCreateRef(path) |
| 32 |
}) |
| 33 |
|
| 34 |
// Define the types we want to check. |
| 35 |
typeTestCases := []any{namedmodels.Struct{}, namedmodels.ContainedStruct{}, namedmodels.AtomicStruct{}} |
| 36 |
|
| 37 |
for _, v := range typeTestCases { |
| 38 |
t.Run(fmt.Sprintf("%T", v), func(t *testing.T) { |
| 39 |
// Get the canonical name using the generator's logic. |
| 40 |
canonicalName := util.GetCanonicalTypeName(v) |
| 41 |
|
| 42 |
// Check if the canonical name exists as a key in the generated map. |
| 43 |
if _, ok := defs[canonicalName]; !ok { |
| 44 |
t.Errorf("canonical type name %q for type %q not found in GetOpenAPIDefinitions", canonicalName, v) |
| 45 |
} |
| 46 |
}) |
| 47 |
} |
| 48 |
|
| 49 |
// Additionally, verify that the number of generated definitions matches our expectation. |
| 50 |
if len(defs) != len(typeTestCases) { |
| 51 |
t.Errorf("expected %d definitions, but got %d", len(typeTestCases), len(defs)) |
| 52 |
} |
| 53 |
} |