Skip to content

Commit

Permalink
support annotations as part of sts generation
Browse files Browse the repository at this point in the history
  • Loading branch information
pasha-codefresh committed Oct 22, 2024
1 parent 1744af0 commit 9c46bbd
Show file tree
Hide file tree
Showing 2 changed files with 56 additions and 2 deletions.
3 changes: 1 addition & 2 deletions controllers/eventbus/installer/nats.go
Original file line number Diff line number Diff line change
Expand Up @@ -68,11 +68,10 @@ func NewNATSInstaller(client client.Client, eventBus *v1alpha1.EventBus, config
}

func getAnnotations(bus *v1alpha1.EventBus) map[string]string {
if bus.Spec.NATS.Metadata != nil && bus.Spec.NATS.Metadata.Annotations != nil {
if bus != nil && bus.Spec.NATS != nil && bus.Spec.NATS.Metadata != nil && bus.Spec.NATS.Metadata.Annotations != nil {
return bus.Spec.NATS.Metadata.Annotations
}
return map[string]string{}

}

// Install creats a StatefulSet and a Service for NATS
Expand Down
55 changes: 55 additions & 0 deletions controllers/eventbus/installer/nats_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,12 @@ package installer
import (
"context"
"fmt"
"reflect"
"strconv"
"strings"
"testing"

"github.com/argoproj/argo-events/pkg/apis/common"
"github.com/stretchr/testify/assert"
"go.uber.org/zap/zaptest"
appv1 "k8s.io/api/apps/v1"
Expand Down Expand Up @@ -299,3 +301,56 @@ func TestBuildConfigMap(t *testing.T) {
}
})
}

// Returns annotations when both Metadata and Annotations are non-nil
func TestGetAnnotationsWithNonNilMetadataAndAnnotations(t *testing.T) {
annotations := map[string]string{"key1": "value1", "key2": "value2"}
bus := &v1alpha1.EventBus{
Spec: v1alpha1.EventBusSpec{
NATS: &v1alpha1.NATSBus{
Metadata: &common.Metadata{
Annotations: annotations,
},
},
},
}
result := getAnnotations(bus)
if !reflect.DeepEqual(result, annotations) {
t.Errorf("Expected %v, got %v", annotations, result)
}
}

// Returns an empty map when Metadata is nil
func TestGetAnnotationsWithNilMetadata(t *testing.T) {
bus := &v1alpha1.EventBus{
Spec: v1alpha1.EventBusSpec{
NATS: &v1alpha1.NATSBus{
Metadata: nil,
},
},
}
result := getAnnotations(bus)
if len(result) != 0 {
t.Errorf("Expected empty map, got %v", result)
}
}

// Handles nil EventBus input gracefully
func TestGetAnnotationsWithNilEventBus(t *testing.T) {
var bus *v1alpha1.EventBus = nil
result := getAnnotations(bus)
if len(result) != 0 {
t.Errorf("Expected empty map, got %v", result)
}
}

// Handles EventBus with nil Spec
func TestGetAnnotationsWithNilSpec(t *testing.T) {
bus := &v1alpha1.EventBus{
Spec: v1alpha1.EventBusSpec{},
}
result := getAnnotations(bus)
if len(result) != 0 {
t.Errorf("Expected empty map, got %v", result)
}
}

0 comments on commit 9c46bbd

Please sign in to comment.