k8s.io/kubernetes@v1.29.3/pkg/api/testing/copy_test.go (about)

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