github.com/choria-io/go-choria@v0.28.1-0.20240416190746-b3bf9c7d5a45/aagent/machine/notifications_test.go (about) 1 // Copyright (c) 2019-2021, R.I. Pienaar and the Choria Project contributors 2 // 3 // SPDX-License-Identifier: Apache-2.0 4 5 package machine 6 7 import ( 8 "github.com/golang/mock/gomock" 9 . "github.com/onsi/ginkgo/v2" 10 . "github.com/onsi/gomega" 11 ) 12 13 var _ = Describe("Aagent/Machine/Notifications", func() { 14 var ( 15 mockctl *gomock.Controller 16 service1 *MockNotificationService 17 service2 *MockNotificationService 18 manager *MockWatcherManager 19 event *MockWatcherStateNotification 20 machine *Machine 21 ) 22 23 BeforeEach(func() { 24 mockctl = gomock.NewController(GinkgoT()) 25 service1 = NewMockNotificationService(mockctl) 26 service2 = NewMockNotificationService(mockctl) 27 manager = NewMockWatcherManager(mockctl) 28 event = NewMockWatcherStateNotification(mockctl) 29 machine = &Machine{ 30 notifiers: []NotificationService{}, 31 manager: manager, 32 MachineName: "ginkgo", 33 } 34 }) 35 36 AfterEach(func() { 37 mockctl.Finish() 38 }) 39 40 Describe("RegisterNotifier", func() { 41 It("Should add the notifier to the list", func() { 42 Expect(machine.notifiers).To(BeEmpty()) 43 machine.RegisterNotifier(service1) 44 Expect(machine.notifiers[0]).To(Equal(service1)) 45 Expect(machine.notifiers).To(HaveLen(1)) 46 }) 47 }) 48 49 Describe("Notifications", func() { 50 BeforeEach(func() { 51 machine.RegisterNotifier(service1, service2) 52 }) 53 54 It("Should support notifying state", func() { 55 service1.EXPECT().NotifyWatcherState("w1", event) 56 service2.EXPECT().NotifyWatcherState("w1", event) 57 58 machine.NotifyWatcherState("w1", event) 59 }) 60 61 It("Should support common loggers", func() { 62 service1.EXPECT().Debugf(machine, "w1", "format", "debugarg") 63 service2.EXPECT().Debugf(machine, "w1", "format", "debugarg") 64 service1.EXPECT().Infof(machine, "w1", "format", "infoarg") 65 service2.EXPECT().Infof(machine, "w1", "format", "infoarg") 66 service1.EXPECT().Warnf(machine, "w1", "format", "warnarg") 67 service2.EXPECT().Warnf(machine, "w1", "format", "warnarg") 68 service1.EXPECT().Errorf(machine, "w1", "format", "errorarg") 69 service2.EXPECT().Errorf(machine, "w1", "format", "errorarg") 70 71 machine.Debugf("w1", "format", "debugarg") 72 machine.Infof("w1", "format", "infoarg") 73 machine.Warnf("w1", "format", "warnarg") 74 machine.Errorf("w1", "format", "errorarg") 75 }) 76 }) 77 })