vitess.io/vitess@v0.16.2/go/vt/topo/test/keyspace.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 "vitess.io/vitess/go/vt/topo" 25 26 topodatapb "vitess.io/vitess/go/vt/proto/topodata" 27 ) 28 29 // checkKeyspace tests the keyspace part of the API 30 func checkKeyspace(t *testing.T, ts *topo.Server) { 31 ctx := context.Background() 32 keyspaces, err := ts.GetKeyspaces(ctx) 33 if err != nil { 34 t.Errorf("GetKeyspaces(empty): %v", err) 35 } 36 if len(keyspaces) != 0 { 37 t.Errorf("len(GetKeyspaces()) != 0: %v", keyspaces) 38 } 39 40 if err := ts.CreateKeyspace(ctx, "test_keyspace", &topodatapb.Keyspace{}); err != nil { 41 t.Errorf("CreateKeyspace: %v", err) 42 } 43 if err := ts.CreateKeyspace(ctx, "test_keyspace", &topodatapb.Keyspace{}); !topo.IsErrType(err, topo.NodeExists) { 44 t.Errorf("CreateKeyspace(again) is not ErrNodeExists: %v", err) 45 } 46 47 // Delete and re-create. 48 if err := ts.DeleteKeyspace(ctx, "test_keyspace"); err != nil { 49 t.Errorf("DeleteKeyspace: %v", err) 50 } 51 if err := ts.DeleteKeyspace(ctx, "test_keyspace"); !topo.IsErrType(err, topo.NoNode) { 52 t.Errorf("DeleteKeyspace(again): %v", err) 53 } 54 if err := ts.CreateKeyspace(ctx, "test_keyspace", &topodatapb.Keyspace{}); err != nil { 55 t.Errorf("CreateKeyspace: %v", err) 56 } 57 58 keyspaces, err = ts.GetKeyspaces(ctx) 59 if err != nil { 60 t.Errorf("GetKeyspaces: %v", err) 61 } 62 if len(keyspaces) != 1 || keyspaces[0] != "test_keyspace" { 63 t.Errorf("GetKeyspaces: want %v, got %v", []string{"test_keyspace"}, keyspaces) 64 } 65 66 k := &topodatapb.Keyspace{ 67 ServedFroms: []*topodatapb.Keyspace_ServedFrom{ 68 { 69 TabletType: topodatapb.TabletType_REPLICA, 70 Cells: []string{"c1", "c2"}, 71 Keyspace: "test_keyspace3", 72 }, 73 { 74 TabletType: topodatapb.TabletType_PRIMARY, 75 Cells: nil, 76 Keyspace: "test_keyspace3", 77 }, 78 }, 79 } 80 if err := ts.CreateKeyspace(ctx, "test_keyspace2", k); err != nil { 81 t.Errorf("CreateKeyspace: %v", err) 82 } 83 keyspaces, err = ts.GetKeyspaces(ctx) 84 if err != nil { 85 t.Errorf("GetKeyspaces: %v", err) 86 } 87 if len(keyspaces) != 2 || 88 keyspaces[0] != "test_keyspace" || 89 keyspaces[1] != "test_keyspace2" { 90 t.Errorf("GetKeyspaces: want %v, got %v", []string{"test_keyspace", "test_keyspace2"}, keyspaces) 91 } 92 93 // Re-read and update. Have to lock it, because UpdateKeyspace 94 // checks for the keyspace lock. 95 storedKI, err := ts.GetKeyspace(ctx, "test_keyspace2") 96 if err != nil { 97 t.Fatalf("GetKeyspace: %v", err) 98 } 99 lockCtx, unlock, err := ts.LockKeyspace(ctx, "test_keyspace2", "fake-action") 100 if err != nil { 101 t.Fatalf("LockKeyspace: %v", err) 102 } 103 if err := ts.UpdateKeyspace(lockCtx, storedKI); err != nil { 104 t.Fatalf("UpdateKeyspace: %v", err) 105 } 106 unlock(&err) 107 if err != nil { 108 t.Fatalf("unlock(test_keyspace2): %v", err) 109 } 110 111 // And read again to make sure it's good. 112 _, err = ts.GetKeyspace(ctx, "test_keyspace2") 113 if err != nil { 114 t.Fatalf("GetKeyspace: %v", err) 115 } 116 }