From 0fb1f59a881274797103beb5304283b0317b5e38 Mon Sep 17 00:00:00 2001 From: Quan Tian Date: Fri, 1 Sep 2023 23:18:45 +0800 Subject: [PATCH] Improve service unit test failure report Mark the helper functions with t.Helper() so that if t.Errorf() in these functions gets called, it will report that the failure occurred on the line number of the caller of the helper, rather than the line number of the helper itself, which makes it hard to identify which case causes the failure. Signed-off-by: Quan Tian --- pkg/api/service/util_test.go | 6 ++++++ pkg/api/v1/service/util_test.go | 7 +++++++ 2 files changed, 13 insertions(+) diff --git a/pkg/api/service/util_test.go b/pkg/api/service/util_test.go index a0fc76861b5..d562a4d2481 100644 --- a/pkg/api/service/util_test.go +++ b/pkg/api/service/util_test.go @@ -26,6 +26,7 @@ import ( func TestGetLoadBalancerSourceRanges(t *testing.T) { checkError := func(v string) { + t.Helper() annotations := make(map[string]string) annotations[api.AnnotationLoadBalancerSourceRangesKey] = v svc := api.Service{} @@ -49,6 +50,7 @@ func TestGetLoadBalancerSourceRanges(t *testing.T) { checkError("10.0.0.1") checkOK := func(v string) utilnet.IPNetSet { + t.Helper() annotations := make(map[string]string) annotations[api.AnnotationLoadBalancerSourceRangesKey] = v svc := api.Service{} @@ -112,6 +114,7 @@ func TestGetLoadBalancerSourceRanges(t *testing.T) { func TestAllowAll(t *testing.T) { checkAllowAll := func(allowAll bool, cidrs ...string) { + t.Helper() ipnets, err := utilnet.ParseIPNets(cidrs...) if err != nil { t.Errorf("Unexpected error parsing cidrs: %v", cidrs) @@ -131,6 +134,7 @@ func TestAllowAll(t *testing.T) { func TestExternallyAccessible(t *testing.T) { checkExternallyAccessible := func(expect bool, service *api.Service) { + t.Helper() res := ExternallyAccessible(service) if res != expect { t.Errorf("Expected ExternallyAccessible = %v, got %v", expect, res) @@ -174,6 +178,7 @@ func TestExternallyAccessible(t *testing.T) { func TestRequestsOnlyLocalTraffic(t *testing.T) { checkRequestsOnlyLocalTraffic := func(requestsOnlyLocalTraffic bool, service *api.Service) { + t.Helper() res := RequestsOnlyLocalTraffic(service) if res != requestsOnlyLocalTraffic { t.Errorf("Expected requests OnlyLocal traffic = %v, got %v", @@ -220,6 +225,7 @@ func TestRequestsOnlyLocalTraffic(t *testing.T) { func TestNeedsHealthCheck(t *testing.T) { checkNeedsHealthCheck := func(needsHealthCheck bool, service *api.Service) { + t.Helper() res := NeedsHealthCheck(service) if res != needsHealthCheck { t.Errorf("Expected needs health check = %v, got %v", diff --git a/pkg/api/v1/service/util_test.go b/pkg/api/v1/service/util_test.go index 5d9f02bc5de..988b69a244b 100644 --- a/pkg/api/v1/service/util_test.go +++ b/pkg/api/v1/service/util_test.go @@ -26,6 +26,7 @@ import ( func TestGetLoadBalancerSourceRanges(t *testing.T) { checkError := func(v string) { + t.Helper() annotations := make(map[string]string) annotations[v1.AnnotationLoadBalancerSourceRangesKey] = v svc := v1.Service{} @@ -49,6 +50,7 @@ func TestGetLoadBalancerSourceRanges(t *testing.T) { checkError("10.0.0.1") checkOK := func(v string) utilnet.IPNetSet { + t.Helper() annotations := make(map[string]string) annotations[v1.AnnotationLoadBalancerSourceRangesKey] = v svc := v1.Service{} @@ -112,6 +114,7 @@ func TestGetLoadBalancerSourceRanges(t *testing.T) { func TestAllowAll(t *testing.T) { checkAllowAll := func(allowAll bool, cidrs ...string) { + t.Helper() ipnets, err := utilnet.ParseIPNets(cidrs...) if err != nil { t.Errorf("Unexpected error parsing cidrs: %v", cidrs) @@ -131,6 +134,7 @@ func TestAllowAll(t *testing.T) { func TestExternallyAccessible(t *testing.T) { checkExternallyAccessible := func(expect bool, service *v1.Service) { + t.Helper() res := ExternallyAccessible(service) if res != expect { t.Errorf("Expected ExternallyAccessible = %v, got %v", expect, res) @@ -174,6 +178,7 @@ func TestExternallyAccessible(t *testing.T) { func TestExternalPolicyLocal(t *testing.T) { checkExternalPolicyLocal := func(requestsOnlyLocalTraffic bool, service *v1.Service) { + t.Helper() res := ExternalPolicyLocal(service) if res != requestsOnlyLocalTraffic { t.Errorf("Expected requests OnlyLocal traffic = %v, got %v", @@ -240,6 +245,7 @@ func TestExternalPolicyLocal(t *testing.T) { func TestNeedsHealthCheck(t *testing.T) { checkNeedsHealthCheck := func(needsHealthCheck bool, service *v1.Service) { + t.Helper() res := NeedsHealthCheck(service) if res != needsHealthCheck { t.Errorf("Expected needs health check = %v, got %v", @@ -280,6 +286,7 @@ func TestNeedsHealthCheck(t *testing.T) { func TestInternalPolicyLocal(t *testing.T) { checkInternalPolicyLocal := func(expected bool, service *v1.Service) { + t.Helper() res := InternalPolicyLocal(service) if res != expected { t.Errorf("Expected internal local traffic = %v, got %v",