mirror of
				https://github.com/k3s-io/kubernetes.git
				synced 2025-10-30 13:19:35 +00:00 
			
		
		
		
	pkg/api/testing: cut off fuzzers from static codecs
This commit is contained in:
		| @@ -31,7 +31,7 @@ import ( | |||||||
| ) | ) | ||||||
|  |  | ||||||
| func BenchmarkPodConversion(b *testing.B) { | func BenchmarkPodConversion(b *testing.B) { | ||||||
| 	apiObjectFuzzer := apitesting.FuzzerFor(kapitesting.FuzzerFuncs(b), rand.NewSource(benchmarkSeed)) | 	apiObjectFuzzer := apitesting.FuzzerFor(kapitesting.FuzzerFuncs(b, api.Codecs), rand.NewSource(benchmarkSeed)) | ||||||
| 	items := make([]api.Pod, 4) | 	items := make([]api.Pod, 4) | ||||||
| 	for i := range items { | 	for i := range items { | ||||||
| 		apiObjectFuzzer.Fuzz(&items[i]) | 		apiObjectFuzzer.Fuzz(&items[i]) | ||||||
|   | |||||||
| @@ -35,7 +35,7 @@ import ( | |||||||
| func TestDeepCopyApiObjects(t *testing.T) { | func TestDeepCopyApiObjects(t *testing.T) { | ||||||
| 	for i := 0; i < *fuzzIters; i++ { | 	for i := 0; i < *fuzzIters; i++ { | ||||||
| 		for _, version := range []schema.GroupVersion{testapi.Default.InternalGroupVersion(), api.Registry.GroupOrDie(api.GroupName).GroupVersion} { | 		for _, version := range []schema.GroupVersion{testapi.Default.InternalGroupVersion(), api.Registry.GroupOrDie(api.GroupName).GroupVersion} { | ||||||
| 			f := apitesting.FuzzerFor(kapitesting.FuzzerFuncs(t), rand.NewSource(rand.Int63())) | 			f := apitesting.FuzzerFor(kapitesting.FuzzerFuncs(t, api.Codecs), rand.NewSource(rand.Int63())) | ||||||
| 			for kind := range api.Scheme.KnownTypes(version) { | 			for kind := range api.Scheme.KnownTypes(version) { | ||||||
| 				doDeepCopyTest(t, version.WithKind(kind), f) | 				doDeepCopyTest(t, version.WithKind(kind), f) | ||||||
| 			} | 			} | ||||||
| @@ -84,7 +84,7 @@ func doDeepCopyTest(t *testing.T, kind schema.GroupVersionKind, f *fuzz.Fuzzer) | |||||||
| func TestDeepCopySingleType(t *testing.T) { | func TestDeepCopySingleType(t *testing.T) { | ||||||
| 	for i := 0; i < *fuzzIters; i++ { | 	for i := 0; i < *fuzzIters; i++ { | ||||||
| 		for _, version := range []schema.GroupVersion{testapi.Default.InternalGroupVersion(), api.Registry.GroupOrDie(api.GroupName).GroupVersion} { | 		for _, version := range []schema.GroupVersion{testapi.Default.InternalGroupVersion(), api.Registry.GroupOrDie(api.GroupName).GroupVersion} { | ||||||
| 			f := apitesting.FuzzerFor(kapitesting.FuzzerFuncs(t), rand.NewSource(rand.Int63())) | 			f := apitesting.FuzzerFor(kapitesting.FuzzerFuncs(t, api.Codecs), rand.NewSource(rand.Int63())) | ||||||
| 			doDeepCopyTest(t, version.WithKind("Pod"), f) | 			doDeepCopyTest(t, version.WithKind("Pod"), f) | ||||||
| 		} | 		} | ||||||
| 	} | 	} | ||||||
|   | |||||||
| @@ -77,7 +77,7 @@ func TestUniversalDeserializer(t *testing.T) { | |||||||
|  |  | ||||||
| func TestProtobufRoundTrip(t *testing.T) { | func TestProtobufRoundTrip(t *testing.T) { | ||||||
| 	obj := &v1.Pod{} | 	obj := &v1.Pod{} | ||||||
| 	apitesting.FuzzerFor(kapitesting.FuzzerFuncs(t), rand.NewSource(benchmarkSeed)).Fuzz(obj) | 	apitesting.FuzzerFor(kapitesting.FuzzerFuncs(t, api.Codecs), rand.NewSource(benchmarkSeed)).Fuzz(obj) | ||||||
| 	// InitContainers are turned into annotations by conversion. | 	// InitContainers are turned into annotations by conversion. | ||||||
| 	obj.Spec.InitContainers = nil | 	obj.Spec.InitContainers = nil | ||||||
| 	obj.Status.InitContainerStatuses = nil | 	obj.Status.InitContainerStatuses = nil | ||||||
|   | |||||||
| @@ -65,7 +65,7 @@ var codecsToTest = []func(version schema.GroupVersion, item runtime.Object) (run | |||||||
| // fuzzInternalObject fuzzes an arbitrary runtime object using the appropriate | // fuzzInternalObject fuzzes an arbitrary runtime object using the appropriate | ||||||
| // fuzzer registered with the apitesting package. | // fuzzer registered with the apitesting package. | ||||||
| func fuzzInternalObject(t *testing.T, forVersion schema.GroupVersion, item runtime.Object, seed int64) runtime.Object { | func fuzzInternalObject(t *testing.T, forVersion schema.GroupVersion, item runtime.Object, seed int64) runtime.Object { | ||||||
| 	apitesting.FuzzerFor(kapitesting.FuzzerFuncs(t), rand.NewSource(seed)).Fuzz(item) | 	apitesting.FuzzerFor(kapitesting.FuzzerFuncs(t, api.Codecs), rand.NewSource(seed)).Fuzz(item) | ||||||
|  |  | ||||||
| 	j, err := meta.TypeAccessor(item) | 	j, err := meta.TypeAccessor(item) | ||||||
| 	if err != nil { | 	if err != nil { | ||||||
| @@ -442,7 +442,7 @@ func TestUnversionedTypes(t *testing.T) { | |||||||
| // TestObjectWatchFraming establishes that a watch event can be encoded and | // TestObjectWatchFraming establishes that a watch event can be encoded and | ||||||
| // decoded correctly through each of the supported RFC2046 media types. | // decoded correctly through each of the supported RFC2046 media types. | ||||||
| func TestObjectWatchFraming(t *testing.T) { | func TestObjectWatchFraming(t *testing.T) { | ||||||
| 	f := apitesting.FuzzerFor(kapitesting.FuzzerFuncs(t), rand.NewSource(benchmarkSeed)) | 	f := apitesting.FuzzerFor(kapitesting.FuzzerFuncs(t, api.Codecs), rand.NewSource(benchmarkSeed)) | ||||||
| 	secret := &api.Secret{} | 	secret := &api.Secret{} | ||||||
| 	f.Fuzz(secret) | 	f.Fuzz(secret) | ||||||
| 	secret.Data["binary"] = []byte{0x00, 0x10, 0x30, 0x55, 0xff, 0x00} | 	secret.Data["binary"] = []byte{0x00, 0x10, 0x30, 0x55, 0xff, 0x00} | ||||||
| @@ -524,7 +524,7 @@ func TestObjectWatchFraming(t *testing.T) { | |||||||
| const benchmarkSeed = 100 | const benchmarkSeed = 100 | ||||||
|  |  | ||||||
| func benchmarkItems(b *testing.B) []v1.Pod { | func benchmarkItems(b *testing.B) []v1.Pod { | ||||||
| 	apiObjectFuzzer := apitesting.FuzzerFor(kapitesting.FuzzerFuncs(b), rand.NewSource(benchmarkSeed)) | 	apiObjectFuzzer := apitesting.FuzzerFor(kapitesting.FuzzerFuncs(b, api.Codecs), rand.NewSource(benchmarkSeed)) | ||||||
| 	items := make([]v1.Pod, 10) | 	items := make([]v1.Pod, 10) | ||||||
| 	for i := range items { | 	for i := range items { | ||||||
| 		var pod api.Pod | 		var pod api.Pod | ||||||
|   | |||||||
| @@ -30,6 +30,7 @@ import ( | |||||||
| 	"k8s.io/apimachinery/pkg/labels" | 	"k8s.io/apimachinery/pkg/labels" | ||||||
| 	"k8s.io/apimachinery/pkg/runtime" | 	"k8s.io/apimachinery/pkg/runtime" | ||||||
| 	"k8s.io/apimachinery/pkg/runtime/schema" | 	"k8s.io/apimachinery/pkg/runtime/schema" | ||||||
|  | 	runtimeserializer "k8s.io/apimachinery/pkg/runtime/serializer" | ||||||
| 	"k8s.io/apimachinery/pkg/util/intstr" | 	"k8s.io/apimachinery/pkg/util/intstr" | ||||||
| 	"k8s.io/kubernetes/cmd/kubeadm/app/apis/kubeadm" | 	"k8s.io/kubernetes/cmd/kubeadm/app/apis/kubeadm" | ||||||
| 	"k8s.io/kubernetes/pkg/api" | 	"k8s.io/kubernetes/pkg/api" | ||||||
| @@ -602,9 +603,9 @@ func certificateFuncs(t apitesting.TestingCommon) []interface{} { | |||||||
| 	} | 	} | ||||||
| } | } | ||||||
|  |  | ||||||
| func FuzzerFuncs(t apitesting.TestingCommon) []interface{} { | func FuzzerFuncs(t apitesting.TestingCommon, codecs runtimeserializer.CodecFactory) []interface{} { | ||||||
| 	return mergeFuncLists(t, | 	return mergeFuncLists(t, | ||||||
| 		apitesting.GenericFuzzerFuncs(t), | 		apitesting.GenericFuzzerFuncs(t, codecs), | ||||||
| 		overrideGenericFuncs(t), | 		overrideGenericFuncs(t), | ||||||
| 		coreFuncs(t), | 		coreFuncs(t), | ||||||
| 		extensionFuncs(t), | 		extensionFuncs(t), | ||||||
|   | |||||||
| @@ -43,7 +43,7 @@ func doRoundTrip(t *testing.T, group testapi.TestGroup, kind string) { | |||||||
| 		t.Fatalf("Couldn't create internal object %v: %v", kind, err) | 		t.Fatalf("Couldn't create internal object %v: %v", kind, err) | ||||||
| 	} | 	} | ||||||
| 	seed := rand.Int63() | 	seed := rand.Int63() | ||||||
| 	apitesting.FuzzerFor(kapitesting.FuzzerFuncs(t), rand.NewSource(seed)). | 	apitesting.FuzzerFor(kapitesting.FuzzerFuncs(t, api.Codecs), rand.NewSource(seed)). | ||||||
| 		// We are explicitly overwriting custom fuzzing functions, to ensure | 		// We are explicitly overwriting custom fuzzing functions, to ensure | ||||||
| 		// that InitContainers and their statuses are not generated. This is | 		// that InitContainers and their statuses are not generated. This is | ||||||
| 		// because in thise test we are simply doing json operations, in which | 		// because in thise test we are simply doing json operations, in which | ||||||
|   | |||||||
| @@ -149,7 +149,7 @@ func TestValidateOk(t *testing.T) { | |||||||
| 	} | 	} | ||||||
|  |  | ||||||
| 	seed := rand.Int63() | 	seed := rand.Int63() | ||||||
| 	apiObjectFuzzer := apitesting.FuzzerFor(kapitesting.FuzzerFuncs(t), rand.NewSource(seed)) | 	apiObjectFuzzer := apitesting.FuzzerFor(kapitesting.FuzzerFuncs(t, api.Codecs), rand.NewSource(seed)) | ||||||
| 	for i := 0; i < 5; i++ { | 	for i := 0; i < 5; i++ { | ||||||
| 		for _, test := range tests { | 		for _, test := range tests { | ||||||
| 			testObj := test.obj | 			testObj := test.obj | ||||||
|   | |||||||
| @@ -21,37 +21,32 @@ import ( | |||||||
| 	"mime" | 	"mime" | ||||||
| 	"fmt" | 	"fmt" | ||||||
| 
 | 
 | ||||||
| 	metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" |  | ||||||
| 	"k8s.io/apimachinery/pkg/runtime" | 	"k8s.io/apimachinery/pkg/runtime" | ||||||
| 	runtimeserializer "k8s.io/apimachinery/pkg/runtime/serializer" | 	runtimeserializer "k8s.io/apimachinery/pkg/runtime/serializer" | ||||||
| 	"k8s.io/apimachinery/pkg/runtime/schema" | 	"k8s.io/apimachinery/pkg/runtime/schema" | ||||||
| ) | ) | ||||||
| 
 | 
 | ||||||
| var scheme = runtime.NewScheme() | var testCodecMediaType string | ||||||
| var codecs = runtimeserializer.NewCodecFactory(scheme) |  | ||||||
| var serializer runtime.SerializerInfo |  | ||||||
| 
 | 
 | ||||||
| // Codec returns the codec for the API version to test against, as set by the | // TestCodec returns the codec for the API version to test against, as set by the | ||||||
| // KUBE_TEST_API_TYPE env var. | // KUBE_TEST_API_TYPE env var. | ||||||
| func Codec(gvs ...schema.GroupVersion) runtime.Codec { | func TestCodec(codecs runtimeserializer.CodecFactory, gvs ...schema.GroupVersion) runtime.Codec { | ||||||
| 	if serializer.Serializer == nil { | 	if len(testCodecMediaType) != 0 { | ||||||
| 		return codecs.LegacyCodec(gvs...) | 		serializer, ok := runtime.SerializerInfoForMediaType(codecs.SupportedMediaTypes(), testCodecMediaType) | ||||||
|  | 		if !ok { | ||||||
|  | 			panic(fmt.Sprintf("no serializer for %s", testCodecMediaType)) | ||||||
|  | 		} | ||||||
|  | 		return codecs.CodecForVersions(serializer.Serializer, codecs.UniversalDeserializer(), schema.GroupVersions(gvs), nil) | ||||||
| 	} | 	} | ||||||
| 	return codecs.CodecForVersions(serializer.Serializer, codecs.UniversalDeserializer(), schema.GroupVersions(gvs), nil) | 	return codecs.LegacyCodec(gvs...) | ||||||
| } | } | ||||||
| 
 | 
 | ||||||
| func init() { | func init() { | ||||||
| 	metav1.AddToGroupVersion(scheme, metav1.SchemeGroupVersion) |  | ||||||
| 
 |  | ||||||
| 	if apiMediaType := os.Getenv("KUBE_TEST_API_TYPE"); len(apiMediaType) > 0 { | 	if apiMediaType := os.Getenv("KUBE_TEST_API_TYPE"); len(apiMediaType) > 0 { | ||||||
| 		var ok bool | 		var err error | ||||||
| 		mediaType, _, err := mime.ParseMediaType(apiMediaType) | 		testCodecMediaType, _, err = mime.ParseMediaType(apiMediaType) | ||||||
| 		if err != nil { | 		if err != nil { | ||||||
| 			panic(err) | 			panic(err) | ||||||
| 		} | 		} | ||||||
| 		serializer, ok = runtime.SerializerInfoForMediaType(codecs.SupportedMediaTypes(), mediaType) |  | ||||||
| 		if !ok { |  | ||||||
| 			panic(fmt.Sprintf("no serializer for %s", apiMediaType)) |  | ||||||
| 		} |  | ||||||
| 	} | 	} | ||||||
| } | } | ||||||
| @@ -26,9 +26,10 @@ import ( | |||||||
| 	metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" | 	metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" | ||||||
| 	"k8s.io/apimachinery/pkg/runtime" | 	"k8s.io/apimachinery/pkg/runtime" | ||||||
| 	"k8s.io/apimachinery/pkg/types" | 	"k8s.io/apimachinery/pkg/types" | ||||||
|  | 	runtimeserializer "k8s.io/apimachinery/pkg/runtime/serializer" | ||||||
| ) | ) | ||||||
|  |  | ||||||
| func GenericFuzzerFuncs(t TestingCommon) []interface{} { | func GenericFuzzerFuncs(t TestingCommon, codecs runtimeserializer.CodecFactory) []interface{} { | ||||||
| 	return []interface{}{ | 	return []interface{}{ | ||||||
| 		func(j *int, c fuzz.Continue) { | 		func(j *int, c fuzz.Continue) { | ||||||
| 			*j = int(c.Int31()) | 			*j = int(c.Int31()) | ||||||
| @@ -92,7 +93,7 @@ func GenericFuzzerFuncs(t TestingCommon) []interface{} { | |||||||
|  |  | ||||||
| 			// Find a codec for converting the object to raw bytes.  This is necessary for the | 			// 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. | 			// api version and kind to be correctly set be serialization. | ||||||
| 			var codec = Codec(metav1.SchemeGroupVersion) | 			var codec = TestCodec(codecs, metav1.SchemeGroupVersion) | ||||||
|  |  | ||||||
| 			// Convert the object to raw bytes | 			// Convert the object to raw bytes | ||||||
| 			bytes, err := runtime.Encode(codec, obj) | 			bytes, err := runtime.Encode(codec, obj) | ||||||
|   | |||||||
		Reference in New Issue
	
	Block a user