Skip to content

Commit 4b440e5

Browse files
Fix test flakiness on TestSameService (#833)
The code added on #818 depends on map sorting to return a static reason for service annotation changes. To avoid tests flakiness and map sorting the tests include a `strings.HasPrefix` instead of comparing the whole string. One of the test cases, `service_removes_a_custom_annotation,_adds_a_new_one_and_change_another`, is trying to test the whole reason string. This commit replaces the test case reason, for only the reason prefix. It removes the flakiness from the tests. As all the cases (annotation adding, removing and value changing) are tested before, it's safe to test only prefixes. Also, it renames the test name from `TestServiceAnnotations` to `TestSameService` and introduces a better description in case of test failure, describing that only prefixes are tested.
1 parent 702a194 commit 4b440e5

File tree

1 file changed

+5
-4
lines changed

1 file changed

+5
-4
lines changed

pkg/util/k8sutil/k8sutil_test.go

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ func newsService(ann map[string]string, svcT v1.ServiceType, lbSr []string) *v1.
2020
return svc
2121
}
2222

23-
func TestServiceAnnotations(t *testing.T) {
23+
func TestSameService(t *testing.T) {
2424
tests := []struct {
2525
about string
2626
current *v1.Service
@@ -267,8 +267,9 @@ func TestServiceAnnotations(t *testing.T) {
267267
},
268268
v1.ServiceTypeLoadBalancer,
269269
[]string{"128.141.0.0/16", "137.138.0.0/16"}),
270-
match: false,
271-
reason: `new service's annotations doesn't match the current one: Removed 'foo'. Added 'bar' with value 'foo'. 'zalan' changed from 'do' to 'do.com'`,
270+
match: false,
271+
// Test just the prefix to avoid flakiness and map sorting
272+
reason: `new service's annotations doesn't match the current one: Removed 'foo'.`,
272273
},
273274
{
274275
about: "service add annotations",
@@ -301,7 +302,7 @@ func TestServiceAnnotations(t *testing.T) {
301302
}
302303
if !match && !tt.match {
303304
if !strings.HasPrefix(reason, tt.reason) {
304-
t.Errorf("expected reason '%s', found '%s'", tt.reason, reason)
305+
t.Errorf("expected reason prefix '%s', found '%s'", tt.reason, reason)
305306
return
306307
}
307308
}

0 commit comments

Comments
 (0)