github.com/choria-io/go-choria@v0.28.1-0.20240416190746-b3bf9c7d5a45/aagent/watchers/timerwatcher/timer_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 timerwatcher
     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 Test(t *testing.T) {
    19  	RegisterFailHandler(Fail)
    20  	RunSpecs(t, "AAgent/Watchers/TimerWatcher")
    21  }
    22  
    23  var _ = Describe("TimerWatcher", func() {
    24  	var (
    25  		mockctl     *gomock.Controller
    26  		mockMachine *model.MockMachine
    27  		watch       *Watcher
    28  		now         time.Time
    29  	)
    30  
    31  	BeforeEach(func() {
    32  		mockctl = gomock.NewController(GinkgoT())
    33  		mockMachine = model.NewMockMachine(mockctl)
    34  
    35  		now = time.Unix(1606924953, 0)
    36  		mockMachine.EXPECT().Name().Return("timer").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().Infof(gomock.Any(), gomock.Any(), gomock.Any()).AnyTimes()
    42  
    43  		wi, err := New(mockMachine, "ginkgo", []string{"always"}, "fail", "", "2m", time.Second, map[string]any{})
    44  		Expect(err).ToNot(HaveOccurred())
    45  		watch = wi.(*Watcher)
    46  	})
    47  
    48  	AfterEach(func() {
    49  		mockctl.Finish()
    50  	})
    51  
    52  	Describe("setProperties", func() {
    53  		It("Should parse valid properties", func() {
    54  			err := watch.setProperties(map[string]any{
    55  				"timer": "1h",
    56  			})
    57  			Expect(err).ToNot(HaveOccurred())
    58  			Expect(watch.properties.Timer).To(Equal(time.Hour))
    59  		})
    60  
    61  		It("Should handle errors", func() {
    62  			err := watch.setProperties(map[string]any{})
    63  			Expect(err).ToNot(HaveOccurred())
    64  			Expect(watch.properties.Timer).To(Equal(time.Second))
    65  		})
    66  
    67  		It("Should handle splay", func() {
    68  			err := watch.setProperties(map[string]any{
    69  				"timer": "1h",
    70  				"splay": true,
    71  			})
    72  			Expect(err).ToNot(HaveOccurred())
    73  			Expect(watch.properties.Timer).To(And(
    74  				BeNumerically("<", time.Hour),
    75  				BeNumerically(">", 0),
    76  			))
    77  		})
    78  	})
    79  
    80  	Describe("CurrentState", func() {
    81  		It("Should be a valid state", func() {
    82  			cs := watch.CurrentState()
    83  			csj, err := cs.(*StateNotification).JSON()
    84  			Expect(err).ToNot(HaveOccurred())
    85  
    86  			event := map[string]any{}
    87  			err = json.Unmarshal(csj, &event)
    88  			Expect(err).ToNot(HaveOccurred())
    89  			delete(event, "id")
    90  
    91  			Expect(event).To(Equal(map[string]any{
    92  				"time":            "2020-12-02T16:02:33Z",
    93  				"type":            "io.choria.machine.watcher.timer.v1.state",
    94  				"subject":         "ginkgo",
    95  				"specversion":     "1.0",
    96  				"source":          "io.choria.machine",
    97  				"datacontenttype": "application/json",
    98  				"data": map[string]any{
    99  					"id":        "1234567890",
   100  					"identity":  "ginkgo",
   101  					"machine":   "timer",
   102  					"name":      "ginkgo",
   103  					"protocol":  "io.choria.machine.watcher.timer.v1.state",
   104  					"state":     "stopped",
   105  					"timer":     float64(time.Second),
   106  					"type":      "timer",
   107  					"version":   "1.0.0",
   108  					"timestamp": float64(now.Unix()),
   109  				},
   110  			}))
   111  		})
   112  	})
   113  })