api test: update TestDefaulting
Logging and sub-tests were added to help debug this problem: the test passes for ResourceClaim (same defaulting!) and fails for the list, but only if run together with the other test cases?! $ go test ./pkg/api/testing --- FAIL: TestDefaulting (1.76s) --- FAIL: TestDefaulting/resource.k8s.io/v1alpha3,_Kind=ResourceClaimList (0.01s) defaulting_test.go:238: expected resource.k8s.io/v1alpha3, Kind=ResourceClaimList to trigger defaulting due to fuzzing FAIL FAIL k8s.io/kubernetes/pkg/api/testing 17.294s FAIL $ go test -run=TestDefaulting/resource.k8s.io/v1alpha3,_Kind=ResourceClaimList ./pkg/api/testing ok k8s.io/kubernetes/pkg/api/testing 0.062s What fixed that problem was increasing the likelihood of generating the right test object by iterating more often before giving up.
This commit is contained in:
@@ -192,6 +192,21 @@ func TestDefaulting(t *testing.T) {
|
|||||||
{Group: "flowcontrol.apiserver.k8s.io", Version: "v1", Kind: "PriorityLevelConfigurationList"}: {},
|
{Group: "flowcontrol.apiserver.k8s.io", Version: "v1", Kind: "PriorityLevelConfigurationList"}: {},
|
||||||
}
|
}
|
||||||
|
|
||||||
|
scheme := legacyscheme.Scheme
|
||||||
|
var testTypes orderedGroupVersionKinds
|
||||||
|
for gvk := range scheme.AllKnownTypes() {
|
||||||
|
if gvk.Version == runtime.APIVersionInternal {
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
testTypes = append(testTypes, gvk)
|
||||||
|
}
|
||||||
|
sort.Sort(testTypes)
|
||||||
|
|
||||||
|
for _, gvk := range testTypes {
|
||||||
|
gvk := gvk
|
||||||
|
t.Run(gvk.String(), func(t *testing.T) {
|
||||||
|
// Each sub-tests gets its own fuzzer instance to make running it independent
|
||||||
|
// from what other tests ran before.
|
||||||
f := fuzz.New().NilChance(.5).NumElements(1, 1).RandSource(rand.NewSource(1))
|
f := fuzz.New().NilChance(.5).NumElements(1, 1).RandSource(rand.NewSource(1))
|
||||||
f.Funcs(
|
f.Funcs(
|
||||||
func(s *runtime.RawExtension, c fuzz.Continue) {},
|
func(s *runtime.RawExtension, c fuzz.Continue) {},
|
||||||
@@ -210,17 +225,6 @@ func TestDefaulting(t *testing.T) {
|
|||||||
},
|
},
|
||||||
)
|
)
|
||||||
|
|
||||||
scheme := legacyscheme.Scheme
|
|
||||||
var testTypes orderedGroupVersionKinds
|
|
||||||
for gvk := range scheme.AllKnownTypes() {
|
|
||||||
if gvk.Version == runtime.APIVersionInternal {
|
|
||||||
continue
|
|
||||||
}
|
|
||||||
testTypes = append(testTypes, gvk)
|
|
||||||
}
|
|
||||||
sort.Sort(testTypes)
|
|
||||||
|
|
||||||
for _, gvk := range testTypes {
|
|
||||||
_, expectedChanged := typesWithDefaulting[gvk]
|
_, expectedChanged := typesWithDefaulting[gvk]
|
||||||
iter := 0
|
iter := 0
|
||||||
changedOnce := false
|
changedOnce := false
|
||||||
@@ -229,7 +233,11 @@ func TestDefaulting(t *testing.T) {
|
|||||||
if !expectedChanged || changedOnce {
|
if !expectedChanged || changedOnce {
|
||||||
break
|
break
|
||||||
}
|
}
|
||||||
if iter > 300 {
|
// This uses to be 300, but for ResourceClaimList that was not high enough
|
||||||
|
// because depending on the starting conditions, the fuzzer never created the
|
||||||
|
// one combination where defaulting kicked in (empty string in non-empty slice
|
||||||
|
// in another non-empty slice).
|
||||||
|
if iter > 3000 {
|
||||||
t.Errorf("expected %s to trigger defaulting due to fuzzing", gvk)
|
t.Errorf("expected %s to trigger defaulting due to fuzzing", gvk)
|
||||||
break
|
break
|
||||||
}
|
}
|
||||||
@@ -250,16 +258,22 @@ func TestDefaulting(t *testing.T) {
|
|||||||
|
|
||||||
// get internal
|
// get internal
|
||||||
withDefaults := src.DeepCopyObject()
|
withDefaults := src.DeepCopyObject()
|
||||||
scheme.Default(withDefaults.(runtime.Object))
|
scheme.Default(withDefaults)
|
||||||
|
|
||||||
if !reflect.DeepEqual(original, withDefaults) {
|
if !reflect.DeepEqual(original, withDefaults) {
|
||||||
|
diff := cmp.Diff(original, withDefaults)
|
||||||
|
if !changedOnce {
|
||||||
|
t.Logf("got diff (-fuzzed, +with defaults):\n%s", diff)
|
||||||
changedOnce = true
|
changedOnce = true
|
||||||
|
}
|
||||||
if !expectedChanged {
|
if !expectedChanged {
|
||||||
t.Errorf("{Group: \"%s\", Version: \"%s\", Kind: \"%s\"} did not expect defaults to be set - update expected or check defaulter registering: %s", gvk.Group, gvk.Version, gvk.Kind, cmp.Diff(original, withDefaults))
|
t.Errorf("{Group: \"%s\", Version: \"%s\", Kind: \"%s\"} did not expect defaults to be set - update expected or check defaulter registering: %s", gvk.Group, gvk.Version, gvk.Kind, diff)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func BenchmarkPodDefaulting(b *testing.B) {
|
func BenchmarkPodDefaulting(b *testing.B) {
|
||||||
|
Reference in New Issue
Block a user