vitess.io/vitess@v0.16.2/go/vt/topo/test/tablet.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 // checkTablet verifies the topo server API is correct for managing tablets. 32 func checkTablet(t *testing.T, ts *topo.Server) { 33 ctx := context.Background() 34 35 tablet := &topodatapb.Tablet{ 36 Alias: &topodatapb.TabletAlias{ 37 Cell: LocalCellName, 38 Uid: 1, 39 }, 40 Hostname: "localhost", 41 MysqlHostname: "localhost", 42 PortMap: map[string]int32{ 43 "vt": 3333, 44 }, 45 46 Tags: map[string]string{"tag": "value"}, 47 Keyspace: "test_keyspace", 48 Type: topodatapb.TabletType_PRIMARY, 49 KeyRange: newKeyRange("-10"), 50 } 51 tablet.MysqlPort = 3334 52 if err := ts.CreateTablet(ctx, tablet); err != nil { 53 t.Fatalf("CreateTablet: %v", err) 54 } 55 if err := ts.CreateTablet(ctx, tablet); !topo.IsErrType(err, topo.NodeExists) { 56 t.Fatalf("CreateTablet(again): %v", err) 57 } 58 59 if _, err := ts.GetTablet(ctx, &topodatapb.TabletAlias{ 60 Cell: LocalCellName, 61 Uid: 666, 62 }); !topo.IsErrType(err, topo.NoNode) { 63 t.Fatalf("GetTablet(666): %v", err) 64 } 65 66 ti, err := ts.GetTablet(ctx, tablet.Alias) 67 if err != nil { 68 t.Fatalf("GetTablet %v: %v", tablet.Alias, err) 69 } 70 if !proto.Equal(ti.Tablet, tablet) { 71 t.Errorf("put and got tablets are not identical:\n%#v\n%#v", tablet, t) 72 } 73 74 if _, err := ts.GetTabletAliasesByCell(ctx, "666"); !topo.IsErrType(err, topo.NoNode) { 75 t.Errorf("GetTabletsByCell(666): %v", err) 76 } 77 78 inCell, err := ts.GetTabletAliasesByCell(ctx, LocalCellName) 79 if err != nil { 80 t.Fatalf("GetTabletsByCell: %v", err) 81 } 82 if len(inCell) != 1 || !proto.Equal(inCell[0], tablet.Alias) { 83 t.Errorf("GetTabletsByCell: want [%v], got %v", tablet.Alias, inCell) 84 } 85 86 ti.Tablet.Hostname = "remotehost" 87 if err := ts.UpdateTablet(ctx, ti); err != nil { 88 t.Errorf("UpdateTablet: %v", err) 89 } 90 91 ti, err = ts.GetTablet(ctx, tablet.Alias) 92 if err != nil { 93 t.Fatalf("GetTablet %v: %v", tablet.Alias, err) 94 } 95 if want := "remotehost"; ti.Tablet.Hostname != want { 96 t.Errorf("nt.Hostname: want %v, got %v", want, ti.Tablet.Hostname) 97 } 98 99 // test UpdateTabletFields works 100 updatedTablet, err := ts.UpdateTabletFields(ctx, tablet.Alias, func(t *topodatapb.Tablet) error { 101 t.Hostname = "anotherhost" 102 return nil 103 }) 104 if err != nil { 105 t.Fatalf("UpdateTabletFields: %v", err) 106 } 107 if got, want := updatedTablet.Hostname, "anotherhost"; got != want { 108 t.Errorf("updatedTablet.Hostname = %q, want %q", got, want) 109 } 110 ti, err = ts.GetTablet(ctx, tablet.Alias) 111 if err != nil { 112 t.Fatalf("GetTablet %v: %v", tablet.Alias, err) 113 } 114 if got, want := ti.Tablet.Hostname, "anotherhost"; got != want { 115 t.Errorf("nt.Hostname = %q, want %q", got, want) 116 } 117 118 // test UpdateTabletFields that returns ErrNoUpdateNeeded works 119 if _, err := ts.UpdateTabletFields(ctx, tablet.Alias, func(t *topodatapb.Tablet) error { 120 return topo.NewError(topo.NoUpdateNeeded, tablet.Alias.String()) 121 }); err != nil { 122 t.Errorf("UpdateTabletFields: %v", err) 123 } 124 if nti, nerr := ts.GetTablet(ctx, tablet.Alias); nti.Version() != ti.Version() { 125 t.Fatalf("GetTablet %v: %v %v", tablet.Alias, nti, nerr) 126 } 127 128 if want := "anotherhost"; ti.Tablet.Hostname != want { 129 t.Errorf("nt.Hostname: want %v, got %v", want, ti.Tablet.Hostname) 130 } 131 132 if err := ts.DeleteTablet(ctx, tablet.Alias); err != nil { 133 t.Errorf("DeleteTablet: %v", err) 134 } 135 if err := ts.DeleteTablet(ctx, tablet.Alias); !topo.IsErrType(err, topo.NoNode) { 136 t.Errorf("DeleteTablet(again): %v", err) 137 } 138 139 if _, err := ts.GetTablet(ctx, tablet.Alias); !topo.IsErrType(err, topo.NoNode) { 140 t.Errorf("GetTablet: expected error, tablet was deleted: %v", err) 141 } 142 143 }