mirror of
https://github.com/k3s-io/kubernetes.git
synced 2025-07-27 05:27:21 +00:00
pkg/api/testing: cut off fuzzers from static codecs
This commit is contained in:
parent
6ec7fdbec2
commit
55466b51bf
@ -31,7 +31,7 @@ import (
|
||||
)
|
||||
|
||||
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)
|
||||
for i := range items {
|
||||
apiObjectFuzzer.Fuzz(&items[i])
|
||||
|
@ -35,7 +35,7 @@ import (
|
||||
func TestDeepCopyApiObjects(t *testing.T) {
|
||||
for i := 0; i < *fuzzIters; i++ {
|
||||
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) {
|
||||
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) {
|
||||
for i := 0; i < *fuzzIters; i++ {
|
||||
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)
|
||||
}
|
||||
}
|
||||
|
@ -77,7 +77,7 @@ func TestUniversalDeserializer(t *testing.T) {
|
||||
|
||||
func TestProtobufRoundTrip(t *testing.T) {
|
||||
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.
|
||||
obj.Spec.InitContainers = 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
|
||||
// fuzzer registered with the apitesting package.
|
||||
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)
|
||||
if err != nil {
|
||||
@ -442,7 +442,7 @@ func TestUnversionedTypes(t *testing.T) {
|
||||
// TestObjectWatchFraming establishes that a watch event can be encoded and
|
||||
// decoded correctly through each of the supported RFC2046 media types.
|
||||
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{}
|
||||
f.Fuzz(secret)
|
||||
secret.Data["binary"] = []byte{0x00, 0x10, 0x30, 0x55, 0xff, 0x00}
|
||||
@ -524,7 +524,7 @@ func TestObjectWatchFraming(t *testing.T) {
|
||||
const benchmarkSeed = 100
|
||||
|
||||
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)
|
||||
for i := range items {
|
||||
var pod api.Pod
|
||||
|
@ -30,6 +30,7 @@ import (
|
||||
"k8s.io/apimachinery/pkg/labels"
|
||||
"k8s.io/apimachinery/pkg/runtime"
|
||||
"k8s.io/apimachinery/pkg/runtime/schema"
|
||||
runtimeserializer "k8s.io/apimachinery/pkg/runtime/serializer"
|
||||
"k8s.io/apimachinery/pkg/util/intstr"
|
||||
"k8s.io/kubernetes/cmd/kubeadm/app/apis/kubeadm"
|
||||
"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,
|
||||
apitesting.GenericFuzzerFuncs(t),
|
||||
apitesting.GenericFuzzerFuncs(t, codecs),
|
||||
overrideGenericFuncs(t),
|
||||
coreFuncs(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)
|
||||
}
|
||||
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
|
||||
// that InitContainers and their statuses are not generated. This is
|
||||
// because in thise test we are simply doing json operations, in which
|
||||
|
@ -149,7 +149,7 @@ func TestValidateOk(t *testing.T) {
|
||||
}
|
||||
|
||||
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 _, test := range tests {
|
||||
testObj := test.obj
|
||||
|
@ -21,37 +21,32 @@ import (
|
||||
"mime"
|
||||
"fmt"
|
||||
|
||||
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
|
||||
"k8s.io/apimachinery/pkg/runtime"
|
||||
runtimeserializer "k8s.io/apimachinery/pkg/runtime/serializer"
|
||||
"k8s.io/apimachinery/pkg/runtime/schema"
|
||||
)
|
||||
|
||||
var scheme = runtime.NewScheme()
|
||||
var codecs = runtimeserializer.NewCodecFactory(scheme)
|
||||
var serializer runtime.SerializerInfo
|
||||
var testCodecMediaType string
|
||||
|
||||
// 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.
|
||||
func Codec(gvs ...schema.GroupVersion) runtime.Codec {
|
||||
if serializer.Serializer == nil {
|
||||
return codecs.LegacyCodec(gvs...)
|
||||
func TestCodec(codecs runtimeserializer.CodecFactory, gvs ...schema.GroupVersion) runtime.Codec {
|
||||
if len(testCodecMediaType) != 0 {
|
||||
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() {
|
||||
metav1.AddToGroupVersion(scheme, metav1.SchemeGroupVersion)
|
||||
|
||||
if apiMediaType := os.Getenv("KUBE_TEST_API_TYPE"); len(apiMediaType) > 0 {
|
||||
var ok bool
|
||||
mediaType, _, err := mime.ParseMediaType(apiMediaType)
|
||||
var err error
|
||||
testCodecMediaType, _, err = mime.ParseMediaType(apiMediaType)
|
||||
if err != nil {
|
||||
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"
|
||||
"k8s.io/apimachinery/pkg/runtime"
|
||||
"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{}{
|
||||
func(j *int, c fuzz.Continue) {
|
||||
*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
|
||||
// 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
|
||||
bytes, err := runtime.Encode(codec, obj)
|
||||
|
Loading…
Reference in New Issue
Block a user