github.com/choria-io/go-choria@v0.28.1-0.20240416190746-b3bf9c7d5a45/server/registration/registration_test.go (about)

     1  // Copyright (c) 2017-2022, R.I. Pienaar and the Choria Project contributors
     2  //
     3  // SPDX-License-Identifier: Apache-2.0
     4  
     5  package registration
     6  
     7  import (
     8  	"errors"
     9  	"os"
    10  	"testing"
    11  
    12  	"github.com/choria-io/go-choria/config"
    13  	"github.com/choria-io/go-choria/inter"
    14  	imock "github.com/choria-io/go-choria/inter/imocks"
    15  	"github.com/choria-io/go-choria/message"
    16  	"github.com/choria-io/go-choria/server/data"
    17  	"github.com/golang/mock/gomock"
    18  	. "github.com/onsi/ginkgo/v2"
    19  	. "github.com/onsi/gomega"
    20  	"github.com/sirupsen/logrus"
    21  )
    22  
    23  func TestRegistration(t *testing.T) {
    24  	os.Setenv("MCOLLECTIVE_CERTNAME", "rip.mcollective")
    25  	RegisterFailHandler(Fail)
    26  	RunSpecs(t, "Server/Registration")
    27  }
    28  
    29  var _ = Describe("Server/Registration", func() {
    30  	var _ = Describe("publish", func() {
    31  		var (
    32  			conn    *imock.MockConnector
    33  			si      *MockServerInfoSource
    34  			fw      *imock.MockFramework
    35  			cfg     *config.Config
    36  			log     *logrus.Entry
    37  			manager *Manager
    38  			mockctl *gomock.Controller
    39  		)
    40  
    41  		BeforeEach(func() {
    42  			mockctl = gomock.NewController(GinkgoT())
    43  			fw, cfg = imock.NewFrameworkForTests(mockctl, GinkgoWriter, imock.WithCallerID())
    44  			cfg.DisableTLS = true
    45  			cfg.OverrideCertname = "test.example.net"
    46  			cfg.Collectives = []string{"test_collective"}
    47  			cfg.MainCollective = "test_collective"
    48  			cfg.RegistrationCollective = "test_collective"
    49  
    50  			fw.EXPECT().NewMessage(gomock.Any(), gomock.Any(), gomock.Any(), gomock.Any(), gomock.Any()).DoAndReturn(func(payload []byte, agent string, collective string, msgType string, request inter.Message) (msg inter.Message, err error) {
    51  				return message.NewMessage(payload, agent, collective, msgType, request, fw)
    52  			}).AnyTimes()
    53  
    54  			log = logrus.WithFields(logrus.Fields{"test": true})
    55  			logrus.SetLevel(logrus.FatalLevel)
    56  
    57  			conn = imock.NewMockConnector(mockctl)
    58  			si = NewMockServerInfoSource(mockctl)
    59  			manager = New(fw, si, conn, log)
    60  		})
    61  
    62  		AfterEach(func() {
    63  			mockctl.Finish()
    64  		})
    65  
    66  		It("Should do nothing when the message is nil", func() {
    67  			manager.publish(nil)
    68  		})
    69  
    70  		It("Should do nothing when the  data is nil", func() {
    71  			manager.publish(&data.RegistrationItem{})
    72  		})
    73  
    74  		It("Should do nothing for empty data", func() {
    75  			dat := []byte{}
    76  			manager.publish(&data.RegistrationItem{Data: dat})
    77  		})
    78  
    79  		It("Should publish to registration agent when not set", func() {
    80  			dat := []byte("hello world")
    81  
    82  			msg := &message.Message{}
    83  			conn.EXPECT().IsConnected().Return(true)
    84  			conn.EXPECT().Publish(gomock.AssignableToTypeOf(msg)).DoAndReturn(func(m *message.Message) {
    85  				Expect(m.Agent()).To(Equal("registration"))
    86  			}).Return(nil).AnyTimes()
    87  
    88  			manager.publish(&data.RegistrationItem{Data: dat})
    89  		})
    90  
    91  		It("Should publish to the configured agent when set", func() {
    92  			dat := []byte("hello world")
    93  			msg := &message.Message{}
    94  			conn.EXPECT().IsConnected().Return(true)
    95  			conn.EXPECT().Publish(gomock.AssignableToTypeOf(msg)).DoAndReturn(func(m *message.Message) {
    96  				Expect(m.Agent()).To(Equal("ginkgo"))
    97  			}).Return(nil).AnyTimes()
    98  
    99  			manager.publish(&data.RegistrationItem{Data: dat, TargetAgent: "ginkgo"})
   100  		})
   101  
   102  		It("Should handle publish failures gracefully", func() {
   103  			dat := []byte("hello world")
   104  			msg := &message.Message{}
   105  			conn.EXPECT().IsConnected().Return(true)
   106  			conn.EXPECT().Publish(gomock.AssignableToTypeOf(msg)).Return(errors.New("simulated failure")).AnyTimes()
   107  			manager.publish(&data.RegistrationItem{Data: dat, TargetAgent: "ginkgo"})
   108  		})
   109  
   110  		It("Should not publish when not connected", func() {
   111  			dat := []byte("hello world")
   112  			conn.EXPECT().IsConnected().Return(false)
   113  			manager.publish(&data.RegistrationItem{Data: dat, TargetAgent: "ginkgo"})
   114  		})
   115  	})
   116  })