Skip to content

Commit

Permalink
Add literal event comparison support
Browse files Browse the repository at this point in the history
For those who are writing unit tests for the first time, they may
instinctively copy the event information from the error message.
However, if the error message contains special characters that are
not properly escaped, the unit test will fail.
This requires the user to examine the source code to understand they
reason For the failure.
With this optimization, it could be more beginner-friendly.
  • Loading branch information
l-qing committed Mar 5, 2023
1 parent eb0486a commit eb29c6d
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 0 deletions.
18 changes: 18 additions & 0 deletions pkg/reconciler/events/k8sevent/event_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,24 @@ func TestEmitK8sEventsOnConditions(t *testing.T) {
Status: corev1.ConditionFalse,
},
wantEvents: []string{"Warning Failed "},
}, {
name: "condition message with wildcard character",
before: nil,
after: &apis.Condition{
Type: apis.ConditionSucceeded,
Status: corev1.ConditionFalse,
Message: "contains a * character",
},
wantEvents: []string{"Warning Failed contains a \\* character"},
}, {
name: "condition message with literal wildcard character",
before: nil,
after: &apis.Condition{
Type: apis.ConditionSucceeded,
Status: corev1.ConditionFalse,
Message: "contains a * character",
},
wantEvents: []string{"Warning Failed contains a * character"},
}}

for _, ts := range testcases {
Expand Down
5 changes: 5 additions & 0 deletions pkg/reconciler/events/k8sevent/events.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,11 @@ func eventsFromChannel(c chan string, wantEvents []string) error {
case event := <-c:
foundEvents = append(foundEvents, event)
wantEvent := wantEvents[ii]
// If the event is an exact match, there is no need to use regular expressions for matching.
// This can avoid the need to escape special characters, such as *, in the event to match.
if wantEvent == event {
continue
}
matching, err := regexp.MatchString(wantEvent, event)
if err == nil {
if !matching {
Expand Down

0 comments on commit eb29c6d

Please sign in to comment.