From e9daf55b56aeafc6d0eb140ffb416ecf59b68a1c Mon Sep 17 00:00:00 2001 From: Joe Betz Date: Wed, 10 Sep 2025 15:52:59 -0400 Subject: [PATCH] Add tests Signed-off-by: Joe Betz --- staging/src/k8s.io/api/openapi_models_test.go | 77 +++++++++++++++++++ .../pkg/apis/openapi_models_test.go | 60 +++++++++++++++ .../pkg/apis/openapi_models_test.go | 68 ++++++++++++++++ 3 files changed, 205 insertions(+) create mode 100644 staging/src/k8s.io/api/openapi_models_test.go create mode 100644 staging/src/k8s.io/apiextensions-apiserver/pkg/apis/openapi_models_test.go create mode 100644 staging/src/k8s.io/kube-aggregator/pkg/apis/openapi_models_test.go diff --git a/staging/src/k8s.io/api/openapi_models_test.go b/staging/src/k8s.io/api/openapi_models_test.go new file mode 100644 index 00000000000..aeeae938927 --- /dev/null +++ b/staging/src/k8s.io/api/openapi_models_test.go @@ -0,0 +1,77 @@ +/* +Copyright 2025 The Kubernetes Authors. + +Licensed under the Apache License, Version 2.0 (the "License"); +you may not use this file except in compliance with the License. +You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + +Unless required by applicable law or agreed to in writing, software +distributed under the License is distributed on an "AS IS" BASIS, +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +See the License for the specific language governing permissions and +limitations under the License. +*/ + +package api + +import ( + "reflect" + "strings" + "testing" + + "k8s.io/apimachinery/pkg/runtime" +) + +func TestOpenAPIDefinitionNames(t *testing.T) { + scheme := runtime.NewScheme() + for _, builder := range groups { + if err := builder.AddToScheme(scheme); err != nil { + t.Fatalf("unexpected error adding to scheme: %v", err) + } + } + + kinds := scheme.AllKnownTypes() + for gvk := range kinds { + if gvk.Version == runtime.APIVersionInternal { + continue + } + t.Run(gvk.String(), func(t *testing.T) { + example, err := scheme.New(gvk) + if err != nil { + t.Fatalf("unexpected error creating example: %v", err) + } + + namer, ok := example.(OpenAPIModelNamer) + if !ok { + t.Fatalf("type %v does not implement OpenAPICanonicalTypeName\n", gvk) + } + lookupName := namer.OpenAPIModelName() + + rtype := reflect.TypeOf(example).Elem() + reflectName := ToRESTFriendlyName(rtype.PkgPath() + "." + rtype.Name()) + + if lookupName != reflectName { + t.Errorf("expected %v, got %v", reflectName, lookupName) + } + }) + } +} + +type OpenAPIModelNamer interface { + OpenAPIModelName() string +} + +func ToRESTFriendlyName(name string) string { + nameParts := strings.Split(name, "/") + // Reverse first part. e.g., io.k8s... instead of k8s.io... + if len(nameParts) > 0 && strings.Contains(nameParts[0], ".") { + parts := strings.Split(nameParts[0], ".") + for i, j := 0, len(parts)-1; i < j; i, j = i+1, j-1 { + parts[i], parts[j] = parts[j], parts[i] + } + nameParts[0] = strings.Join(parts, ".") + } + return strings.Join(nameParts, ".") +} diff --git a/staging/src/k8s.io/apiextensions-apiserver/pkg/apis/openapi_models_test.go b/staging/src/k8s.io/apiextensions-apiserver/pkg/apis/openapi_models_test.go new file mode 100644 index 00000000000..d30671b1195 --- /dev/null +++ b/staging/src/k8s.io/apiextensions-apiserver/pkg/apis/openapi_models_test.go @@ -0,0 +1,60 @@ +/* +Copyright 2025 The Kubernetes Authors. + +Licensed under the Apache License, Version 2.0 (the "License"); +you may not use this file except in compliance with the License. +You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + +Unless required by applicable law or agreed to in writing, software +distributed under the License is distributed on an "AS IS" BASIS, +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +See the License for the specific language governing permissions and +limitations under the License. +*/ + +package api + +import ( + "reflect" + "testing" + + "k8s.io/apimachinery/pkg/runtime" + "k8s.io/kube-openapi/pkg/util" +) + +func TestOpenAPIDefinitionNames(t *testing.T) { + scheme := runtime.NewScheme() + for _, builder := range groups { + if err := builder.AddToScheme(scheme); err != nil { + t.Fatalf("unexpected error adding to scheme: %v", err) + } + } + + kinds := scheme.AllKnownTypes() + for gvk := range kinds { + if gvk.Version == runtime.APIVersionInternal { + continue + } + t.Run(gvk.String(), func(t *testing.T) { + example, err := scheme.New(gvk) + if err != nil { + t.Fatalf("unexpected error creating example: %v", err) + } + + namer, ok := example.(util.OpenAPIModelNamer) + if !ok { + t.Fatalf("type %v does not implement OpenAPICanonicalTypeName\n", gvk) + } + lookupName := namer.OpenAPIModelName() + + rtype := reflect.TypeOf(example).Elem() + reflectName := util.ToRESTFriendlyName(rtype.PkgPath() + "." + rtype.Name()) + + if lookupName != reflectName { + t.Errorf("expected %v, got %v", reflectName, lookupName) + } + }) + } +} diff --git a/staging/src/k8s.io/kube-aggregator/pkg/apis/openapi_models_test.go b/staging/src/k8s.io/kube-aggregator/pkg/apis/openapi_models_test.go new file mode 100644 index 00000000000..bad90b73dc7 --- /dev/null +++ b/staging/src/k8s.io/kube-aggregator/pkg/apis/openapi_models_test.go @@ -0,0 +1,68 @@ +/* +Copyright 2025 The Kubernetes Authors. + +Licensed under the Apache License, Version 2.0 (the "License"); +you may not use this file except in compliance with the License. +You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + +Unless required by applicable law or agreed to in writing, software +distributed under the License is distributed on an "AS IS" BASIS, +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +See the License for the specific language governing permissions and +limitations under the License. +*/ + +package apis + +import ( + "reflect" + "testing" + + "k8s.io/apimachinery/pkg/runtime" + "k8s.io/kube-openapi/pkg/util" + + apiregistrationv1 "k8s.io/kube-aggregator/pkg/apis/apiregistration/v1" + apiregistrationv1beta1 "k8s.io/kube-aggregator/pkg/apis/apiregistration/v1beta1" +) + +var groups = []runtime.SchemeBuilder{ + apiregistrationv1.SchemeBuilder, + apiregistrationv1beta1.SchemeBuilder, +} + +func TestOpenAPIDefinitionNames(t *testing.T) { + scheme := runtime.NewScheme() + for _, builder := range groups { + if err := builder.AddToScheme(scheme); err != nil { + t.Fatalf("unexpected error adding to scheme: %v", err) + } + } + + kinds := scheme.AllKnownTypes() + for gvk := range kinds { + if gvk.Version == runtime.APIVersionInternal { + continue + } + t.Run(gvk.String(), func(t *testing.T) { + example, err := scheme.New(gvk) + if err != nil { + t.Fatalf("unexpected error creating example: %v", err) + } + + namer, ok := example.(util.OpenAPIModelNamer) + if !ok { + t.Fatalf("type %v does not implement OpenAPICanonicalTypeName\n", gvk) + } + lookupName := namer.OpenAPIModelName() + + rtype := reflect.TypeOf(example).Elem() + reflectName := util.ToRESTFriendlyName(rtype.PkgPath() + "." + rtype.Name()) + + if lookupName != reflectName { + t.Errorf("expected %v, got %v", reflectName, lookupName) + } + }) + } +}