Skip to content
This repository has been archived by the owner on Dec 21, 2023. It is now read-only.

Commit

Permalink
fix: Gracefully handle missing event labels (#446)
Browse files Browse the repository at this point in the history
  • Loading branch information
thisthat authored Apr 14, 2022
1 parent 1a9a056 commit 2e23eb7
Show file tree
Hide file tree
Showing 2 changed files with 80 additions and 0 deletions.
3 changes: 3 additions & 0 deletions pkg/lib/v0_2_0/keptn.go
Original file line number Diff line number Diff line change
Expand Up @@ -206,6 +206,9 @@ func ensureContextAttributesAreSet(srcEvent, newEvent keptn.EventProperties) kep
newEvent.SetStage(srcEvent.GetStage())
newEvent.SetService(srcEvent.GetService())
labels := srcEvent.GetLabels()
if labels == nil {
labels = make(map[string]string)
}

// make sure labels from triggered event are included. Existing labels cannot be changed, but new ones can be added
for key, value := range newEvent.GetLabels() {
Expand Down
77 changes: 77 additions & 0 deletions pkg/lib/v0_2_0/keptn_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,7 @@ func Test_ensureContextAttributesAreSet(t *testing.T) {
},
},
},

{
name: "merge labels - do not overwrite existing ones",
args: args{
Expand Down Expand Up @@ -103,6 +104,82 @@ func Test_ensureContextAttributesAreSet(t *testing.T) {
},
},
},

{
name: "gracefully handle missing src labels",
args: args{
srcEvent: &EventData{
Project: "my-project",
Stage: "my-stage",
Service: "my-service",
},
newEvent: &EventData{
Project: "my-project",
Stage: "my-stage",
Service: "my-service",
Labels: map[string]string{
"bar": "foo",
},
},
},
want: &EventData{
Project: "my-project",
Stage: "my-stage",
Service: "my-service",
Labels: map[string]string{
"bar": "foo",
},
},
},

{
name: "gracefully handle missing dest labels",
args: args{
srcEvent: &EventData{
Project: "my-project",
Stage: "my-stage",
Service: "my-service",
Labels: map[string]string{
"bar": "foo",
},
},
newEvent: &EventData{
Project: "my-project",
Stage: "my-stage",
Service: "my-service",
},
},
want: &EventData{
Project: "my-project",
Stage: "my-stage",
Service: "my-service",
Labels: map[string]string{
"bar": "foo",
},
},
},

{
name: "gracefully handle missing labels",
args: args{
srcEvent: &EventData{
Project: "my-project",
Stage: "my-stage",
Service: "my-service",
},
newEvent: &EventData{
Project: "my-project",
Stage: "my-stage",
Service: "my-service",
},
},
want: &EventData{
Project: "my-project",
Stage: "my-stage",
Service: "my-service",
Labels: map[string]string{},
},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
Expand Down

0 comments on commit 2e23eb7

Please sign in to comment.