mirror of
https://github.com/k3s-io/kubernetes.git
synced 2025-07-29 06:27:05 +00:00
Merge pull request #35912 from liggitt/test-deep-copy
Automatic merge from submit-queue Add tests for deepcopy of structs Until https://github.com/kubernetes/kubernetes/pull/35728 merges, we want to at least fuzz/test that deepcopy isn't shallow-copying problematic fields
This commit is contained in:
commit
87584919e5
@ -17,6 +17,7 @@ limitations under the License.
|
|||||||
package api_test
|
package api_test
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"bytes"
|
||||||
"math/rand"
|
"math/rand"
|
||||||
"reflect"
|
"reflect"
|
||||||
"testing"
|
"testing"
|
||||||
@ -57,6 +58,27 @@ func doDeepCopyTest(t *testing.T, kind unversioned.GroupVersionKind, f *fuzz.Fuz
|
|||||||
if !reflect.DeepEqual(item, itemCopy) {
|
if !reflect.DeepEqual(item, itemCopy) {
|
||||||
t.Errorf("\nexpected: %#v\n\ngot: %#v\n\ndiff: %v", item, itemCopy, diff.ObjectReflectDiff(item, itemCopy))
|
t.Errorf("\nexpected: %#v\n\ngot: %#v\n\ndiff: %v", item, itemCopy, diff.ObjectReflectDiff(item, itemCopy))
|
||||||
}
|
}
|
||||||
|
|
||||||
|
prefuzzData := &bytes.Buffer{}
|
||||||
|
if err := api.Codecs.LegacyCodec(kind.GroupVersion()).Encode(item, prefuzzData); err != nil {
|
||||||
|
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{}
|
||||||
|
if err := api.Codecs.LegacyCodec(kind.GroupVersion()).Encode(item, postfuzzData); err != nil {
|
||||||
|
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
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestDeepCopySingleType(t *testing.T) {
|
func TestDeepCopySingleType(t *testing.T) {
|
||||||
|
Loading…
Reference in New Issue
Block a user