vitess.io/vitess@v0.16.2/go/vt/wrangler/testlib/shard_test.go (about) 1 /* 2 Copyright 2019 The Vitess Authors. 3 4 Licensed under the Apache License, Version 2.0 (the "License"); 5 you may not use this file except in compliance with the License. 6 You may obtain a copy of the License at 7 8 http://www.apache.org/licenses/LICENSE-2.0 9 10 Unless required by applicable law or agreed to in writing, software 11 distributed under the License is distributed on an "AS IS" BASIS, 12 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 See the License for the specific language governing permissions and 14 limitations under the License. 15 */ 16 17 package testlib 18 19 import ( 20 "strings" 21 "testing" 22 23 "context" 24 25 "vitess.io/vitess/go/vt/logutil" 26 "vitess.io/vitess/go/vt/topo" 27 "vitess.io/vitess/go/vt/topo/memorytopo" 28 "vitess.io/vitess/go/vt/topotools" 29 "vitess.io/vitess/go/vt/vttablet/tmclient" 30 "vitess.io/vitess/go/vt/wrangler" 31 32 topodatapb "vitess.io/vitess/go/vt/proto/topodata" 33 ) 34 35 func TestDeleteShardCleanup(t *testing.T) { 36 ctx := context.Background() 37 ts := memorytopo.NewServer("cell1", "cell2") 38 wr := wrangler.New(logutil.NewConsoleLogger(), ts, tmclient.NewTabletManagerClient()) 39 vp := NewVtctlPipe(t, ts) 40 defer vp.Close() 41 42 // Create a primary, a couple good replicas 43 primary := NewFakeTablet(t, wr, "cell1", 0, topodatapb.TabletType_PRIMARY, nil) 44 replica := NewFakeTablet(t, wr, "cell1", 1, topodatapb.TabletType_REPLICA, nil) 45 remoteReplica := NewFakeTablet(t, wr, "cell2", 2, topodatapb.TabletType_REPLICA, nil) 46 47 // Build keyspace graph 48 err := topotools.RebuildKeyspace(context.Background(), logutil.NewConsoleLogger(), ts, primary.Tablet.Keyspace, []string{"cell1", "cell2"}, false) 49 if err != nil { 50 t.Fatalf("RebuildKeyspaceLocked failed: %v", err) 51 } 52 53 // Delete the ShardReplication record in cell2 54 if err := ts.DeleteShardReplication(ctx, "cell2", remoteReplica.Tablet.Keyspace, remoteReplica.Tablet.Shard); err != nil { 55 t.Fatalf("DeleteShardReplication failed: %v", err) 56 } 57 58 // Now try to delete the shard without even_if_serving or 59 // recursive flag, should fail on serving check first. 60 if err := vp.Run([]string{ 61 "DeleteShard", 62 primary.Tablet.Keyspace + "/" + primary.Tablet.Shard, 63 }); err == nil || !strings.Contains(err.Error(), "is still serving, cannot delete it") { 64 t.Fatalf("DeleteShard() returned wrong error: %v", err) 65 } 66 67 // Now try to delete the shard with even_if_serving, but 68 // without recursive flag, should fail on existing tablets. 69 if err := vp.Run([]string{ 70 "DeleteShard", 71 "--even_if_serving", 72 primary.Tablet.Keyspace + "/" + primary.Tablet.Shard, 73 }); err == nil || !strings.Contains(err.Error(), "use -recursive or remove them manually") { 74 t.Fatalf("DeleteShard(evenIfServing=true) returned wrong error: %v", err) 75 } 76 77 // Now try to delete the shard with even_if_serving and recursive, 78 // it should just work. 79 if err := vp.Run([]string{ 80 "DeleteShard", 81 "--recursive", 82 "--even_if_serving", 83 primary.Tablet.Keyspace + "/" + primary.Tablet.Shard, 84 }); err != nil { 85 t.Fatalf("DeleteShard(recursive=true, evenIfServing=true) should have worked but returned: %v", err) 86 } 87 88 // Make sure all tablets are gone. 89 for _, ft := range []*FakeTablet{primary, replica, remoteReplica} { 90 if _, err := ts.GetTablet(ctx, ft.Tablet.Alias); !topo.IsErrType(err, topo.NoNode) { 91 t.Errorf("tablet %v is still in topo: %v", ft.Tablet.Alias, err) 92 } 93 } 94 95 // Make sure the shard is gone. 96 if _, err := ts.GetShard(ctx, primary.Tablet.Keyspace, primary.Tablet.Shard); !topo.IsErrType(err, topo.NoNode) { 97 t.Errorf("shard %v/%v is still in topo: %v", primary.Tablet.Keyspace, primary.Tablet.Shard, err) 98 } 99 }