Skip to content

Commit 350792f

Browse files
committed
fix: improve unexpected event filtering in tests
Refactored the event checking logic to specifically target and report warning events instead of filtering out individual event reasons. This ensures that unexpected errors are correctly identified during test validation.
1 parent 29725a3 commit 350792f

File tree

1 file changed

+16
-10
lines changed

1 file changed

+16
-10
lines changed

test/pkg/gitea/test.go

Lines changed: 16 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -78,24 +78,30 @@ func PostCommentOnPullRequest(t *testing.T, topt *TestOpts, body string) {
7878

7979
func checkEvents(t *testing.T, events *corev1.EventList, topts *TestOpts) {
8080
t.Helper()
81-
newEvents := make([]corev1.Event, 0)
82-
// filter out events that are not related to the test like checking for cancelled pipelineruns
83-
for i := len(events.Items) - 1; i >= 0; i-- {
81+
unexpected := unexpectedEvents(events)
82+
for i := range events.Items {
8483
topts.ParamsRun.Clients.Log.Infof("Reason is %s", events.Items[i].Reason)
85-
if events.Items[i].Reason == "CancelInProgress" {
86-
continue
87-
}
88-
newEvents = append(newEvents, events.Items[i])
8984
}
90-
if len(newEvents) > 0 {
91-
topts.ParamsRun.Clients.Log.Infof("0 events expected in case of failure but got %d", len(newEvents))
92-
for _, em := range newEvents {
85+
if len(unexpected) > 0 {
86+
topts.ParamsRun.Clients.Log.Infof("0 warning events expected in case of failure but got %d", len(unexpected))
87+
for _, em := range unexpected {
9388
topts.ParamsRun.Clients.Log.Infof("Event: Reason: %s Type: %s ReportingInstance: %s Message: %s", em.Reason, em.Type, em.ReportingInstance, em.Message)
9489
}
9590
t.FailNow()
9691
}
9792
}
9893

94+
func unexpectedEvents(events *corev1.EventList) []corev1.Event {
95+
unexpected := make([]corev1.Event, 0)
96+
for i := len(events.Items) - 1; i >= 0; i-- {
97+
if events.Items[i].Type == corev1.EventTypeNormal {
98+
continue
99+
}
100+
unexpected = append(unexpected, events.Items[i])
101+
}
102+
return unexpected
103+
}
104+
99105
func AddLabelToIssue(t *testing.T, topt *TestOpts, label string) {
100106
var targetID int64
101107
allLabels, _, err := topt.GiteaCNX.Client().ListRepoLabels(topt.Opts.Organization, topt.Opts.Repo, forgejo.ListLabelsOptions{})

0 commit comments

Comments
 (0)