github.com/choria-io/go-choria@v0.28.1-0.20240416190746-b3bf9c7d5a45/broker/federation/choria_nats_ingest_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/inter"
    13  	"github.com/choria-io/go-choria/protocol"
    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("Choria NATS Ingest", func() {
    21  	var (
    22  		request   protocol.Request
    23  		srequest  protocol.SecureRequest
    24  		transport protocol.TransportMessage
    25  		connector *pooledWorker
    26  		manager   *stubConnectionManager
    27  		in        inter.ConnectorMessage
    28  		logbuf    *gbytes.Buffer
    29  		logger    *log.Entry
    30  		broker    *FederationBroker
    31  		ctx       context.Context
    32  		cancel    func()
    33  	)
    34  
    35  	BeforeEach(func() {
    36  		ctx, cancel = context.WithCancel(context.Background())
    37  		var gblogger *log.Logger
    38  		logbuf, gblogger = testutil.GbytesLogger(log.DebugLevel)
    39  		logger = log.NewEntry(gblogger)
    40  
    41  		c.Config.OverrideCertname = "rip.mcollective"
    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  		request.SetMessage([]byte(`{"hello":"world"}`))
    49  
    50  		srequest, err = c.NewSecureRequest(ctx, request)
    51  		Expect(err).ToNot(HaveOccurred())
    52  
    53  		transport, err = c.NewTransportForSecureRequest(srequest)
    54  		Expect(err).ToNot(HaveOccurred())
    55  		transport.SetFederationRequestID(request.RequestID())
    56  
    57  		j, err := transport.JSON()
    58  		Expect(err).ToNot(HaveOccurred())
    59  
    60  		in = choria.NewConnectorMessage("test", "", []byte(j), nil)
    61  
    62  		broker, _ = NewFederationBroker("test", c)
    63  		connector, err = NewChoriaNatsIngest(1, Federation, 10, broker, logger)
    64  		Expect(err).ToNot(HaveOccurred())
    65  
    66  		manager = &stubConnectionManager{}
    67  		manager.Init()
    68  		connector.connection = manager
    69  
    70  		connector.choria.Configuration().Choria.FederationMiddlewareHosts = []string{"c1:4222", "c2:4222"}
    71  		connector.choria.Configuration().Choria.MiddlewareHosts = []string{"c3:4222", "c4:4222"}
    72  
    73  		go connector.Run(ctx)
    74  	})
    75  
    76  	AfterEach(func() {
    77  		cancel()
    78  	})
    79  
    80  	It("Should fail for invalid JSON", func() {
    81  		in = choria.NewConnectorMessage(in.Subject(), in.Reply(), []byte("{}"), nil)
    82  		manager.connection.PublishToQueueSub("ingest", in)
    83  		Eventually(logbuf).Should(gbytes.Say("Could not parse received message into a TransportMessage: do not know how to create a TransportMessage from an expected JSON format message with content: {}"))
    84  	})
    85  
    86  	It("Should fail for unfederated messages", func() {
    87  		transport.SetUnfederated()
    88  		j, _ := transport.JSON()
    89  		in = choria.NewConnectorMessage(in.Subject(), in.Reply(), []byte(j), nil)
    90  		manager.connection.PublishToQueueSub("ingest", in)
    91  		Eventually(logbuf).Should(gbytes.Say("Received a message on test that was not federated"))
    92  	})
    93  
    94  	It("Should subscribe to the right target in Federation mode", func() {
    95  		manager.connection.PublishToQueueSub("ingest", in)
    96  		<-connector.Output()
    97  		Expect(manager.connection.Subs["ingest"]).To(Equal([3]string{"ingest", "choria.federation.test.federation", "test_federation"}))
    98  
    99  	})
   100  
   101  	It("Should subscribe to the right target in Collective mode", func() {
   102  		cancel()
   103  		ctx, cancel = context.WithCancel(context.Background())
   104  
   105  		connector, _ := NewChoriaNatsIngest(1, Collective, 10, broker, logger)
   106  		manager := &stubConnectionManager{}
   107  		manager.Init()
   108  		connector.connection = manager
   109  
   110  		go connector.Run(ctx)
   111  
   112  		manager.connection.PublishToQueueSub("ingest", in)
   113  		<-connector.Output()
   114  		Expect(manager.connection.Subs["ingest"]).To(Equal([3]string{"ingest", "choria.federation.test.collective", "test_collective"}))
   115  
   116  		cancel()
   117  	})
   118  
   119  	It("Should subscribe and process the message", func() {
   120  		manager.connection.PublishToQueueSub("ingest", in)
   121  		out := <-connector.Output()
   122  
   123  		Expect(out.Message).To(Equal(transport))
   124  		Expect(out.RequestID).To(Equal(request.RequestID()))
   125  		Expect(out.Seen).To(Equal([]string{"nats://stub:4222", "choria_nats_ingest:0"}))
   126  	})
   127  })