From 88f1a1c06b84f829222d4c879364aa692969410f Mon Sep 17 00:00:00 2001 From: Jim Fulton Date: Wed, 30 Jun 2021 17:05:02 -0400 Subject: [PATCH 1/2] fix(bigtable/bttest): Emulator too lenient for empty RowMutation --- bigtable/bttest/inmem.go | 16 +++++++++++++ bigtable/bttest/inmem_test.go | 44 +++++++++++++++++++++++++++++++++++ 2 files changed, 60 insertions(+) diff --git a/bigtable/bttest/inmem.go b/bigtable/bttest/inmem.go index ceea0e7e006..fc7aa7fdcac 100644 --- a/bigtable/bttest/inmem.go +++ b/bigtable/bttest/inmem.go @@ -806,6 +806,12 @@ func newRegexp(pat []byte) (*binaryregexp.Regexp, error) { } func (s *server) MutateRow(ctx context.Context, req *btpb.MutateRowRequest) (*btpb.MutateRowResponse, error) { + if len(req.Mutations) == 0 { + return nil, status.Errorf( + codes.InvalidArgument, + "No mutations provided", + ) + } s.mu.Lock() tbl, ok := s.tables[req.TableName] s.mu.Unlock() @@ -823,6 +829,16 @@ func (s *server) MutateRow(ctx context.Context, req *btpb.MutateRowRequest) (*bt } func (s *server) MutateRows(req *btpb.MutateRowsRequest, stream btpb.Bigtable_MutateRowsServer) error { + nMutations := 0 + for _, entry := range req.Entries { + nMutations += len(entry.Mutations) + } + if nMutations == 0 { + return status.Errorf( + codes.InvalidArgument, + "No mutations provided", + ) + } s.mu.Lock() tbl, ok := s.tables[req.TableName] s.mu.Unlock() diff --git a/bigtable/bttest/inmem_test.go b/bigtable/bttest/inmem_test.go index e7eb91d2358..92ecad9f983 100644 --- a/bigtable/bttest/inmem_test.go +++ b/bigtable/bttest/inmem_test.go @@ -1979,3 +1979,47 @@ func TestValueFilterRowWithAlternationInRegex(t *testing.T) { t.Fatalf("Response chunks mismatch: got: + want -\n%s", diff) } } + +func TestMutateRowEmptyMutationErrors(t *testing.T) { + srv := &server{tables: make(map[string]*table)} + ctx := context.Background() + req := &btpb.MutateRowRequest{ + TableName: "mytable", + RowKey: []byte("r"), + Mutations: []*btpb.Mutation{}, + } + + resp, err := srv.MutateRow(ctx, req) + if resp != nil || + fmt.Sprint(err) != + "rpc error: code = InvalidArgument" + + " desc = No mutations provided" { + t.Fatalf("Failed to error %s", err) + } +} + +type bigtableTestingMutateRowsServer struct { + grpc.ServerStream +} + +func (x *bigtableTestingMutateRowsServer) Send(m *btpb.MutateRowsResponse) error { + return nil +} + +func TestMutateRowsEmptyMutationErrors(t *testing.T) { + srv := &server{tables: make(map[string]*table)} + req := &btpb.MutateRowsRequest{ + TableName: "mytable", + Entries: []*btpb.MutateRowsRequest_Entry{ + {Mutations: []*btpb.Mutation{}}, + {Mutations: []*btpb.Mutation{}}, + }, + } + + err := srv.MutateRows(req, &bigtableTestingMutateRowsServer{}) + if fmt.Sprint(err) != + "rpc error: code = InvalidArgument " + + "desc = No mutations provided" { + t.Fatalf("Failed to error %s", err) + } +} From e896a565d90645fcc8bacea47817b07cd82b585b Mon Sep 17 00:00:00 2001 From: Jim Fulton Date: Thu, 1 Jul 2021 08:58:45 -0400 Subject: [PATCH 2/2] gofmt --- bigtable/bttest/inmem.go | 2 +- bigtable/bttest/inmem_test.go | 4 ++-- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/bigtable/bttest/inmem.go b/bigtable/bttest/inmem.go index fc7aa7fdcac..9bd08e02fec 100644 --- a/bigtable/bttest/inmem.go +++ b/bigtable/bttest/inmem.go @@ -811,7 +811,7 @@ func (s *server) MutateRow(ctx context.Context, req *btpb.MutateRowRequest) (*bt codes.InvalidArgument, "No mutations provided", ) - } + } s.mu.Lock() tbl, ok := s.tables[req.TableName] s.mu.Unlock() diff --git a/bigtable/bttest/inmem_test.go b/bigtable/bttest/inmem_test.go index 92ecad9f983..6bdf18c6a92 100644 --- a/bigtable/bttest/inmem_test.go +++ b/bigtable/bttest/inmem_test.go @@ -1992,7 +1992,7 @@ func TestMutateRowEmptyMutationErrors(t *testing.T) { resp, err := srv.MutateRow(ctx, req) if resp != nil || fmt.Sprint(err) != - "rpc error: code = InvalidArgument" + + "rpc error: code = InvalidArgument"+ " desc = No mutations provided" { t.Fatalf("Failed to error %s", err) } @@ -2018,7 +2018,7 @@ func TestMutateRowsEmptyMutationErrors(t *testing.T) { err := srv.MutateRows(req, &bigtableTestingMutateRowsServer{}) if fmt.Sprint(err) != - "rpc error: code = InvalidArgument " + + "rpc error: code = InvalidArgument "+ "desc = No mutations provided" { t.Fatalf("Failed to error %s", err) }