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