kubernetes/pkg/api/testing/validation_test.go
Patrick Ohly 566dc7f3f3 DRA device taints: graduate to beta
The fields become beta, enabled by default. DeviceTaintRule gets
added to the v1beta2 API, but support for it must remain off by default
because that API group is also off by default.

The v1beta1 API is left unchanged. No-one should be using it
anymore (deprecated in 1.33, could be removed now if it wasn't for
reading old objects and version emulation).

To achieve consistent validation, declarative validation must be enabled also
for v1alpha3 (was already enabled for other versions). Otherwise,
TestVersionedValidationByFuzzing fails:

    --- FAIL: TestVersionedValidationByFuzzing (0.09s)
        --- FAIL: TestVersionedValidationByFuzzing/resource.k8s.io/v1beta2,_Kind=DeviceTaintRule (0.00s)
            validation_test.go:109: different error count (0 vs. 1)
                resource.k8s.io/v1alpha3: <no errors>
                resource.k8s.io/v1beta2: "spec.taint.effect: Unsupported value: \"幤HxÒQP¹¬永唂ȳ垞ş]嘨鶊\": supported values: \"NoExecute\", \"NoSchedule\", \"None\""
            ...
2026-03-12 18:26:02 +01:00

122 lines
5.1 KiB
Go

/*
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 testing
import (
"math/rand"
"testing"
"k8s.io/apimachinery/pkg/api/apitesting/fuzzer"
"k8s.io/apimachinery/pkg/api/apitesting/roundtrip"
"k8s.io/apimachinery/pkg/runtime/schema"
"k8s.io/apimachinery/pkg/util/validation/field"
"k8s.io/kubernetes/pkg/api/legacyscheme"
nodevalidation "k8s.io/kubernetes/pkg/apis/node/validation"
resourcevalidation "k8s.io/kubernetes/pkg/apis/resource/validation"
)
// FIXME: Automatically finds all group/versions supporting declarative validation, or add
// a reflexive test that verifies that they are all registered.
func TestVersionedValidationByFuzzing(t *testing.T) {
typesWithDeclarativeValidation := []schema.GroupVersion{
// Registered group versions for versioned validation fuzz testing:
{Group: "", Version: "v1"},
{Group: "batch", Version: "v1"},
{Group: "batch", Version: "v1beta1"},
{Group: "certificates.k8s.io", Version: "v1"},
{Group: "certificates.k8s.io", Version: "v1alpha1"},
{Group: "certificates.k8s.io", Version: "v1beta1"},
{Group: "resource.k8s.io", Version: "v1"},
{Group: "resource.k8s.io", Version: "v1alpha3"},
{Group: "resource.k8s.io", Version: "v1beta1"},
{Group: "resource.k8s.io", Version: "v1beta2"},
{Group: "storage.k8s.io", Version: "v1"},
{Group: "storage.k8s.io", Version: "v1alpha1"},
{Group: "storage.k8s.io", Version: "v1beta1"},
{Group: "node.k8s.io", Version: "v1"},
{Group: "node.k8s.io", Version: "v1alpha1"},
{Group: "node.k8s.io", Version: "v1beta1"},
{Group: "network.k8s.io", Version: "v1"},
{Group: "network.k8s.io", Version: "v1beta1"},
{Group: "autoscaling", Version: "v1"},
{Group: "autoscaling", Version: "v1beta1"},
{Group: "autoscaling", Version: "v1beta2"},
{Group: "autoscaling", Version: "v2"},
{Group: "admissionregistration.k8s.io", Version: "v1"},
{Group: "admissionregistration.k8s.io", Version: "v1beta1"},
{Group: "admissionregistration.k8s.io", Version: "v1alpha1"},
{Group: "discovery.k8s.io", Version: "v1"},
{Group: "discovery.k8s.io", Version: "v1beta1"},
{Group: "admissionregistration.k8s.io", Version: "v1"},
{Group: "admissionregistration.k8s.io", Version: "v1beta1"},
{Group: "admissionregistration.k8s.io", Version: "v1alpha1"},
}
// subresourceOnly specifies the subresource path for types that can only be validated
// as subresources (e.g. autoscaling/Scale) and do not support root-level validation.
// For GVKs not in this map, the test defaults to fuzzing the root resource ("").
// Other resources with subresources (e.g. Pod status, exec) share validation logic with
// the root resource, so fuzzing the root is sufficient to verify validation equivalence.
subresourceOnly := map[schema.GroupVersionKind]string{
{Group: "autoscaling", Version: "v1", Kind: "Scale"}: "scale",
{Group: "autoscaling", Version: "v1beta1", Kind: "Scale"}: "scale",
{Group: "autoscaling", Version: "v1beta2", Kind: "Scale"}: "scale",
{Group: "autoscaling", Version: "v2", Kind: "Scale"}: "scale",
}
fuzzIters := *roundtrip.FuzzIters / 10 // TODO: Find a better way to manage test running time
f := fuzzer.FuzzerFor(FuzzerFuncs, rand.NewSource(rand.Int63()), legacyscheme.Codecs)
for _, gv := range typesWithDeclarativeValidation {
for kind := range legacyscheme.Scheme.KnownTypes(gv) {
gvk := gv.WithKind(kind)
t.Run(gvk.String(), func(t *testing.T) {
for i := 0; i < fuzzIters; i++ {
obj, err := legacyscheme.Scheme.New(gvk)
if err != nil {
t.Fatalf("could not create a %v: %s", kind, err)
}
subresource := ""
if specific, ok := subresourceOnly[gvk]; ok {
subresource = specific
}
var opts []ValidationTestConfig
// TODO(API group level configuration): Consider configuring normalization rules at the
// API group level to avoid potential collisions when multiple rule sets are combined.
// This would allow each API group to register its own normalization rules independently.
allRules := append([]field.NormalizationRule{}, resourcevalidation.ResourceNormalizationRules...)
allRules = append(allRules, nodevalidation.NodeNormalizationRules...)
opts = append(opts, WithNormalizationRules(allRules...), WithFuzzer(f))
if subresource != "" {
opts = append(opts, WithSubResources(subresource))
}
VerifyVersionedValidationEquivalence(t, obj, nil, opts...)
old, err := legacyscheme.Scheme.New(gv.WithKind(kind))
if err != nil {
t.Fatalf("could not create a %v: %s", kind, err)
}
VerifyVersionedValidationEquivalence(t, obj, old, opts...)
}
})
}
}
}