vitess.io/vitess@v0.16.2/go/vt/topo/test/vschema.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 "context" 23 24 "github.com/stretchr/testify/require" 25 "google.golang.org/protobuf/proto" 26 27 "vitess.io/vitess/go/vt/topo" 28 29 topodatapb "vitess.io/vitess/go/vt/proto/topodata" 30 vschemapb "vitess.io/vitess/go/vt/proto/vschema" 31 ) 32 33 // checkVSchema runs the tests on the VSchema part of the API 34 func checkVSchema(t *testing.T, ts *topo.Server) { 35 ctx := context.Background() 36 if err := ts.CreateKeyspace(ctx, "test_keyspace", &topodatapb.Keyspace{}); err != nil { 37 t.Fatalf("CreateKeyspace: %v", err) 38 } 39 40 if err := ts.CreateShard(ctx, "test_keyspace", "b0-c0"); err != nil { 41 t.Fatalf("CreateShard: %v", err) 42 } 43 44 _, err := ts.GetVSchema(ctx, "test_keyspace") 45 if !topo.IsErrType(err, topo.NoNode) { 46 t.Error(err) 47 } 48 49 err = ts.SaveVSchema(ctx, "test_keyspace", &vschemapb.Keyspace{ 50 Tables: map[string]*vschemapb.Table{ 51 "unsharded": {}, 52 }, 53 }) 54 if err != nil { 55 t.Fatal(err) 56 } 57 58 got, err := ts.GetVSchema(ctx, "test_keyspace") 59 require.NoError(t, err) 60 want := &vschemapb.Keyspace{ 61 Tables: map[string]*vschemapb.Table{ 62 "unsharded": {}, 63 }, 64 } 65 if !proto.Equal(got, want) { 66 t.Errorf("GetVSchema: %s, want %s", got, want) 67 } 68 69 err = ts.SaveVSchema(ctx, "test_keyspace", &vschemapb.Keyspace{ 70 Sharded: true, 71 }) 72 require.NoError(t, err) 73 74 got, err = ts.GetVSchema(ctx, "test_keyspace") 75 require.NoError(t, err) 76 want = &vschemapb.Keyspace{ 77 Sharded: true, 78 } 79 if !proto.Equal(got, want) { 80 t.Errorf("GetVSchema: %s, want %s", got, want) 81 } 82 83 // Make sure the vschema is not returned as a shard name, 84 // because they share the same directory location. 85 shards, err := ts.GetShardNames(ctx, "test_keyspace") 86 if err != nil { 87 t.Errorf("GetShardNames: %v", err) 88 } 89 if len(shards) != 1 || shards[0] != "b0-c0" { 90 t.Errorf(`GetShardNames: want [ "b0-c0" ], got %v`, shards) 91 } 92 } 93 94 // checkRoutingRules runs the tests on the routing rules part of the API 95 func checkRoutingRules(t *testing.T, ts *topo.Server) { 96 ctx := context.Background() 97 98 if _, err := ts.GetRoutingRules(ctx); err != nil { 99 t.Fatal(err) 100 } 101 102 want := &vschemapb.RoutingRules{ 103 Rules: []*vschemapb.RoutingRule{{ 104 FromTable: "t1", 105 ToTables: []string{"t2", "t3"}, 106 }}, 107 } 108 if err := ts.SaveRoutingRules(ctx, want); err != nil { 109 t.Fatal(err) 110 } 111 112 got, err := ts.GetRoutingRules(ctx) 113 require.NoError(t, err) 114 if !proto.Equal(got, want) { 115 t.Errorf("GetRoutingRules: %v, want %v", got, want) 116 } 117 }