From 53cc85a29d6589a54c6fc1e0523530904690966b Mon Sep 17 00:00:00 2001 From: Dave Protasowski Date: Wed, 8 Jan 2020 09:25:41 -0500 Subject: [PATCH] Expose k8s types that do not roundtrip and a helper to roundtrip without protobuf --- .../pkg/api/apitesting/roundtrip/roundtrip.go | 33 ++++++++++++++++++- 1 file changed, 32 insertions(+), 1 deletion(-) diff --git a/staging/src/k8s.io/apimachinery/pkg/api/apitesting/roundtrip/roundtrip.go b/staging/src/k8s.io/apimachinery/pkg/api/apitesting/roundtrip/roundtrip.go index a227b69944b..f7961043ac5 100644 --- a/staging/src/k8s.io/apimachinery/pkg/api/apitesting/roundtrip/roundtrip.go +++ b/staging/src/k8s.io/apimachinery/pkg/api/apitesting/roundtrip/roundtrip.go @@ -26,7 +26,7 @@ import ( "github.com/davecgh/go-spew/spew" "github.com/golang/protobuf/proto" - "github.com/google/gofuzz" + fuzz "github.com/google/gofuzz" flag "github.com/spf13/pflag" apitesting "k8s.io/apimachinery/pkg/api/apitesting" @@ -103,6 +103,23 @@ var globalNonRoundTrippableTypes = sets.NewString( "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 *fuzz.Fuzzer, nonRoundTrippableTypes map[schema.GroupVersionKind]bool) { @@ -146,6 +163,20 @@ func RoundTripExternalTypes(t *testing.T, scheme *runtime.Scheme, codecFactory r } } +// 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 *fuzz.Fuzzer, 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 *fuzz.Fuzzer, nonRoundTrippableTypes map[schema.GroupVersionKind]bool) { roundTripSpecificKind(t, gvk, scheme, codecFactory, fuzzer, nonRoundTrippableTypes, true) }