mirror of
https://github.com/k3s-io/kubernetes.git
synced 2025-08-03 17:30:00 +00:00
apitesting: external serialization roundtrip test
This commit is contained in:
parent
110f410e55
commit
7a06299f4a
@ -36,6 +36,7 @@ import (
|
|||||||
"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"
|
runtimeserializer "k8s.io/apimachinery/pkg/runtime/serializer"
|
||||||
|
"k8s.io/apimachinery/pkg/runtime/serializer/json"
|
||||||
"k8s.io/apimachinery/pkg/util/diff"
|
"k8s.io/apimachinery/pkg/util/diff"
|
||||||
"k8s.io/apimachinery/pkg/util/sets"
|
"k8s.io/apimachinery/pkg/util/sets"
|
||||||
)
|
)
|
||||||
@ -125,24 +126,28 @@ func roundTripTypes(t *testing.T, scheme *runtime.Scheme, codecFactory runtimese
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func RoundTripSpecificKindWithoutProtobuf(t *testing.T, internalGVK schema.GroupVersionKind, scheme *runtime.Scheme, codecFactory runtimeserializer.CodecFactory, fuzzer *fuzz.Fuzzer, nonRoundTrippableTypes map[schema.GroupVersionKind]bool) {
|
func RoundTripSpecificKindWithoutProtobuf(t *testing.T, gvk schema.GroupVersionKind, scheme *runtime.Scheme, codecFactory runtimeserializer.CodecFactory, fuzzer *fuzz.Fuzzer, nonRoundTrippableTypes map[schema.GroupVersionKind]bool) {
|
||||||
roundTripSpecificKind(t, internalGVK, scheme, codecFactory, fuzzer, nonRoundTrippableTypes, true)
|
roundTripSpecificKind(t, gvk, scheme, codecFactory, fuzzer, nonRoundTrippableTypes, true)
|
||||||
}
|
}
|
||||||
|
|
||||||
func RoundTripSpecificKind(t *testing.T, internalGVK schema.GroupVersionKind, scheme *runtime.Scheme, codecFactory runtimeserializer.CodecFactory, fuzzer *fuzz.Fuzzer, nonRoundTrippableTypes map[schema.GroupVersionKind]bool) {
|
func RoundTripSpecificKind(t *testing.T, gvk schema.GroupVersionKind, scheme *runtime.Scheme, codecFactory runtimeserializer.CodecFactory, fuzzer *fuzz.Fuzzer, nonRoundTrippableTypes map[schema.GroupVersionKind]bool) {
|
||||||
roundTripSpecificKind(t, internalGVK, scheme, codecFactory, fuzzer, nonRoundTrippableTypes, false)
|
roundTripSpecificKind(t, gvk, scheme, codecFactory, fuzzer, nonRoundTrippableTypes, false)
|
||||||
}
|
}
|
||||||
|
|
||||||
func roundTripSpecificKind(t *testing.T, internalGVK schema.GroupVersionKind, scheme *runtime.Scheme, codecFactory runtimeserializer.CodecFactory, fuzzer *fuzz.Fuzzer, nonRoundTrippableTypes map[schema.GroupVersionKind]bool, skipProtobuf bool) {
|
func roundTripSpecificKind(t *testing.T, gvk schema.GroupVersionKind, scheme *runtime.Scheme, codecFactory runtimeserializer.CodecFactory, fuzzer *fuzz.Fuzzer, nonRoundTrippableTypes map[schema.GroupVersionKind]bool, skipProtobuf bool) {
|
||||||
if nonRoundTrippableTypes[internalGVK] {
|
if nonRoundTrippableTypes[gvk] {
|
||||||
t.Logf("skipping %v", internalGVK)
|
t.Logf("skipping %v", gvk)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
t.Logf("round tripping %v", internalGVK)
|
t.Logf("round tripping %v", gvk)
|
||||||
|
|
||||||
// Try a few times, since runTest uses random values.
|
// Try a few times, since runTest uses random values.
|
||||||
for i := 0; i < *FuzzIters; i++ {
|
for i := 0; i < *FuzzIters; i++ {
|
||||||
roundTripToAllExternalVersions(t, scheme, codecFactory, fuzzer, internalGVK, nonRoundTrippableTypes, skipProtobuf)
|
if gvk.Version == runtime.APIVersionInternal {
|
||||||
|
roundTripToAllExternalVersions(t, scheme, codecFactory, fuzzer, gvk, nonRoundTrippableTypes, skipProtobuf)
|
||||||
|
} else {
|
||||||
|
roundTripOfExternalType(t, scheme, codecFactory, fuzzer, gvk, skipProtobuf)
|
||||||
|
}
|
||||||
if t.Failed() {
|
if t.Failed() {
|
||||||
break
|
break
|
||||||
}
|
}
|
||||||
@ -209,6 +214,32 @@ func roundTripToAllExternalVersions(t *testing.T, scheme *runtime.Scheme, codecF
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func roundTripOfExternalType(t *testing.T, scheme *runtime.Scheme, codecFactory runtimeserializer.CodecFactory, fuzzer *fuzz.Fuzzer, 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 := meta.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)
|
||||||
|
|
||||||
|
externalGoType := reflect.TypeOf(object).PkgPath()
|
||||||
|
t.Logf("\tround tripping external type %v %v", externalGVK, externalGoType)
|
||||||
|
|
||||||
|
typeAcc.SetKind(externalGVK.Kind)
|
||||||
|
typeAcc.SetAPIVersion(externalGVK.GroupVersion().String())
|
||||||
|
|
||||||
|
roundTrip(t, scheme, json.NewSerializer(json.DefaultMetaFactory, scheme, scheme, false), object)
|
||||||
|
|
||||||
|
// TODO remove this hack after we're past the intermediate steps
|
||||||
|
if !skipProtobuf {
|
||||||
|
roundTrip(t, scheme, protobuf.NewSerializer(scheme, scheme, "application/protobuf"), object)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
// roundTrip applies a single round-trip test to the given runtime 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
|
// using the given codec. The round-trip test ensures that an object can be
|
||||||
// deep-copied and converted from internal -> versioned -> internal without
|
// deep-copied and converted from internal -> versioned -> internal without
|
||||||
|
Loading…
Reference in New Issue
Block a user