github.com/choria-io/go-choria@v0.28.1-0.20240416190746-b3bf9c7d5a45/broker/federation/reply_transformer_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 federation 6 7 import ( 8 "context" 9 10 "github.com/choria-io/go-choria/integration/testutil" 11 "github.com/onsi/gomega/gbytes" 12 log "github.com/sirupsen/logrus" 13 14 "github.com/choria-io/go-choria/choria" 15 "github.com/choria-io/go-choria/protocol" 16 17 . "github.com/onsi/ginkgo/v2" 18 . "github.com/onsi/gomega" 19 ) 20 21 var _ = Describe("Reply Transformer", func() { 22 var ( 23 c *choria.Framework 24 request protocol.Request 25 reply protocol.Reply 26 sreply protocol.SecureReply 27 transformer *pooledWorker 28 in chainmessage 29 err error 30 logbuf *gbytes.Buffer 31 logger *log.Entry 32 ctx context.Context 33 cancel func() 34 ) 35 36 BeforeEach(func() { 37 ctx, cancel = context.WithCancel(context.Background()) 38 var gblogger *log.Logger 39 logbuf, gblogger = testutil.GbytesLogger(log.DebugLevel) 40 logger = log.NewEntry(gblogger) 41 42 c, err = choria.New("testdata/federation.cfg") 43 Expect(err).ToNot(HaveOccurred()) 44 45 rid, err := c.NewRequestID() 46 Expect(err).ToNot(HaveOccurred()) 47 48 request, err = c.NewRequest(protocol.RequestV1, "test", "tester", "choria=tester", 60, rid, "mcollective") 49 Expect(err).ToNot(HaveOccurred()) 50 request.SetMessage([]byte(`{"hello":"world"}`)) 51 52 reply, err = c.NewReply(request) 53 Expect(err).ToNot(HaveOccurred()) 54 55 sreply, err = c.NewSecureReply(reply) 56 Expect(err).ToNot(HaveOccurred()) 57 58 in.Message, err = c.NewTransportForSecureReply(sreply) 59 Expect(err).ToNot(HaveOccurred()) 60 61 broker, _ := NewFederationBroker("test", c) 62 63 transformer, err = NewChoriaReplyTransformer(1, 10, broker, logger) 64 Expect(err).ToNot(HaveOccurred()) 65 66 go transformer.Run(ctx) 67 }) 68 69 AfterEach(func() { 70 cancel() 71 }) 72 73 It("should correctly transform a message", func() { 74 tr, err := c.NewTransportForSecureReply(sreply) 75 Expect(err).ToNot(HaveOccurred()) 76 77 tr.SetFederationRequestID(request.RequestID()) 78 tr.SetFederationReplyTo("mcollective.reply") 79 80 in.Message = tr 81 in.RequestID = reply.RequestID() 82 83 transformer.Input() <- in 84 out := <-transformer.Output() 85 86 Expect(out.Targets).To(Equal([]string{"mcollective.reply"})) 87 88 id, federated := out.Message.FederationRequestID() 89 Expect(id).To(BeEmpty()) 90 Expect(federated).To(BeFalse()) 91 }) 92 93 It("should fail for unfederated messages", func() { 94 transformer.Input() <- in 95 96 Eventually(logbuf).Should(gbytes.Say("Received a message from rip.mcollective that is not federated")) 97 }) 98 99 It("Should fail for messages with no reply-to", func() { 100 in.Message.SetFederationRequestID("80a1ac20463745c0b12cfe6e3db61dff") 101 transformer.Input() <- in 102 103 Eventually(logbuf).Should(gbytes.Say("Received message 80a1ac20463745c0b12cfe6e3db61dff with no reply-to set")) 104 }) 105 106 It("Should support Quit", func() { 107 cancel() 108 Eventually(logbuf).Should(gbytes.Say("Worker routine choria_reply_transformer exiting")) 109 }) 110 })