| 1 |
// Copyright 2022 Google LLC |
| 2 |
// |
| 3 |
// Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 |
// you may not use this file except in compliance with the License. |
| 5 |
// You may obtain a copy of the License at |
| 6 |
// |
| 7 |
// http://www.apache.org/licenses/LICENSE-2.0 |
| 8 |
// |
| 9 |
// Unless required by applicable law or agreed to in writing, software |
| 10 |
// distributed under the License is distributed on an "AS IS" BASIS, |
| 11 |
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 |
// See the License for the specific language governing permissions and |
| 13 |
// limitations under the License. |
| 14 |
|
| 15 |
package main |
| 16 |
|
| 17 |
import ( |
| 18 |
"context" |
| 19 |
"fmt" |
| 20 |
"log" |
| 21 |
"net" |
| 22 |
"os" |
| 23 |
|
| 24 |
"testing" |
| 25 |
|
| 26 |
"cloud.google.com/go/bigtable" |
| 27 |
btpb "cloud.google.com/go/bigtable/apiv2/bigtablepb" |
| 28 |
"cloud.google.com/go/bigtable/bttest" |
| 29 |
pb "github.com/googleapis/cloud-bigtable-clients-test/testproxypb" |
| 30 |
"google.golang.org/api/option" |
| 31 |
"google.golang.org/grpc" |
| 32 |
"google.golang.org/grpc/codes" |
| 33 |
"google.golang.org/grpc/credentials/insecure" |
| 34 |
"google.golang.org/grpc/test/bufconn" |
| 35 |
) |
| 36 |
|
| 37 |
const ( |
| 38 |
buffer = 1024 * 1024 |
| 39 |
tableName = "projects/my-project/instances/my-instance/tables/table" |
| 40 |
tableID = "table" |
| 41 |
columnFamily = "cf" |
| 42 |
testProxyClient = "testProxyClient" |
| 43 |
testProxyAddress = "localhost:9990" |
| 44 |
bigtableAddress = "localhost:9999" |
| 45 |
rowKey = "row" |
| 46 |
) |
| 47 |
|
| 48 |
var ( |
| 49 |
lis *bufconn.Listener |
| 50 |
client pb.CloudBigtableV2TestProxyClient |
| 51 |
) |
| 52 |
|
| 53 |
func bufDialer(context.Context, string) (net.Conn, error) { |
| 54 |
return lis.Dial() |
| 55 |
} |
| 56 |
|
| 57 |
// helper function to populate the in-memory BT table. |
| 58 |
func populateTable(bts *bttest.Server) error { |
| 59 |
ctx := context.Background() |
| 60 |
|
| 61 |
conn, err := grpc.Dial(bts.Addr, grpc.WithTransportCredentials(insecure.NewCredentials()), grpc.WithBlock()) |
| 62 |
if err != nil { |
| 63 |
return fmt.Errorf("testproxy setup: can't dial inmem Bigtable server: %v", err) |
| 64 |
} |
| 65 |
defer conn.Close() |
| 66 |
|
| 67 |
adminClient, err := bigtable.NewAdminClient(ctx, "client", "instance", |
| 68 |
option.WithGRPCConn(conn), option.WithGRPCDialOption(grpc.WithBlock())) |
| 69 |
if err != nil { |
| 70 |
return fmt.Errorf("testproxy setup: can't create AdminClient: %v", err) |
| 71 |
} |
| 72 |
defer adminClient.Close() |
| 73 |
|
| 74 |
if err := adminClient.CreateTable(ctx, tableID); err != nil { |
| 75 |
return fmt.Errorf("testproxy setup: can't create table: %v", err) |
| 76 |
} |
| 77 |
|
| 78 |
// Create column families (3 is an arbitrarily sufficient number) |
| 79 |
count := 3 |
| 80 |
for i := 0; i < count; i++ { |
| 81 |
cfName := fmt.Sprintf("%s%d", columnFamily, i) |
| 82 |
if err := adminClient.CreateColumnFamily(ctx, tableID, cfName); err != nil { |
| 83 |
return fmt.Errorf("testproxy setup: can't create column family: %s", cfName) |
| 84 |
} |
| 85 |
} |
| 86 |
|
| 87 |
dataClient, err := bigtable.NewClientWithConfig(ctx, "client", "instance", bigtable.ClientConfig{MetricsProvider: bigtable.NoopMetricsProvider{}}, |
| 88 |
option.WithGRPCConn(conn), option.WithGRPCDialOption(grpc.WithBlock())) |
| 89 |
if err != nil { |
| 90 |
return fmt.Errorf("testproxy setup: can't create Bigtable client: %v", err) |
| 91 |
} |
| 92 |
defer dataClient.Close() |
| 93 |
|
| 94 |
t := dataClient.Open(tableID) |
| 95 |
|
| 96 |
for fc := 0; fc < count; fc++ { |
| 97 |
for cc := count; cc > 0; cc-- { |
| 98 |
for tc := 0; tc < count; tc++ { |
| 99 |
rmw := bigtable.NewReadModifyWrite() |
| 100 |
rmw.AppendValue(fmt.Sprintf("%s%d", columnFamily, fc), fmt.Sprintf("coll%d", cc), []byte("test data")) |
| 101 |
|
| 102 |
_, err = t.ApplyReadModifyWrite(ctx, rowKey, rmw) |
| 103 |
if err != nil { |
| 104 |
return fmt.Errorf("testproxy setup: failure populating row: %v", err) |
| 105 |
} |
| 106 |
} |
| 107 |
} |
| 108 |
} |
| 109 |
|
| 110 |
return nil |
| 111 |
} |
| 112 |
|
| 113 |
/* |
| 114 |
TestMain has three threads that it needs to start: |
| 115 |
1) The mocked Bigtable service (server) |
| 116 |
2) The NewCloudBigtableV2TestProxyClient client that sends requests to the testproxy server. |
| 117 |
3) The testproxy server under test |
| 118 |
|
| 119 |
The communication sequence looks kind of like: |
| 120 |
|
| 121 |
TestProxyClient <=> test proxy server (what we're testing) <=> mocked BT server |
| 122 |
*/ |
| 123 |
func TestMain(m *testing.M) { |
| 124 |
ctx := context.Background() |
| 125 |
|
| 126 |
// 1) Start the mocked Bigtable service |
| 127 |
// This requires creating a "table" in memory |
| 128 |
bts, err := bttest.NewServer(bigtableAddress) |
| 129 |
if err != nil { |
| 130 |
log.Fatalf("testproxy setup: can't create inmem Bigtable server") |
| 131 |
} |
| 132 |
err = populateTable(bts) |
| 133 |
if err != nil { |
| 134 |
log.Fatalf("testproxy setup: can't populate mock table") |
| 135 |
} |
| 136 |
|
| 137 |
// 3) Start the test proxy server |
| 138 |
lis = bufconn.Listen(buffer) |
| 139 |
s := newProxyServer(lis) |
| 140 |
defer s.Stop() |
| 141 |
go func() { |
| 142 |
if err := s.Serve(lis); err != nil { |
| 143 |
log.Fatalf("failed to serve: %v", err) |
| 144 |
} |
| 145 |
}() |
| 146 |
|
| 147 |
// 2) Create the test proxy client |
| 148 |
conn2, err := grpc.DialContext(ctx, lis.Addr().String(), |
| 149 |
grpc.WithContextDialer(bufDialer), |
| 150 |
grpc.WithTransportCredentials(insecure.NewCredentials())) |
| 151 |
if err != nil { |
| 152 |
log.Fatalf("testproxy setup: failed to dial testproxy: %v", err) |
| 153 |
} |
| 154 |
defer conn2.Close() |
| 155 |
client = pb.NewCloudBigtableV2TestProxyClient(conn2) |
| 156 |
|
| 157 |
// This could create a little bit of a race condition with the previous |
| 158 |
// go routine ... |
| 159 |
// However, we need to have the test proxy server running in order to create |
| 160 |
// a reusable client stored in its memory |
| 161 |
req := &pb.CreateClientRequest{ |
| 162 |
ClientId: testProxyClient, |
| 163 |
ProjectId: "client", |
| 164 |
DataTarget: bigtableAddress, |
| 165 |
InstanceId: "instance", |
| 166 |
} |
| 167 |
|
| 168 |
_, err = client.CreateClient(ctx, req) |
| 169 |
if err != nil { |
| 170 |
log.Fatalf("testproxy setup: CreateClient() failed: %v", err) |
| 171 |
} |
| 172 |
|
| 173 |
os.Exit(m.Run()) |
| 174 |
} |
| 175 |
|
| 176 |
func TestCreateAndRemoveClient(t *testing.T) { |
| 177 |
// Test |
| 178 |
cid := "testCreateClient" |
| 179 |
ctx := context.Background() |
| 180 |
|
| 181 |
req := &pb.CreateClientRequest{ |
| 182 |
ClientId: cid, |
| 183 |
ProjectId: "client", |
| 184 |
DataTarget: bigtableAddress, |
| 185 |
InstanceId: "instance", |
| 186 |
} |
| 187 |
|
| 188 |
_, err := client.CreateClient(ctx, req) |
| 189 |
|
| 190 |
if err != nil { |
| 191 |
t.Fatalf("testproxy test: CreateClient() failed: %v", err) |
| 192 |
} |
| 193 |
|
| 194 |
t.Log("testproxy test: client created successfully in test proxy") |
| 195 |
|
| 196 |
_, err = client.RemoveClient(ctx, &pb.RemoveClientRequest{ |
| 197 |
ClientId: cid, |
| 198 |
}) |
| 199 |
|
| 200 |
if err != nil { |
| 201 |
t.Errorf("testproxy test: RemoveClient() failed: %v", err) |
| 202 |
} |
| 203 |
} |
| 204 |
|
| 205 |
func TestReadRow(t *testing.T) { |
| 206 |
ctx := context.Background() |
| 207 |
req := &pb.ReadRowRequest{ |
| 208 |
TableName: tableName, |
| 209 |
ClientId: testProxyClient, |
| 210 |
RowKey: rowKey, |
| 211 |
} |
| 212 |
|
| 213 |
resp, err := client.ReadRow(ctx, req) |
| 214 |
if err != nil { |
| 215 |
t.Fatalf("testproxy test: ReadRow() failed: %v", err) |
| 216 |
} |
| 217 |
|
| 218 |
stat := resp.Status.Code |
| 219 |
if stat != int32(codes.OK) { |
| 220 |
t.Errorf("testproxy test: ReadRow() didn't return OK") |
| 221 |
} |
| 222 |
|
| 223 |
row := resp.Row |
| 224 |
if string(row.Key) != "row" { |
| 225 |
t.Errorf("testproxy test: ReadRow() returned wrong row") |
| 226 |
} |
| 227 |
} |
| 228 |
|
| 229 |
func TestBulkMutateRows(t *testing.T) { |
| 230 |
ctx := context.Background() |
| 231 |
req := &pb.MutateRowsRequest{ |
| 232 |
ClientId: testProxyClient, |
| 233 |
Request: &btpb.MutateRowsRequest{ |
| 234 |
TableName: tableName, |
| 235 |
Entries: []*btpb.MutateRowsRequest_Entry{ |
| 236 |
{ |
| 237 |
RowKey: []byte(rowKey), |
| 238 |
Mutations: []*btpb.Mutation{ |
| 239 |
{ |
| 240 |
Mutation: &btpb.Mutation_SetCell_{ |
| 241 |
SetCell: &btpb.Mutation_SetCell{ |
| 242 |
ColumnQualifier: []byte("coll2"), |
| 243 |
FamilyName: "cf0", |
| 244 |
Value: []byte("bulked up mutant!"), |
| 245 |
}, |
| 246 |
}, |
| 247 |
}, |
| 248 |
}, |
| 249 |
}, |
| 250 |
}, |
| 251 |
}, |
| 252 |
} |
| 253 |
|
| 254 |
resp, err := client.BulkMutateRows(ctx, req) |
| 255 |
if err != nil { |
| 256 |
t.Fatalf("testproxy test: BulkMutateRows returned error: %v", err) |
| 257 |
} |
| 258 |
|
| 259 |
if resp.Status.Code != int32(codes.OK) { |
| 260 |
t.Errorf("testproxy test: BulkMutateRows() didn't return OK; got %v", resp.Status.Code) |
| 261 |
} |
| 262 |
|
| 263 |
if len(resp.Entries) != 0 { |
| 264 |
t.Errorf("testproxy test: BulkMutateRows() returned individual errors; got %v", resp.Entries) |
| 265 |
} |
| 266 |
} |
| 267 |
|
| 268 |
func TestMutateRow(t *testing.T) { |
| 269 |
ctx := context.Background() |
| 270 |
req := &pb.MutateRowRequest{ |
| 271 |
ClientId: testProxyClient, |
| 272 |
Request: &btpb.MutateRowRequest{ |
| 273 |
TableName: tableName, |
| 274 |
RowKey: []byte(rowKey), |
| 275 |
Mutations: []*btpb.Mutation{ |
| 276 |
{ |
| 277 |
Mutation: &btpb.Mutation_SetCell_{ |
| 278 |
SetCell: &btpb.Mutation_SetCell{ |
| 279 |
ColumnQualifier: []byte("coll1"), |
| 280 |
FamilyName: "cf0", |
| 281 |
Value: []byte("mutant!"), |
| 282 |
}, |
| 283 |
}, |
| 284 |
}, |
| 285 |
}, |
| 286 |
}, |
| 287 |
} |
| 288 |
|
| 289 |
resp, err := client.MutateRow(ctx, req) |
| 290 |
if err != nil { |
| 291 |
t.Fatalf("testproxy test: MutateRow() returned error: %v", err) |
| 292 |
} |
| 293 |
|
| 294 |
if resp.Status.Code != int32(codes.OK) { |
| 295 |
t.Errorf("testproxy test: MutateRow() didn't return OK; got %v", resp.Status.Code) |
| 296 |
|
| 297 |
} |
| 298 |
} |
| 299 |
|
| 300 |
func TestReadRows(t *testing.T) { |
| 301 |
ctx := context.Background() |
| 302 |
req := &pb.ReadRowsRequest{ |
| 303 |
ClientId: testProxyClient, |
| 304 |
Request: &btpb.ReadRowsRequest{ |
| 305 |
TableName: tableName, |
| 306 |
}, |
| 307 |
} |
| 308 |
|
| 309 |
resp, err := client.ReadRows(ctx, req) |
| 310 |
if err != nil { |
| 311 |
t.Fatalf("testproxy test: ReadRows returned error: %v", err) |
| 312 |
} |
| 313 |
|
| 314 |
if resp.Status.Code != int32(codes.OK) { |
| 315 |
t.Errorf("testproxy test: ReadRows() didn't return OK; got %v", resp.Status.Code) |
| 316 |
} |
| 317 |
|
| 318 |
if len(resp.Rows) != 1 { |
| 319 |
t.Errorf("testproxy test: SampleRowKeys() returned wrong number of results; got: %d", len(resp.Rows)) |
| 320 |
|
| 321 |
} |
| 322 |
} |
| 323 |
|
| 324 |
func TestCheckAndMutateRow(t *testing.T) { |
| 325 |
ctx := context.Background() |
| 326 |
req := &pb.CheckAndMutateRowRequest{ |
| 327 |
ClientId: testProxyClient, |
| 328 |
Request: &btpb.CheckAndMutateRowRequest{ |
| 329 |
TableName: tableName, |
| 330 |
RowKey: []byte(rowKey), |
| 331 |
PredicateFilter: &btpb.RowFilter{ |
| 332 |
Filter: &btpb.RowFilter_PassAllFilter{}, |
| 333 |
}, |
| 334 |
TrueMutations: []*btpb.Mutation{ |
| 335 |
{ |
| 336 |
Mutation: &btpb.Mutation_SetCell_{ |
| 337 |
SetCell: &btpb.Mutation_SetCell{ |
| 338 |
ColumnQualifier: []byte("coll1"), |
| 339 |
FamilyName: "cf0", |
| 340 |
Value: []byte("check success"), |
| 341 |
}, |
| 342 |
}, |
| 343 |
}, |
| 344 |
}, |
| 345 |
FalseMutations: []*btpb.Mutation{ |
| 346 |
{ |
| 347 |
Mutation: &btpb.Mutation_SetCell_{ |
| 348 |
SetCell: &btpb.Mutation_SetCell{ |
| 349 |
ColumnQualifier: []byte("coll1"), |
| 350 |
FamilyName: "cf0", |
| 351 |
Value: []byte("check failed!"), |
| 352 |
}, |
| 353 |
}, |
| 354 |
}, |
| 355 |
}, |
| 356 |
}, |
| 357 |
} |
| 358 |
|
| 359 |
resp, err := client.CheckAndMutateRow(ctx, req) |
| 360 |
if err != nil { |
| 361 |
t.Fatalf("testproxy test: CheckAndMutateRow() returned error: %v", err) |
| 362 |
} |
| 363 |
|
| 364 |
if resp.Status.Code != int32(codes.OK) { |
| 365 |
t.Errorf("testproxy test: CheckAndMutateRow() didn't return OK; got %v", resp.Status.Code) |
| 366 |
} |
| 367 |
|
| 368 |
if resp.Result.PredicateMatched != true { |
| 369 |
t.Errorf("testproxy test: CheckAndMutateRow() returned wrong results; got: %v", resp.Result.PredicateMatched) |
| 370 |
} |
| 371 |
} |
| 372 |
|
| 373 |
func TestSampleRowKeys(t *testing.T) { |
| 374 |
ctx := context.Background() |
| 375 |
req := &pb.SampleRowKeysRequest{ |
| 376 |
ClientId: testProxyClient, |
| 377 |
Request: &btpb.SampleRowKeysRequest{ |
| 378 |
TableName: tableName, |
| 379 |
}, |
| 380 |
} |
| 381 |
|
| 382 |
resp, err := client.SampleRowKeys(ctx, req) |
| 383 |
if err != nil { |
| 384 |
t.Fatalf("testproxy test: SampleRowKeys() returned error: %v", err) |
| 385 |
} |
| 386 |
|
| 387 |
if resp.Status.Code != int32(codes.OK) { |
| 388 |
t.Errorf("testproxy test: SampleRowKeys() didn't return OK; got %v", resp.Status.Code) |
| 389 |
} |
| 390 |
|
| 391 |
if len(resp.Samples) != 1 { |
| 392 |
t.Errorf("testproxy test: SampleRowKeys() returned wrong number of results; got: %d", len(resp.Samples)) |
| 393 |
} |
| 394 |
} |
| 395 |
|
| 396 |
func TestReadWriteRow(t *testing.T) { |
| 397 |
ctx := context.Background() |
| 398 |
req := &pb.ReadModifyWriteRowRequest{ |
| 399 |
ClientId: testProxyClient, |
| 400 |
Request: &btpb.ReadModifyWriteRowRequest{ |
| 401 |
TableName: tableName, |
| 402 |
RowKey: []byte(rowKey), |
| 403 |
Rules: []*btpb.ReadModifyWriteRule{ |
| 404 |
{ |
| 405 |
Rule: &btpb.ReadModifyWriteRule_AppendValue{ |
| 406 |
AppendValue: []byte("appended!"), |
| 407 |
}, |
| 408 |
FamilyName: "cf0", |
| 409 |
ColumnQualifier: []byte("coll1"), |
| 410 |
}, |
| 411 |
}, |
| 412 |
}, |
| 413 |
} |
| 414 |
|
| 415 |
resp, err := client.ReadModifyWriteRow(ctx, req) |
| 416 |
if err != nil { |
| 417 |
t.Fatalf("testproxy test: ReadModifyWriteRow() returned error: %v", err) |
| 418 |
} |
| 419 |
|
| 420 |
if resp.Status.Code != int32(codes.OK) { |
| 421 |
t.Errorf("testproxy test: ReadModifyWriteRow() didn't return OK; got %v", resp.Status.Code) |
| 422 |
} |
| 423 |
|
| 424 |
if string(resp.Row.Key) != rowKey { |
| 425 |
t.Errorf("testproxy test: ReadModifyWriteRow() returned wrong results; got: %v", resp.Row.Key) |
| 426 |
} |
| 427 |
} |