From 6272eb0e84e98454c07159d40b5902091c3adc5b Mon Sep 17 00:00:00 2001 From: Muhammad Faizan Date: Mon, 19 Aug 2024 16:48:13 +0200 Subject: [PATCH] fixed test --- internal/connection/nats/mocks/builder.go | 4 ++ .../operator/eventing/controller.go | 7 +++ .../natsconnection/integration_test.go | 58 ++++++++++++++----- test/utils/integration/integration.go | 45 +++++++------- 4 files changed, 78 insertions(+), 36 deletions(-) diff --git a/internal/connection/nats/mocks/builder.go b/internal/connection/nats/mocks/builder.go index 8ab36d3f1..28a6fa843 100644 --- a/internal/connection/nats/mocks/builder.go +++ b/internal/connection/nats/mocks/builder.go @@ -15,3 +15,7 @@ func NewBuilder(conn *Connection) *Builder { func (b *Builder) Build() natsconnection.Interface { return b.conn } + +func (b *Builder) SetConnection(conn *Connection) { + b.conn = conn +} diff --git a/internal/controller/operator/eventing/controller.go b/internal/controller/operator/eventing/controller.go index 9c9d8d507..3e135be11 100644 --- a/internal/controller/operator/eventing/controller.go +++ b/internal/controller/operator/eventing/controller.go @@ -711,6 +711,13 @@ func (r *Reconciler) SetKubeClient(kubeClient k8s.Client) { r.kubeClient = kubeClient } +func (r *Reconciler) ResetNATSConnection() { + if r.natsConnection != nil { + r.natsConnection.Disconnect() + } + r.natsConnection = nil +} + func (r *Reconciler) namedLogger() *zap.SugaredLogger { return r.logger.WithContext().Named(ControllerName) } diff --git a/internal/controller/operator/eventing/integrationtests/natsconnection/integration_test.go b/internal/controller/operator/eventing/integrationtests/natsconnection/integration_test.go index 64dc42c76..5ba4f2f21 100644 --- a/internal/controller/operator/eventing/integrationtests/natsconnection/integration_test.go +++ b/internal/controller/operator/eventing/integrationtests/natsconnection/integration_test.go @@ -1,6 +1,7 @@ package natsconnection import ( + "os" "testing" natstestutils "github.com/kyma-project/nats-manager/testutils" @@ -8,7 +9,6 @@ import ( 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" operatorv1alpha1 "github.com/kyma-project/eventing-manager/api/operator/v1alpha1" @@ -20,9 +20,41 @@ import ( testutilsintegration "github.com/kyma-project/eventing-manager/test/utils/integration" ) +const ( + projectRootDir = "../../../../../../" +) + +var testEnvironment *testutilsintegration.TestEnvironment + +// TestMain pre-hook and post-hook to run before and after all tests. +func TestMain(m *testing.M) { + // Note: The setup will provision a single K8s env and + // all the tests need to create and use a separate namespace + + // setup env test + var err error + testEnvironment, err = testutilsintegration.NewTestEnvironment(testutilsintegration.TestEnvironmentConfig{ + ProjectRootDir: projectRootDir, + NATSCRDEnabled: true, + }, nil) + if err != nil { + panic(err) + } + + // run tests + code := m.Run() + + // tear down test env + if err = testEnvironment.TearDown(); err != nil { + panic(err) + } + + os.Exit(code) +} + // Test_NATSConnection tests the Eventing CR status when connecting to NATS. // -//nolint:tparallel // multiple controllers with same name cannot co-exist. +//nolint:tparallel // test-cases uses a shared NATSConnectionBuilder and requires different mocks. func Test_NATSConnection(t *testing.T) { t.Parallel() // given @@ -40,6 +72,7 @@ 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("Disconnect").Return() return conn }, wantMatches: gomega.And( @@ -55,6 +88,7 @@ func Test_NATSConnection(t *testing.T) { conn := &natsconnectionmocks.Connection{} conn.On("Connect", mock.Anything, mock.Anything).Return(natsconnectionerrors.ErrCannotConnect) conn.On("IsConnected").Return(false) + conn.On("Disconnect").Return() return conn }, wantMatches: gomega.And( @@ -72,6 +106,7 @@ func Test_NATSConnection(t *testing.T) { conn := &natsconnectionmocks.Connection{} conn.On("Connect", mock.Anything, mock.Anything).Return(ErrAny) conn.On("IsConnected").Return(false) + conn.On("Disconnect").Return() return conn }, wantMatches: gomega.And( @@ -86,19 +121,12 @@ func Test_NATSConnection(t *testing.T) { } for _, tc := range testCases { - tcc := tc - - t.Run(tcc.name, func(t *testing.T) { + tc := tc + t.Run(tc.name, func(t *testing.T) { // setup environment - testEnvironment, err := testutilsintegration.NewTestEnvironment( - testutilsintegration.TestEnvironmentConfig{ - NATSCRDEnabled: true, - ProjectRootDir: "../../../../../../", - }, - tcc.givenNATSConnectionMock(), - ) - require.NoError(t, err) - defer func() { require.NoError(t, testEnvironment.TearDown()) }() // always cleanup + testEnvironment.Reconciler.ResetNATSConnection() + testEnvironment.NATSConnectionBuilder.SetConnection(tc.givenNATSConnectionMock()) + eventingcontroller.IsDeploymentReady = func(deployment *kappsv1.Deployment) bool { return true } // prepare resources @@ -112,7 +140,7 @@ func Test_NATSConnection(t *testing.T) { testEnvironment.EnsureK8sResourceCreated(t, eventingCR) // then - testEnvironment.GetEventingAssert(gomega.NewWithT(t), eventingCR).Should(tcc.wantMatches) + testEnvironment.GetEventingAssert(gomega.NewWithT(t), eventingCR).Should(tc.wantMatches) }) } } diff --git a/test/utils/integration/integration.go b/test/utils/integration/integration.go index 23ad01200..44842f7fc 100644 --- a/test/utils/integration/integration.go +++ b/test/utils/integration/integration.go @@ -66,16 +66,17 @@ const ( // TestEnvironment provides mocked resources for integration tests. type TestEnvironment struct { - EnvTestInstance *envtest.Environment - k8sClient client.Client - KubeClient k8s.Client - K8sDynamicClient *dynamic.DynamicClient - Reconciler *eventingcontroller.Reconciler - Logger *logger.Logger - Recorder *record.EventRecorder - TestCancelFn context.CancelFunc - SubManagerFactory subscriptionmanager.ManagerFactory - JetStreamSubManager manager.Manager + EnvTestInstance *envtest.Environment + k8sClient client.Client + KubeClient k8s.Client + K8sDynamicClient *dynamic.DynamicClient + Reconciler *eventingcontroller.Reconciler + Logger *logger.Logger + Recorder *record.EventRecorder + TestCancelFn context.CancelFunc + SubManagerFactory subscriptionmanager.ManagerFactory + JetStreamSubManager manager.Manager + NATSConnectionBuilder *natsconnectionmocks.Builder } type TestEnvironmentConfig struct { @@ -191,6 +192,7 @@ func NewTestEnvironment(config TestEnvironmentConfig, connMock *natsconnectionmo connMock.On("Disconnect").Return() } + natsConnectionBuilder := natsconnectionmocks.NewBuilder(connMock) // create a new watcher eventingReconciler := eventingcontroller.NewReconciler( k8sClient, @@ -204,7 +206,7 @@ func NewTestEnvironment(config TestEnvironmentConfig, connMock *natsconnectionmo subManagerFactoryMock, opts, config.AllowedEventingCR, - natsconnectionmocks.NewBuilder(connMock), + natsConnectionBuilder, ) if err = (eventingReconciler).SetupWithManager(ctrlMgr); err != nil { @@ -229,16 +231,17 @@ func NewTestEnvironment(config TestEnvironmentConfig, connMock *natsconnectionmo } return &TestEnvironment{ - k8sClient: k8sClient, - KubeClient: kubeClient, - K8sDynamicClient: dynamicClient, - Reconciler: eventingReconciler, - Logger: ctrLogger, - Recorder: &recorder, - EnvTestInstance: testEnv, - TestCancelFn: cancelCtx, - SubManagerFactory: subManagerFactoryMock, - JetStreamSubManager: jetStreamSubManagerMock, + k8sClient: k8sClient, + KubeClient: kubeClient, + K8sDynamicClient: dynamicClient, + Reconciler: eventingReconciler, + Logger: ctrLogger, + Recorder: &recorder, + EnvTestInstance: testEnv, + TestCancelFn: cancelCtx, + SubManagerFactory: subManagerFactoryMock, + JetStreamSubManager: jetStreamSubManagerMock, + NATSConnectionBuilder: natsConnectionBuilder, }, nil }