Skip to content
This repository has been archived by the owner on Jan 12, 2024. It is now read-only.

fix: Service entities tagged with multiple keptn_service tags should produce an error #673

Merged
merged 2 commits into from
Jan 13, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 12 additions & 6 deletions internal/onboard/service_sync.go
Original file line number Diff line number Diff line change
Expand Up @@ -212,14 +212,20 @@ func (s *serviceSynchronizer) fetchExistingServices() error {
}

func getKeptnServiceName(entity dynatrace.Entity) (string, error) {
if entity.Tags != nil {
for _, tag := range entity.Tags {
if tag.Key == "keptn_service" && tag.Value != "" {
return tag.Value, nil
}
serviceTags := make([]string, 0)
for _, tag := range entity.Tags {
if tag.Key == "keptn_service" && tag.Value != "" {
serviceTags = append(serviceTags, tag.Value)
}
}
return "", fmt.Errorf("entity %v has no 'keptn_service' tag", entity.EntityID)
if len(serviceTags) == 0 {
return "", fmt.Errorf("entity %v has no valid 'keptn_service' tag", entity.EntityID)
}
if len(serviceTags) > 1 {
return "", fmt.Errorf("entity %v has multiple 'keptn_service' tags", entity.EntityID)
}

return serviceTags[0], nil
}

func doesServiceExist(services []string, serviceName string) bool {
Expand Down
54 changes: 47 additions & 7 deletions internal/onboard/service_sync_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -474,7 +474,6 @@ func Test_getKeptnServiceName(t *testing.T) {
Tags: nil,
},
},
want: "",
wantErr: true,
},
{
Expand All @@ -496,16 +495,57 @@ func Test_getKeptnServiceName(t *testing.T) {
want: "my-service",
wantErr: false,
},
{
name: "keptn_service tag with no value",
args: args{
entity: dynatrace.Entity{
EntityID: "entity-id",
DisplayName: ":10999",
Tags: []dynatrace.Tag{
{
Context: "CONTEXTLESS",
Key: "keptn_service",
StringRepresentation: "keptn_service",
},
},
},
},
wantErr: true,
},
{
name: "two keptn_service tags",
args: args{
entity: dynatrace.Entity{
EntityID: "entity-id",
DisplayName: ":10999",
Tags: []dynatrace.Tag{
{
Context: "CONTEXTLESS",
Key: "keptn_service",
StringRepresentation: "keptn_service:value1",
Value: "value1",
},
{
Context: "CONTEXTLESS",
Key: "keptn_service",
StringRepresentation: "keptn_service:value2",
Value: "value2",
},
},
},
},
wantErr: true,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
got, err := getKeptnServiceName(tt.args.entity)
if (err != nil) != tt.wantErr {
t.Errorf("getKeptnServiceName() error = %v, wantErr %v", err, tt.wantErr)
return
}
if got != tt.want {
t.Errorf("getKeptnServiceName() = %v, want %v", got, tt.want)

if tt.wantErr {
assert.Error(t, err)
} else {
assert.NoError(t, err)
assert.EqualValues(t, tt.want, got)
}
})
}
Expand Down