vitess.io/vitess@v0.16.2/go/vt/wrangler/testlib/find_tablet_test.go (about) 1 /* 2 Copyright 2018 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 "testing" 21 22 "context" 23 24 "vitess.io/vitess/go/vt/logutil" 25 "vitess.io/vitess/go/vt/topo" 26 "vitess.io/vitess/go/vt/topo/memorytopo" 27 "vitess.io/vitess/go/vt/topo/topoproto" 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 TestFindTablet(t *testing.T) { 36 ctx := context.Background() 37 ts := memorytopo.NewServer("cell1", "cell2") 38 wr := wrangler.New(logutil.NewConsoleLogger(), ts, tmclient.NewTabletManagerClient()) 39 40 // Create an old primary, two good replicas 41 oldPrimary := NewFakeTablet(t, wr, "cell1", 0, topodatapb.TabletType_PRIMARY, nil) 42 goodReplica1 := NewFakeTablet(t, wr, "cell1", 2, topodatapb.TabletType_REPLICA, nil) 43 goodReplica2 := NewFakeTablet(t, wr, "cell2", 3, topodatapb.TabletType_REPLICA, nil) 44 45 // Build keyspace graph 46 err := topotools.RebuildKeyspace(context.Background(), logutil.NewConsoleLogger(), ts, oldPrimary.Tablet.Keyspace, []string{"cell1", "cell2"}, false) 47 if err != nil { 48 t.Fatalf("RebuildKeyspaceLocked failed: %v", err) 49 } 50 51 // make sure we can find the tablets 52 // even with a datacenter being down. 53 tabletMap, err := ts.GetTabletMapForShardByCell(ctx, "test_keyspace", "0", []string{"cell1"}) 54 if err != nil { 55 t.Fatalf("GetTabletMapForShardByCell should have worked but got: %v", err) 56 } 57 primary, err := topotools.FindTabletByHostAndPort(tabletMap, oldPrimary.Tablet.Hostname, "vt", oldPrimary.Tablet.PortMap["vt"]) 58 if err != nil || !topoproto.TabletAliasEqual(primary, oldPrimary.Tablet.Alias) { 59 t.Fatalf("FindTabletByHostAndPort(primary) failed: %v %v", err, primary) 60 } 61 replica1, err := topotools.FindTabletByHostAndPort(tabletMap, goodReplica1.Tablet.Hostname, "vt", goodReplica1.Tablet.PortMap["vt"]) 62 if err != nil || !topoproto.TabletAliasEqual(replica1, goodReplica1.Tablet.Alias) { 63 t.Fatalf("FindTabletByHostAndPort(replica1) failed: %v %v", err, primary) 64 } 65 replica2, err := topotools.FindTabletByHostAndPort(tabletMap, goodReplica2.Tablet.Hostname, "vt", goodReplica2.Tablet.PortMap["vt"]) 66 if !topo.IsErrType(err, topo.NoNode) { 67 t.Fatalf("FindTabletByHostAndPort(replica2) worked: %v %v", err, replica2) 68 } 69 70 // Make sure the primary is not exported in other cells 71 tabletMap, _ = ts.GetTabletMapForShardByCell(ctx, "test_keyspace", "0", []string{"cell2"}) 72 primary, err = topotools.FindTabletByHostAndPort(tabletMap, oldPrimary.Tablet.Hostname, "vt", oldPrimary.Tablet.PortMap["vt"]) 73 if !topo.IsErrType(err, topo.NoNode) { 74 t.Fatalf("FindTabletByHostAndPort(primary) worked in cell2: %v %v", err, primary) 75 } 76 77 // Get tablet map for all cells. If there were to be failures talking to local cells, this will return the tablet map 78 // and forward a partial result error 79 tabletMap, err = ts.GetTabletMapForShard(ctx, "test_keyspace", "0") 80 if err != nil { 81 t.Fatalf("GetTabletMapForShard should nil but got: %v", err) 82 } 83 primary, err = topotools.FindTabletByHostAndPort(tabletMap, oldPrimary.Tablet.Hostname, "vt", oldPrimary.Tablet.PortMap["vt"]) 84 if err != nil || !topoproto.TabletAliasEqual(primary, oldPrimary.Tablet.Alias) { 85 t.Fatalf("FindTabletByHostAndPort(primary) failed: %v %v", err, primary) 86 } 87 88 }