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 <qtian@vmware.com>
This commit is contained in:
Quan Tian 2023-09-01 23:18:45 +08:00
parent a42f4f61c2
commit 0fb1f59a88
2 changed files with 13 additions and 0 deletions

View File

@ -26,6 +26,7 @@ import (
func TestGetLoadBalancerSourceRanges(t *testing.T) { func TestGetLoadBalancerSourceRanges(t *testing.T) {
checkError := func(v string) { checkError := func(v string) {
t.Helper()
annotations := make(map[string]string) annotations := make(map[string]string)
annotations[api.AnnotationLoadBalancerSourceRangesKey] = v annotations[api.AnnotationLoadBalancerSourceRangesKey] = v
svc := api.Service{} svc := api.Service{}
@ -49,6 +50,7 @@ func TestGetLoadBalancerSourceRanges(t *testing.T) {
checkError("10.0.0.1") checkError("10.0.0.1")
checkOK := func(v string) utilnet.IPNetSet { checkOK := func(v string) utilnet.IPNetSet {
t.Helper()
annotations := make(map[string]string) annotations := make(map[string]string)
annotations[api.AnnotationLoadBalancerSourceRangesKey] = v annotations[api.AnnotationLoadBalancerSourceRangesKey] = v
svc := api.Service{} svc := api.Service{}
@ -112,6 +114,7 @@ func TestGetLoadBalancerSourceRanges(t *testing.T) {
func TestAllowAll(t *testing.T) { func TestAllowAll(t *testing.T) {
checkAllowAll := func(allowAll bool, cidrs ...string) { checkAllowAll := func(allowAll bool, cidrs ...string) {
t.Helper()
ipnets, err := utilnet.ParseIPNets(cidrs...) ipnets, err := utilnet.ParseIPNets(cidrs...)
if err != nil { if err != nil {
t.Errorf("Unexpected error parsing cidrs: %v", cidrs) t.Errorf("Unexpected error parsing cidrs: %v", cidrs)
@ -131,6 +134,7 @@ func TestAllowAll(t *testing.T) {
func TestExternallyAccessible(t *testing.T) { func TestExternallyAccessible(t *testing.T) {
checkExternallyAccessible := func(expect bool, service *api.Service) { checkExternallyAccessible := func(expect bool, service *api.Service) {
t.Helper()
res := ExternallyAccessible(service) res := ExternallyAccessible(service)
if res != expect { if res != expect {
t.Errorf("Expected ExternallyAccessible = %v, got %v", expect, res) t.Errorf("Expected ExternallyAccessible = %v, got %v", expect, res)
@ -174,6 +178,7 @@ func TestExternallyAccessible(t *testing.T) {
func TestRequestsOnlyLocalTraffic(t *testing.T) { func TestRequestsOnlyLocalTraffic(t *testing.T) {
checkRequestsOnlyLocalTraffic := func(requestsOnlyLocalTraffic bool, service *api.Service) { checkRequestsOnlyLocalTraffic := func(requestsOnlyLocalTraffic bool, service *api.Service) {
t.Helper()
res := RequestsOnlyLocalTraffic(service) res := RequestsOnlyLocalTraffic(service)
if res != requestsOnlyLocalTraffic { if res != requestsOnlyLocalTraffic {
t.Errorf("Expected requests OnlyLocal traffic = %v, got %v", t.Errorf("Expected requests OnlyLocal traffic = %v, got %v",
@ -220,6 +225,7 @@ func TestRequestsOnlyLocalTraffic(t *testing.T) {
func TestNeedsHealthCheck(t *testing.T) { func TestNeedsHealthCheck(t *testing.T) {
checkNeedsHealthCheck := func(needsHealthCheck bool, service *api.Service) { checkNeedsHealthCheck := func(needsHealthCheck bool, service *api.Service) {
t.Helper()
res := NeedsHealthCheck(service) res := NeedsHealthCheck(service)
if res != needsHealthCheck { if res != needsHealthCheck {
t.Errorf("Expected needs health check = %v, got %v", t.Errorf("Expected needs health check = %v, got %v",

View File

@ -26,6 +26,7 @@ import (
func TestGetLoadBalancerSourceRanges(t *testing.T) { func TestGetLoadBalancerSourceRanges(t *testing.T) {
checkError := func(v string) { checkError := func(v string) {
t.Helper()
annotations := make(map[string]string) annotations := make(map[string]string)
annotations[v1.AnnotationLoadBalancerSourceRangesKey] = v annotations[v1.AnnotationLoadBalancerSourceRangesKey] = v
svc := v1.Service{} svc := v1.Service{}
@ -49,6 +50,7 @@ func TestGetLoadBalancerSourceRanges(t *testing.T) {
checkError("10.0.0.1") checkError("10.0.0.1")
checkOK := func(v string) utilnet.IPNetSet { checkOK := func(v string) utilnet.IPNetSet {
t.Helper()
annotations := make(map[string]string) annotations := make(map[string]string)
annotations[v1.AnnotationLoadBalancerSourceRangesKey] = v annotations[v1.AnnotationLoadBalancerSourceRangesKey] = v
svc := v1.Service{} svc := v1.Service{}
@ -112,6 +114,7 @@ func TestGetLoadBalancerSourceRanges(t *testing.T) {
func TestAllowAll(t *testing.T) { func TestAllowAll(t *testing.T) {
checkAllowAll := func(allowAll bool, cidrs ...string) { checkAllowAll := func(allowAll bool, cidrs ...string) {
t.Helper()
ipnets, err := utilnet.ParseIPNets(cidrs...) ipnets, err := utilnet.ParseIPNets(cidrs...)
if err != nil { if err != nil {
t.Errorf("Unexpected error parsing cidrs: %v", cidrs) t.Errorf("Unexpected error parsing cidrs: %v", cidrs)
@ -131,6 +134,7 @@ func TestAllowAll(t *testing.T) {
func TestExternallyAccessible(t *testing.T) { func TestExternallyAccessible(t *testing.T) {
checkExternallyAccessible := func(expect bool, service *v1.Service) { checkExternallyAccessible := func(expect bool, service *v1.Service) {
t.Helper()
res := ExternallyAccessible(service) res := ExternallyAccessible(service)
if res != expect { if res != expect {
t.Errorf("Expected ExternallyAccessible = %v, got %v", expect, res) t.Errorf("Expected ExternallyAccessible = %v, got %v", expect, res)
@ -174,6 +178,7 @@ func TestExternallyAccessible(t *testing.T) {
func TestExternalPolicyLocal(t *testing.T) { func TestExternalPolicyLocal(t *testing.T) {
checkExternalPolicyLocal := func(requestsOnlyLocalTraffic bool, service *v1.Service) { checkExternalPolicyLocal := func(requestsOnlyLocalTraffic bool, service *v1.Service) {
t.Helper()
res := ExternalPolicyLocal(service) res := ExternalPolicyLocal(service)
if res != requestsOnlyLocalTraffic { if res != requestsOnlyLocalTraffic {
t.Errorf("Expected requests OnlyLocal traffic = %v, got %v", t.Errorf("Expected requests OnlyLocal traffic = %v, got %v",
@ -240,6 +245,7 @@ func TestExternalPolicyLocal(t *testing.T) {
func TestNeedsHealthCheck(t *testing.T) { func TestNeedsHealthCheck(t *testing.T) {
checkNeedsHealthCheck := func(needsHealthCheck bool, service *v1.Service) { checkNeedsHealthCheck := func(needsHealthCheck bool, service *v1.Service) {
t.Helper()
res := NeedsHealthCheck(service) res := NeedsHealthCheck(service)
if res != needsHealthCheck { if res != needsHealthCheck {
t.Errorf("Expected needs health check = %v, got %v", t.Errorf("Expected needs health check = %v, got %v",
@ -280,6 +286,7 @@ func TestNeedsHealthCheck(t *testing.T) {
func TestInternalPolicyLocal(t *testing.T) { func TestInternalPolicyLocal(t *testing.T) {
checkInternalPolicyLocal := func(expected bool, service *v1.Service) { checkInternalPolicyLocal := func(expected bool, service *v1.Service) {
t.Helper()
res := InternalPolicyLocal(service) res := InternalPolicyLocal(service)
if res != expected { if res != expected {
t.Errorf("Expected internal local traffic = %v, got %v", t.Errorf("Expected internal local traffic = %v, got %v",