github.com/fafucoder/cilium@v1.6.11/pkg/clustermesh/config_test.go (about) 1 // Copyright 2018 Authors of Cilium 2 // 3 // Licensed under the Apache License, Version 2.0 (the "License"); 4 // you may not use this file except in compliance with the License. 5 // You may obtain a copy of the License at 6 // 7 // http://www.apache.org/licenses/LICENSE-2.0 8 // 9 // Unless required by applicable law or agreed to in writing, software 10 // distributed under the License is distributed on an "AS IS" BASIS, 11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 // See the License for the specific language governing permissions and 13 // limitations under the License. 14 15 // +build !privileged_tests 16 17 package clustermesh 18 19 import ( 20 "io/ioutil" 21 "os" 22 "path" 23 "time" 24 25 "github.com/cilium/cilium/pkg/testutils" 26 27 . "gopkg.in/check.v1" 28 ) 29 30 func createFile(c *C, name string) { 31 err := ioutil.WriteFile(name, []byte("endpoints:\n- https://cluster1.cilium-etcd.cilium.svc:2379\n"), 0644) 32 c.Assert(err, IsNil) 33 } 34 35 func expectExists(c *C, cm *ClusterMesh, name string) { 36 c.Assert(cm.clusters[name], Not(IsNil)) 37 } 38 39 func expectChange(c *C, cm *ClusterMesh, name string) { 40 cluster := cm.clusters[name] 41 c.Assert(cluster, Not(IsNil)) 42 43 select { 44 case <-cluster.changed: 45 case <-time.After(time.Second): 46 c.Fatal("timeout while waiting for changed event") 47 } 48 } 49 50 func expectNotExist(c *C, cm *ClusterMesh, name string) { 51 c.Assert(cm.clusters[name], IsNil) 52 } 53 54 func (s *ClusterMeshTestSuite) TestWatchConfigDirectory(c *C) { 55 skipKvstoreConnection = true 56 defer func() { 57 skipKvstoreConnection = false 58 }() 59 60 dir, err := ioutil.TempDir("", "multicluster") 61 c.Assert(err, IsNil) 62 defer os.RemoveAll(dir) 63 64 file1 := path.Join(dir, "cluster1") 65 file2 := path.Join(dir, "cluster2") 66 file3 := path.Join(dir, "cluster3") 67 68 createFile(c, file1) 69 createFile(c, file2) 70 71 cm, err := NewClusterMesh(Configuration{ 72 Name: "test1", 73 ConfigDirectory: dir, 74 NodeKeyCreator: testNodeCreator, 75 }) 76 c.Assert(err, IsNil) 77 c.Assert(cm, Not(IsNil)) 78 defer cm.Close() 79 80 // wait for cluster1 and cluster2 to appear 81 c.Assert(testutils.WaitUntil(func() bool { return len(cm.clusters) == 2 }, time.Second), IsNil) 82 expectExists(c, cm, "cluster1") 83 expectExists(c, cm, "cluster2") 84 expectNotExist(c, cm, "cluster3") 85 86 err = os.RemoveAll(file1) 87 c.Assert(err, IsNil) 88 89 // wait for cluster1 to disappear 90 c.Assert(testutils.WaitUntil(func() bool { return len(cm.clusters) == 1 }, time.Second), IsNil) 91 92 createFile(c, file3) 93 94 // wait for cluster3 to appear 95 c.Assert(testutils.WaitUntil(func() bool { return len(cm.clusters) == 2 }, time.Second), IsNil) 96 expectNotExist(c, cm, "cluster1") 97 expectExists(c, cm, "cluster2") 98 expectExists(c, cm, "cluster3") 99 100 // Test renaming of file from cluster3 to cluster1 101 err = os.Rename(file3, file1) 102 c.Assert(err, IsNil) 103 104 // wait for cluster1 to appear 105 c.Assert(testutils.WaitUntil(func() bool { return cm.clusters["cluster1"] != nil }, time.Second), IsNil) 106 expectExists(c, cm, "cluster2") 107 expectNotExist(c, cm, "cluster3") 108 109 // touch file 110 err = os.Chtimes(file1, time.Now(), time.Now()) 111 c.Assert(err, IsNil) 112 113 // give time for events to be processed 114 time.Sleep(100 * time.Millisecond) 115 expectChange(c, cm, "cluster1") 116 117 err = os.RemoveAll(file1) 118 c.Assert(err, IsNil) 119 err = os.RemoveAll(file2) 120 c.Assert(err, IsNil) 121 122 // wait for all clusters to disappear 123 c.Assert(testutils.WaitUntil(func() bool { return len(cm.clusters) == 0 }, time.Second), IsNil) 124 expectNotExist(c, cm, "cluster1") 125 expectNotExist(c, cm, "cluster2") 126 expectNotExist(c, cm, "cluster3") 127 128 } 129 130 func (s *ClusterMeshTestSuite) TestIsEtcdConfigFile(c *C) { 131 dir, err := ioutil.TempDir("", "etcdconfig") 132 c.Assert(err, IsNil) 133 defer os.RemoveAll(dir) 134 135 validPath := path.Join(dir, "valid") 136 err = ioutil.WriteFile(validPath, []byte("endpoints:\n- https://cluster1.cilium-etcd.cilium.svc:2379\n"), 0644) 137 c.Assert(err, IsNil) 138 c.Assert(isEtcdConfigFile(validPath), Equals, true) 139 140 invalidPath := path.Join(dir, "valid") 141 err = ioutil.WriteFile(invalidPath, []byte("sf324kj234lkjsdvl\nwl34kj23l4k\nendpoints"), 0644) 142 c.Assert(err, IsNil) 143 c.Assert(isEtcdConfigFile(invalidPath), Equals, false) 144 }