Skip to content

Instantly share code, notes, and snippets.

@munnerz
Created November 14, 2019 17:16
Show Gist options
  • Save munnerz/19a72d4683bb388cddfb22397dfa747e to your computer and use it in GitHub Desktop.
Save munnerz/19a72d4683bb388cddfb22397dfa747e to your computer and use it in GitHub Desktop.
func objectNTimes(t *testing.T, fuzzer *gofuzz.Fuzzer, obj runtime.Object, schema *structuralschema.Structural, iterations int) {
t.Logf("Running CRD schema pruning fuzz test for object %v", obj.GetObjectKind())
for i := 0; i < iterations; i++ {
fuzzed := obj.DeepCopyObject()
// fuzz *before* converting to unstructured, so we get typed fuzzing
fuzzer.Fuzz(fuzzed)
unstructuredFuzzed, err := runtime.DefaultUnstructuredConverter.ToUnstructured(fuzzed)
if err != nil {
t.Fatalf("Failed to convert type to `runtime.Unstructured`: %v", err)
return
}
pruned, err := runtime.DefaultUnstructuredConverter.ToUnstructured(fuzzed)
if err != nil {
t.Fatalf("Failed to convert type to `runtime.Unstructured`: %v", err)
return
}
structuralpruning.Prune(pruned, schema, true)
//t.Logf("Fuzzed: %+v", fuzzed)
//t.Logf("Pruned: %+v", pruned)
if !cmp.Equal(unstructuredFuzzed, pruned) {
t.Errorf("Failed fuzz test, difference: %v", cmp.Diff(unstructuredFuzzed, pruned))
}
t.Logf("Passed fuzz test iteration %d", i)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment