github.com/argoproj/argo-events@v1.9.1/controllers/eventbus/validate_test.go (about) 1 package eventbus 2 3 import ( 4 "strings" 5 "testing" 6 7 "github.com/stretchr/testify/assert" 8 metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" 9 "k8s.io/utils/pointer" 10 11 "github.com/argoproj/argo-events/common" 12 "github.com/argoproj/argo-events/pkg/apis/eventbus/v1alpha1" 13 ) 14 15 var ( 16 testNatsEventBus = &v1alpha1.EventBus{ 17 ObjectMeta: metav1.ObjectMeta{ 18 Namespace: "test-ns", 19 Name: common.DefaultEventBusName, 20 }, 21 Spec: v1alpha1.EventBusSpec{ 22 NATS: &v1alpha1.NATSBus{ 23 Native: &v1alpha1.NativeStrategy{ 24 Auth: &v1alpha1.AuthStrategyToken, 25 }, 26 }, 27 }, 28 } 29 30 testJetStreamEventBus = &v1alpha1.EventBus{ 31 ObjectMeta: metav1.ObjectMeta{ 32 Namespace: "test-ns", 33 Name: common.DefaultEventBusName, 34 }, 35 Spec: v1alpha1.EventBusSpec{ 36 JetStream: &v1alpha1.JetStreamBus{ 37 Version: "2.7.3", 38 }, 39 }, 40 } 41 42 testJetStreamExoticBus = &v1alpha1.EventBus{ 43 ObjectMeta: metav1.ObjectMeta{ 44 Namespace: "test-ns", 45 Name: common.DefaultEventBusName, 46 }, 47 Spec: v1alpha1.EventBusSpec{ 48 JetStreamExotic: &v1alpha1.JetStreamConfig{ 49 URL: "nats://nats:4222", 50 }, 51 }, 52 } 53 54 testKafkaEventBus = &v1alpha1.EventBus{ 55 ObjectMeta: metav1.ObjectMeta{ 56 Namespace: "test-ns", 57 Name: common.DefaultEventBusName, 58 }, 59 Spec: v1alpha1.EventBusSpec{ 60 Kafka: &v1alpha1.KafkaBus{ 61 URL: "127.0.0.1:9092", 62 }, 63 }, 64 } 65 ) 66 67 func TestValidate(t *testing.T) { 68 t.Run("test good nats eventbus", func(t *testing.T) { 69 err := ValidateEventBus(testNatsEventBus) 70 assert.NoError(t, err) 71 }) 72 73 t.Run("test good js eventbus", func(t *testing.T) { 74 err := ValidateEventBus(testJetStreamEventBus) 75 assert.NoError(t, err) 76 }) 77 78 t.Run("test good kafka eventbus", func(t *testing.T) { 79 err := ValidateEventBus(testKafkaEventBus) 80 assert.NoError(t, err) 81 }) 82 83 t.Run("test good js exotic eventbus", func(t *testing.T) { 84 err := ValidateEventBus(testJetStreamExoticBus) 85 assert.NoError(t, err) 86 }) 87 88 t.Run("test bad eventbus", func(t *testing.T) { 89 eb := testNatsEventBus.DeepCopy() 90 eb.Spec.NATS = nil 91 err := ValidateEventBus(eb) 92 assert.Error(t, err) 93 assert.Contains(t, err.Error(), "invalid spec: either") 94 }) 95 96 t.Run("test native nats exotic conflicting eventbus", func(t *testing.T) { 97 eb := testNatsEventBus.DeepCopy() 98 eb.Spec.NATS.Exotic = &v1alpha1.NATSConfig{} 99 err := ValidateEventBus(eb) 100 assert.Error(t, err) 101 assert.True(t, strings.Contains(err.Error(), "can not be defined together")) 102 }) 103 104 t.Run("test exotic nats eventbus no clusterID", func(t *testing.T) { 105 eb := testNatsEventBus.DeepCopy() 106 eb.Spec.NATS.Native = nil 107 eb.Spec.NATS.Exotic = &v1alpha1.NATSConfig{} 108 err := ValidateEventBus(eb) 109 assert.Error(t, err) 110 assert.True(t, strings.Contains(err.Error(), "\"spec.nats.exotic.clusterID\" is missing")) 111 }) 112 113 t.Run("test exotic nats eventbus empty URL", func(t *testing.T) { 114 eb := testNatsEventBus.DeepCopy() 115 eb.Spec.NATS.Native = nil 116 cID := "test-cluster-id" 117 eb.Spec.NATS.Exotic = &v1alpha1.NATSConfig{ 118 ClusterID: &cID, 119 } 120 err := ValidateEventBus(eb) 121 assert.Error(t, err) 122 assert.True(t, strings.Contains(err.Error(), "\"spec.nats.exotic.url\" is missing")) 123 }) 124 125 t.Run("test js eventbus no version", func(t *testing.T) { 126 eb := testJetStreamEventBus.DeepCopy() 127 eb.Spec.JetStream.Version = "" 128 err := ValidateEventBus(eb) 129 assert.Error(t, err) 130 assert.Contains(t, err.Error(), "invalid spec: a version") 131 }) 132 133 t.Run("test js eventbus replica", func(t *testing.T) { 134 eb := testJetStreamEventBus.DeepCopy() 135 eb.Spec.JetStream.Replicas = pointer.Int32(3) 136 err := ValidateEventBus(eb) 137 assert.NoError(t, err) 138 eb.Spec.JetStream.Replicas = nil 139 err = ValidateEventBus(eb) 140 assert.NoError(t, err) 141 }) 142 143 t.Run("test kafka eventbus no URL", func(t *testing.T) { 144 eb := testKafkaEventBus.DeepCopy() 145 eb.Spec.Kafka.URL = "" 146 err := ValidateEventBus(eb) 147 assert.Error(t, err) 148 assert.Contains(t, err.Error(), "\"spec.kafka.url\" is missing") 149 }) 150 151 t.Run("test exotic js eventbus empty URL", func(t *testing.T) { 152 eb := testJetStreamExoticBus.DeepCopy() 153 eb.Spec.JetStreamExotic.URL = "" 154 err := ValidateEventBus(eb) 155 assert.Error(t, err) 156 assert.True(t, strings.Contains(err.Error(), "\"spec.jetstreamExotic.url\" is missing")) 157 }) 158 }