From 00f863aea099f47ca6adf99fca888b6a28d2eff6 Mon Sep 17 00:00:00 2001 From: gokulav137 <52277763+gokulav137@users.noreply.github.com> Date: Sat, 21 Oct 2023 09:56:37 +0530 Subject: [PATCH] Add unit test cases for exotic jetstream eventbus - add test to update native nats to exotic js - add test when url is missing for exotic js - add test when exotic js has correct properties - add test for exotic js install/uninstall Signed-off-by: gokulav137 <52277763+gokulav137@users.noreply.github.com> --- common/leaderelection/leaderelection_test.go | 2 + .../installer/exotic_jetstream_test.go | 50 +++++++++++++++++++ .../eventbus/installer/installer_test.go | 6 +++ .../eventbus/installer/jetstream_test.go | 16 ++++++ controllers/eventbus/validate_test.go | 25 ++++++++++ webhook/validator/eventbus_test.go | 12 +++++ 6 files changed, 111 insertions(+) create mode 100644 controllers/eventbus/installer/exotic_jetstream_test.go diff --git a/common/leaderelection/leaderelection_test.go b/common/leaderelection/leaderelection_test.go index 3a3778bdd5..7a0de21d6c 100644 --- a/common/leaderelection/leaderelection_test.go +++ b/common/leaderelection/leaderelection_test.go @@ -8,12 +8,14 @@ import ( "github.com/argoproj/argo-events/common" eventbusv1alpha1 "github.com/argoproj/argo-events/pkg/apis/eventbus/v1alpha1" "github.com/stretchr/testify/assert" + v1 "k8s.io/api/core/v1" ) var ( configs = []eventbusv1alpha1.BusConfig{ {NATS: &eventbusv1alpha1.NATSConfig{}}, {JetStream: &eventbusv1alpha1.JetStreamConfig{}}, + {JetStream: &eventbusv1alpha1.JetStreamConfig{AccessSecret: &v1.SecretKeySelector{}}}, } ) diff --git a/controllers/eventbus/installer/exotic_jetstream_test.go b/controllers/eventbus/installer/exotic_jetstream_test.go new file mode 100644 index 0000000000..6b1dba87cc --- /dev/null +++ b/controllers/eventbus/installer/exotic_jetstream_test.go @@ -0,0 +1,50 @@ +package installer + +import ( + "context" + "testing" + + "github.com/stretchr/testify/assert" + metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" + + "github.com/argoproj/argo-events/common/logging" + "github.com/argoproj/argo-events/pkg/apis/eventbus/v1alpha1" +) + +var ( + testJSExoticURL = "nats://nats:4222" + + testJSExoticBus = &v1alpha1.EventBus{ + TypeMeta: metav1.TypeMeta{ + APIVersion: v1alpha1.SchemeGroupVersion.String(), + Kind: "EventBus", + }, + ObjectMeta: metav1.ObjectMeta{ + Namespace: testNamespace, + Name: testExoticName, + }, + Spec: v1alpha1.EventBusSpec{ + JetStreamExotic: &v1alpha1.JetStreamConfig{ + URL: testJSExoticURL, + }, + }, + } +) + +func TestInstallationJSExotic(t *testing.T) { + t.Run("installation with exotic jetstream config", func(t *testing.T) { + installer := NewExoticJetStreamInstaller(testJSExoticBus, logging.NewArgoEventsLogger()) + conf, err := installer.Install(context.TODO()) + assert.NoError(t, err) + assert.NotNil(t, conf.JetStream) + assert.Equal(t, conf.JetStream.URL, testJSExoticURL) + }) +} + +func TestUninstallationJSExotic(t *testing.T) { + t.Run("uninstallation with exotic jetstream config", func(t *testing.T) { + installer := NewExoticJetStreamInstaller(testJSExoticBus, logging.NewArgoEventsLogger()) + err := installer.Uninstall(context.TODO()) + assert.NoError(t, err) + }) +} diff --git a/controllers/eventbus/installer/installer_test.go b/controllers/eventbus/installer/installer_test.go index 647de02569..ece0f1af3c 100644 --- a/controllers/eventbus/installer/installer_test.go +++ b/controllers/eventbus/installer/installer_test.go @@ -70,6 +70,12 @@ func TestGetInstaller(t *testing.T) { assert.NotNil(t, installer) _, ok := installer.(*jetStreamInstaller) assert.True(t, ok) + + installer, err = getInstaller(testJetStreamExoticBus, nil, nil, fakeConfig, zaptest.NewLogger(t).Sugar()) + assert.NoError(t, err) + assert.NotNil(t, installer) + _, ok = installer.(*exoticJetStreamInstaller) + assert.True(t, ok) }) } diff --git a/controllers/eventbus/installer/jetstream_test.go b/controllers/eventbus/installer/jetstream_test.go index b2371f1eca..dfc83fa318 100644 --- a/controllers/eventbus/installer/jetstream_test.go +++ b/controllers/eventbus/installer/jetstream_test.go @@ -34,6 +34,22 @@ var ( }, }, } + + testJetStreamExoticBus = &v1alpha1.EventBus{ + TypeMeta: metav1.TypeMeta{ + APIVersion: v1alpha1.SchemeGroupVersion.String(), + Kind: "EventBus", + }, + ObjectMeta: metav1.ObjectMeta{ + Namespace: testNamespace, + Name: testName, + }, + Spec: v1alpha1.EventBusSpec{ + JetStreamExotic: &v1alpha1.JetStreamConfig{ + URL: "nats://nats:4222", + }, + }, + } ) func TestJetStreamBadInstallation(t *testing.T) { diff --git a/controllers/eventbus/validate_test.go b/controllers/eventbus/validate_test.go index 6041e64d54..2a28229aa6 100644 --- a/controllers/eventbus/validate_test.go +++ b/controllers/eventbus/validate_test.go @@ -39,6 +39,18 @@ var ( }, } + testJetStreamExoticBus = &v1alpha1.EventBus{ + ObjectMeta: metav1.ObjectMeta{ + Namespace: "test-ns", + Name: common.DefaultEventBusName, + }, + Spec: v1alpha1.EventBusSpec{ + JetStreamExotic: &v1alpha1.JetStreamConfig{ + URL: "nats://nats:4222", + }, + }, + } + testKafkaEventBus = &v1alpha1.EventBus{ ObjectMeta: metav1.ObjectMeta{ Namespace: "test-ns", @@ -68,6 +80,11 @@ func TestValidate(t *testing.T) { assert.NoError(t, err) }) + t.Run("test good js exotic eventbus", func(t *testing.T) { + err := ValidateEventBus(testJetStreamExoticBus) + assert.NoError(t, err) + }) + t.Run("test bad eventbus", func(t *testing.T) { eb := testNatsEventBus.DeepCopy() eb.Spec.NATS = nil @@ -130,4 +147,12 @@ func TestValidate(t *testing.T) { assert.Error(t, err) assert.Contains(t, err.Error(), "\"spec.kafka.url\" is missing") }) + + t.Run("test exotic js eventbus empty URL", func(t *testing.T) { + eb := testJetStreamExoticBus.DeepCopy() + eb.Spec.JetStreamExotic.URL = "" + err := ValidateEventBus(eb) + assert.Error(t, err) + assert.True(t, strings.Contains(err.Error(), "\"spec.jetstreamExotic.url\" is missing")) + }) } diff --git a/webhook/validator/eventbus_test.go b/webhook/validator/eventbus_test.go index 5934dc5e71..91203aa25d 100644 --- a/webhook/validator/eventbus_test.go +++ b/webhook/validator/eventbus_test.go @@ -50,4 +50,16 @@ func TestValidateEventBusUpdate(t *testing.T) { r := v.ValidateUpdate(contextWithLogger(t)) assert.False(t, r.Allowed) }) + + t.Run("test update native nats to exotic js", func(t *testing.T) { + newEb := eb.DeepCopy() + newEb.Generation++ + newEb.Spec.NATS = nil + newEb.Spec.JetStreamExotic = &eventbusv1alpha1.JetStreamConfig{ + URL: "nats://nats:4222", + } + v := NewEventBusValidator(fakeK8sClient, fakeEventBusClient, fakeEventSourceClient, fakeSensorClient, eb, newEb) + r := v.ValidateUpdate(contextWithLogger(t)) + assert.False(t, r.Allowed) + }) }