From 379be115ffc975cfeee0c1f9b0e0d978493259f4 Mon Sep 17 00:00:00 2001 From: marcobebway Date: Mon, 5 Feb 2024 17:12:24 +0100 Subject: [PATCH] Add a unit-test --- .../natsconnection/integration_test.go | 25 ++++++++++++++++++- 1 file changed, 24 insertions(+), 1 deletion(-) diff --git a/internal/controller/operator/eventing/integrationtests/natsconnection/integration_test.go b/internal/controller/operator/eventing/integrationtests/natsconnection/integration_test.go index e6c1c02c..42037806 100644 --- a/internal/controller/operator/eventing/integrationtests/natsconnection/integration_test.go +++ b/internal/controller/operator/eventing/integrationtests/natsconnection/integration_test.go @@ -6,6 +6,7 @@ import ( natstestutils "github.com/kyma-project/nats-manager/testutils" "github.com/onsi/gomega" gomegatypes "github.com/onsi/gomega/types" + "github.com/pkg/errors" "github.com/stretchr/testify/mock" "github.com/stretchr/testify/require" kappsv1 "k8s.io/api/apps/v1" @@ -22,6 +23,9 @@ import ( // Test_NATSConnection tests the Eventing CR status when connecting to NATS. func Test_NATSConnection(t *testing.T) { // given + + ErrAny := errors.New("any") + testCases := []struct { name string givenNATSConnectionMock func() *natsconnectionmocks.Connection @@ -45,7 +49,7 @@ func Test_NATSConnection(t *testing.T) { ), }, { - name: "Eventing CR should be in warning state if not connected to NATS", + name: "Eventing CR should be in warning state if the connect behaviour returned a cannot connect error", givenNATSConnectionMock: func() *natsconnectionmocks.Connection { conn := &natsconnectionmocks.Connection{} conn.On("Connect", mock.Anything, mock.Anything).Return(natsconnectionerrors.ErrCannotConnect) @@ -63,6 +67,25 @@ func Test_NATSConnection(t *testing.T) { matchers.HaveFinalizer(), ), }, + { + name: "Eventing CR should be in warning state if the connect behaviour returned any error", + givenNATSConnectionMock: func() *natsconnectionmocks.Connection { + conn := &natsconnectionmocks.Connection{} + conn.On("Connect", mock.Anything, mock.Anything).Return(ErrAny) + conn.On("IsConnected").Return(false) + conn.On("RegisterReconnectHandler", mock.Anything).Return() + conn.On("RegisterDisconnectErrHandler", mock.Anything).Return() + return conn + }, + wantMatches: gomega.And( + matchers.HaveStatusWarning(), + matchers.HaveBackendNotAvailableConditionWith( + ErrAny.Error(), + operatorv1alpha1.ConditionReasonNATSNotAvailable, + ), + matchers.HaveFinalizer(), + ), + }, } for _, tc := range testCases {