Skip to content

Commit

Permalink
Check that events are at least expected ones
Browse files Browse the repository at this point in the history
Events are not guaranteed to be sent once, so allow for more than
one in the cancel_test.

Fixes #3374

Signed-off-by: Andrea Frittoli <[email protected]>
  • Loading branch information
afrittoli committed Oct 15, 2020
1 parent 2436f0d commit 037ddf7
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 10 deletions.
28 changes: 23 additions & 5 deletions test/cancel_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -144,23 +144,41 @@ func TestTaskRunPipelineRunCancel(t *testing.T) {
trName = append(trName, taskrunItem.Name)
}

matchKinds := map[string][]string{"PipelineRun": {pipelineRun.Name}, "TaskRun": trName}
// Expected failure events: 1 for the pipelinerun cancel, 1 for each TaskRun
expectedNumberOfEvents := 1 + len(trName)
matchKinds := map[string][]string{"PipelineRun": {pipelineRun.Name}}
// Expected failure events: 1 for the pipelinerun cancel
expectedNumberOfEvents := 1
t.Logf("Making sure %d events were created from pipelinerun with kinds %v", expectedNumberOfEvents, matchKinds)
events, err := collectMatchingEvents(ctx, c.KubeClient, namespace, matchKinds, "Failed")
if err != nil {
t.Fatalf("Failed to collect matching events: %q", err)
}
if len(events) != expectedNumberOfEvents {
if len(events) < expectedNumberOfEvents {
collectedEvents := ""
for i, event := range events {
collectedEvents += fmt.Sprintf("%#v", event)
if i < (len(events) - 1) {
collectedEvents += ", "
}
}
t.Fatalf("Expected %d number of successful events from pipelinerun and taskrun but got %d; list of received events : %#v", expectedNumberOfEvents, len(events), collectedEvents)
t.Fatalf("Expected %d number of failed events from pipelinerun but got %d; list of received events : %#v", expectedNumberOfEvents, len(events), collectedEvents)
}
matchKinds = map[string][]string{"TaskRun": trName}
// Expected failure events: 1 for each TaskRun
expectedNumberOfEvents = len(trName)
t.Logf("Making sure %d events were created from taskruns with kinds %v", expectedNumberOfEvents, matchKinds)
events, err = collectMatchingEvents(ctx, c.KubeClient, namespace, matchKinds, "Failed")
if err != nil {
t.Fatalf("Failed to collect matching events: %q", err)
}
if len(events) < expectedNumberOfEvents {
collectedEvents := ""
for i, event := range events {
collectedEvents += fmt.Sprintf("%#v", event)
if i < (len(events) - 1) {
collectedEvents += ", "
}
}
t.Fatalf("Expected %d number of failed events from taskrun but got %d; list of received events : %#v", expectedNumberOfEvents, len(events), collectedEvents)
}
})
}
Expand Down
28 changes: 23 additions & 5 deletions test/v1alpha1/cancel_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -144,23 +144,41 @@ func TestTaskRunPipelineRunCancel(t *testing.T) {
for _, taskrunItem := range taskrunList.Items {
trName = append(trName, taskrunItem.Name)
}
matchKinds := map[string][]string{"PipelineRun": {pipelineRunName}, "TaskRun": trName}
// Expected failure events: 1 for the pipelinerun cancel, 1 for each TaskRun
expectedNumberOfEvents := 1 + len(trName)
matchKinds := map[string][]string{"PipelineRun": {pipelineRunName}}
// Expected failure events: 1 for the pipelinerun cancel
expectedNumberOfEvents := 1
t.Logf("Making sure %d events were created from pipelinerun with kinds %v", expectedNumberOfEvents, matchKinds)
events, err := collectMatchingEvents(ctx, c.KubeClient, namespace, matchKinds, "Failed")
if err != nil {
t.Fatalf("Failed to collect matching events: %q", err)
}
if len(events) != expectedNumberOfEvents {
if len(events) < expectedNumberOfEvents {
collectedEvents := ""
for i, event := range events {
collectedEvents += fmt.Sprintf("%#v", event)
if i < (len(events) - 1) {
collectedEvents += ", "
}
}
t.Fatalf("Expected %d number of successful events from pipelinerun and taskrun but got %d; list of received events : %#v", expectedNumberOfEvents, len(events), collectedEvents)
t.Fatalf("Expected %d number of failed events from pipelinerun but got %d; list of received events : %#v", expectedNumberOfEvents, len(events), collectedEvents)
}
matchKinds = map[string][]string{"TaskRun": trName}
// Expected failure events: 1 for each TaskRun
expectedNumberOfEvents = len(trName)
t.Logf("Making sure %d events were created from taskruns with kinds %v", expectedNumberOfEvents, matchKinds)
events, err = collectMatchingEvents(ctx, c.KubeClient, namespace, matchKinds, "Failed")
if err != nil {
t.Fatalf("Failed to collect matching events: %q", err)
}
if len(events) < expectedNumberOfEvents {
collectedEvents := ""
for i, event := range events {
collectedEvents += fmt.Sprintf("%#v", event)
if i < (len(events) - 1) {
collectedEvents += ", "
}
}
t.Fatalf("Expected %d number of failed events from taskrun but got %d; list of received events : %#v", expectedNumberOfEvents, len(events), collectedEvents)
}
})
}
Expand Down

0 comments on commit 037ddf7

Please sign in to comment.