Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Change Eventing CR status to WARN if cannot connect to NATS #456

Merged
merged 4 commits into from
Feb 6, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions internal/controller/operator/eventing/controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -575,11 +575,11 @@ func (r *Reconciler) reconcileNATSBackend(ctx context.Context,
if connErr := r.connectToNATS(eventingCR); connErr != nil {
if errors.Is(connErr, natsconnectionerrors.ErrCannotConnect) {
return kctrl.Result{}, reconcile.TerminalError(
r.syncStatusWithNATSErr(ctx, eventingCR, connErr, log),
r.syncStatusWithNATSState(ctx, operatorv1alpha1.StateWarning, eventingCR, connErr, log),
)
}

return kctrl.Result{}, r.syncStatusWithNATSErr(ctx, eventingCR, connErr, log)
return kctrl.Result{}, r.syncStatusWithNATSState(ctx, operatorv1alpha1.StateWarning, eventingCR, connErr, log)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do we have test case for this case too? I see we have only for ErrCannotConnect error.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done

}

// set NATSAvailable condition to true and update status
Expand Down
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

ErrAny := errors.New("any")

testCases := []struct {
name string
givenNATSConnectionMock func() *natsconnectionmocks.Connection
Expand All @@ -33,8 +37,6 @@ func Test_NATSConnection(t *testing.T) {
conn := &natsconnectionmocks.Connection{}
conn.On("Connect", mock.Anything, mock.Anything).Return(nil)
conn.On("IsConnected").Return(true)
conn.On("RegisterReconnectHandler", mock.Anything).Return()
conn.On("RegisterDisconnectErrHandler", mock.Anything).Return()
return conn
},
wantMatches: gomega.And(
Expand All @@ -45,24 +47,39 @@ func Test_NATSConnection(t *testing.T) {
),
},
{
name: "Eventing CR should be in error 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)
conn.On("IsConnected").Return(false)
conn.On("RegisterReconnectHandler", mock.Anything).Return()
conn.On("RegisterDisconnectErrHandler", mock.Anything).Return()
return conn
},
wantMatches: gomega.And(
matchers.HaveStatusError(),
matchers.HaveStatusWarning(),
matchers.HaveBackendNotAvailableConditionWith(
natsconnectionerrors.ErrCannotConnect.Error(),
operatorv1alpha1.ConditionReasonNATSNotAvailable,
),
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)
return conn
},
wantMatches: gomega.And(
matchers.HaveStatusWarning(),
matchers.HaveBackendNotAvailableConditionWith(
ErrAny.Error(),
operatorv1alpha1.ConditionReasonNATSNotAvailable,
),
matchers.HaveFinalizer(),
),
},
}

for _, tc := range testCases {
Expand Down
2 changes: 0 additions & 2 deletions test/utils/integration/integration.go
Original file line number Diff line number Diff line change
Expand Up @@ -199,8 +199,6 @@ func NewTestEnvironment(config TestEnvironmentConfig, connMock *natsconnectionmo
connMock.On("Connect", mock.Anything, mock.Anything).Return(nil)
connMock.On("IsConnected").Return(true)
connMock.On("Disconnect").Return()
connMock.On("RegisterReconnectHandler", mock.Anything).Return()
connMock.On("RegisterDisconnectErrHandler", mock.Anything).Return()
}

// create a new watcher
Expand Down
Loading