autogen: update vendor

This commit is contained in:
justinsb 2025-07-31 08:41:44 -04:00
parent 63b991795c
commit 12d2f2439c
11 changed files with 2065 additions and 0 deletions

116
vendor/k8s.io/apimachinery/pkg/api/apitesting/codec.go generated vendored Normal file
View File

@ -0,0 +1,116 @@
/*
Copyright 2017 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 apitesting
import (
"fmt"
"mime"
"os"
"k8s.io/apimachinery/pkg/runtime"
"k8s.io/apimachinery/pkg/runtime/schema"
runtimeserializer "k8s.io/apimachinery/pkg/runtime/serializer"
"k8s.io/apimachinery/pkg/runtime/serializer/recognizer"
)
var (
testCodecMediaType string
testStorageCodecMediaType string
)
// TestCodec returns the codec for the API version to test against, as set by the
// KUBE_TEST_API_TYPE env var.
func TestCodec(codecs runtimeserializer.CodecFactory, gvs ...schema.GroupVersion) runtime.Codec {
if len(testCodecMediaType) != 0 {
serializerInfo, ok := runtime.SerializerInfoForMediaType(codecs.SupportedMediaTypes(), testCodecMediaType)
if !ok {
panic(fmt.Sprintf("no serializer for %s", testCodecMediaType))
}
return codecs.CodecForVersions(serializerInfo.Serializer, codecs.UniversalDeserializer(), schema.GroupVersions(gvs), nil)
}
return codecs.LegacyCodec(gvs...)
}
// TestStorageCodec returns the codec for the API version to test against used in storage, as set by the
// KUBE_TEST_API_STORAGE_TYPE env var.
func TestStorageCodec(codecs runtimeserializer.CodecFactory, gvs ...schema.GroupVersion) runtime.Codec {
if len(testStorageCodecMediaType) != 0 {
serializerInfo, ok := runtime.SerializerInfoForMediaType(codecs.SupportedMediaTypes(), testStorageCodecMediaType)
if !ok {
panic(fmt.Sprintf("no serializer for %s", testStorageCodecMediaType))
}
// etcd2 only supports string data - we must wrap any result before returning
// TODO: remove for etcd3 / make parameterizable
serializer := serializerInfo.Serializer
if !serializerInfo.EncodesAsText {
serializer = runtime.NewBase64Serializer(serializer, serializer)
}
decoder := recognizer.NewDecoder(serializer, codecs.UniversalDeserializer())
return codecs.CodecForVersions(serializer, decoder, schema.GroupVersions(gvs), nil)
}
return codecs.LegacyCodec(gvs...)
}
func init() {
var err error
if apiMediaType := os.Getenv("KUBE_TEST_API_TYPE"); len(apiMediaType) > 0 {
testCodecMediaType, _, err = mime.ParseMediaType(apiMediaType)
if err != nil {
panic(err)
}
}
if storageMediaType := os.Getenv("KUBE_TEST_API_STORAGE_TYPE"); len(storageMediaType) > 0 {
testStorageCodecMediaType, _, err = mime.ParseMediaType(storageMediaType)
if err != nil {
panic(err)
}
}
}
// InstallOrDieFunc mirrors install functions that require success
type InstallOrDieFunc func(scheme *runtime.Scheme)
// SchemeForInstallOrDie builds a simple test scheme and codecfactory pair for easy unit testing from higher level install methods
func SchemeForInstallOrDie(installFns ...InstallOrDieFunc) (*runtime.Scheme, runtimeserializer.CodecFactory) {
scheme := runtime.NewScheme()
codecFactory := runtimeserializer.NewCodecFactory(scheme)
for _, installFn := range installFns {
installFn(scheme)
}
return scheme, codecFactory
}
// InstallFunc mirrors install functions that can return an error
type InstallFunc func(scheme *runtime.Scheme) error
// SchemeForOrDie builds a simple test scheme and codecfactory pair for easy unit testing from the bare registration methods.
func SchemeForOrDie(installFns ...InstallFunc) (*runtime.Scheme, runtimeserializer.CodecFactory) {
scheme := runtime.NewScheme()
codecFactory := runtimeserializer.NewCodecFactory(scheme)
for _, installFn := range installFns {
if err := installFn(scheme); err != nil {
panic(err)
}
}
return scheme, codecFactory
}

View File

@ -0,0 +1,73 @@
/*
Copyright 2017 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 fuzzer
import (
"encoding/json"
"fmt"
"math/rand"
"sigs.k8s.io/randfill"
"k8s.io/apimachinery/pkg/runtime"
runtimeserializer "k8s.io/apimachinery/pkg/runtime/serializer"
kjson "k8s.io/apimachinery/pkg/util/json"
)
// FuzzerFuncs returns a list of func(*SomeType, c randfill.Continue) functions.
type FuzzerFuncs func(codecs runtimeserializer.CodecFactory) []interface{}
// FuzzerFor can randomly populate api objects that are destined for version.
func FuzzerFor(funcs FuzzerFuncs, src rand.Source, codecs runtimeserializer.CodecFactory) *randfill.Filler {
f := randfill.New().NilChance(.5).NumElements(0, 1)
if src != nil {
f.RandSource(src)
}
f.Funcs(funcs(codecs)...)
return f
}
// MergeFuzzerFuncs will merge the given funcLists, overriding early funcs with later ones if there first
// argument has the same type.
func MergeFuzzerFuncs(funcs ...FuzzerFuncs) FuzzerFuncs {
return FuzzerFuncs(func(codecs runtimeserializer.CodecFactory) []interface{} {
result := []interface{}{}
for _, f := range funcs {
if f != nil {
result = append(result, f(codecs)...)
}
}
return result
})
}
func NormalizeJSONRawExtension(ext *runtime.RawExtension) {
if json.Valid(ext.Raw) {
// RawExtension->JSON encodes struct fields in field index order while map[string]interface{}->JSON encodes
// struct fields (i.e. keys in the map) lexicographically. We have to sort the fields here to ensure the
// JSON in the (RawExtension->)JSON->map[string]interface{}->JSON round trip results in identical JSON.
var u any
err := kjson.Unmarshal(ext.Raw, &u)
if err != nil {
panic(fmt.Sprintf("Failed to encode object: %v", err))
}
ext.Raw, err = kjson.Marshal(&u)
if err != nil {
panic(fmt.Sprintf("Failed to encode object: %v", err))
}
}
}

View File

@ -0,0 +1,86 @@
/*
Copyright 2017 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 fuzzer
import (
"reflect"
)
// ValueFuzz recursively changes all basic type values in an object. Any kind of references will not
// be touch, i.e. the addresses of slices, maps, pointers will stay unchanged.
func ValueFuzz(obj interface{}) {
valueFuzz(reflect.ValueOf(obj))
}
func valueFuzz(obj reflect.Value) {
switch obj.Kind() {
case reflect.Array:
for i := 0; i < obj.Len(); i++ {
valueFuzz(obj.Index(i))
}
case reflect.Slice:
if obj.IsNil() {
// TODO: set non-nil value
} else {
for i := 0; i < obj.Len(); i++ {
valueFuzz(obj.Index(i))
}
}
case reflect.Interface, reflect.Pointer:
if obj.IsNil() {
// TODO: set non-nil value
} else {
valueFuzz(obj.Elem())
}
case reflect.Struct:
for i, n := 0, obj.NumField(); i < n; i++ {
valueFuzz(obj.Field(i))
}
case reflect.Map:
if obj.IsNil() {
// TODO: set non-nil value
} else {
for _, k := range obj.MapKeys() {
// map values are not addressable. We need a copy.
v := obj.MapIndex(k)
copy := reflect.New(v.Type())
copy.Elem().Set(v)
valueFuzz(copy.Elem())
obj.SetMapIndex(k, copy.Elem())
}
// TODO: set some new value
}
case reflect.Func: // ignore, we don't have function types in our API
default:
if !obj.CanSet() {
return
}
switch obj.Kind() {
case reflect.String:
obj.SetString(obj.String() + "x")
case reflect.Bool:
obj.SetBool(!obj.Bool())
case reflect.Float32, reflect.Float64:
obj.SetFloat(obj.Float()*2.0 + 1.0)
case reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64, reflect.Int:
obj.SetInt(obj.Int() + 1)
case reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64, reflect.Uint:
obj.SetUint(obj.Uint() + 1)
default:
}
}
}

View File

@ -0,0 +1,529 @@
/*
Copyright 2019 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 roundtrip
import (
"bytes"
gojson "encoding/json"
"io/ioutil"
"os"
"os/exec"
"path/filepath"
"reflect"
"sort"
"strings"
"testing"
"github.com/google/go-cmp/cmp" //nolint:depguard
apiequality "k8s.io/apimachinery/pkg/api/equality"
"k8s.io/apimachinery/pkg/runtime"
"k8s.io/apimachinery/pkg/runtime/schema"
"k8s.io/apimachinery/pkg/runtime/serializer/json"
"k8s.io/apimachinery/pkg/runtime/serializer/protobuf"
"k8s.io/apimachinery/pkg/util/sets"
)
// CompatibilityTestOptions holds configuration for running a compatibility test using in-memory objects
// and serialized files on disk representing the current code and serialized data from previous versions.
//
// Example use: `NewCompatibilityTestOptions(scheme).Complete(t).Run(t)`
type CompatibilityTestOptions struct {
// Scheme is used to create new objects for filling, decoding, and for constructing serializers.
// Required.
Scheme *runtime.Scheme
// TestDataDir points to a directory containing compatibility test data.
// Complete() populates this with "testdata" if unset.
TestDataDir string
// TestDataDirCurrentVersion points to a directory containing compatibility test data for the current version.
// Complete() populates this with "<TestDataDir>/HEAD" if unset.
// Within this directory, `<group>.<version>.<kind>.[json|yaml|pb]` files are required to exist, and are:
// * verified to match serialized FilledObjects[GVK]
// * verified to decode without error
// * verified to round-trip byte-for-byte when re-encoded
// * verified to be semantically equal when decoded into memory
TestDataDirCurrentVersion string
// TestDataDirsPreviousVersions is a list of directories containing compatibility test data for previous versions.
// Complete() populates this with "<TestDataDir>/v*" directories if nil.
// Within these directories, `<group>.<version>.<kind>.[json|yaml|pb]` files are optional. If present, they are:
// * verified to decode without error
// * verified to round-trip byte-for-byte when re-encoded (or to match a `<group>.<version>.<kind>.[json|yaml|pb].after_roundtrip.[json|yaml|pb]` file if it exists)
// * verified to be semantically equal when decoded into memory
TestDataDirsPreviousVersions []string
// Kinds is a list of fully qualified kinds to test.
// Complete() populates this with Scheme.AllKnownTypes() if unset.
Kinds []schema.GroupVersionKind
// FilledObjects is an optional set of pre-filled objects to use for verifying HEAD fixtures.
// Complete() populates this with the result of CompatibilityTestObject(Kinds[*], Scheme, FillFuncs) for any missing kinds.
// Objects must deterministically populate every field and be identical on every invocation.
FilledObjects map[schema.GroupVersionKind]runtime.Object
// FillFuncs is an optional map of custom functions to use to fill instances of particular types.
FillFuncs map[reflect.Type]FillFunc
JSON runtime.Serializer
YAML runtime.Serializer
Proto runtime.Serializer
}
// FillFunc is a function that populates all serializable fields in obj.
// s and i are string and integer values relevant to the object being populated
// (for example, the json key or protobuf tag containing the object)
// that can be used when filling the object to make the object content identifiable
type FillFunc func(s string, i int, obj interface{})
func NewCompatibilityTestOptions(scheme *runtime.Scheme) *CompatibilityTestOptions {
return &CompatibilityTestOptions{Scheme: scheme}
}
// coreKinds includes kinds that typically only need to be tested in a single API group
var coreKinds = sets.NewString(
"CreateOptions", "UpdateOptions", "PatchOptions", "DeleteOptions",
"GetOptions", "ListOptions", "ExportOptions",
"WatchEvent",
)
func (c *CompatibilityTestOptions) Complete(t *testing.T) *CompatibilityTestOptions {
t.Helper()
// Verify scheme
if c.Scheme == nil {
t.Fatal("scheme is required")
}
// Populate testdata dirs
if c.TestDataDir == "" {
c.TestDataDir = "testdata"
}
if c.TestDataDirCurrentVersion == "" {
c.TestDataDirCurrentVersion = filepath.Join(c.TestDataDir, "HEAD")
}
if c.TestDataDirsPreviousVersions == nil {
dirs, err := filepath.Glob(filepath.Join(c.TestDataDir, "v*"))
if err != nil {
t.Fatal(err)
}
sort.Strings(dirs)
c.TestDataDirsPreviousVersions = dirs
}
// Populate kinds
if len(c.Kinds) == 0 {
gvks := []schema.GroupVersionKind{}
for gvk := range c.Scheme.AllKnownTypes() {
if gvk.Version == "" || gvk.Version == runtime.APIVersionInternal {
// only test external types
continue
}
if strings.HasSuffix(gvk.Kind, "List") {
// omit list types
continue
}
if gvk.Group != "" && coreKinds.Has(gvk.Kind) {
// only test options types in the core API group
continue
}
gvks = append(gvks, gvk)
}
c.Kinds = gvks
}
// Sort kinds to get deterministic test order
sort.Slice(c.Kinds, func(i, j int) bool {
if c.Kinds[i].Group != c.Kinds[j].Group {
return c.Kinds[i].Group < c.Kinds[j].Group
}
if c.Kinds[i].Version != c.Kinds[j].Version {
return c.Kinds[i].Version < c.Kinds[j].Version
}
if c.Kinds[i].Kind != c.Kinds[j].Kind {
return c.Kinds[i].Kind < c.Kinds[j].Kind
}
return false
})
// Fill any missing objects
if c.FilledObjects == nil {
c.FilledObjects = map[schema.GroupVersionKind]runtime.Object{}
}
fillFuncs := defaultFillFuncs()
for k, v := range c.FillFuncs {
fillFuncs[k] = v
}
for _, gvk := range c.Kinds {
if _, ok := c.FilledObjects[gvk]; ok {
continue
}
obj, err := CompatibilityTestObject(c.Scheme, gvk, fillFuncs)
if err != nil {
t.Fatal(err)
}
c.FilledObjects[gvk] = obj
}
if c.JSON == nil {
c.JSON = json.NewSerializerWithOptions(json.DefaultMetaFactory, c.Scheme, c.Scheme, json.SerializerOptions{Pretty: true})
}
if c.YAML == nil {
c.YAML = json.NewSerializerWithOptions(json.DefaultMetaFactory, c.Scheme, c.Scheme, json.SerializerOptions{Yaml: true})
}
if c.Proto == nil {
c.Proto = protobuf.NewSerializer(c.Scheme, c.Scheme)
}
return c
}
func (c *CompatibilityTestOptions) Run(t *testing.T) {
usedHEADFixtures := sets.NewString()
for _, gvk := range c.Kinds {
t.Run(makeName(gvk), func(t *testing.T) {
t.Run("HEAD", func(t *testing.T) {
c.runCurrentVersionTest(t, gvk, usedHEADFixtures)
})
for _, previousVersionDir := range c.TestDataDirsPreviousVersions {
t.Run(filepath.Base(previousVersionDir), func(t *testing.T) {
c.runPreviousVersionTest(t, gvk, previousVersionDir, nil)
})
}
})
}
// Check for unused HEAD fixtures
t.Run("unused_fixtures", func(t *testing.T) {
files, err := os.ReadDir(c.TestDataDirCurrentVersion)
if err != nil {
t.Fatal(err)
}
allFixtures := sets.NewString()
for _, file := range files {
allFixtures.Insert(file.Name())
}
if unused := allFixtures.Difference(usedHEADFixtures); len(unused) > 0 {
t.Fatalf("remove unused fixtures from %s:\n%s", c.TestDataDirCurrentVersion, strings.Join(unused.List(), "\n"))
}
})
}
func (c *CompatibilityTestOptions) runCurrentVersionTest(t *testing.T, gvk schema.GroupVersionKind, usedFiles sets.String) {
expectedObject := c.FilledObjects[gvk]
expectedJSON, expectedYAML, expectedProto := c.encode(t, expectedObject)
actualJSON, actualYAML, actualProto, err := read(c.TestDataDirCurrentVersion, gvk, "", usedFiles)
if err != nil && !os.IsNotExist(err) {
t.Fatal(err)
}
needsUpdate := false
if os.IsNotExist(err) {
t.Errorf("current version compatibility files did not exist: %v", err)
needsUpdate = true
} else {
if !bytes.Equal(expectedJSON, actualJSON) {
t.Errorf("json differs")
t.Log(cmp.Diff(string(actualJSON), string(expectedJSON)))
needsUpdate = true
}
if !bytes.Equal(expectedYAML, actualYAML) {
t.Errorf("yaml differs")
t.Log(cmp.Diff(string(actualYAML), string(expectedYAML)))
needsUpdate = true
}
if !bytes.Equal(expectedProto, actualProto) {
t.Errorf("proto differs")
needsUpdate = true
t.Log(cmp.Diff(dumpProto(t, actualProto[4:]), dumpProto(t, expectedProto[4:])))
// t.Logf("json (for locating the offending field based on surrounding data): %s", string(expectedJSON))
}
}
if needsUpdate {
const updateEnvVar = "UPDATE_COMPATIBILITY_FIXTURE_DATA"
if os.Getenv(updateEnvVar) == "true" {
writeFile(t, c.TestDataDirCurrentVersion, gvk, "", "json", expectedJSON)
writeFile(t, c.TestDataDirCurrentVersion, gvk, "", "yaml", expectedYAML)
writeFile(t, c.TestDataDirCurrentVersion, gvk, "", "pb", expectedProto)
t.Logf("wrote expected compatibility data... verify, commit, and rerun tests")
} else {
t.Logf("if the diff is expected because of a new type or a new field, re-run with %s=true to update the compatibility data", updateEnvVar)
}
return
}
emptyObj, err := c.Scheme.New(gvk)
if err != nil {
t.Fatal(err)
}
{
// compact before decoding since embedded RawExtension fields retain indenting
compacted := &bytes.Buffer{}
if err := gojson.Compact(compacted, actualJSON); err != nil {
t.Error(err)
}
jsonDecoded := emptyObj.DeepCopyObject()
jsonDecoded, _, err = c.JSON.Decode(compacted.Bytes(), &gvk, jsonDecoded)
if err != nil {
t.Error(err)
} else if !apiequality.Semantic.DeepEqual(expectedObject, jsonDecoded) {
t.Errorf("expected and decoded json objects differed:\n%s", cmp.Diff(expectedObject, jsonDecoded))
}
}
{
yamlDecoded := emptyObj.DeepCopyObject()
yamlDecoded, _, err = c.YAML.Decode(actualYAML, &gvk, yamlDecoded)
if err != nil {
t.Error(err)
} else if !apiequality.Semantic.DeepEqual(expectedObject, yamlDecoded) {
t.Errorf("expected and decoded yaml objects differed:\n%s", cmp.Diff(expectedObject, yamlDecoded))
}
}
{
protoDecoded := emptyObj.DeepCopyObject()
protoDecoded, _, err = c.Proto.Decode(actualProto, &gvk, protoDecoded)
if err != nil {
t.Error(err)
} else if !apiequality.Semantic.DeepEqual(expectedObject, protoDecoded) {
t.Errorf("expected and decoded proto objects differed:\n%s", cmp.Diff(expectedObject, protoDecoded))
}
}
}
func (c *CompatibilityTestOptions) encode(t *testing.T, obj runtime.Object) (json, yaml, proto []byte) {
jsonBytes := bytes.NewBuffer(nil)
if err := c.JSON.Encode(obj, jsonBytes); err != nil {
t.Fatalf("error encoding json: %v", err)
}
yamlBytes := bytes.NewBuffer(nil)
if err := c.YAML.Encode(obj, yamlBytes); err != nil {
t.Fatalf("error encoding yaml: %v", err)
}
protoBytes := bytes.NewBuffer(nil)
if err := c.Proto.Encode(obj, protoBytes); err != nil {
t.Fatalf("error encoding proto: %v", err)
}
return jsonBytes.Bytes(), yamlBytes.Bytes(), protoBytes.Bytes()
}
func read(dir string, gvk schema.GroupVersionKind, suffix string, usedFiles sets.String) (json, yaml, proto []byte, err error) {
jsonFilename := makeName(gvk) + suffix + ".json"
actualJSON, jsonErr := ioutil.ReadFile(filepath.Join(dir, jsonFilename))
yamlFilename := makeName(gvk) + suffix + ".yaml"
actualYAML, yamlErr := ioutil.ReadFile(filepath.Join(dir, yamlFilename))
protoFilename := makeName(gvk) + suffix + ".pb"
actualProto, protoErr := ioutil.ReadFile(filepath.Join(dir, protoFilename))
if usedFiles != nil {
usedFiles.Insert(jsonFilename)
usedFiles.Insert(yamlFilename)
usedFiles.Insert(protoFilename)
}
if jsonErr != nil {
return actualJSON, actualYAML, actualProto, jsonErr
}
if yamlErr != nil {
return actualJSON, actualYAML, actualProto, yamlErr
}
if protoErr != nil {
return actualJSON, actualYAML, actualProto, protoErr
}
return actualJSON, actualYAML, actualProto, nil
}
func writeFile(t *testing.T, dir string, gvk schema.GroupVersionKind, suffix, extension string, data []byte) {
if err := os.MkdirAll(dir, os.FileMode(0755)); err != nil {
t.Fatal("error making directory", err)
}
if err := ioutil.WriteFile(filepath.Join(dir, makeName(gvk)+suffix+"."+extension), data, os.FileMode(0644)); err != nil {
t.Fatalf("error writing %s: %v", extension, err)
}
}
func deleteFile(t *testing.T, dir string, gvk schema.GroupVersionKind, suffix, extension string) {
if err := os.Remove(filepath.Join(dir, makeName(gvk)+suffix+"."+extension)); err != nil {
t.Fatalf("error removing %s: %v", extension, err)
}
}
func (c *CompatibilityTestOptions) runPreviousVersionTest(t *testing.T, gvk schema.GroupVersionKind, previousVersionDir string, usedFiles sets.String) {
jsonBeforeRoundTrip, yamlBeforeRoundTrip, protoBeforeRoundTrip, err := read(previousVersionDir, gvk, "", usedFiles)
if os.IsNotExist(err) || (len(jsonBeforeRoundTrip) == 0 && len(yamlBeforeRoundTrip) == 0 && len(protoBeforeRoundTrip) == 0) {
t.SkipNow()
return
}
if err != nil {
t.Fatal(err)
}
emptyObj, err := c.Scheme.New(gvk)
if err != nil {
t.Fatal(err)
}
// compact before decoding since embedded RawExtension fields retain indenting
compacted := &bytes.Buffer{}
if err := gojson.Compact(compacted, jsonBeforeRoundTrip); err != nil {
t.Fatal(err)
}
jsonDecoded := emptyObj.DeepCopyObject()
jsonDecoded, _, err = c.JSON.Decode(compacted.Bytes(), &gvk, jsonDecoded)
if err != nil {
t.Fatal(err)
}
jsonBytes := bytes.NewBuffer(nil)
if err := c.JSON.Encode(jsonDecoded, jsonBytes); err != nil {
t.Fatalf("error encoding json: %v", err)
}
jsonAfterRoundTrip := jsonBytes.Bytes()
yamlDecoded := emptyObj.DeepCopyObject()
yamlDecoded, _, err = c.YAML.Decode(yamlBeforeRoundTrip, &gvk, yamlDecoded)
if err != nil {
t.Fatal(err)
} else if !apiequality.Semantic.DeepEqual(jsonDecoded, yamlDecoded) {
t.Errorf("decoded json and yaml objects differ:\n%s", cmp.Diff(jsonDecoded, yamlDecoded))
}
yamlBytes := bytes.NewBuffer(nil)
if err := c.YAML.Encode(yamlDecoded, yamlBytes); err != nil {
t.Fatalf("error encoding yaml: %v", err)
}
yamlAfterRoundTrip := yamlBytes.Bytes()
protoDecoded := emptyObj.DeepCopyObject()
protoDecoded, _, err = c.Proto.Decode(protoBeforeRoundTrip, &gvk, protoDecoded)
if err != nil {
t.Fatal(err)
} else if !apiequality.Semantic.DeepEqual(jsonDecoded, protoDecoded) {
t.Errorf("decoded json and proto objects differ:\n%s", cmp.Diff(jsonDecoded, protoDecoded))
}
protoBytes := bytes.NewBuffer(nil)
if err := c.Proto.Encode(protoDecoded, protoBytes); err != nil {
t.Fatalf("error encoding proto: %v", err)
}
protoAfterRoundTrip := protoBytes.Bytes()
jsonNeedsRemove := false
yamlNeedsRemove := false
protoNeedsRemove := false
expectedJSONAfterRoundTrip, expectedYAMLAfterRoundTrip, expectedProtoAfterRoundTrip, _ := read(previousVersionDir, gvk, ".after_roundtrip", usedFiles)
if len(expectedJSONAfterRoundTrip) == 0 {
expectedJSONAfterRoundTrip = jsonBeforeRoundTrip
} else if bytes.Equal(jsonBeforeRoundTrip, expectedJSONAfterRoundTrip) {
t.Errorf("JSON after_roundtrip file is identical and should be removed")
jsonNeedsRemove = true
}
if len(expectedYAMLAfterRoundTrip) == 0 {
expectedYAMLAfterRoundTrip = yamlBeforeRoundTrip
} else if bytes.Equal(yamlBeforeRoundTrip, expectedYAMLAfterRoundTrip) {
t.Errorf("YAML after_roundtrip file is identical and should be removed")
yamlNeedsRemove = true
}
if len(expectedProtoAfterRoundTrip) == 0 {
expectedProtoAfterRoundTrip = protoBeforeRoundTrip
} else if bytes.Equal(protoBeforeRoundTrip, expectedProtoAfterRoundTrip) {
t.Errorf("Proto after_roundtrip file is identical and should be removed")
protoNeedsRemove = true
}
jsonNeedsUpdate := false
yamlNeedsUpdate := false
protoNeedsUpdate := false
if !bytes.Equal(expectedJSONAfterRoundTrip, jsonAfterRoundTrip) {
t.Errorf("json differs")
t.Log(cmp.Diff(string(expectedJSONAfterRoundTrip), string(jsonAfterRoundTrip)))
jsonNeedsUpdate = true
}
if !bytes.Equal(expectedYAMLAfterRoundTrip, yamlAfterRoundTrip) {
t.Errorf("yaml differs")
t.Log(cmp.Diff(string(expectedYAMLAfterRoundTrip), string(yamlAfterRoundTrip)))
yamlNeedsUpdate = true
}
if !bytes.Equal(expectedProtoAfterRoundTrip, protoAfterRoundTrip) {
t.Errorf("proto differs")
protoNeedsUpdate = true
t.Log(cmp.Diff(dumpProto(t, expectedProtoAfterRoundTrip[4:]), dumpProto(t, protoAfterRoundTrip[4:])))
// t.Logf("json (for locating the offending field based on surrounding data): %s", string(expectedJSON))
}
if jsonNeedsUpdate || yamlNeedsUpdate || protoNeedsUpdate || jsonNeedsRemove || yamlNeedsRemove || protoNeedsRemove {
const updateEnvVar = "UPDATE_COMPATIBILITY_FIXTURE_DATA"
if os.Getenv(updateEnvVar) == "true" {
if jsonNeedsUpdate {
writeFile(t, previousVersionDir, gvk, ".after_roundtrip", "json", jsonAfterRoundTrip)
} else if jsonNeedsRemove {
deleteFile(t, previousVersionDir, gvk, ".after_roundtrip", "json")
}
if yamlNeedsUpdate {
writeFile(t, previousVersionDir, gvk, ".after_roundtrip", "yaml", yamlAfterRoundTrip)
} else if yamlNeedsRemove {
deleteFile(t, previousVersionDir, gvk, ".after_roundtrip", "yaml")
}
if protoNeedsUpdate {
writeFile(t, previousVersionDir, gvk, ".after_roundtrip", "pb", protoAfterRoundTrip)
} else if protoNeedsRemove {
deleteFile(t, previousVersionDir, gvk, ".after_roundtrip", "pb")
}
t.Logf("wrote expected compatibility data... verify, commit, and rerun tests")
} else {
t.Logf("if the diff is expected because of a new type or a new field, re-run with %s=true to update the compatibility data", updateEnvVar)
}
return
}
}
func makeName(gvk schema.GroupVersionKind) string {
g := gvk.Group
if g == "" {
g = "core"
}
return g + "." + gvk.Version + "." + gvk.Kind
}
func dumpProto(t *testing.T, data []byte) string {
t.Helper()
protoc, err := exec.LookPath("protoc")
if err != nil {
t.Log(err)
return ""
}
cmd := exec.Command(protoc, "--decode_raw")
cmd.Stdin = bytes.NewBuffer(data)
d, err := cmd.CombinedOutput()
if err != nil {
t.Log(err)
return ""
}
return string(d)
}

View File

@ -0,0 +1,191 @@
/*
Copyright 2022 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 roundtrip
import (
"fmt"
"reflect"
"strconv"
"strings"
"time"
apimeta "k8s.io/apimachinery/pkg/api/meta"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/runtime"
"k8s.io/apimachinery/pkg/runtime/schema"
"k8s.io/apimachinery/pkg/util/intstr"
)
func defaultFillFuncs() map[reflect.Type]FillFunc {
funcs := map[reflect.Type]FillFunc{}
funcs[reflect.TypeOf(&runtime.RawExtension{})] = func(s string, i int, obj interface{}) {
// generate a raw object in normalized form
// TODO: test non-normalized round-tripping... YAMLToJSON normalizes and makes exact comparisons fail
obj.(*runtime.RawExtension).Raw = []byte(`{"apiVersion":"example.com/v1","kind":"CustomType","spec":{"replicas":1},"status":{"available":1}}`)
}
funcs[reflect.TypeOf(&metav1.TypeMeta{})] = func(s string, i int, obj interface{}) {
// APIVersion and Kind are not serialized in all formats (notably protobuf), so clear by default for cross-format checking.
obj.(*metav1.TypeMeta).APIVersion = ""
obj.(*metav1.TypeMeta).Kind = ""
}
funcs[reflect.TypeOf(&metav1.FieldsV1{})] = func(s string, i int, obj interface{}) {
obj.(*metav1.FieldsV1).Raw = []byte(`{}`)
}
funcs[reflect.TypeOf(&metav1.Time{})] = func(s string, i int, obj interface{}) {
// use the integer as an offset from the year
obj.(*metav1.Time).Time = time.Date(2000+i, 1, 1, 1, 1, 1, 0, time.UTC)
}
funcs[reflect.TypeOf(&metav1.MicroTime{})] = func(s string, i int, obj interface{}) {
// use the integer as an offset from the year, and as a microsecond
obj.(*metav1.MicroTime).Time = time.Date(2000+i, 1, 1, 1, 1, 1, i*int(time.Microsecond), time.UTC)
}
funcs[reflect.TypeOf(&intstr.IntOrString{})] = func(s string, i int, obj interface{}) {
// use the string as a string value
obj.(*intstr.IntOrString).Type = intstr.String
obj.(*intstr.IntOrString).StrVal = s + "Value"
}
return funcs
}
// CompatibilityTestObject returns a deterministically filled object for the specified GVK
func CompatibilityTestObject(scheme *runtime.Scheme, gvk schema.GroupVersionKind, fillFuncs map[reflect.Type]FillFunc) (runtime.Object, error) {
// Construct the object
obj, err := scheme.New(gvk)
if err != nil {
return nil, err
}
fill("", 0, reflect.TypeOf(obj), reflect.ValueOf(obj), fillFuncs, map[reflect.Type]bool{})
// Set the kind and apiVersion
if typeAcc, err := apimeta.TypeAccessor(obj); err != nil {
return nil, err
} else {
typeAcc.SetKind(gvk.Kind)
typeAcc.SetAPIVersion(gvk.GroupVersion().String())
}
return obj, nil
}
func fill(dataString string, dataInt int, t reflect.Type, v reflect.Value, fillFuncs map[reflect.Type]FillFunc, filledTypes map[reflect.Type]bool) {
if filledTypes[t] {
// we already filled this type, avoid recursing infinitely
return
}
filledTypes[t] = true
defer delete(filledTypes, t)
// if nil, populate pointers with a zero-value instance of the underlying type
if t.Kind() == reflect.Pointer && v.IsNil() {
if v.CanSet() {
v.Set(reflect.New(t.Elem()))
} else if v.IsNil() {
panic(fmt.Errorf("unsettable nil pointer of type %v in field %s", t, dataString))
}
}
if f, ok := fillFuncs[t]; ok {
// use the custom fill function for this type
f(dataString, dataInt, v.Interface())
return
}
switch t.Kind() {
case reflect.Slice:
// populate with a single-item slice
v.Set(reflect.MakeSlice(t, 1, 1))
// recurse to populate the item, preserving the data context
if t.Elem().Kind() == reflect.Pointer {
fill(dataString, dataInt, t.Elem(), v.Index(0), fillFuncs, filledTypes)
} else {
fill(dataString, dataInt, reflect.PointerTo(t.Elem()), v.Index(0).Addr(), fillFuncs, filledTypes)
}
case reflect.Map:
// construct the key, which must be a string type, possibly converted to a type alias of string
key := reflect.ValueOf(dataString + "Key").Convert(t.Key())
// construct a zero-value item
item := reflect.New(t.Elem())
// recurse to populate the item, preserving the data context
fill(dataString, dataInt, t.Elem(), item.Elem(), fillFuncs, filledTypes)
// store in the map
v.Set(reflect.MakeMap(t))
v.SetMapIndex(key, item.Elem())
case reflect.Struct:
for i := 0; i < t.NumField(); i++ {
field := t.Field(i)
if !field.IsExported() {
continue
}
// use the json field name, which must be stable
dataString := strings.Split(field.Tag.Get("json"), ",")[0]
if dataString == "-" {
// unserialized field, no need to fill it
continue
}
if len(dataString) == 0 {
// fall back to the struct field name if there is no json field name
dataString = "<no json tag> " + field.Name
}
// use the protobuf tag, which must be stable
dataInt := 0
if protobufTagParts := strings.Split(field.Tag.Get("protobuf"), ","); len(protobufTagParts) > 1 {
if tag, err := strconv.Atoi(protobufTagParts[1]); err != nil {
panic(err)
} else {
dataInt = tag
}
}
if dataInt == 0 {
// fall back to the length of dataString as a backup
dataInt = -len(dataString)
}
fieldType := field.Type
fieldValue := v.Field(i)
fill(dataString, dataInt, reflect.PointerTo(fieldType), fieldValue.Addr(), fillFuncs, filledTypes)
}
case reflect.Pointer:
fill(dataString, dataInt, t.Elem(), v.Elem(), fillFuncs, filledTypes)
case reflect.String:
// use Convert to set into string alias types correctly
v.Set(reflect.ValueOf(dataString + "Value").Convert(t))
case reflect.Bool:
// set to true to ensure we serialize omitempty fields
v.Set(reflect.ValueOf(true).Convert(t))
case reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64:
// use Convert to set into int alias types and different int widths correctly
v.Set(reflect.ValueOf(dataInt).Convert(t))
case reflect.Float32, reflect.Float64:
// use Convert to set into float types
v.Set(reflect.ValueOf(float32(dataInt) + 0.5).Convert(t))
default:
panic(fmt.Errorf("unhandled type %v in field %s", t, dataString))
}
}

View File

@ -0,0 +1,23 @@
//go:build !race
// +build !race
/*
Copyright 2021 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 roundtrip
// in non-race-detection mode, a higher number of iterations is reasonable
const defaultFuzzIters = 20

View File

@ -0,0 +1,23 @@
//go:build race
// +build race
/*
Copyright 2021 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 roundtrip
// in race-detection mode, lower the number of iterations to keep reasonable runtimes in CI
const defaultFuzzIters = 5

View File

@ -0,0 +1,434 @@
/*
Copyright 2017 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 roundtrip
import (
"bytes"
"encoding/hex"
"math/rand"
"reflect"
"strings"
"testing"
"github.com/google/go-cmp/cmp" //nolint:depguard
flag "github.com/spf13/pflag"
"sigs.k8s.io/randfill"
apitesting "k8s.io/apimachinery/pkg/api/apitesting"
"k8s.io/apimachinery/pkg/api/apitesting/fuzzer"
apiequality "k8s.io/apimachinery/pkg/api/equality"
apimeta "k8s.io/apimachinery/pkg/api/meta"
metafuzzer "k8s.io/apimachinery/pkg/apis/meta/fuzzer"
"k8s.io/apimachinery/pkg/runtime"
"k8s.io/apimachinery/pkg/runtime/schema"
runtimeserializer "k8s.io/apimachinery/pkg/runtime/serializer"
"k8s.io/apimachinery/pkg/runtime/serializer/json"
"k8s.io/apimachinery/pkg/runtime/serializer/protobuf"
"k8s.io/apimachinery/pkg/util/dump"
"k8s.io/apimachinery/pkg/util/sets"
)
type InstallFunc func(scheme *runtime.Scheme)
// RoundTripTestForAPIGroup is convenient to call from your install package to make sure that a "bare" install of your group provides
// enough information to round trip
func RoundTripTestForAPIGroup(t *testing.T, installFn InstallFunc, fuzzingFuncs fuzzer.FuzzerFuncs) {
scheme := runtime.NewScheme()
installFn(scheme)
RoundTripTestForScheme(t, scheme, fuzzingFuncs)
}
// RoundTripTestForScheme is convenient to call if you already have a scheme and want to make sure that its well-formed
func RoundTripTestForScheme(t *testing.T, scheme *runtime.Scheme, fuzzingFuncs fuzzer.FuzzerFuncs) {
codecFactory := runtimeserializer.NewCodecFactory(scheme)
f := fuzzer.FuzzerFor(
fuzzer.MergeFuzzerFuncs(metafuzzer.Funcs, fuzzingFuncs),
rand.NewSource(rand.Int63()),
codecFactory,
)
RoundTripTypesWithoutProtobuf(t, scheme, codecFactory, f, nil)
}
// RoundTripProtobufTestForAPIGroup is convenient to call from your install package to make sure that a "bare" install of your group provides
// enough information to round trip
func RoundTripProtobufTestForAPIGroup(t *testing.T, installFn InstallFunc, fuzzingFuncs fuzzer.FuzzerFuncs) {
scheme := runtime.NewScheme()
installFn(scheme)
RoundTripProtobufTestForScheme(t, scheme, fuzzingFuncs)
}
// RoundTripProtobufTestForScheme is convenient to call if you already have a scheme and want to make sure that its well-formed
func RoundTripProtobufTestForScheme(t *testing.T, scheme *runtime.Scheme, fuzzingFuncs fuzzer.FuzzerFuncs) {
codecFactory := runtimeserializer.NewCodecFactory(scheme)
fuzzer := fuzzer.FuzzerFor(
fuzzer.MergeFuzzerFuncs(metafuzzer.Funcs, fuzzingFuncs),
rand.NewSource(rand.Int63()),
codecFactory,
)
RoundTripTypes(t, scheme, codecFactory, fuzzer, nil)
}
var FuzzIters = flag.Int("fuzz-iters", defaultFuzzIters, "How many fuzzing iterations to do.")
// globalNonRoundTrippableTypes are kinds that are effectively reserved across all GroupVersions
// They don't roundtrip
var globalNonRoundTrippableTypes = sets.NewString(
"ExportOptions",
"GetOptions",
// WatchEvent does not include kind and version and can only be deserialized
// implicitly (if the caller expects the specific object). The watch call defines
// the schema by content type, rather than via kind/version included in each
// object.
"WatchEvent",
// ListOptions is now part of the meta group
"ListOptions",
// Delete options is only read in metav1
"DeleteOptions",
)
// GlobalNonRoundTrippableTypes returns the kinds that are effectively reserved across all GroupVersions.
// They don't roundtrip and thus can be excluded in any custom/downstream roundtrip tests
//
// kinds := scheme.AllKnownTypes()
// for gvk := range kinds {
// if roundtrip.GlobalNonRoundTrippableTypes().Has(gvk.Kind) {
// continue
// }
// t.Run(gvk.Group+"."+gvk.Version+"."+gvk.Kind, func(t *testing.T) {
// // roundtrip test
// })
// }
func GlobalNonRoundTrippableTypes() sets.String {
return sets.NewString(globalNonRoundTrippableTypes.List()...)
}
// RoundTripTypesWithoutProtobuf applies the round-trip test to all round-trippable Kinds
// in the scheme. It will skip all the GroupVersionKinds in the skip list.
func RoundTripTypesWithoutProtobuf(t *testing.T, scheme *runtime.Scheme, codecFactory runtimeserializer.CodecFactory, fuzzer *randfill.Filler, nonRoundTrippableTypes map[schema.GroupVersionKind]bool) {
roundTripTypes(t, scheme, codecFactory, fuzzer, nonRoundTrippableTypes, true)
}
func RoundTripTypes(t *testing.T, scheme *runtime.Scheme, codecFactory runtimeserializer.CodecFactory, fuzzer *randfill.Filler, nonRoundTrippableTypes map[schema.GroupVersionKind]bool) {
roundTripTypes(t, scheme, codecFactory, fuzzer, nonRoundTrippableTypes, false)
}
func roundTripTypes(t *testing.T, scheme *runtime.Scheme, codecFactory runtimeserializer.CodecFactory, fuzzer *randfill.Filler, nonRoundTrippableTypes map[schema.GroupVersionKind]bool, skipProtobuf bool) {
for _, group := range groupsFromScheme(scheme) {
t.Logf("starting group %q", group)
internalVersion := schema.GroupVersion{Group: group, Version: runtime.APIVersionInternal}
internalKindToGoType := scheme.KnownTypes(internalVersion)
for kind := range internalKindToGoType {
if globalNonRoundTrippableTypes.Has(kind) {
continue
}
internalGVK := internalVersion.WithKind(kind)
roundTripSpecificKind(t, internalGVK, scheme, codecFactory, fuzzer, nonRoundTrippableTypes, skipProtobuf)
}
t.Logf("finished group %q", group)
}
}
// RoundTripExternalTypes applies the round-trip test to all external round-trippable Kinds
// in the scheme. It will skip all the GroupVersionKinds in the nonRoundTripExternalTypes list .
func RoundTripExternalTypes(t *testing.T, scheme *runtime.Scheme, codecFactory runtimeserializer.CodecFactory, fuzzer *randfill.Filler, nonRoundTrippableTypes map[schema.GroupVersionKind]bool) {
kinds := scheme.AllKnownTypes()
for gvk := range kinds {
if gvk.Version == runtime.APIVersionInternal || globalNonRoundTrippableTypes.Has(gvk.Kind) {
continue
}
t.Run(gvk.Group+"."+gvk.Version+"."+gvk.Kind, func(t *testing.T) {
roundTripSpecificKind(t, gvk, scheme, codecFactory, fuzzer, nonRoundTrippableTypes, false)
})
}
}
// RoundTripExternalTypesWithoutProtobuf applies the round-trip test to all external round-trippable Kinds
// in the scheme. It will skip all the GroupVersionKinds in the nonRoundTripExternalTypes list.
func RoundTripExternalTypesWithoutProtobuf(t *testing.T, scheme *runtime.Scheme, codecFactory runtimeserializer.CodecFactory, fuzzer *randfill.Filler, nonRoundTrippableTypes map[schema.GroupVersionKind]bool) {
kinds := scheme.AllKnownTypes()
for gvk := range kinds {
if gvk.Version == runtime.APIVersionInternal || globalNonRoundTrippableTypes.Has(gvk.Kind) {
continue
}
t.Run(gvk.Group+"."+gvk.Version+"."+gvk.Kind, func(t *testing.T) {
roundTripSpecificKind(t, gvk, scheme, codecFactory, fuzzer, nonRoundTrippableTypes, true)
})
}
}
func RoundTripSpecificKindWithoutProtobuf(t *testing.T, gvk schema.GroupVersionKind, scheme *runtime.Scheme, codecFactory runtimeserializer.CodecFactory, fuzzer *randfill.Filler, nonRoundTrippableTypes map[schema.GroupVersionKind]bool) {
roundTripSpecificKind(t, gvk, scheme, codecFactory, fuzzer, nonRoundTrippableTypes, true)
}
func RoundTripSpecificKind(t *testing.T, gvk schema.GroupVersionKind, scheme *runtime.Scheme, codecFactory runtimeserializer.CodecFactory, fuzzer *randfill.Filler, nonRoundTrippableTypes map[schema.GroupVersionKind]bool) {
roundTripSpecificKind(t, gvk, scheme, codecFactory, fuzzer, nonRoundTrippableTypes, false)
}
func roundTripSpecificKind(t *testing.T, gvk schema.GroupVersionKind, scheme *runtime.Scheme, codecFactory runtimeserializer.CodecFactory, fuzzer *randfill.Filler, nonRoundTrippableTypes map[schema.GroupVersionKind]bool, skipProtobuf bool) {
if nonRoundTrippableTypes[gvk] {
t.Logf("skipping %v", gvk)
return
}
// Try a few times, since runTest uses random values.
for i := 0; i < *FuzzIters; i++ {
if gvk.Version == runtime.APIVersionInternal {
roundTripToAllExternalVersions(t, scheme, codecFactory, fuzzer, gvk, nonRoundTrippableTypes, skipProtobuf)
} else {
roundTripOfExternalType(t, scheme, codecFactory, fuzzer, gvk, skipProtobuf)
}
if t.Failed() {
break
}
}
}
// fuzzInternalObject fuzzes an arbitrary runtime object using the appropriate
// fuzzer registered with the apitesting package.
func fuzzInternalObject(t *testing.T, fuzzer *randfill.Filler, object runtime.Object) runtime.Object {
fuzzer.Fill(object)
j, err := apimeta.TypeAccessor(object)
if err != nil {
t.Fatalf("Unexpected error %v for %#v", err, object)
}
j.SetKind("")
j.SetAPIVersion("")
return object
}
func groupsFromScheme(scheme *runtime.Scheme) []string {
ret := sets.String{}
for gvk := range scheme.AllKnownTypes() {
ret.Insert(gvk.Group)
}
return ret.List()
}
func roundTripToAllExternalVersions(t *testing.T, scheme *runtime.Scheme, codecFactory runtimeserializer.CodecFactory, fuzzer *randfill.Filler, internalGVK schema.GroupVersionKind, nonRoundTrippableTypes map[schema.GroupVersionKind]bool, skipProtobuf bool) {
object, err := scheme.New(internalGVK)
if err != nil {
t.Fatalf("Couldn't make a %v? %v", internalGVK, err)
}
if _, err := apimeta.TypeAccessor(object); err != nil {
t.Fatalf("%q is not a TypeMeta and cannot be tested - add it to nonRoundTrippableInternalTypes: %v", internalGVK, err)
}
fuzzInternalObject(t, fuzzer, object)
// find all potential serializations in the scheme.
// TODO fix this up to handle kinds that cross registered with different names.
for externalGVK, externalGoType := range scheme.AllKnownTypes() {
if externalGVK.Version == runtime.APIVersionInternal {
continue
}
if externalGVK.GroupKind() != internalGVK.GroupKind() {
continue
}
if nonRoundTrippableTypes[externalGVK] {
t.Logf("\tskipping %v %v", externalGVK, externalGoType)
continue
}
t.Logf("\tround tripping to %v %v", externalGVK, externalGoType)
roundTrip(t, scheme, apitesting.TestCodec(codecFactory, externalGVK.GroupVersion()), object)
// TODO remove this hack after we're past the intermediate steps
if !skipProtobuf && externalGVK.Group != "kubeadm.k8s.io" {
s := protobuf.NewSerializer(scheme, scheme)
protobufCodec := codecFactory.CodecForVersions(s, s, externalGVK.GroupVersion(), nil)
roundTrip(t, scheme, protobufCodec, object)
}
}
}
func roundTripOfExternalType(t *testing.T, scheme *runtime.Scheme, codecFactory runtimeserializer.CodecFactory, fuzzer *randfill.Filler, externalGVK schema.GroupVersionKind, skipProtobuf bool) {
object, err := scheme.New(externalGVK)
if err != nil {
t.Fatalf("Couldn't make a %v? %v", externalGVK, err)
}
typeAcc, err := apimeta.TypeAccessor(object)
if err != nil {
t.Fatalf("%q is not a TypeMeta and cannot be tested - add it to nonRoundTrippableInternalTypes: %v", externalGVK, err)
}
fuzzInternalObject(t, fuzzer, object)
typeAcc.SetKind(externalGVK.Kind)
typeAcc.SetAPIVersion(externalGVK.GroupVersion().String())
roundTrip(t, scheme, json.NewSerializerWithOptions(json.DefaultMetaFactory, scheme, scheme, json.SerializerOptions{}), object)
// TODO remove this hack after we're past the intermediate steps
if !skipProtobuf {
roundTrip(t, scheme, protobuf.NewSerializer(scheme, scheme), object)
}
}
// roundTrip applies a single round-trip test to the given runtime object
// using the given codec. The round-trip test ensures that an object can be
// deep-copied, converted, marshaled and back without loss of data.
//
// For internal types this means
//
// internal -> external -> json/protobuf -> external -> internal.
//
// For external types this means
//
// external -> json/protobuf -> external.
func roundTrip(t *testing.T, scheme *runtime.Scheme, codec runtime.Codec, object runtime.Object) {
original := object
// deep copy the original object
object = object.DeepCopyObject()
name := reflect.TypeOf(object).Elem().Name()
if !apiequality.Semantic.DeepEqual(original, object) {
t.Errorf("%v: DeepCopy altered the object, diff: %v", name, cmp.Diff(original, object))
t.Errorf("%s", dump.Pretty(original))
t.Errorf("%s", dump.Pretty(object))
return
}
// encode (serialize) the deep copy using the provided codec
data, err := runtime.Encode(codec, object)
if err != nil {
if runtime.IsNotRegisteredError(err) {
t.Logf("%v: not registered: %v (%s)", name, err, dump.Pretty(object))
} else {
t.Errorf("%v: %v (%s)", name, err, dump.Pretty(object))
}
return
}
// ensure that the deep copy is equal to the original; neither the deep
// copy or conversion should alter the object
// TODO eliminate this global
if !apiequality.Semantic.DeepEqual(original, object) {
t.Errorf("%v: encode altered the object, diff: %v", name, cmp.Diff(original, object))
return
}
// encode (serialize) a second time to verify that it was not varying
secondData, err := runtime.Encode(codec, object)
if err != nil {
if runtime.IsNotRegisteredError(err) {
t.Logf("%v: not registered: %v (%s)", name, err, dump.Pretty(object))
} else {
t.Errorf("%v: %v (%s)", name, err, dump.Pretty(object))
}
return
}
// serialization to the wire must be stable to ensure that we don't write twice to the DB
// when the object hasn't changed.
if !bytes.Equal(data, secondData) {
t.Errorf("%v: serialization is not stable: %s", name, dump.Pretty(object))
}
// decode (deserialize) the encoded data back into an object
obj2, err := runtime.Decode(codec, data)
if err != nil {
t.Errorf("%v: %v\nCodec: %#v\nData: %s\nSource: %s", name, err, codec, dataAsString(data), dump.Pretty(object))
panic("failed")
}
// ensure that the object produced from decoding the encoded data is equal
// to the original object
if !apiequality.Semantic.DeepEqual(original, obj2) {
t.Errorf("%v: diff: %v\nCodec: %#v\nSource:\n\n%s\n\nEncoded:\n\n%s\n\nFinal:\n\n%s", name, cmp.Diff(original, obj2), codec, dump.Pretty(original), dataAsString(data), dump.Pretty(obj2))
return
}
// decode the encoded data into a new object (instead of letting the codec
// create a new object)
obj3 := reflect.New(reflect.TypeOf(object).Elem()).Interface().(runtime.Object)
if err := runtime.DecodeInto(codec, data, obj3); err != nil {
t.Errorf("%v: %v", name, err)
return
}
// special case for kinds which are internal and external at the same time (many in meta.k8s.io are). For those
// runtime.DecodeInto above will return the external variant and set the APIVersion and kind, while the input
// object might be internal. Hence, we clear those values for obj3 for that case to correctly compare.
intAndExt, err := internalAndExternalKind(scheme, object)
if err != nil {
t.Errorf("%v: %v", name, err)
return
}
if intAndExt {
typeAcc, err := apimeta.TypeAccessor(object)
if err != nil {
t.Fatalf("%v: error accessing TypeMeta: %v", name, err)
}
if len(typeAcc.GetAPIVersion()) == 0 {
typeAcc, err := apimeta.TypeAccessor(obj3)
if err != nil {
t.Fatalf("%v: error accessing TypeMeta: %v", name, err)
}
typeAcc.SetAPIVersion("")
typeAcc.SetKind("")
}
}
// ensure that the new runtime object is equal to the original after being
// decoded into
if !apiequality.Semantic.DeepEqual(object, obj3) {
t.Errorf("%v: diff: %v\nCodec: %#v", name, cmp.Diff(object, obj3), codec)
return
}
// do structure-preserving fuzzing of the deep-copied object. If it shares anything with the original,
// the deep-copy was actually only a shallow copy. Then original and obj3 will be different after fuzzing.
// NOTE: we use the encoding+decoding here as an alternative, guaranteed deep-copy to compare against.
fuzzer.ValueFuzz(object)
if !apiequality.Semantic.DeepEqual(original, obj3) {
t.Errorf("%v: fuzzing a copy altered the original, diff: %v", name, cmp.Diff(original, obj3))
return
}
}
func internalAndExternalKind(scheme *runtime.Scheme, object runtime.Object) (bool, error) {
kinds, _, err := scheme.ObjectKinds(object)
if err != nil {
return false, err
}
internal, external := false, false
for _, k := range kinds {
if k.Version == runtime.APIVersionInternal {
internal = true
} else {
external = true
}
}
return internal && external, nil
}
// dataAsString returns the given byte array as a string; handles detecting
// protocol buffers.
func dataAsString(data []byte) string {
dataString := string(data)
if !strings.HasPrefix(dataString, "{") {
dataString = "\n" + hex.Dump(data)
}
return dataString
}

View File

@ -0,0 +1,247 @@
/*
Copyright 2024 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 roundtrip
import (
"bytes"
"fmt"
"math/rand"
"os"
"strconv"
"testing"
"time"
"k8s.io/apimachinery/pkg/api/apitesting/fuzzer"
apiequality "k8s.io/apimachinery/pkg/api/equality"
metafuzzer "k8s.io/apimachinery/pkg/apis/meta/fuzzer"
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
"k8s.io/apimachinery/pkg/runtime"
"k8s.io/apimachinery/pkg/runtime/schema"
"k8s.io/apimachinery/pkg/runtime/serializer"
cborserializer "k8s.io/apimachinery/pkg/runtime/serializer/cbor"
cbor "k8s.io/apimachinery/pkg/runtime/serializer/cbor/direct"
jsonserializer "k8s.io/apimachinery/pkg/runtime/serializer/json"
"k8s.io/apimachinery/pkg/util/sets"
"github.com/google/go-cmp/cmp" //nolint:depguard
)
// RoundtripToUnstructured verifies the roundtrip faithfulness of all external types in a scheme
// from native to unstructured and back using both the JSON and CBOR serializers. The intermediate
// unstructured objects produced by both encodings must be identical and be themselves
// roundtrippable to JSON and CBOR.
//
// Values for all external types in the scheme are generated by fuzzing the a value of the
// corresponding internal type and converting it, except for types whose registered GVK appears in
// the "nointernal" set, which are fuzzed directly.
func RoundtripToUnstructured(t *testing.T, scheme *runtime.Scheme, funcs fuzzer.FuzzerFuncs, skipped sets.Set[schema.GroupVersionKind], nointernal sets.Set[schema.GroupVersionKind]) {
codecs := serializer.NewCodecFactory(scheme)
seed := int64(time.Now().Nanosecond())
if override := os.Getenv("TEST_RAND_SEED"); len(override) > 0 {
overrideSeed, err := strconv.ParseInt(override, 10, 64)
if err != nil {
t.Fatal(err)
}
seed = overrideSeed
t.Logf("using overridden seed: %d", seed)
} else {
t.Logf("seed (override with TEST_RAND_SEED if desired): %d", seed)
}
var buf bytes.Buffer
for gvk := range scheme.AllKnownTypes() {
if globalNonRoundTrippableTypes.Has(gvk.Kind) {
continue
}
if gvk.Version == runtime.APIVersionInternal {
continue
}
subtestName := fmt.Sprintf("%s.%s/%s", gvk.Version, gvk.Group, gvk.Kind)
if gvk.Group == "" {
subtestName = fmt.Sprintf("%s/%s", gvk.Version, gvk.Kind)
}
t.Run(subtestName, func(t *testing.T) {
if skipped.Has(gvk) {
t.SkipNow()
}
fuzzer := fuzzer.FuzzerFor(fuzzer.MergeFuzzerFuncs(metafuzzer.Funcs, funcs), rand.NewSource(seed), codecs)
for i := 0; i < *FuzzIters; i++ {
item, err := scheme.New(gvk)
if err != nil {
t.Fatalf("couldn't create external object %v: %v", gvk.Kind, err)
}
if nointernal.Has(gvk) {
fuzzer.Fill(item)
} else {
internalObj, err := scheme.New(gvk.GroupKind().WithVersion(runtime.APIVersionInternal))
if err != nil {
t.Fatalf("couldn't create internal object %v: %v", gvk.Kind, err)
}
fuzzer.Fill(internalObj)
if err := scheme.Convert(internalObj, item, nil); err != nil {
t.Fatalf("conversion for %v failed: %v", gvk.Kind, err)
}
}
// Decoding into Unstructured requires that apiVersion and kind be
// serialized, so populate TypeMeta.
item.GetObjectKind().SetGroupVersionKind(gvk)
jsonSerializer := jsonserializer.NewSerializerWithOptions(jsonserializer.DefaultMetaFactory, scheme, scheme, jsonserializer.SerializerOptions{})
cborSerializer := cborserializer.NewSerializer(scheme, scheme)
// original->JSON->Unstructured
buf.Reset()
if err := jsonSerializer.Encode(item, &buf); err != nil {
t.Fatalf("error encoding native to json: %v", err)
}
var uJSON runtime.Object = &unstructured.Unstructured{}
uJSON, _, err = jsonSerializer.Decode(buf.Bytes(), &gvk, uJSON)
if err != nil {
t.Fatalf("error decoding json to unstructured: %v", err)
}
// original->CBOR->Unstructured
buf.Reset()
if err := cborSerializer.Encode(item, &buf); err != nil {
t.Fatalf("error encoding native to cbor: %v", err)
}
var uCBOR runtime.Object = &unstructured.Unstructured{}
uCBOR, _, err = cborSerializer.Decode(buf.Bytes(), &gvk, uCBOR)
if err != nil {
diag, _ := cbor.Diagnose(buf.Bytes())
t.Fatalf("error decoding cbor to unstructured: %v, diag: %s", err, diag)
}
// original->JSON->Unstructured == original->CBOR->Unstructured
if !apiequality.Semantic.DeepEqual(uJSON, uCBOR) {
t.Fatalf("unstructured via json differed from unstructured via cbor: %v", cmp.Diff(uJSON, uCBOR))
}
// original->CBOR(nondeterministic)->Unstructured
buf.Reset()
if err := cborSerializer.EncodeNondeterministic(item, &buf); err != nil {
t.Fatalf("error encoding native to cbor: %v", err)
}
var uCBORNondeterministic runtime.Object = &unstructured.Unstructured{}
uCBORNondeterministic, _, err = cborSerializer.Decode(buf.Bytes(), &gvk, uCBORNondeterministic)
if err != nil {
diag, _ := cbor.Diagnose(buf.Bytes())
t.Fatalf("error decoding cbor to unstructured: %v, diag: %s", err, diag)
}
// original->CBOR->Unstructured == original->CBOR(nondeterministic)->Unstructured
if !apiequality.Semantic.DeepEqual(uCBOR, uCBORNondeterministic) {
t.Fatalf("unstructured via nondeterministic cbor differed from unstructured via cbor: %v", cmp.Diff(uCBOR, uCBORNondeterministic))
}
// original->JSON/CBOR->Unstructured == original->JSON/CBOR->Unstructured->JSON->Unstructured
buf.Reset()
if err := jsonSerializer.Encode(uJSON, &buf); err != nil {
t.Fatalf("error encoding unstructured to json: %v", err)
}
var uJSON2 runtime.Object = &unstructured.Unstructured{}
uJSON2, _, err = jsonSerializer.Decode(buf.Bytes(), &gvk, uJSON2)
if err != nil {
t.Fatalf("error decoding json to unstructured: %v", err)
}
if !apiequality.Semantic.DeepEqual(uJSON, uJSON2) {
t.Errorf("object changed during native-json-unstructured-json-unstructured roundtrip, diff: %s", cmp.Diff(uJSON, uJSON2))
}
// original->JSON/CBOR->Unstructured == original->JSON/CBOR->Unstructured->CBOR->Unstructured
buf.Reset()
if err := cborSerializer.Encode(uCBOR, &buf); err != nil {
t.Fatalf("error encoding unstructured to cbor: %v", err)
}
var uCBOR2 runtime.Object = &unstructured.Unstructured{}
uCBOR2, _, err = cborSerializer.Decode(buf.Bytes(), &gvk, uCBOR2)
if err != nil {
diag, _ := cbor.Diagnose(buf.Bytes())
t.Fatalf("error decoding cbor to unstructured: %v, diag: %s", err, diag)
}
if !apiequality.Semantic.DeepEqual(uCBOR, uCBOR2) {
t.Errorf("object changed during native-cbor-unstructured-cbor-unstructured roundtrip, diff: %s", cmp.Diff(uCBOR, uCBOR2))
}
// original->JSON/CBOR->Unstructured->CBOR->Unstructured == original->JSON/CBOR->Unstructured->CBOR(nondeterministic)->Unstructured
buf.Reset()
if err := cborSerializer.EncodeNondeterministic(uCBOR, &buf); err != nil {
t.Fatalf("error encoding unstructured to cbor: %v", err)
}
var uCBOR2Nondeterministic runtime.Object = &unstructured.Unstructured{}
uCBOR2Nondeterministic, _, err = cborSerializer.Decode(buf.Bytes(), &gvk, uCBOR2Nondeterministic)
if err != nil {
diag, _ := cbor.Diagnose(buf.Bytes())
t.Fatalf("error decoding cbor to unstructured: %v, diag: %s", err, diag)
}
if !apiequality.Semantic.DeepEqual(uCBOR, uCBOR2Nondeterministic) {
t.Errorf("object changed during native-cbor-unstructured-cbor(nondeterministic)-unstructured roundtrip, diff: %s", cmp.Diff(uCBOR, uCBOR2Nondeterministic))
}
// original->JSON/CBOR->Unstructured->JSON->final == original
buf.Reset()
if err := jsonSerializer.Encode(uJSON, &buf); err != nil {
t.Fatalf("error encoding unstructured to json: %v", err)
}
finalJSON, _, err := jsonSerializer.Decode(buf.Bytes(), &gvk, nil)
if err != nil {
t.Fatalf("error decoding json to native: %v", err)
}
if !apiequality.Semantic.DeepEqual(item, finalJSON) {
t.Errorf("object changed during native-json-unstructured-json-native roundtrip, diff: %s", cmp.Diff(item, finalJSON))
}
// original->JSON/CBOR->Unstructured->CBOR->final == original
buf.Reset()
if err := cborSerializer.Encode(uCBOR, &buf); err != nil {
t.Fatalf("error encoding unstructured to cbor: %v", err)
}
finalCBOR, _, err := cborSerializer.Decode(buf.Bytes(), &gvk, nil)
if err != nil {
diag, _ := cbor.Diagnose(buf.Bytes())
t.Fatalf("error decoding cbor to native: %v, diag: %s", err, diag)
}
if !apiequality.Semantic.DeepEqual(item, finalCBOR) {
t.Errorf("object changed during native-cbor-unstructured-cbor-native roundtrip, diff: %s", cmp.Diff(item, finalCBOR))
}
// original->JSON/CBOR->Unstructured->CBOR(nondeterministic)->final == original
buf.Reset()
if err := cborSerializer.EncodeNondeterministic(uCBOR, &buf); err != nil {
t.Fatalf("error encoding unstructured to cbor: %v", err)
}
finalCBORNondeterministic, _, err := cborSerializer.Decode(buf.Bytes(), &gvk, nil)
if err != nil {
diag, _ := cbor.Diagnose(buf.Bytes())
t.Fatalf("error decoding cbor to native: %v, diag: %s", err, diag)
}
if !apiequality.Semantic.DeepEqual(item, finalCBORNondeterministic) {
t.Errorf("object changed during native-cbor-unstructured-cbor-native roundtrip, diff: %s", cmp.Diff(item, finalCBORNondeterministic))
}
}
})
}
}

View File

@ -0,0 +1,339 @@
/*
Copyright 2017 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 fuzzer
import (
"fmt"
"math/rand"
"sort"
"strconv"
"strings"
"sigs.k8s.io/randfill"
apitesting "k8s.io/apimachinery/pkg/api/apitesting"
"k8s.io/apimachinery/pkg/api/apitesting/fuzzer"
"k8s.io/apimachinery/pkg/api/resource"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
metav1beta1 "k8s.io/apimachinery/pkg/apis/meta/v1beta1"
"k8s.io/apimachinery/pkg/runtime"
runtimeserializer "k8s.io/apimachinery/pkg/runtime/serializer"
"k8s.io/apimachinery/pkg/types"
"k8s.io/apimachinery/pkg/util/sets"
)
func genericFuzzerFuncs(codecs runtimeserializer.CodecFactory) []interface{} {
return []interface{}{
func(q *resource.Quantity, c randfill.Continue) {
*q = *resource.NewQuantity(c.Int63n(1000), resource.DecimalExponent)
},
func(j *int, c randfill.Continue) {
*j = int(c.Int31())
},
func(j **int, c randfill.Continue) {
if c.Bool() {
i := int(c.Int31())
*j = &i
} else {
*j = nil
}
},
func(j *runtime.TypeMeta, c randfill.Continue) {
// We have to customize the randomization of TypeMetas because their
// APIVersion and Kind must remain blank in memory.
j.APIVersion = ""
j.Kind = ""
},
func(j *runtime.Object, c randfill.Continue) {
// TODO: uncomment when round trip starts from a versioned object
if true { // c.Bool() {
*j = &runtime.Unknown{
// We do not set TypeMeta here because it is not carried through a round trip
Raw: []byte(`{"apiVersion":"unknown.group/unknown","kind":"Something","someKey":"someValue"}`),
ContentType: runtime.ContentTypeJSON,
}
} else {
types := []runtime.Object{&metav1.Status{}, &metav1.APIGroup{}}
t := types[c.Rand.Intn(len(types))]
c.Fill(t)
*j = t
}
},
func(r *runtime.RawExtension, c randfill.Continue) {
// Pick an arbitrary type and fuzz it
types := []runtime.Object{&metav1.Status{}, &metav1.APIGroup{}}
obj := types[c.Rand.Intn(len(types))]
c.Fill(obj)
// Find a codec for converting the object to raw bytes. This is necessary for the
// api version and kind to be correctly set be serialization.
var codec = apitesting.TestCodec(codecs, metav1.SchemeGroupVersion)
// Convert the object to raw bytes
bytes, err := runtime.Encode(codec, obj)
if err != nil {
panic(fmt.Sprintf("Failed to encode object: %v", err))
}
// strip trailing newlines which do not survive roundtrips
for len(bytes) >= 1 && bytes[len(bytes)-1] == 10 {
bytes = bytes[:len(bytes)-1]
}
// Set the bytes field on the RawExtension
r.Raw = bytes
},
}
}
// taken from randfill (nee gofuzz) internals for RandString
type charRange struct {
first, last rune
}
func (c *charRange) choose(r *rand.Rand) rune {
count := int64(c.last - c.first + 1)
ch := c.first + rune(r.Int63n(count))
return ch
}
// randomLabelPart produces a valid random label value or name-part
// of a label key.
func randomLabelPart(c randfill.Continue, canBeEmpty bool) string {
validStartEnd := []charRange{{'0', '9'}, {'a', 'z'}, {'A', 'Z'}}
validMiddle := []charRange{{'0', '9'}, {'a', 'z'}, {'A', 'Z'},
{'.', '.'}, {'-', '-'}, {'_', '_'}}
partLen := c.Rand.Intn(64) // len is [0, 63]
if !canBeEmpty {
partLen = c.Rand.Intn(63) + 1 // len is [1, 63]
}
runes := make([]rune, partLen)
if partLen == 0 {
return string(runes)
}
runes[0] = validStartEnd[c.Rand.Intn(len(validStartEnd))].choose(c.Rand)
for i := range runes[1:] {
runes[i+1] = validMiddle[c.Rand.Intn(len(validMiddle))].choose(c.Rand)
}
runes[len(runes)-1] = validStartEnd[c.Rand.Intn(len(validStartEnd))].choose(c.Rand)
return string(runes)
}
func randomDNSLabel(c randfill.Continue) string {
validStartEnd := []charRange{{'0', '9'}, {'a', 'z'}}
validMiddle := []charRange{{'0', '9'}, {'a', 'z'}, {'-', '-'}}
partLen := c.Rand.Intn(63) + 1 // len is [1, 63]
runes := make([]rune, partLen)
runes[0] = validStartEnd[c.Rand.Intn(len(validStartEnd))].choose(c.Rand)
for i := range runes[1:] {
runes[i+1] = validMiddle[c.Rand.Intn(len(validMiddle))].choose(c.Rand)
}
runes[len(runes)-1] = validStartEnd[c.Rand.Intn(len(validStartEnd))].choose(c.Rand)
return string(runes)
}
func randomLabelKey(c randfill.Continue) string {
namePart := randomLabelPart(c, false)
prefixPart := ""
usePrefix := c.Bool()
if usePrefix {
// we can fit, with dots, at most 3 labels in the 253 allotted characters
prefixPartsLen := c.Rand.Intn(2) + 1
prefixParts := make([]string, prefixPartsLen)
for i := range prefixParts {
prefixParts[i] = randomDNSLabel(c)
}
prefixPart = strings.Join(prefixParts, ".") + "/"
}
return prefixPart + namePart
}
func v1FuzzerFuncs(codecs runtimeserializer.CodecFactory) []interface{} {
return []interface{}{
func(j *metav1.TypeMeta, c randfill.Continue) {
// We have to customize the randomization of TypeMetas because their
// APIVersion and Kind must remain blank in memory.
j.APIVersion = ""
j.Kind = ""
},
func(j *metav1.ObjectMeta, c randfill.Continue) {
c.FillNoCustom(j)
j.ResourceVersion = strconv.FormatUint(c.Uint64(), 10)
j.UID = types.UID(c.String(0))
// Fuzzing sec and nsec in a smaller range (uint32 instead of int64),
// so that the result Unix time is a valid date and can be parsed into RFC3339 format.
var sec, nsec uint32
c.Fill(&sec)
c.Fill(&nsec)
j.CreationTimestamp = metav1.Unix(int64(sec), int64(nsec)).Rfc3339Copy()
if j.DeletionTimestamp != nil {
c.Fill(&sec)
c.Fill(&nsec)
t := metav1.Unix(int64(sec), int64(nsec)).Rfc3339Copy()
j.DeletionTimestamp = &t
}
if len(j.Labels) == 0 {
j.Labels = nil
} else {
delete(j.Labels, "")
}
if len(j.Annotations) == 0 {
j.Annotations = nil
} else {
delete(j.Annotations, "")
}
if len(j.OwnerReferences) == 0 {
j.OwnerReferences = nil
}
if len(j.Finalizers) == 0 {
j.Finalizers = nil
}
},
func(j *metav1.ResourceVersionMatch, c randfill.Continue) {
matches := []metav1.ResourceVersionMatch{"", metav1.ResourceVersionMatchExact, metav1.ResourceVersionMatchNotOlderThan}
*j = matches[c.Rand.Intn(len(matches))]
},
func(j *metav1.ListMeta, c randfill.Continue) {
j.ResourceVersion = strconv.FormatUint(c.Uint64(), 10)
j.SelfLink = c.String(0) //nolint:staticcheck // SA1019 backwards compatibility
},
func(j *metav1.LabelSelector, c randfill.Continue) {
c.FillNoCustom(j)
// we can't have an entirely empty selector, so force
// use of MatchExpression if necessary
if len(j.MatchLabels) == 0 && len(j.MatchExpressions) == 0 {
j.MatchExpressions = make([]metav1.LabelSelectorRequirement, c.Rand.Intn(2)+1)
}
if j.MatchLabels != nil {
fuzzedMatchLabels := make(map[string]string, len(j.MatchLabels))
for i := 0; i < len(j.MatchLabels); i++ {
fuzzedMatchLabels[randomLabelKey(c)] = randomLabelPart(c, true)
}
j.MatchLabels = fuzzedMatchLabels
}
validOperators := []metav1.LabelSelectorOperator{
metav1.LabelSelectorOpIn,
metav1.LabelSelectorOpNotIn,
metav1.LabelSelectorOpExists,
metav1.LabelSelectorOpDoesNotExist,
}
if j.MatchExpressions != nil {
// NB: the label selector parser code sorts match expressions by key, and
// sorts and deduplicates the values, so we need to make sure ours are
// sorted and deduplicated as well here to preserve round-trip comparison.
// In practice, not sorting doesn't hurt anything...
for i := range j.MatchExpressions {
req := metav1.LabelSelectorRequirement{}
c.Fill(&req)
req.Key = randomLabelKey(c)
req.Operator = validOperators[c.Rand.Intn(len(validOperators))]
if req.Operator == metav1.LabelSelectorOpIn || req.Operator == metav1.LabelSelectorOpNotIn {
if len(req.Values) == 0 {
// we must have some values here, so randomly choose a short length
req.Values = make([]string, c.Rand.Intn(2)+1)
}
for i := range req.Values {
req.Values[i] = randomLabelPart(c, true)
}
req.Values = sets.List(sets.New(req.Values...))
} else {
req.Values = nil
}
j.MatchExpressions[i] = req
}
sort.Slice(j.MatchExpressions, func(a, b int) bool { return j.MatchExpressions[a].Key < j.MatchExpressions[b].Key })
}
},
func(j *metav1.ManagedFieldsEntry, c randfill.Continue) {
c.FillNoCustom(j)
j.FieldsV1 = nil
},
}
}
func v1beta1FuzzerFuncs(codecs runtimeserializer.CodecFactory) []interface{} {
return []interface{}{
func(r *metav1beta1.TableOptions, c randfill.Continue) {
c.FillNoCustom(r)
// NoHeaders is not serialized to the wire but is allowed within the versioned
// type because we don't use meta internal types in the client and API server.
r.NoHeaders = false
},
func(r *metav1beta1.TableRow, c randfill.Continue) {
c.Fill(&r.Object)
c.Fill(&r.Conditions)
if len(r.Conditions) == 0 {
r.Conditions = nil
}
n := c.Intn(10)
if n > 0 {
r.Cells = make([]interface{}, n)
}
for i := range r.Cells {
t := c.Intn(6)
switch t {
case 0:
r.Cells[i] = c.String(0)
case 1:
r.Cells[i] = c.Int63()
case 2:
r.Cells[i] = c.Bool()
case 3:
x := map[string]interface{}{}
for j := c.Intn(10) + 1; j >= 0; j-- {
x[c.String(0)] = c.String(0)
}
r.Cells[i] = x
case 4:
x := make([]interface{}, c.Intn(10))
for i := range x {
x[i] = c.Int63()
}
r.Cells[i] = x
default:
r.Cells[i] = nil
}
}
},
}
}
var Funcs = fuzzer.MergeFuzzerFuncs(
genericFuzzerFuncs,
v1FuzzerFuncs,
v1beta1FuzzerFuncs,
)

4
vendor/modules.txt generated vendored
View File

@ -1518,6 +1518,9 @@ k8s.io/apiextensions-apiserver/pkg/apis/apiextensions/v1
k8s.io/apiextensions-apiserver/pkg/apis/apiextensions/v1beta1
# k8s.io/apimachinery v0.33.2
## explicit; go 1.24.0
k8s.io/apimachinery/pkg/api/apitesting
k8s.io/apimachinery/pkg/api/apitesting/fuzzer
k8s.io/apimachinery/pkg/api/apitesting/roundtrip
k8s.io/apimachinery/pkg/api/equality
k8s.io/apimachinery/pkg/api/errors
k8s.io/apimachinery/pkg/api/meta
@ -1525,6 +1528,7 @@ k8s.io/apimachinery/pkg/api/meta/testrestmapper
k8s.io/apimachinery/pkg/api/operation
k8s.io/apimachinery/pkg/api/resource
k8s.io/apimachinery/pkg/api/validation
k8s.io/apimachinery/pkg/apis/meta/fuzzer
k8s.io/apimachinery/pkg/apis/meta/internalversion
k8s.io/apimachinery/pkg/apis/meta/internalversion/scheme
k8s.io/apimachinery/pkg/apis/meta/internalversion/validation