vitess.io/vitess@v0.16.2/go/vt/topo/zk2topo/server_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 zk2topo 18 19 import ( 20 "fmt" 21 "path" 22 "testing" 23 24 "context" 25 26 "vitess.io/vitess/go/testfiles" 27 "vitess.io/vitess/go/vt/topo" 28 "vitess.io/vitess/go/vt/topo/test" 29 "vitess.io/vitess/go/vt/zkctl" 30 31 topodatapb "vitess.io/vitess/go/vt/proto/topodata" 32 ) 33 34 func TestZk2Topo(t *testing.T) { 35 // Start a real single ZK daemon, and close it after all tests are done. 36 zkd, serverAddr := zkctl.StartLocalZk(testfiles.GoVtTopoZk2topoZkID, testfiles.GoVtTopoZk2topoPort) 37 defer zkd.Teardown() 38 39 // Run the test suite. 40 testIndex := 0 41 test.TopoServerTestSuite(t, func() *topo.Server { 42 // Each test will use its own sub-directories. 43 // The directories will be created when used the first time. 44 testRoot := fmt.Sprintf("/test-%v", testIndex) 45 testIndex++ 46 47 globalRoot := path.Join(testRoot, topo.GlobalCell) 48 cellRoot := path.Join(testRoot, test.LocalCellName) 49 50 // Note we exercise the observer feature here by passing in 51 // the same server twice, with a "|" separator. 52 ts, err := topo.OpenServer("zk2", serverAddr+"|"+serverAddr, globalRoot) 53 if err != nil { 54 t.Fatalf("OpenServer() failed: %v", err) 55 } 56 if err := ts.CreateCellInfo(context.Background(), test.LocalCellName, &topodatapb.CellInfo{ 57 ServerAddress: serverAddr, 58 Root: cellRoot, 59 }); err != nil { 60 t.Fatalf("CreateCellInfo() failed: %v", err) 61 } 62 63 return ts 64 }, []string{}) 65 } 66 67 func TestHasObservers(t *testing.T) { 68 s1, s2, ok := hasObservers("s1:p1,s2:p2") 69 if ok { 70 t.Errorf("hasObservers(s1:p1,s2:p2): got unexpected %v %v %v", s1, s2, ok) 71 } 72 73 s1, s2, ok = hasObservers("s1:p1,s2:p2|o1:p1,o2:p2") 74 if !ok || s1 != "s1:p1,s2:p2" || s2 != "o1:p1,o2:p2" { 75 t.Errorf("hasObservers(s1:p1,s2:p2|o1:p1,o2:p2): got unexpected %v %v %v", s1, s2, ok) 76 } 77 }