vitess.io/vitess@v0.16.2/go/vt/topo/test/serving.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 vschemapb "vitess.io/vitess/go/vt/proto/vschema" 30 ) 31 32 // checkSrvKeyspace tests the SrvKeyspace methods (other than watch). 33 func checkSrvKeyspace(t *testing.T, ts *topo.Server) { 34 ctx := context.Background() 35 36 // Test GetSrvKeyspaceNames returns an empty list correctly. 37 if names, err := ts.GetSrvKeyspaceNames(ctx, LocalCellName); err != nil || len(names) != 0 { 38 t.Errorf("GetSrvKeyspace(not there): %v %v", names, err) 39 } 40 41 // test cell/keyspace entries (SrvKeyspace) 42 srvKeyspace := &topodatapb.SrvKeyspace{ 43 Partitions: []*topodatapb.SrvKeyspace_KeyspacePartition{ 44 { 45 ServedType: topodatapb.TabletType_PRIMARY, 46 ShardReferences: []*topodatapb.ShardReference{ 47 { 48 Name: "-80", 49 KeyRange: &topodatapb.KeyRange{ 50 End: []byte{0x80}, 51 }, 52 }, 53 }, 54 }, 55 }, 56 ServedFrom: []*topodatapb.SrvKeyspace_ServedFrom{ 57 { 58 TabletType: topodatapb.TabletType_REPLICA, 59 Keyspace: "other_keyspace", 60 }, 61 }, 62 } 63 if err := ts.UpdateSrvKeyspace(ctx, LocalCellName, "test_keyspace", srvKeyspace); err != nil { 64 t.Errorf("UpdateSrvKeyspace(1): %v", err) 65 } 66 if _, err := ts.GetSrvKeyspace(ctx, LocalCellName, "test_keyspace666"); !topo.IsErrType(err, topo.NoNode) { 67 t.Errorf("GetSrvKeyspace(invalid): %v", err) 68 } 69 if k, err := ts.GetSrvKeyspace(ctx, LocalCellName, "test_keyspace"); err != nil || !proto.Equal(srvKeyspace, k) { 70 t.Errorf("GetSrvKeyspace(valid): %v %v", err, k) 71 } 72 if k, err := ts.GetSrvKeyspaceNames(ctx, LocalCellName); err != nil || len(k) != 1 || k[0] != "test_keyspace" { 73 t.Errorf("GetSrvKeyspaceNames(): %v", err) 74 } 75 76 // check that updating a SrvKeyspace out of the blue works 77 if err := ts.UpdateSrvKeyspace(ctx, LocalCellName, "unknown_keyspace_so_far", srvKeyspace); err != nil { 78 t.Fatalf("UpdateSrvKeyspace(2): %v", err) 79 } 80 if k, err := ts.GetSrvKeyspace(ctx, LocalCellName, "unknown_keyspace_so_far"); err != nil || !proto.Equal(srvKeyspace, k) { 81 t.Errorf("GetSrvKeyspace(out of the blue): %v %v", err, k) 82 } 83 84 // Delete the SrvKeyspace. 85 if err := ts.DeleteSrvKeyspace(ctx, LocalCellName, "unknown_keyspace_so_far"); err != nil { 86 t.Fatalf("DeleteSrvKeyspace: %v", err) 87 } 88 if _, err := ts.GetSrvKeyspace(ctx, LocalCellName, "unknown_keyspace_so_far"); !topo.IsErrType(err, topo.NoNode) { 89 t.Errorf("GetSrvKeyspace(deleted) got %v, want ErrNoNode", err) 90 } 91 } 92 93 // checkSrvVSchema tests the SrvVSchema methods (other than watch). 94 func checkSrvVSchema(t *testing.T, ts *topo.Server) { 95 ctx := context.Background() 96 97 // check GetSrvVSchema returns topo.ErrNoNode if no SrvVSchema 98 if _, err := ts.GetSrvVSchema(ctx, LocalCellName); !topo.IsErrType(err, topo.NoNode) { 99 t.Errorf("GetSrvVSchema(not set): %v", err) 100 } 101 102 srvVSchema := &vschemapb.SrvVSchema{ 103 Keyspaces: map[string]*vschemapb.Keyspace{ 104 "test_keyspace": { 105 Sharded: true, 106 }, 107 }, 108 } 109 if err := ts.UpdateSrvVSchema(ctx, LocalCellName, srvVSchema); err != nil { 110 t.Errorf("UpdateSrvVSchema(1): %v", err) 111 } 112 if v, err := ts.GetSrvVSchema(ctx, LocalCellName); err != nil || !proto.Equal(srvVSchema, v) { 113 t.Errorf("GetSrvVSchema(valid): %v %v", err, v) 114 } 115 }