copy_test.go 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. /*
  2. Copyright 2015 The Kubernetes Authors.
  3. Licensed under the Apache License, Version 2.0 (the "License");
  4. you may not use this file except in compliance with the License.
  5. You may obtain a copy of the License at
  6. http://www.apache.org/licenses/LICENSE-2.0
  7. Unless required by applicable law or agreed to in writing, software
  8. distributed under the License is distributed on an "AS IS" BASIS,
  9. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  10. See the License for the specific language governing permissions and
  11. limitations under the License.
  12. */
  13. package testing
  14. import (
  15. "bytes"
  16. "math/rand"
  17. "reflect"
  18. "testing"
  19. "github.com/google/gofuzz"
  20. "k8s.io/apimachinery/pkg/api/apitesting/fuzzer"
  21. "k8s.io/apimachinery/pkg/api/apitesting/roundtrip"
  22. "k8s.io/apimachinery/pkg/runtime"
  23. "k8s.io/apimachinery/pkg/runtime/schema"
  24. "k8s.io/apimachinery/pkg/util/diff"
  25. "k8s.io/kubernetes/pkg/api/legacyscheme"
  26. )
  27. func TestDeepCopyApiObjects(t *testing.T) {
  28. for i := 0; i < *roundtrip.FuzzIters; i++ {
  29. for _, version := range []schema.GroupVersion{{Group: "", Version: runtime.APIVersionInternal}, {Group: "", Version: "v1"}} {
  30. f := fuzzer.FuzzerFor(FuzzerFuncs, rand.NewSource(rand.Int63()), legacyscheme.Codecs)
  31. for kind := range legacyscheme.Scheme.KnownTypes(version) {
  32. doDeepCopyTest(t, version.WithKind(kind), f)
  33. }
  34. }
  35. }
  36. }
  37. func doDeepCopyTest(t *testing.T, kind schema.GroupVersionKind, f *fuzz.Fuzzer) {
  38. item, err := legacyscheme.Scheme.New(kind)
  39. if err != nil {
  40. t.Fatalf("Could not create a %v: %s", kind, err)
  41. }
  42. f.Fuzz(item)
  43. itemCopy := item.DeepCopyObject()
  44. if !reflect.DeepEqual(item, itemCopy) {
  45. t.Errorf("\nexpected: %#v\n\ngot: %#v\n\ndiff: %v", item, itemCopy, diff.ObjectReflectDiff(item, itemCopy))
  46. }
  47. prefuzzData := &bytes.Buffer{}
  48. if err := legacyscheme.Codecs.LegacyCodec(kind.GroupVersion()).Encode(item, prefuzzData); err != nil {
  49. t.Errorf("Could not encode a %v: %s", kind, err)
  50. return
  51. }
  52. // Refuzz the copy, which should have no effect on the original
  53. f.Fuzz(itemCopy)
  54. postfuzzData := &bytes.Buffer{}
  55. if err := legacyscheme.Codecs.LegacyCodec(kind.GroupVersion()).Encode(item, postfuzzData); err != nil {
  56. t.Errorf("Could not encode a %v: %s", kind, err)
  57. return
  58. }
  59. if !bytes.Equal(prefuzzData.Bytes(), postfuzzData.Bytes()) {
  60. t.Log(diff.StringDiff(prefuzzData.String(), postfuzzData.String()))
  61. t.Errorf("Fuzzing copy modified original of %#v", kind)
  62. return
  63. }
  64. }
  65. func TestDeepCopySingleType(t *testing.T) {
  66. for i := 0; i < *roundtrip.FuzzIters; i++ {
  67. for _, version := range []schema.GroupVersion{{Group: "", Version: runtime.APIVersionInternal}, {Group: "", Version: "v1"}} {
  68. f := fuzzer.FuzzerFor(FuzzerFuncs, rand.NewSource(rand.Int63()), legacyscheme.Codecs)
  69. doDeepCopyTest(t, version.WithKind("Pod"), f)
  70. }
  71. }
  72. }