Add test for stable encoding

This commit is contained in:
Clayton Coleman 2017-06-17 14:32:35 -04:00
parent 606825eea4
commit c1ff7592fa
No known key found for this signature in database
GPG Key ID: 3D16906B4F1C5CB3

View File

@ -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 {