istio.io/istio@v0.0.0-20240520182934-d79c90f27776/pkg/config/mesh/networks_watcher_test.go (about) 1 // Copyright Istio Authors 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 package mesh_test 16 17 import ( 18 "testing" 19 "time" 20 21 . "github.com/onsi/gomega" 22 23 meshconfig "istio.io/api/mesh/v1alpha1" 24 "istio.io/istio/pkg/config/mesh" 25 "istio.io/istio/pkg/filewatcher" 26 ) 27 28 func TestNewNetworksWatcherWithBadInputShouldFail(t *testing.T) { 29 g := NewWithT(t) 30 _, err := mesh.NewNetworksWatcher(filewatcher.NewWatcher(), "") 31 g.Expect(err).ToNot(BeNil()) 32 } 33 34 func TestNetworksWatcherShouldNotifyHandlers(t *testing.T) { 35 g := NewWithT(t) 36 37 path := newTempFile(t) 38 defer removeSilent(path) 39 40 n := meshconfig.MeshNetworks{ 41 Networks: make(map[string]*meshconfig.Network), 42 } 43 writeMessage(t, path, &n) 44 45 w := newNetworksWatcher(t, path) 46 g.Expect(w.Networks()).To(Equal(&n)) 47 48 doneCh := make(chan struct{}, 1) 49 50 var newN *meshconfig.MeshNetworks 51 w.AddNetworksHandler(func() { 52 newN = w.Networks() 53 close(doneCh) 54 }) 55 56 // Change the file to trigger the update. 57 n.Networks["test"] = &meshconfig.Network{} 58 writeMessage(t, path, &n) 59 60 select { 61 case <-doneCh: 62 g.Expect(newN).To(Equal(&n)) 63 g.Expect(w.Networks()).To(Equal(newN)) 64 break 65 case <-time.After(time.Second * 5): 66 t.Fatal("timed out waiting for update") 67 } 68 } 69 70 func newNetworksWatcher(t *testing.T, filename string) mesh.NetworksWatcher { 71 t.Helper() 72 w, err := mesh.NewNetworksWatcher(filewatcher.NewWatcher(), filename) 73 if err != nil { 74 t.Fatal(err) 75 } 76 return w 77 }