github.com/choria-io/go-choria@v0.28.1-0.20240416190746-b3bf9c7d5a45/broker/federation/federation_test.go (about) 1 // Copyright (c) 2017-2021, R.I. Pienaar and the Choria Project contributors 2 // 3 // SPDX-License-Identifier: Apache-2.0 4 5 package federation 6 7 import ( 8 "context" 9 "fmt" 10 "io" 11 "os" 12 "sync" 13 "testing" 14 15 "github.com/choria-io/go-choria/config" 16 "github.com/choria-io/go-choria/inter" 17 "github.com/nats-io/nats.go" 18 . "github.com/onsi/ginkgo/v2" 19 . "github.com/onsi/gomega" 20 log "github.com/sirupsen/logrus" 21 22 "github.com/choria-io/go-choria/choria" 23 "github.com/choria-io/go-choria/srvcache" 24 ) 25 26 var c *choria.Framework 27 28 func init() { 29 os.Setenv("MCOLLECTIVE_CERTNAME", "rip.mcollective") 30 cfg, _ := config.NewConfig("testdata/federation.cfg") 31 cfg.OverrideCertname = "rip.mcollective" 32 c, _ = choria.NewWithConfig(cfg) 33 } 34 35 func TestFederation(t *testing.T) { 36 log.SetOutput(io.Discard) 37 38 RegisterFailHandler(Fail) 39 RunSpecs(t, "Broker/Federation") 40 } 41 42 type stubConnectionManager struct { 43 connection *stubConnection 44 } 45 46 type stubConnection struct { 47 Outq chan [2]string 48 Subs map[string][3]string 49 SubChannels map[string]chan inter.ConnectorMessage 50 mu *sync.Mutex 51 } 52 53 func (s *stubConnection) PublishToQueueSub(name string, msg inter.ConnectorMessage) { 54 s.mu.Lock() 55 defer s.mu.Unlock() 56 57 c, ok := s.SubChannels[name] 58 if !ok { 59 s.SubChannels[name] = make(chan inter.ConnectorMessage, 1000) 60 c = s.SubChannels[name] 61 } 62 63 c <- msg 64 } 65 66 func (s *stubConnection) AgentBroadcastTarget(collective string, agent string) string { 67 return fmt.Sprintf("%s.broadcast.agent.%s", collective, agent) 68 } 69 70 func (s *stubConnection) ServiceBroadcastTarget(collective string, agent string) string { 71 return fmt.Sprintf("%s.broadcast.service.%s", collective, agent) 72 } 73 74 func (s *stubConnection) NodeDirectedTarget(collective string, identity string) string { 75 return fmt.Sprintf("%s.node.%s", collective, identity) 76 } 77 78 func (s *stubConnection) ConnectedServer() string { 79 return "nats://stub:4222" 80 } 81 82 func (s *stubConnection) ConnectionOptions() nats.Options { 83 return nats.Options{} 84 } 85 86 func (s *stubConnection) ConnectionStats() nats.Statistics { 87 return nats.Statistics{} 88 } 89 90 func (s *stubConnection) InboxPrefix() string { return "_INBOX.>" } 91 92 func (s *stubConnection) IsConnected() bool { return true } 93 94 func (s *stubConnection) Unsubscribe(name string) error { 95 s.mu.Lock() 96 defer s.mu.Unlock() 97 98 delete(s.Subs, name) 99 delete(s.SubChannels, name) 100 101 return nil 102 } 103 104 func (s *stubConnection) ChanQueueSubscribe(name string, subject string, group string, capacity int) (chan inter.ConnectorMessage, error) { 105 s.mu.Lock() 106 defer s.mu.Unlock() 107 108 s.Subs[name] = [3]string{name, subject, group} 109 110 _, ok := s.SubChannels[name] 111 if !ok { 112 s.SubChannels[name] = make(chan inter.ConnectorMessage, 1000) 113 } 114 115 return s.SubChannels[name], nil 116 } 117 118 func (s *stubConnection) QueueSubscribe(ctx context.Context, name string, subject string, group string, output chan inter.ConnectorMessage) error { 119 return nil 120 } 121 122 func (s *stubConnection) PublishRaw(target string, data []byte) error { 123 s.Outq <- [2]string{target, string(data)} 124 125 return nil 126 } 127 128 func (s *stubConnection) Publish(msg inter.Message) error { 129 return nil 130 } 131 132 func (s *stubConnection) Connect(ctx context.Context) error { 133 return nil 134 } 135 136 func (s *stubConnection) Close() {} 137 138 func (s *stubConnection) ReplyTarget(msg inter.Message) (string, error) { 139 return "stubreplytarget", nil 140 } 141 142 func (s *stubConnection) Nats() *nats.Conn { 143 return &nats.Conn{} 144 } 145 146 func (s *stubConnection) PublishRawMsg(msg *nats.Msg) error { return fmt.Errorf("not implemented") } 147 func (s *stubConnection) RequestRawMsgWithContext(ctx context.Context, msg *nats.Msg) (*nats.Msg, error) { 148 return nil, fmt.Errorf("not implemented") 149 } 150 151 func (s *stubConnectionManager) NewConnector(ctx context.Context, servers func() (srvcache.Servers, error), name string, logger *log.Entry) (conn inter.Connector, err error) { 152 if s.connection != nil { 153 return s.connection, nil 154 } 155 156 conn = &stubConnection{ 157 Outq: make(chan [2]string, 64), 158 SubChannels: make(map[string]chan inter.ConnectorMessage), 159 Subs: make(map[string][3]string), 160 mu: &sync.Mutex{}, 161 } 162 163 s.connection = conn.(*stubConnection) 164 165 return 166 } 167 168 func (s *stubConnectionManager) Init() *stubConnectionManager { 169 s.connection = &stubConnection{ 170 Outq: make(chan [2]string, 64), 171 SubChannels: make(map[string]chan inter.ConnectorMessage), 172 Subs: make(map[string][3]string), 173 mu: &sync.Mutex{}, 174 } 175 176 return s 177 } 178 179 var _ = Describe("Federation Broker", func() { 180 It("Should initialize correctly", func() { 181 log.SetOutput(io.Discard) 182 183 c, err := choria.New("testdata/federation.cfg") 184 Expect(err).ToNot(HaveOccurred()) 185 186 _, err = NewFederationBroker("test_cluster", c) 187 Expect(err).ToNot(HaveOccurred()) 188 }) 189 })