From c1ff7592fa90e6b8cfb4beb8b7c3b10acb4abed3 Mon Sep 17 00:00:00 2001 From: Clayton Coleman Date: Sat, 17 Jun 2017 14:32:35 -0400 Subject: [PATCH] Add test for stable encoding --- .../apimachinery/pkg/api/testing/roundtrip.go | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) 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 {