From 4918e713000ae1d3460b09496d3e4eb4fc8c0f48 Mon Sep 17 00:00:00 2001 From: Kazuyoshi Kato Date: Sat, 20 Jan 2024 00:57:31 -0800 Subject: [PATCH] Keep serialized protobuf binaries for testing These serialized binaries can be used to test changes like #4422. Signed-off-by: Kazuyoshi Kato --- solver/pb/proto_test.go | 47 +++++++++++++++++++++++++++++++++ solver/pb/testdata/SourceOp.bin | 5 ++++ 2 files changed, 52 insertions(+) create mode 100644 solver/pb/proto_test.go create mode 100644 solver/pb/testdata/SourceOp.bin diff --git a/solver/pb/proto_test.go b/solver/pb/proto_test.go new file mode 100644 index 0000000000000..a1100a1ffc6e8 --- /dev/null +++ b/solver/pb/proto_test.go @@ -0,0 +1,47 @@ +package pb + +import ( + "flag" + "fmt" + "os" + "testing" + + "github.com/stretchr/testify/assert" + "github.com/stretchr/testify/require" + + "github.com/golang/protobuf/proto" +) + +var record = flag.Bool("record", false, "record actual values as fixtures") + +func TestProto(t *testing.T) { + // Because of stable_marshaler_all, the map will be sorted in the result binary. + r := &SourceOp{ + Identifier: "docker-image://docker.io/library/foo:latest", + Attrs: map[string]string{ + "a": "foo", + "b": "bar", + "c": "baz", + }, + } + + gogo, err := r.Marshal() + require.NoError(t, err) + assertProto(t, gogo, "SourceOp") + + google, err := proto.Marshal(r) + require.NoError(t, err) + assertProto(t, google, "SourceOp") +} + +func assertProto(tb testing.TB, actual []byte, name string) { + path := fmt.Sprintf("testdata/%s.bin", name) + if *record { + err := os.WriteFile(path, actual, 0600) + require.NoError(tb, err) + return + } + expected, err := os.ReadFile(path) + require.NoError(tb, err) + assert.Equal(tb, expected, actual) +} diff --git a/solver/pb/testdata/SourceOp.bin b/solver/pb/testdata/SourceOp.bin new file mode 100644 index 0000000000000..c755f83af8106 --- /dev/null +++ b/solver/pb/testdata/SourceOp.bin @@ -0,0 +1,5 @@ + ++docker-image://docker.io/library/foo:latest +afoo +bbar +cbaz \ No newline at end of file