Skip to content

Commit

Permalink
fixed test
Browse files Browse the repository at this point in the history
  • Loading branch information
mfaizanse committed Aug 19, 2024
1 parent 39945b0 commit 6272eb0
Show file tree
Hide file tree
Showing 4 changed files with 78 additions and 36 deletions.
4 changes: 4 additions & 0 deletions internal/connection/nats/mocks/builder.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
7 changes: 7 additions & 0 deletions internal/controller/operator/eventing/controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
package natsconnection

import (
"os"
"testing"

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"

operatorv1alpha1 "github.com/kyma-project/eventing-manager/api/operator/v1alpha1"
Expand All @@ -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
Expand All @@ -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(
Expand All @@ -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(
Expand All @@ -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(
Expand All @@ -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
Expand All @@ -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)
})
}
}
45 changes: 24 additions & 21 deletions test/utils/integration/integration.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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,
Expand All @@ -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 {
Expand All @@ -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
}

Expand Down

0 comments on commit 6272eb0

Please sign in to comment.