vitess.io/vitess@v0.16.2/go/vt/topo/topoproto/shard_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 topoproto 18 19 import ( 20 "testing" 21 22 topodatapb "vitess.io/vitess/go/vt/proto/topodata" 23 ) 24 25 func TestParseKeyspaceShard(t *testing.T) { 26 zkPath := "/zk/global/ks/sh" 27 keyspace := "key01" 28 shard := "shard0" 29 30 for _, delim := range []string{"/", ":"} { 31 keyspaceShard := keyspace + delim + shard 32 if _, _, err := ParseKeyspaceShard(zkPath); err == nil { 33 t.Errorf("zk path: %s should cause error.", zkPath) 34 } 35 k, s, err := ParseKeyspaceShard(keyspaceShard) 36 if err != nil { 37 t.Fatalf("Failed to parse valid keyspace%sshard pair: %s", delim, keyspaceShard) 38 } 39 if k != keyspace { 40 t.Errorf("keyspace parsed from keyspace%sshard pair %s is %s, but expect %s", delim, keyspaceShard, k, keyspace) 41 } 42 if s != shard { 43 t.Errorf("shard parsed from keyspace%sshard pair %s is %s, but expect %s", delim, keyspaceShard, s, shard) 44 } 45 } 46 } 47 48 func TestSourceShardAsHTML(t *testing.T) { 49 s := &topodatapb.Shard_SourceShard{ 50 Uid: 123, 51 Keyspace: "source_keyspace", 52 Shard: "source_shard", 53 KeyRange: &topodatapb.KeyRange{ 54 Start: []byte{0x80}, 55 }, 56 Tables: []string{"table1", "table2"}, 57 } 58 got := string(SourceShardAsHTML(s)) 59 expected := "<b>Uid</b>: 123</br>\n" + 60 "<b>Source</b>: source_keyspace/source_shard</br>\n" + 61 "<b>KeyRange</b>: 80-</br>\n" + 62 "<b>Tables</b>: table1 table2</br>\n" 63 if got != expected { 64 t.Errorf("got wrong SourceShardAsHTML output, got:\n%vexpected:\n%v", got, expected) 65 } 66 }