vitess.io/vitess@v0.16.2/go/vt/topo/topoproto/tablet_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 "fmt" 21 "testing" 22 23 "github.com/stretchr/testify/assert" 24 25 topodatapb "vitess.io/vitess/go/vt/proto/topodata" 26 ) 27 28 func TestParseTabletAlias(t *testing.T) { 29 for aliasStr, expectedAlias := range map[string]*topodatapb.TabletAlias{ 30 // valid cases 31 "cell1-42": {Cell: "cell1", Uid: 42}, 32 "cell1_1-42": {Cell: "cell1_1", Uid: 42}, 33 "cell1_1-0": {Cell: "cell1_1", Uid: 0}, 34 "cell1_1--1": {Cell: "cell1_1-", Uid: 1}, 35 "1-022222": {Cell: "1", Uid: 22222}, 36 "global-read-only-000000000000042": {Cell: "global-read-only", Uid: 42}, 37 "-cell1-1---42": {Cell: "-cell1-1--", Uid: 42}, 38 "cell1____-42": {Cell: "cell1____", Uid: 42}, 39 "__cell1-1-1-2-42": {Cell: "__cell1-1-1-2", Uid: 42}, 40 41 // invalid cases 42 "": nil, 43 "42": nil, 44 "-42": nil, 45 "cell1": nil, 46 "cell1-": nil, 47 "cell1_42": nil, 48 ",cell1-42": nil, 49 } { 50 alias, err := ParseTabletAlias(aliasStr) 51 52 if expectedAlias == nil { 53 if err == nil { 54 t.Fatalf("Expected to fail parsing invalid tablet alias: %s but got no error", aliasStr) 55 } else { 56 expectedErr := fmt.Errorf("invalid tablet alias: '%s', expecting format: '%s'", aliasStr, tabletAliasFormat) 57 if err.Error() != expectedErr.Error() { 58 t.Fatalf("Expected error: %s but got: %s", expectedErr, err) 59 } 60 continue 61 } 62 } 63 64 if err != nil { 65 t.Fatalf("Failed to parse valid tablet alias: %s, err: %s", aliasStr, err) 66 } 67 if alias.Cell != expectedAlias.Cell { 68 t.Fatalf("Cell parsed from tabletAlias: %s is %s but expected %s", aliasStr, alias.Cell, expectedAlias.Cell) 69 } 70 if alias.Uid != expectedAlias.Uid { 71 t.Fatalf("Uid parsed from tabletAlias: %s is %d but expected %d", aliasStr, alias.Uid, expectedAlias.Uid) 72 } 73 } 74 } 75 76 func TestIsTabletsInList(t *testing.T) { 77 t.Parallel() 78 tablet1 := &topodatapb.Tablet{ 79 Alias: &topodatapb.TabletAlias{ 80 Cell: "zone-1", 81 Uid: 1, 82 }, 83 } 84 tablet2 := &topodatapb.Tablet{ 85 Alias: &topodatapb.TabletAlias{ 86 Cell: "zone-1", 87 Uid: 2, 88 }, 89 } 90 tablet3 := &topodatapb.Tablet{ 91 Alias: &topodatapb.TabletAlias{ 92 Cell: "zone-1", 93 Uid: 3, 94 }, 95 } 96 testcases := []struct { 97 name string 98 tablet *topodatapb.Tablet 99 allTablets []*topodatapb.Tablet 100 isInList bool 101 }{ 102 { 103 name: "empty list", 104 tablet: tablet1, 105 allTablets: nil, 106 isInList: false, 107 }, { 108 name: "tablet in list", 109 tablet: tablet2, 110 allTablets: []*topodatapb.Tablet{tablet1, tablet2, tablet3}, 111 isInList: true, 112 }, { 113 name: "tablet not in list", 114 tablet: tablet1, 115 allTablets: []*topodatapb.Tablet{tablet2, tablet3}, 116 isInList: false, 117 }, 118 } 119 120 for _, testcase := range testcases { 121 // We create an explicit copy of the range variable for each parallel runner 122 // to be sure that they each run as expected. You can see more information on 123 // this here: https://pkg.go.dev/testing#hdr-Subtests_and_Sub_benchmarks 124 testcase := testcase 125 t.Run(testcase.name, func(t *testing.T) { 126 t.Parallel() 127 out := IsTabletInList(testcase.tablet, testcase.allTablets) 128 assert.Equal(t, testcase.isInList, out) 129 }) 130 } 131 }