From d4e6c6632cbf80e053964a930ee6121bdf8feaae 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 --- api/proto_test.go | 45 +++++++++++++++++++++++++++++++++++ api/testdata/WorkerRecord.bin | 5 ++++ 2 files changed, 50 insertions(+) create mode 100644 api/proto_test.go create mode 100644 api/testdata/WorkerRecord.bin diff --git a/api/proto_test.go b/api/proto_test.go new file mode 100644 index 0000000000000..91e036b938539 --- /dev/null +++ b/api/proto_test.go @@ -0,0 +1,45 @@ +package api + +import ( + "flag" + "fmt" + "os" + "testing" + + "github.com/golang/protobuf/proto" + moby_buildkit_v1_types "github.com/moby/buildkit/api/types" + "github.com/stretchr/testify/assert" + "github.com/stretchr/testify/require" +) + +var record = flag.Bool("record", false, "record actual values as fixtures") + +func TestProto(t *testing.T) { + r := &moby_buildkit_v1_types.WorkerRecord{ + ID: "hello", + Labels: map[string]string{ + "a": "foo", + "b": "bar", + "c": "baz", + }, + } + + var buf proto.Buffer + buf.SetDeterministic(true) + err := buf.Marshal(r) + require.NoError(t, err) + + assertProto(t, buf.Bytes(), "WorkerRecord") +} + +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/api/testdata/WorkerRecord.bin b/api/testdata/WorkerRecord.bin new file mode 100644 index 0000000000000..e32e9452c9aaa --- /dev/null +++ b/api/testdata/WorkerRecord.bin @@ -0,0 +1,5 @@ + +hello +afoo +cbaz +bbar \ No newline at end of file