vitess.io/vitess@v0.16.2/go/vt/topo/test/replication.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 test 18 19 import ( 20 "testing" 21 22 "google.golang.org/protobuf/proto" 23 24 "context" 25 26 "vitess.io/vitess/go/vt/topo" 27 28 topodatapb "vitess.io/vitess/go/vt/proto/topodata" 29 ) 30 31 // checkShardReplication tests ShardReplication objects 32 func checkShardReplication(t *testing.T, ts *topo.Server) { 33 ctx := context.Background() 34 if _, err := ts.GetShardReplication(ctx, LocalCellName, "test_keyspace", "-10"); !topo.IsErrType(err, topo.NoNode) { 35 t.Errorf("GetShardReplication(not there): %v", err) 36 } 37 38 sr := &topodatapb.ShardReplication{ 39 Nodes: []*topodatapb.ShardReplication_Node{ 40 { 41 TabletAlias: &topodatapb.TabletAlias{ 42 Cell: "c1", 43 Uid: 1, 44 }, 45 }, 46 }, 47 } 48 if err := ts.UpdateShardReplicationFields(ctx, LocalCellName, "test_keyspace", "-10", func(oldSr *topodatapb.ShardReplication) error { 49 return topo.NewError(topo.NoUpdateNeeded, LocalCellName) 50 }); err != nil { 51 t.Fatalf("UpdateShardReplicationFields() failed: %v", err) 52 } 53 if err := ts.UpdateShardReplicationFields(ctx, LocalCellName, "test_keyspace", "-10", func(oldSr *topodatapb.ShardReplication) error { 54 proto.Reset(oldSr) 55 proto.Merge(oldSr, sr) 56 return nil 57 }); err != nil { 58 t.Fatalf("UpdateShardReplicationFields() failed: %v", err) 59 } 60 61 if sri, err := ts.GetShardReplication(ctx, LocalCellName, "test_keyspace", "-10"); err != nil { 62 t.Errorf("GetShardReplication(new guy) failed: %v", err) 63 } else { 64 if len(sri.Nodes) != 1 || 65 sri.Nodes[0].TabletAlias.Cell != "c1" || 66 sri.Nodes[0].TabletAlias.Uid != 1 { 67 t.Errorf("GetShardReplication(new guy) returned wrong value: %v", *sri) 68 } 69 } 70 71 if err := ts.UpdateShardReplicationFields(ctx, LocalCellName, "test_keyspace", "-10", func(sr *topodatapb.ShardReplication) error { 72 sr.Nodes = append(sr.Nodes, &topodatapb.ShardReplication_Node{ 73 TabletAlias: &topodatapb.TabletAlias{ 74 Cell: "c3", 75 Uid: 3, 76 }, 77 }) 78 return nil 79 }); err != nil { 80 t.Errorf("UpdateShardReplicationFields() failed: %v", err) 81 } 82 83 if sri, err := ts.GetShardReplication(ctx, LocalCellName, "test_keyspace", "-10"); err != nil { 84 t.Errorf("GetShardReplication(after append) failed: %v", err) 85 } else { 86 if len(sri.Nodes) != 2 || 87 sri.Nodes[0].TabletAlias.Cell != "c1" || 88 sri.Nodes[0].TabletAlias.Uid != 1 || 89 sri.Nodes[1].TabletAlias.Cell != "c3" || 90 sri.Nodes[1].TabletAlias.Uid != 3 { 91 t.Errorf("GetShardReplication(new guy) returned wrong value: %v", *sri) 92 } 93 } 94 95 if err := ts.DeleteShardReplication(ctx, LocalCellName, "test_keyspace", "-10"); err != nil { 96 t.Errorf("DeleteShardReplication(existing) failed: %v", err) 97 } 98 if err := ts.DeleteShardReplication(ctx, LocalCellName, "test_keyspace", "-10"); !topo.IsErrType(err, topo.NoNode) { 99 t.Errorf("DeleteShardReplication(again) returned: %v", err) 100 } 101 102 // Some implementations may already remove the directory if not data is in there, so we ignore topo.ErrNoNode. 103 if err := ts.DeleteKeyspaceReplication(ctx, LocalCellName, "test_keyspace"); err != nil && !topo.IsErrType(err, topo.NoNode) { 104 t.Errorf("DeleteKeyspaceReplication(existing) failed: %v", err) 105 } 106 // The second time though, it should be gone. 107 if err := ts.DeleteKeyspaceReplication(ctx, LocalCellName, "test_keyspace"); !topo.IsErrType(err, topo.NoNode) { 108 t.Errorf("DeleteKeyspaceReplication(again) returned: %v", err) 109 } 110 }