github.com/choria-io/go-choria@v0.28.1-0.20240416190746-b3bf9c7d5a45/aagent/watchers/watchers_test.go (about) 1 // Copyright (c) 2019-2022, R.I. Pienaar and the Choria Project contributors 2 // 3 // SPDX-License-Identifier: Apache-2.0 4 5 package watchers 6 7 import ( 8 "context" 9 "path/filepath" 10 "testing" 11 "time" 12 13 "github.com/choria-io/go-choria/aagent/model" 14 "github.com/golang/mock/gomock" 15 . "github.com/onsi/ginkgo/v2" 16 . "github.com/onsi/gomega" 17 18 "github.com/choria-io/go-choria/build" 19 ) 20 21 func TestWatchers(t *testing.T) { 22 RegisterFailHandler(Fail) 23 RunSpecs(t, "Aagent/Watchers") 24 } 25 26 var _ = Describe("Aagent/Watchers", func() { 27 var ( 28 mockctl *gomock.Controller 29 machine *MockMachine 30 watcherC *model.MockWatcherConstructor 31 watcher *model.MockWatcher 32 manager *Manager 33 err error 34 ) 35 36 BeforeEach(func() { 37 mockctl = gomock.NewController(GinkgoT()) 38 machine = NewMockMachine(mockctl) 39 watcherC = model.NewMockWatcherConstructor(mockctl) 40 watcher = model.NewMockWatcher(mockctl) 41 manager = New(context.Background()) 42 manager.machine = machine 43 }) 44 45 AfterEach(func() { 46 mockctl.Finish() 47 }) 48 49 Describe("SetMachine", func() { 50 It("Should set the machine", func() { 51 err = manager.SetMachine(1) 52 Expect(err).To(MatchError("supplied machine does not implement watchers.Machine")) 53 err = manager.SetMachine(machine) 54 Expect(manager.machine).To(Equal(machine)) 55 }) 56 }) 57 58 Describe("Plugins", func() { 59 BeforeEach(func() { 60 build.MachineWatchers = []string{} 61 plugins = nil 62 watcherC.EXPECT().Type().Return("mock").AnyTimes() 63 }) 64 65 It("Should register watchers", func() { 66 err = RegisterWatcherPlugin("mock watcher version 1", watcherC) 67 Expect(err).ToNot(HaveOccurred()) 68 Expect(plugins).To(Equal(map[string]model.WatcherConstructor{ 69 "mock": watcherC, 70 })) 71 Expect(build.MachineWatchers).To(Equal([]string{"mock watcher version 1"})) 72 }) 73 }) 74 75 Describe("configureWatchers", func() { 76 BeforeEach(func() { 77 watcherC.EXPECT().Type().Return("mock").AnyTimes() 78 plugins = nil 79 watcherC.EXPECT().Type().Return("mock").AnyTimes() 80 err = RegisterWatcherPlugin("mock watcher version 1", watcherC) 81 Expect(err).ToNot(HaveOccurred()) 82 }) 83 84 It("Should support registered watchers", func() { 85 machine.EXPECT().Infof(gomock.Any(), gomock.Any(), gomock.Any()) 86 machine.EXPECT().Directory().Return(filepath.Dir(".")).AnyTimes() 87 machine.EXPECT().Watchers().Return([]*WatcherDef{ 88 { 89 Name: "mwatcher", 90 Type: "mock", 91 StateMatch: []string{"one"}, 92 FailTransition: "failed", 93 SuccessTransition: "passed", 94 Interval: "1m", 95 AnnounceDuration: 0, 96 Properties: map[string]any{ 97 "path": "/dev/null", 98 }, 99 }, 100 }) 101 102 watcher.EXPECT().Name().Return("mwatcher").AnyTimes() 103 watcherC.EXPECT().New(machine, "mwatcher", []string{"one"}, "failed", "passed", "1m", 0*time.Second, map[string]any{ 104 "path": "/dev/null", 105 }).Return(watcher, nil).AnyTimes() 106 107 err = manager.configureWatchers() 108 Expect(err).ToNot(HaveOccurred()) 109 110 w, ok := manager.watchers["mwatcher"] 111 Expect(ok).To(BeTrue()) 112 Expect(w).To(Equal(watcher)) 113 }) 114 }) 115 })