github.com/choria-io/go-choria@v0.28.1-0.20240416190746-b3bf9c7d5a45/protocol/v2/reply_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 v2
     6  
     7  import (
     8  	"time"
     9  
    10  	"github.com/choria-io/go-choria/protocol"
    11  	. "github.com/onsi/ginkgo/v2"
    12  	. "github.com/onsi/gomega"
    13  )
    14  
    15  var _ = Describe("Reply", func() {
    16  	BeforeEach(func() {
    17  		protocol.ClientStrictValidation = false
    18  	})
    19  
    20  	Describe("NewReplyFromSecureReply", func() {
    21  		It("Should check the version secure reply", func() {
    22  			r, err := NewReplyFromSecureReply(&SecureReply{Protocol: protocol.SecureReplyV1})
    23  			Expect(err).To(MatchError("cannot create a version 2 Reply from a choria:secure:reply:1 SecureReply"))
    24  			Expect(r).To(BeNil())
    25  		})
    26  
    27  		It("Should validate the message", func() {
    28  			r, err := NewReplyFromSecureReply(&SecureReply{Protocol: protocol.SecureReplyV2, MessageBody: []byte(`{"x":"y"}`)})
    29  			Expect(err).ToNot(HaveOccurred())
    30  			Expect(r).ToNot(BeNil())
    31  
    32  			protocol.ClientStrictValidation = true
    33  
    34  			r, err = NewReplyFromSecureReply(&SecureReply{Protocol: protocol.SecureReplyV2, MessageBody: []byte(`{"x":"y"}`)})
    35  			Expect(err).To(MatchError(ErrInvalidJSON))
    36  			Expect(r).To(BeNil())
    37  		})
    38  
    39  		It("Should correctly create a reply", func() {
    40  			request, err := NewRequest("test", "go.tests", "choria=test", 120, "a2f0ca717c694f2086cfa81b6c494648", "mcollective")
    41  			Expect(err).ToNot(HaveOccurred())
    42  			request.SetMessage([]byte("hello world"))
    43  			reply, err := NewReply(request, request.SenderID())
    44  			Expect(err).ToNot(HaveOccurred())
    45  
    46  			j, err := reply.JSON()
    47  			Expect(err).ToNot(HaveOccurred())
    48  
    49  			reply, err = NewReplyFromSecureReply(&SecureReply{Protocol: protocol.SecureReplyV2, MessageBody: j})
    50  			Expect(err).ToNot(HaveOccurred())
    51  			Expect(reply.SenderID()).To(Equal("go.tests"))
    52  		})
    53  	})
    54  
    55  	Describe("RecordNetworkHop", func() {
    56  		It("Should record the hop correctly", func() {
    57  			r := &Reply{}
    58  			Expect(r.seenBy).To(BeEmpty())
    59  			r.RecordNetworkHop("ginkgo.in", "server1", "ginkgo.out")
    60  			Expect(r.seenBy).To(HaveLen(1))
    61  			r.RecordNetworkHop("ginkgo.in", "server2", "ginkgo.out")
    62  			Expect(r.seenBy).To(HaveLen(2))
    63  			Expect(r.seenBy[0]).To(Equal([3]string{"ginkgo.in", "server1", "ginkgo.out"}))
    64  		})
    65  	})
    66  
    67  	Describe("NetworkHops", func() {
    68  		It("Should report the correct hops", func() {
    69  			r := &Reply{}
    70  			r.RecordNetworkHop("ginkgo.in", "server1", "ginkgo.out")
    71  			r.RecordNetworkHop("ginkgo.in", "server2", "ginkgo.out")
    72  			Expect(r.seenBy).To(HaveLen(2))
    73  			hops := r.NetworkHops()
    74  			Expect(hops).To(HaveLen(2))
    75  			Expect(hops[0]).To(Equal([3]string{"ginkgo.in", "server1", "ginkgo.out"}))
    76  		})
    77  	})
    78  
    79  	Describe("Federation", func() {
    80  		Describe("SetFederationRequestID", func() {
    81  			It("Should set the id correctly", func() {
    82  				r := Reply{}
    83  				id, federated := r.FederationRequestID()
    84  				Expect(federated).To(BeFalse())
    85  				Expect(id).To(Equal(""))
    86  
    87  				r.SetFederationRequestID("123")
    88  				id, federated = r.FederationRequestID()
    89  				Expect(federated).To(BeTrue())
    90  				Expect(id).To(Equal("123"))
    91  			})
    92  		})
    93  
    94  		Describe("SetUnfederated", func() {
    95  			It("Should correctly unfederate the message", func() {
    96  				r := Reply{}
    97  				r.SetFederationRequestID("123")
    98  				Expect(r.IsFederated()).To(BeTrue())
    99  				r.SetUnfederated()
   100  				Expect(r.IsFederated()).To(BeFalse())
   101  			})
   102  		})
   103  
   104  		Describe("SetFederationTargets", func() {
   105  			It("Should set the federation targets correctly", func() {
   106  				r := &Reply{}
   107  				t, federated := r.FederationTargets()
   108  				Expect(federated).To(BeFalse())
   109  				Expect(t).To(BeEmpty())
   110  				r.SetFederationTargets([]string{"1", "2"})
   111  				t, federated = r.FederationTargets()
   112  				Expect(federated).To(BeTrue())
   113  				Expect(t).To(Equal([]string{"1", "2"}))
   114  			})
   115  		})
   116  
   117  		Describe("SetFederationReplyTo", func() {
   118  			It("Should set the federation reply correctly", func() {
   119  				r := Reply{}
   120  				rt, federated := r.FederationReplyTo()
   121  				Expect(federated).To(BeFalse())
   122  				Expect(rt).To(Equal(""))
   123  
   124  				r.SetFederationReplyTo("reply.to")
   125  				rt, federated = r.FederationReplyTo()
   126  				Expect(federated).To(BeTrue())
   127  				Expect(rt).To(Equal("reply.to"))
   128  			})
   129  		})
   130  
   131  		Describe("IsFederated", func() {
   132  			It("Should report correctly", func() {
   133  				r := Reply{}
   134  				Expect(r.IsFederated()).To(BeFalse())
   135  				r.SetFederationReplyTo("reply.to")
   136  				Expect(r.IsFederated()).To(BeTrue())
   137  			})
   138  		})
   139  	})
   140  
   141  	Describe("NewReply", func() {
   142  		It("should create the correct reply from a request", func() {
   143  			request, err := NewRequest("test", "go.tests", "choria=test", 120, "a2f0ca717c694f2086cfa81b6c494648", "mcollective")
   144  			Expect(err).ToNot(HaveOccurred())
   145  			request.SetMessage([]byte("hello world"))
   146  			reply, err := NewReply(request, "testing")
   147  			Expect(err).ToNot(HaveOccurred())
   148  
   149  			reply.SetMessage([]byte("hello world"))
   150  			j, _ := reply.JSON()
   151  
   152  			Expect(protocol.VersionFromJSON(j)).To(Equal(protocol.ReplyV2))
   153  			Expect(reply.Version()).To(Equal(protocol.ReplyV2))
   154  			Expect(reply.Message()).To(Equal([]byte("hello world")))
   155  			Expect(reply.RequestID()).To(HaveLen(32))
   156  			Expect(reply.SenderID()).To(Equal("testing"))
   157  			Expect(reply.Agent()).To(Equal("test"))
   158  			Expect(reply.Time()).To(BeTemporally("~", time.Now(), time.Second))
   159  		})
   160  	})
   161  })