2015-05-13 17:12:36 +00:00
|
|
|
/*
|
2016-06-03 00:25:58 +00:00
|
|
|
Copyright 2015 The Kubernetes Authors.
|
2015-05-13 17:12:36 +00:00
|
|
|
|
|
|
|
Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
you may not use this file except in compliance with the License.
|
|
|
|
You may obtain a copy of the License at
|
|
|
|
|
|
|
|
http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
|
|
|
|
Unless required by applicable law or agreed to in writing, software
|
|
|
|
distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
See the License for the specific language governing permissions and
|
|
|
|
limitations under the License.
|
|
|
|
*/
|
|
|
|
|
2017-10-09 13:53:03 +00:00
|
|
|
package testing
|
2015-05-13 17:12:36 +00:00
|
|
|
|
|
|
|
import (
|
2016-10-31 17:03:58 +00:00
|
|
|
"bytes"
|
2015-05-13 17:12:36 +00:00
|
|
|
"math/rand"
|
|
|
|
"reflect"
|
|
|
|
"testing"
|
|
|
|
|
2017-07-12 14:10:48 +00:00
|
|
|
"github.com/google/gofuzz"
|
|
|
|
|
2018-08-01 14:01:32 +00:00
|
|
|
"k8s.io/apimachinery/pkg/api/apitesting/fuzzer"
|
|
|
|
"k8s.io/apimachinery/pkg/api/apitesting/roundtrip"
|
2018-05-01 14:54:37 +00:00
|
|
|
"k8s.io/apimachinery/pkg/runtime"
|
2017-01-11 14:09:48 +00:00
|
|
|
"k8s.io/apimachinery/pkg/runtime/schema"
|
|
|
|
"k8s.io/apimachinery/pkg/util/diff"
|
2017-10-16 11:41:50 +00:00
|
|
|
"k8s.io/kubernetes/pkg/api/legacyscheme"
|
2015-05-13 17:12:36 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
func TestDeepCopyApiObjects(t *testing.T) {
|
2017-07-12 14:10:48 +00:00
|
|
|
for i := 0; i < *roundtrip.FuzzIters; i++ {
|
2018-05-01 14:54:37 +00:00
|
|
|
for _, version := range []schema.GroupVersion{{Group: "", Version: runtime.APIVersionInternal}, {Group: "", Version: "v1"}} {
|
2017-10-16 11:41:50 +00:00
|
|
|
f := fuzzer.FuzzerFor(FuzzerFuncs, rand.NewSource(rand.Int63()), legacyscheme.Codecs)
|
|
|
|
for kind := range legacyscheme.Scheme.KnownTypes(version) {
|
2015-12-08 19:40:23 +00:00
|
|
|
doDeepCopyTest(t, version.WithKind(kind), f)
|
2015-05-13 17:12:36 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2015-09-14 21:56:51 +00:00
|
|
|
|
2016-11-21 02:55:31 +00:00
|
|
|
func doDeepCopyTest(t *testing.T, kind schema.GroupVersionKind, f *fuzz.Fuzzer) {
|
2017-10-16 11:41:50 +00:00
|
|
|
item, err := legacyscheme.Scheme.New(kind)
|
2015-09-14 21:56:51 +00:00
|
|
|
if err != nil {
|
2015-12-08 19:40:23 +00:00
|
|
|
t.Fatalf("Could not create a %v: %s", kind, err)
|
2015-09-14 21:56:51 +00:00
|
|
|
}
|
|
|
|
f.Fuzz(item)
|
2017-08-15 12:16:45 +00:00
|
|
|
itemCopy := item.DeepCopyObject()
|
2015-09-14 21:56:51 +00:00
|
|
|
if !reflect.DeepEqual(item, itemCopy) {
|
2016-06-12 19:47:36 +00:00
|
|
|
t.Errorf("\nexpected: %#v\n\ngot: %#v\n\ndiff: %v", item, itemCopy, diff.ObjectReflectDiff(item, itemCopy))
|
2015-09-14 21:56:51 +00:00
|
|
|
}
|
2016-10-31 17:03:58 +00:00
|
|
|
|
|
|
|
prefuzzData := &bytes.Buffer{}
|
2017-10-16 11:41:50 +00:00
|
|
|
if err := legacyscheme.Codecs.LegacyCodec(kind.GroupVersion()).Encode(item, prefuzzData); err != nil {
|
2016-10-31 17:03:58 +00:00
|
|
|
t.Errorf("Could not encode a %v: %s", kind, err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// Refuzz the copy, which should have no effect on the original
|
|
|
|
f.Fuzz(itemCopy)
|
|
|
|
|
|
|
|
postfuzzData := &bytes.Buffer{}
|
2017-10-16 11:41:50 +00:00
|
|
|
if err := legacyscheme.Codecs.LegacyCodec(kind.GroupVersion()).Encode(item, postfuzzData); err != nil {
|
2016-10-31 17:03:58 +00:00
|
|
|
t.Errorf("Could not encode a %v: %s", kind, err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
if bytes.Compare(prefuzzData.Bytes(), postfuzzData.Bytes()) != 0 {
|
|
|
|
t.Log(diff.StringDiff(prefuzzData.String(), postfuzzData.String()))
|
|
|
|
t.Errorf("Fuzzing copy modified original of %#v", kind)
|
|
|
|
return
|
|
|
|
}
|
2015-09-14 21:56:51 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestDeepCopySingleType(t *testing.T) {
|
2017-07-12 14:10:48 +00:00
|
|
|
for i := 0; i < *roundtrip.FuzzIters; i++ {
|
2018-05-01 14:54:37 +00:00
|
|
|
for _, version := range []schema.GroupVersion{{Group: "", Version: runtime.APIVersionInternal}, {Group: "", Version: "v1"}} {
|
2017-10-16 11:41:50 +00:00
|
|
|
f := fuzzer.FuzzerFor(FuzzerFuncs, rand.NewSource(rand.Int63()), legacyscheme.Codecs)
|
2015-12-08 19:40:23 +00:00
|
|
|
doDeepCopyTest(t, version.WithKind("Pod"), f)
|
2015-09-14 21:56:51 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|