Skip to content

Commit

Permalink
Add unit test cases for exotic jetstream eventbus
Browse files Browse the repository at this point in the history
	- 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 <[email protected]>
  • Loading branch information
gokulav137 committed Oct 21, 2023
1 parent cfdf53d commit 00f863a
Show file tree
Hide file tree
Showing 6 changed files with 111 additions and 0 deletions.
2 changes: 2 additions & 0 deletions common/leaderelection/leaderelection_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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{}}},
}
)

Expand Down
50 changes: 50 additions & 0 deletions controllers/eventbus/installer/exotic_jetstream_test.go
Original file line number Diff line number Diff line change
@@ -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)
})
}
6 changes: 6 additions & 0 deletions controllers/eventbus/installer/installer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
})
}

Expand Down
16 changes: 16 additions & 0 deletions controllers/eventbus/installer/jetstream_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
25 changes: 25 additions & 0 deletions controllers/eventbus/validate_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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"))
})
}
12 changes: 12 additions & 0 deletions webhook/validator/eventbus_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
})
}

0 comments on commit 00f863a

Please sign in to comment.