1// Copyright 2019 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 15package bttest 16 17import ( 18 "context" 19 "testing" 20 21 btapb "google.golang.org/genproto/googleapis/bigtable/admin/v2" 22 "google.golang.org/grpc/codes" 23 "google.golang.org/grpc/status" 24) 25 26func TestDeleteInstance(t *testing.T) { 27 srv := &server{ 28 instances: map[string]*btapb.Instance{ 29 "projects/test/instances/a1042-instance": { 30 Name: "projects/test/instances/a1042-instance/test1", 31 DisplayName: "test1", 32 State: btapb.Instance_READY, 33 Labels: map[string]string{ 34 "component": "ingest", 35 "cost-center": "sales", 36 }, 37 }, 38 }, 39 } 40 41 ctx := context.Background() 42 43 // 1. Deletion of a just created instance should succeed. 44 delReq := &btapb.DeleteInstanceRequest{Name: "projects/test/instances/a1042-instance"} 45 if _, err := srv.DeleteInstance(ctx, delReq); err != nil { 46 t.Fatalf("Unexpected failed to delete the newly created instance: %v", err) 47 } 48 49 // 2. Deleting a now non-existent instance should fail. 50 _, err := srv.DeleteInstance(ctx, delReq) 51 if err == nil { 52 t.Fatal("Expected an error from deleting a non-existent instance") 53 } 54 gstatus, _ := status.FromError(err) 55 if g, w := gstatus.Code(), codes.NotFound; g != w { 56 t.Errorf("Mismatched status code\ngot %d %s\nwant %d %s", g, g, w, w) 57 } 58 if g, w := gstatus.Message(), `instance "projects/test/instances/a1042-instance" not found`; g != w { 59 t.Errorf("Mismatched status message\ngot %q\nwant %q", g, w) 60 } 61 62 // 3. Now test deletion with invalid names which should always fail. 63 invalidNames := []string{ 64 "", " ", "//", "/", 65 "projects/foo", 66 "projects/foo/instances", 67 "projects/foo/instances/", 68 "projects/foo/instances/a10/bar", 69 "projects/foo/instances/a10/bar/fizz", 70 "/projects/foo*", 71 } 72 73 for _, invalidName := range invalidNames { 74 req := &btapb.DeleteInstanceRequest{Name: invalidName} 75 _, err := srv.DeleteInstance(ctx, req) 76 gstatus, _ := status.FromError(err) 77 if g, w := gstatus.Code(), codes.InvalidArgument; g != w { 78 t.Errorf("Mismatched status code\ngot %d %s\nwant %d %s", g, g, w, w) 79 } 80 81 wantMsg := "Error in field 'instance_name' : Invalid name for collection instances : " + 82 "Should match " + `^projects/[a-z][a-z0-9\\-]+[a-z0-9]/instances/[a-z][a-z0-9\\-]+[a-z0-9]$` + 83 " but found '" + invalidName + "'" 84 if g, w := gstatus.Message(), wantMsg; g != w { 85 t.Errorf("%q: mismatched status message\ngot %q\nwant %q\n\n", invalidName, g, w) 86 } 87 } 88} 89