vitess.io/vitess@v0.16.2/go/vt/topo/test/testing.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 contains utilities to test topo.Conn 18 // implementations. If you are testing your implementation, you will 19 // want to call TopoServerTestSuite in your test method. For an 20 // example, look at the tests in 21 // vitess.io/vitess/go/vt/topo/memorytopo. 22 package test 23 24 import ( 25 "testing" 26 27 "vitess.io/vitess/go/vt/topo" 28 29 topodatapb "vitess.io/vitess/go/vt/proto/topodata" 30 ) 31 32 // LocalCellName is the cell name used by this test suite. 33 const LocalCellName = "test" 34 35 func newKeyRange(value string) *topodatapb.KeyRange { 36 _, result, err := topo.ValidateShardName(value) 37 if err != nil { 38 panic(err) 39 } 40 return result 41 } 42 43 func executeTestSuite(f func(*testing.T, *topo.Server), t *testing.T, ts *topo.Server, ignoreList []string, name string) { 44 // some test does not apply every where therefore we ignore them 45 for _, n := range ignoreList { 46 if n == name { 47 t.Logf("=== ignoring test %s", name) 48 return 49 } 50 } 51 f(t, ts) 52 } 53 54 // TopoServerTestSuite runs the full topo.Server/Conn test suite. 55 // The factory method should return a topo.Server that has a single cell 56 // called LocalCellName. 57 // Not all tests are applicable for each Topo server, therefore we provide ignoreList in order to 58 // avoid them for given Topo server tests. For example `TryLock` implementation is same as `Lock` for some Topo servers. 59 // Hence, for these Topo servers we ignore executing TryLock Tests. 60 func TopoServerTestSuite(t *testing.T, factory func() *topo.Server, ignoreList []string) { 61 var ts *topo.Server 62 63 t.Log("=== checkKeyspace") 64 ts = factory() 65 executeTestSuite(checkKeyspace, t, ts, ignoreList, "checkKeyspace") 66 ts.Close() 67 68 t.Log("=== checkShard") 69 ts = factory() 70 executeTestSuite(checkShard, t, ts, ignoreList, "checkShard") 71 ts.Close() 72 73 t.Log("=== checkShardWithLock") 74 ts = factory() 75 executeTestSuite(checkShardWithLock, t, ts, ignoreList, "checkShardWithLock") 76 ts.Close() 77 78 t.Log("=== checkTablet") 79 ts = factory() 80 executeTestSuite(checkTablet, t, ts, ignoreList, "checkTablet") 81 ts.Close() 82 83 t.Log("=== checkShardReplication") 84 ts = factory() 85 executeTestSuite(checkShardReplication, t, ts, ignoreList, "checkShardReplication") 86 ts.Close() 87 88 t.Log("=== checkSrvKeyspace") 89 ts = factory() 90 executeTestSuite(checkSrvKeyspace, t, ts, ignoreList, "checkSrvKeyspace") 91 ts.Close() 92 93 t.Log("=== checkSrvVSchema") 94 ts = factory() 95 executeTestSuite(checkSrvVSchema, t, ts, ignoreList, "checkSrvVSchema") 96 ts.Close() 97 98 t.Log("=== checkLock") 99 ts = factory() 100 executeTestSuite(checkLock, t, ts, ignoreList, "checkLock") 101 ts.Close() 102 103 t.Log("=== checkTryLock") 104 ts = factory() 105 executeTestSuite(checkTryLock, t, ts, ignoreList, "checkTryLock") 106 ts.Close() 107 108 t.Log("=== checkVSchema") 109 ts = factory() 110 executeTestSuite(checkVSchema, t, ts, ignoreList, "checkVSchema") 111 ts.Close() 112 113 t.Log("=== checkRoutingRules") 114 ts = factory() 115 executeTestSuite(checkRoutingRules, t, ts, ignoreList, "checkRoutingRules") 116 ts.Close() 117 118 t.Log("=== checkElection") 119 ts = factory() 120 executeTestSuite(checkElection, t, ts, ignoreList, "checkElection") 121 ts.Close() 122 123 t.Log("=== checkWaitForNewLeader") 124 ts = factory() 125 executeTestSuite(checkWaitForNewLeader, t, ts, ignoreList, "checkWaitForNewLeader") 126 ts.Close() 127 128 t.Log("=== checkDirectory") 129 ts = factory() 130 executeTestSuite(checkDirectory, t, ts, ignoreList, "checkDirectory") 131 ts.Close() 132 133 t.Log("=== checkFile") 134 ts = factory() 135 executeTestSuite(checkFile, t, ts, ignoreList, "checkFile") 136 ts.Close() 137 138 t.Log("=== checkWatch") 139 ts = factory() 140 executeTestSuite(checkWatch, t, ts, ignoreList, "checkWatch") 141 ts.Close() 142 143 ts = factory() 144 t.Log("=== checkWatchInterrupt") 145 executeTestSuite(checkWatchInterrupt, t, ts, ignoreList, "checkWatchInterrupt") 146 ts.Close() 147 148 ts = factory() 149 t.Log("=== checkList") 150 executeTestSuite(checkList, t, ts, ignoreList, "checkList") 151 ts.Close() 152 153 ts = factory() 154 t.Log("=== checkWatchRecursive") 155 executeTestSuite(checkWatchRecursive, t, ts, ignoreList, "checkWatchRecursive") 156 ts.Close() 157 }