github.com/choria-io/go-choria@v0.28.1-0.20240416190746-b3bf9c7d5a45/integration/suites/broker_mappings/broker_mappings_test.go (about)

     1  // Copyright (c) 2022, R.I. Pienaar and the Choria Project contributors
     2  //
     3  // SPDX-License-Identifier: Apache-2.0
     4  
     5  package broker_mappings
     6  
     7  import (
     8  	"context"
     9  	"sync"
    10  	"testing"
    11  	"time"
    12  
    13  	"github.com/choria-io/go-choria/integration/testbroker"
    14  	"github.com/choria-io/go-choria/integration/testutil"
    15  	"github.com/nats-io/nats.go"
    16  	. "github.com/onsi/ginkgo/v2"
    17  	. "github.com/onsi/gomega"
    18  	"github.com/onsi/gomega/gbytes"
    19  	"github.com/sirupsen/logrus"
    20  )
    21  
    22  func TestBrokerRemapping(t *testing.T) {
    23  	RegisterFailHandler(Fail)
    24  	RunSpecs(t, "Integration/Broker Remapping")
    25  }
    26  
    27  var _ = Describe("Authentication", func() {
    28  	var (
    29  		ctx     context.Context
    30  		cancel  context.CancelFunc
    31  		wg      sync.WaitGroup
    32  		logger  *logrus.Logger
    33  		logbuff *gbytes.Buffer
    34  	)
    35  
    36  	BeforeEach(func() {
    37  		ctx, cancel = context.WithTimeout(context.Background(), 45*time.Second)
    38  		DeferCleanup(func() {
    39  			cancel()
    40  			Eventually(logbuff, 5).Should(gbytes.Say("Choria Network Broker shut down"))
    41  		})
    42  
    43  		logbuff, logger = testutil.GbytesLogger(logrus.DebugLevel)
    44  	})
    45  
    46  	Describe("Mappings", func() {
    47  		BeforeEach(func() {
    48  			_, err := testbroker.StartNetworkBrokerWithConfigFile(ctx, &wg, "testdata/mappings.conf", logger)
    49  			Expect(err).ToNot(HaveOccurred())
    50  
    51  			Eventually(logbuff, 2).Should(gbytes.Say("TLS required for client connections"))
    52  			Eventually(logbuff, 1).Should(gbytes.Say("Server is ready"))
    53  		})
    54  
    55  		It("Should add correct mappings", func() {
    56  			nc, err := nats.Connect("tls://localhost:4222",
    57  				nats.ClientCert(testutil.CertPath("one", "rip.mcollective"), testutil.KeyPath("one", "rip.mcollective")),
    58  				nats.RootCAs(testutil.CertPath("one", "ca")),
    59  			)
    60  			Expect(err).ToNot(HaveOccurred())
    61  			defer nc.Close()
    62  
    63  			Eventually(logbuff, 1).Should(gbytes.Say("Registering user '' in account 'choria'"))
    64  			Expect(nc.ConnectedUrl()).To(Equal("tls://localhost:4222"))
    65  
    66  			sub, err := nc.SubscribeSync("registration.>")
    67  			Expect(err).ToNot(HaveOccurred())
    68  
    69  			Expect(nc.Publish("in.registration.dev1.example.net", []byte("dev1.example.net"))).To(Succeed())
    70  			Expect(nc.Publish("in.registration.dev2.example.net", []byte("dev2.example.net"))).To(Succeed())
    71  			Expect(nc.Publish("in.registration.dev3.example.net", []byte("dev3.example.net"))).To(Succeed())
    72  			Expect(nc.Publish("in.registration.dev4.example.net", []byte("dev4.example.net"))).To(Succeed())
    73  			Expect(nc.Publish("in.registration.dev5.example.net", []byte("dev5.example.net"))).To(Succeed())
    74  			Expect(nc.Publish("in.registration.dev1.example.net", []byte("dev1.example.net"))).To(Succeed())
    75  
    76  			check := func(body, subj string) {
    77  				msg, err := sub.NextMsg(time.Second)
    78  				ExpectWithOffset(1, err).ToNot(HaveOccurred())
    79  				ExpectWithOffset(1, msg.Data).To(Equal([]byte(body)))
    80  				ExpectWithOffset(1, msg.Subject).To(Equal(subj))
    81  			}
    82  
    83  			check("dev1.example.net", "registration.2.dev1.example.net")
    84  			check("dev2.example.net", "registration.1.dev2.example.net")
    85  			check("dev3.example.net", "registration.0.dev3.example.net")
    86  			check("dev4.example.net", "registration.2.dev4.example.net")
    87  			check("dev5.example.net", "registration.1.dev5.example.net")
    88  			check("dev1.example.net", "registration.2.dev1.example.net")
    89  		})
    90  	})
    91  })