github.com/choria-io/go-choria@v0.28.1-0.20240416190746-b3bf9c7d5a45/aagent/watchers/kvwatcher/kv_test.go (about) 1 // Copyright (c) 2021-2023, R.I. Pienaar and the Choria Project contributors 2 // 3 // SPDX-License-Identifier: Apache-2.0 4 5 package kvwatcher 6 7 import ( 8 "encoding/json" 9 "testing" 10 "time" 11 12 "github.com/choria-io/go-choria/aagent/model" 13 "github.com/golang/mock/gomock" 14 . "github.com/onsi/ginkgo/v2" 15 . "github.com/onsi/gomega" 16 ) 17 18 func TestMachine(t *testing.T) { 19 RegisterFailHandler(Fail) 20 RunSpecs(t, "AAgent/Watchers/KvWatcher") 21 } 22 23 var _ = Describe("AAgent/Watchers/KvWatcher", func() { 24 var ( 25 w *Watcher 26 machine *model.MockMachine 27 mockctl *gomock.Controller 28 kv *MockKeyValue 29 kve *MockKeyValueEntry 30 ) 31 32 BeforeEach(func() { 33 mockctl = gomock.NewController(GinkgoT()) 34 35 machine = model.NewMockMachine(mockctl) 36 machine.EXPECT().Infof(gomock.Any(), gomock.Any(), gomock.Any()).AnyTimes() 37 machine.EXPECT().Facts().Return(json.RawMessage(`{}`)).MinTimes(1) 38 machine.EXPECT().Data().Return(map[string]any{}).MinTimes(1) 39 machine.EXPECT().DataGet("machines").MinTimes(1) 40 41 wi, err := New(machine, "kv", nil, "", "", "1m", time.Hour, map[string]any{ 42 "bucket": "PLUGINS", 43 "key": "machines", 44 "mode": "poll", 45 "bucket_prefix": false, 46 }) 47 Expect(err).ToNot(HaveOccurred()) 48 w = wi.(*Watcher) 49 kv = NewMockKeyValue(mockctl) 50 kve = NewMockKeyValueEntry(mockctl) 51 kve.EXPECT().Revision().Return(uint64(1)).MinTimes(1) 52 kv.EXPECT().Get("machines").Return(kve, nil) 53 w.kv = kv 54 }) 55 56 AfterEach(func() { 57 mockctl.Finish() 58 }) 59 60 Describe("Specification/Poll json parsing (#2037)", func() { 61 It("Should handle a trailing newline", func() { 62 kve.EXPECT().Value().Return([]byte("{\"spec\": \"foo\"}\n")).MinTimes(1) 63 machine.EXPECT().DataPut("machines", map[string]any{"spec": "foo"}).Return(nil).Times(1) 64 _, err := w.poll() 65 Expect(err).ToNot(HaveOccurred()) 66 }) 67 68 It("Should handle a leading and trailing newline", func() { 69 kve.EXPECT().Value().Return([]byte("\n{\"spec\": \"foo\"}\n")).MinTimes(1) 70 machine.EXPECT().DataPut("machines", map[string]any{"spec": "foo"}).Return(nil).Times(1) 71 _, err := w.poll() 72 Expect(err).ToNot(HaveOccurred()) 73 }) 74 75 It("Should handle a leading and trailing unicode whitespace", func() { 76 kve.EXPECT().Value().Return([]byte("\n \t{\"spec\": \"foo\"}\t \n")).MinTimes(1) 77 machine.EXPECT().DataPut("machines", map[string]any{"spec": "foo"}).Return(nil).Times(1) 78 _, err := w.poll() 79 Expect(err).ToNot(HaveOccurred()) 80 }) 81 }) 82 })