From b5a86311807c1ab297aa5cb6e6078dfecb8f7a33 Mon Sep 17 00:00:00 2001 From: Tom Pantelis Date: Mon, 30 Oct 2023 19:16:25 -0400 Subject: [PATCH] Fix panic due to invalid nil check This code results in a panic if the underlying value of 'result' is nil: func(result interface{}) (bool, string, error) { if result == nil { return false, "gateway not found yet", nil } gw := result.(*unstructured.Unstructured) haStatus := NestedString(gw.Object, "status", "haStatus") => PANIC ... }) But it first checks if 'result' is nil so how can this be? A simple nil check does not work because interfaces in Go contain both a type and a value. In this case, we want to check for nil after casting. Signed-off-by: Tom Pantelis --- test/e2e/framework/clusterglobalegressip.go | 5 +++-- test/e2e/framework/framework.go | 8 ++------ test/e2e/framework/gateways.go | 8 ++++---- test/e2e/framework/globalingressips.go | 5 +++-- test/e2e/framework/pods.go | 4 ++-- 5 files changed, 14 insertions(+), 16 deletions(-) diff --git a/test/e2e/framework/clusterglobalegressip.go b/test/e2e/framework/clusterglobalegressip.go index 66f231c7e..d0a67aa13 100644 --- a/test/e2e/framework/clusterglobalegressip.go +++ b/test/e2e/framework/clusterglobalegressip.go @@ -52,11 +52,12 @@ func AwaitAllocatedEgressIPs(client dynamic.ResourceInterface, name string) []st return resGip, err }, func(result interface{}) (bool, string, error) { - if result == nil { + obj := result.(*unstructured.Unstructured) + if obj == nil { return false, fmt.Sprintf("Egress IP resource %q not found yet", name), nil } - globalIPs := getGlobalIPs(result.(*unstructured.Unstructured)) + globalIPs := getGlobalIPs(obj) if len(globalIPs) == 0 { return false, fmt.Sprintf("Egress IP resource %q exists but allocatedIPs not available yet", name), nil } diff --git a/test/e2e/framework/framework.go b/test/e2e/framework/framework.go index 618030c74..24cd2130a 100644 --- a/test/e2e/framework/framework.go +++ b/test/e2e/framework/framework.go @@ -287,12 +287,8 @@ func DetectGlobalnet() { } return clusters, err }, func(result interface{}) (bool, string, error) { - if result == nil { - return false, "No Cluster found", nil - } - clusterList := result.(*unstructured.UnstructuredList) - if len(clusterList.Items) == 0 { + if clusterList == nil || len(clusterList.Items) == 0 { return false, "No Cluster found", nil } @@ -342,7 +338,7 @@ func fetchClusterIDs() { return ds, err }, func(result interface{}) (bool, string, error) { - if result == nil { + if result.(*appsv1.DaemonSet) == nil { return false, "No DaemonSet found", nil } diff --git a/test/e2e/framework/gateways.go b/test/e2e/framework/gateways.go index 7679bfcd5..37ac23d45 100644 --- a/test/e2e/framework/gateways.go +++ b/test/e2e/framework/gateways.go @@ -59,11 +59,11 @@ func (f *Framework) AwaitGatewayWithStatus(cluster ClusterIndex, name, status st return findGateway(cluster, name) }, func(result interface{}) (bool, string, error) { - if result == nil { + gw := result.(*unstructured.Unstructured) + if gw == nil { return false, "gateway not found yet", nil } - gw := result.(*unstructured.Unstructured) haStatus := NestedString(gw.Object, "status", "haStatus") if haStatus != status { return false, fmt.Sprintf("gateway %q exists but has wrong status %q, expected %q", gw.GetName(), haStatus, status), nil @@ -118,11 +118,11 @@ func (f *Framework) AwaitGatewayFullyConnected(cluster ClusterIndex, name string return findGateway(cluster, name) }, func(result interface{}) (bool, string, error) { - if result == nil { + gw := result.(*unstructured.Unstructured) + if gw == nil { return false, "gateway not found yet", nil } - gw := result.(*unstructured.Unstructured) haStatus := NestedString(gw.Object, "status", "haStatus") if haStatus != "active" { return false, fmt.Sprintf("Gateway %q exists but not active yet", diff --git a/test/e2e/framework/globalingressips.go b/test/e2e/framework/globalingressips.go index f47675d3e..0ea49078f 100644 --- a/test/e2e/framework/globalingressips.go +++ b/test/e2e/framework/globalingressips.go @@ -47,11 +47,12 @@ func (f *Framework) AwaitGlobalIngressIP(cluster ClusterIndex, name, namespace s return resGip, err }, func(result interface{}) (bool, string, error) { - if result == nil { + obj := result.(*unstructured.Unstructured) + if obj == nil { return false, fmt.Sprintf("GlobalEgressIP %s not found yet", name), nil } - globalIP := getGlobalIP(result.(*unstructured.Unstructured)) + globalIP := getGlobalIP(obj) if globalIP == "" { return false, fmt.Sprintf("GlobalIngress %q exists but allocatedIP not available yet", name), nil diff --git a/test/e2e/framework/pods.go b/test/e2e/framework/pods.go index eff6f01be..63b1a8c7d 100644 --- a/test/e2e/framework/pods.go +++ b/test/e2e/framework/pods.go @@ -116,11 +116,11 @@ func (f *Framework) AwaitUntilAnnotationOnPod(cluster ClusterIndex, annotation, } return pod, err }, func(result interface{}) (bool, string, error) { - if result == nil { + pod := result.(*v1.Pod) + if pod == nil { return false, "No Pod found", nil } - pod := result.(*v1.Pod) if pod.GetAnnotations()[annotation] == "" { return false, fmt.Sprintf("Pod %q does not have annotation %q yet", podName, annotation), nil }