From 39effe8f45cc41360f2a2c8698ea458acb145dcd Mon Sep 17 00:00:00 2001 From: Arthur Pitman Date: Thu, 13 Jan 2022 09:54:12 +0100 Subject: [PATCH 1/2] fix: Multiple `keptn_service` tags produce error Signed-off-by: Arthur Pitman --- internal/onboard/service_sync.go | 18 ++++++--- internal/onboard/service_sync_test.go | 54 +++++++++++++++++++++++---- 2 files changed, 59 insertions(+), 13 deletions(-) diff --git a/internal/onboard/service_sync.go b/internal/onboard/service_sync.go index 569a6ff67..4748f4b95 100644 --- a/internal/onboard/service_sync.go +++ b/internal/onboard/service_sync.go @@ -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 { diff --git a/internal/onboard/service_sync_test.go b/internal/onboard/service_sync_test.go index 790e9bb50..2ea925261 100644 --- a/internal/onboard/service_sync_test.go +++ b/internal/onboard/service_sync_test.go @@ -474,7 +474,6 @@ func Test_getKeptnServiceName(t *testing.T) { Tags: nil, }, }, - want: "", wantErr: true, }, { @@ -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) } }) } From 8b4c202fbb0ce272a3a2e02662d439712fd3d2f0 Mon Sep 17 00:00:00 2001 From: Arthur Pitman Date: Thu, 13 Jan 2022 09:57:57 +0100 Subject: [PATCH 2/2] Use PR title as commit message Signed-off-by: Arthur Pitman