Skip to content

Commit

Permalink
Add a unit-test
Browse files Browse the repository at this point in the history
  • Loading branch information
marcobebway committed Feb 5, 2024
1 parent bbd14ef commit af0e513
Showing 1 changed file with 24 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -22,6 +23,9 @@ import (
// Test_NATSConnection tests the Eventing CR status when connecting to NATS.
func Test_NATSConnection(t *testing.T) {
// given

ErrAnything := errors.New("anything")

testCases := []struct {
name string
givenNATSConnectionMock func() *natsconnectionmocks.Connection
Expand All @@ -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)
Expand All @@ -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(ErrAnything)
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(
ErrAnything.Error(),
operatorv1alpha1.ConditionReasonNATSNotAvailable,
),
matchers.HaveFinalizer(),
),
},
}

for _, tc := range testCases {
Expand Down

0 comments on commit af0e513

Please sign in to comment.