func TestValidateOk(t *testing.T) { schema, err := loadSchemaForTest() if err != nil { t.Errorf("Failed to load: %v", err) } tests := []struct { obj runtime.Object typeName string }{ {obj: &api.Pod{}}, {obj: &api.Service{}}, {obj: &api.ReplicationController{}}, } seed := rand.Int63() apiObjectFuzzer := apitesting.FuzzerFor(nil, "", rand.NewSource(seed)) for i := 0; i < 5; i++ { for _, test := range tests { testObj := test.obj apiObjectFuzzer.Fuzz(testObj) data, err := testapi.Codec().Encode(testObj) if err != nil { t.Errorf("unexpected error: %v", err) } err = schema.ValidateBytes(data) if err != nil { t.Errorf("unexpected error: %v", err) } } } }
// BenchmarkEncodeJSON provides a baseline for regular JSON encode performance func BenchmarkEncodeJSON(b *testing.B) { pod := api.Pod{} apiObjectFuzzer := apitesting.FuzzerFor(nil, "", rand.NewSource(benchmarkSeed)) apiObjectFuzzer.Fuzz(&pod) for i := 0; i < b.N; i++ { json.Marshal(&pod) } }
func BenchmarkEncode(b *testing.B) { pod := api.Pod{} apiObjectFuzzer := apitesting.FuzzerFor(nil, "", rand.NewSource(benchmarkSeed)) apiObjectFuzzer.Fuzz(&pod) for i := 0; i < b.N; i++ { latest.Codec.Encode(&pod) } }
// BenchmarkDecodeJSON provides a baseline for regular JSON decode performance func BenchmarkDecodeJSON(b *testing.B) { pod := api.Pod{} apiObjectFuzzer := apitesting.FuzzerFor(nil, "", rand.NewSource(benchmarkSeed)) apiObjectFuzzer.Fuzz(&pod) data, _ := latest.Codec.Encode(&pod) for i := 0; i < b.N; i++ { obj := api.Pod{} json.Unmarshal(data, &obj) } }
func fuzzInternalObject(t *testing.T, forVersion string, item runtime.Object, seed int64) runtime.Object { apitesting.FuzzerFor(t, forVersion, rand.NewSource(seed)).Fuzz(item) j, err := meta.TypeAccessor(item) if err != nil { t.Fatalf("Unexpected error %v for %#v", err, item) } j.SetKind("") j.SetAPIVersion("") return item }
func TestDeepCopyApiObjects(t *testing.T) { for i := 0; i < *fuzzIters; i++ { for _, version := range []string{"", testapi.Version()} { f := apitesting.FuzzerFor(t, version, rand.NewSource(rand.Int63())) for kind := range api.Scheme.KnownTypes(version) { item, err := api.Scheme.New(version, kind) if err != nil { t.Fatalf("Could not create a %s: %s", kind, err) } f.Fuzz(item) itemCopy, err := api.Scheme.DeepCopy(item) if err != nil { t.Errorf("Could not deep copy a %s: %s", kind, err) continue } if !reflect.DeepEqual(item, itemCopy) { t.Errorf("expected %#v\ngot %#v", item, itemCopy) } } } } }