github.com/choria-io/go-choria@v0.28.1-0.20240416190746-b3bf9c7d5a45/aagent/watchers/filewatcher/file_test.go (about) 1 // Copyright (c) 2020-2022, R.I. Pienaar and the Choria Project contributors 2 // 3 // SPDX-License-Identifier: Apache-2.0 4 5 package filewatcher 6 7 import ( 8 "encoding/json" 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 19 func Test(t *testing.T) { 20 RegisterFailHandler(Fail) 21 RunSpecs(t, "AAgent/Watchers/ExecWatcher") 22 } 23 24 var _ = Describe("ExecWatcher", func() { 25 var ( 26 mockctl *gomock.Controller 27 mockMachine *model.MockMachine 28 watch *Watcher 29 now time.Time 30 ) 31 32 BeforeEach(func() { 33 mockctl = gomock.NewController(GinkgoT()) 34 mockMachine = model.NewMockMachine(mockctl) 35 36 mockMachine.EXPECT().Name().Return("file").AnyTimes() 37 mockMachine.EXPECT().Identity().Return("ginkgo").AnyTimes() 38 mockMachine.EXPECT().InstanceID().Return("1234567890").AnyTimes() 39 mockMachine.EXPECT().Version().Return("1.0.0").AnyTimes() 40 mockMachine.EXPECT().TimeStampSeconds().Return(now.Unix()).AnyTimes() 41 mockMachine.EXPECT().Directory().Return(".").AnyTimes() 42 43 now = time.Unix(1606924953, 0) 44 45 wi, err := New(mockMachine, "ginkgo", []string{"always"}, "fail", "success", "2m", time.Second, map[string]any{ 46 "path": filepath.Join("bin", "sh"), 47 }) 48 Expect(err).ToNot(HaveOccurred()) 49 watch = wi.(*Watcher) 50 }) 51 52 AfterEach(func() { 53 mockctl.Finish() 54 }) 55 56 Describe("setProperties", func() { 57 It("Should parse valid properties", func() { 58 prop := map[string]any{ 59 "path": "cmd", 60 "gather_initial_state": "t", 61 } 62 Expect(watch.setProperties(prop)).ToNot(HaveOccurred()) 63 Expect(watch.properties.Path).To(Equal("cmd")) 64 Expect(watch.properties.Initial).To(BeTrue()) 65 }) 66 67 It("Should handle errors", func() { 68 watch.properties = &Properties{} 69 err := watch.setProperties(map[string]any{}) 70 Expect(err).To(MatchError("path is required")) 71 }) 72 }) 73 74 Describe("CurrentState", func() { 75 It("Should be a valid state", func() { 76 watch.previous = Changed 77 cs := watch.CurrentState() 78 csj, err := cs.(*StateNotification).JSON() 79 Expect(err).ToNot(HaveOccurred()) 80 81 event := map[string]any{} 82 err = json.Unmarshal(csj, &event) 83 Expect(err).ToNot(HaveOccurred()) 84 delete(event, "id") 85 86 Expect(event).To(Equal(map[string]any{ 87 "time": "2020-12-02T16:02:33Z", 88 "type": "io.choria.machine.watcher.file.v1.state", 89 "subject": "ginkgo", 90 "specversion": "1.0", 91 "source": "io.choria.machine", 92 "datacontenttype": "application/json", 93 "data": map[string]any{ 94 "id": "1234567890", 95 "identity": "ginkgo", 96 "machine": "file", 97 "name": "ginkgo", 98 "protocol": "io.choria.machine.watcher.file.v1.state", 99 "type": "file", 100 "version": "1.0.0", 101 "timestamp": float64(now.Unix()), 102 "previous_outcome": "changed", 103 "path": filepath.Join("bin", "sh"), 104 }, 105 })) 106 }) 107 }) 108 })