vitess.io/vitess@v0.16.2/go/vt/vtctld/explorer_test.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 vtctld 18 19 import ( 20 "path" 21 "reflect" 22 "testing" 23 24 "context" 25 26 "vitess.io/vitess/go/vt/topo" 27 "vitess.io/vitess/go/vt/topo/memorytopo" 28 29 topodatapb "vitess.io/vitess/go/vt/proto/topodata" 30 ) 31 32 // Test the explorer using MemoryTopo as a backend. 33 func TestHandlePathRoot(t *testing.T) { 34 input := "/" 35 cells := []string{"cell1", "cell2", "cell3"} 36 want := []string{topo.GlobalCell, "cell1", "cell2", "cell3"} 37 38 ts := memorytopo.NewServer(cells...) 39 ex := newBackendExplorer(ts) 40 result := ex.HandlePath(input, nil) 41 if got := result.Children; !reflect.DeepEqual(got, want) { 42 t.Errorf("HandlePath(%q) = %v, want %v", input, got, want) 43 } 44 if got := result.Error; got != "" { 45 t.Errorf("HandlePath(%q).Error = %v", input, got) 46 } 47 } 48 49 func TestHandlePathKeyspace(t *testing.T) { 50 cells := []string{"cell1", "cell2", "cell3"} 51 keyspace := &topodatapb.Keyspace{ 52 KeyspaceType: topodatapb.KeyspaceType_SNAPSHOT, 53 } 54 55 ctx := context.Background() 56 ts := memorytopo.NewServer(cells...) 57 if err := ts.CreateKeyspace(ctx, "test_keyspace", keyspace); err != nil { 58 t.Fatalf("CreateKeyspace error: %v", err) 59 } 60 if err := ts.CreateShard(ctx, "test_keyspace", "10-20"); err != nil { 61 t.Fatalf("CreateShard error: %v", err) 62 } 63 if err := ts.CreateShard(ctx, "test_keyspace", "20-30"); err != nil { 64 t.Fatalf("CreateShard error: %v", err) 65 } 66 67 ex := newBackendExplorer(ts) 68 69 // Test the Keyspace object itself. 70 input := path.Join("/global", "keyspaces", "test_keyspace", "Keyspace") 71 want := "keyspace_type:SNAPSHOT" 72 result := ex.HandlePath(input, nil) 73 if got := result.Data; got != want { 74 t.Errorf("HandlePath(%q) = %q, want %q", input, got, want) 75 } 76 if got, want := result.Children, []string(nil); !reflect.DeepEqual(got, want) { 77 t.Errorf("Children = %v, want %v", got, want) 78 } 79 if got := result.Error; got != "" { 80 t.Errorf("HandlePath(%q).Error = %v", input, got) 81 } 82 83 // Test the shards path. 84 input = path.Join("/global", "keyspaces", "test_keyspace", "shards") 85 result = ex.HandlePath(input, nil) 86 want = "" 87 if got := result.Data; got != want { 88 t.Errorf("HandlePath(%q) = %q, want %q", input, got, want) 89 } 90 if got, want := result.Children, []string{"10-20", "20-30"}; !reflect.DeepEqual(got, want) { 91 t.Errorf("Children = %v, want %v", got, want) 92 } 93 if got := result.Error; got != "" { 94 t.Errorf("HandlePath(%q).Error = %v", input, got) 95 } 96 } 97 98 func TestHandlePathShard(t *testing.T) { 99 input := path.Join("/global", "keyspaces", "test_keyspace", "shards", "-80", "Shard") 100 cells := []string{"cell1", "cell2", "cell3"} 101 keyspace := &topodatapb.Keyspace{} 102 want := "is_primary_serving:true" 103 104 ctx := context.Background() 105 ts := memorytopo.NewServer(cells...) 106 if err := ts.CreateKeyspace(ctx, "test_keyspace", keyspace); err != nil { 107 t.Fatalf("CreateKeyspace error: %v", err) 108 } 109 if err := ts.CreateShard(ctx, "test_keyspace", "-80"); err != nil { 110 t.Fatalf("CreateShard error: %v", err) 111 } 112 if _, err := ts.UpdateShardFields(ctx, "test_keyspace", "-80", func(si *topo.ShardInfo) error { 113 // Set cells, reset other fields so printout is easier to compare. 114 si.KeyRange = nil 115 return nil 116 }); err != nil { 117 t.Fatalf("UpdateShardFields error: %v", err) 118 } 119 120 ex := newBackendExplorer(ts) 121 result := ex.HandlePath(input, nil) 122 if got := result.Data; got != want { 123 t.Errorf("HandlePath(%q) = %q, want %q", input, got, want) 124 } 125 if got, want := result.Children, []string(nil); !reflect.DeepEqual(got, want) { 126 t.Errorf("Children = %v, want %v", got, want) 127 } 128 if got := result.Error; got != "" { 129 t.Errorf("HandlePath(%q).Error = %v", input, got) 130 } 131 } 132 133 func TestHandlePathTablet(t *testing.T) { 134 input := path.Join("/cell1", "tablets", "cell1-0000000123", "Tablet") 135 cells := []string{"cell1", "cell2", "cell3"} 136 tablet := &topodatapb.Tablet{ 137 Alias: &topodatapb.TabletAlias{Cell: "cell1", Uid: 123}, 138 Hostname: "example.com", 139 PortMap: map[string]int32{"vt": 4321}, 140 } 141 want := "alias:{cell:\"cell1\" uid:123} hostname:\"example.com\" port_map:{key:\"vt\" value:4321}" 142 143 ctx := context.Background() 144 ts := memorytopo.NewServer(cells...) 145 if err := ts.CreateTablet(ctx, tablet); err != nil { 146 t.Fatalf("CreateTablet error: %v", err) 147 } 148 149 ex := newBackendExplorer(ts) 150 result := ex.HandlePath(input, nil) 151 if got := result.Data; got != want { 152 t.Errorf("HandlePath(%q) = %q, want %q", input, got, want) 153 } 154 if got, want := result.Children, []string(nil); !reflect.DeepEqual(got, want) { 155 t.Errorf("Children = %v, want %v", got, want) 156 } 157 if got := result.Error; got != "" { 158 t.Errorf("HandlePath(%q).Error = %v", input, got) 159 } 160 } 161 162 func TestHandleBadPath(t *testing.T) { 163 input := "/foo" 164 cells := []string{"cell1", "cell2", "cell3"} 165 want := "Invalid cell: node doesn't exist: cells/foo/CellInfo" 166 167 ts := memorytopo.NewServer(cells...) 168 ex := newBackendExplorer(ts) 169 result := ex.HandlePath(input, nil) 170 if got := result.Error; !reflect.DeepEqual(got, want) { 171 t.Errorf("HandlePath(%q) = %v, want %v", input, got, want) 172 } 173 }