diff --git a/staging/src/k8s.io/apimachinery/pkg/api/testing/roundtrip.go b/staging/src/k8s.io/apimachinery/pkg/api/testing/roundtrip.go index 8e665d86f89..b3070599156 100644 --- a/staging/src/k8s.io/apimachinery/pkg/api/testing/roundtrip.go +++ b/staging/src/k8s.io/apimachinery/pkg/api/testing/roundtrip.go @@ -17,6 +17,7 @@ limitations under the License. package testing import ( + "bytes" "encoding/hex" "fmt" "reflect" @@ -282,6 +283,23 @@ func roundTrip(t *testing.T, scheme *runtime.Scheme, codec runtime.Codec, object return } + // encode (serialize) a second time to verify that it was not varying + secondData, err := runtime.Encode(codec, object) + if err != nil { + if runtime.IsNotRegisteredError(err) { + t.Logf("%v: not registered: %v (%s)", name, err, printer.Sprintf("%#v", object)) + } else { + t.Errorf("%v: %v (%s)", name, err, printer.Sprintf("%#v", object)) + } + return + } + + // serialization to the wire must be stable to ensure that we don't write twice to the DB + // when the object hasn't changed. + if !bytes.Equal(data, secondData) { + t.Errorf("%v: serialization is not stable: %s", name, printer.Sprintf("%#v", object)) + } + // decode (deserialize) the encoded data back into an object obj2, err := runtime.Decode(codec, data) if err != nil {