From 08a3fef7a4d0e040b06724237d284273b9101c55 Mon Sep 17 00:00:00 2001 From: David Cheung Date: Tue, 17 Sep 2024 00:25:51 +0000 Subject: [PATCH] Move test variables as const and var. --- .../providers/gce/gateway_converter_test.go | 64 ++++++++++--------- pkg/i2gw/providers/gce/ir_converter_test.go | 33 ++++++---- 2 files changed, 54 insertions(+), 43 deletions(-) diff --git a/pkg/i2gw/providers/gce/gateway_converter_test.go b/pkg/i2gw/providers/gce/gateway_converter_test.go index b0f0aa4f..5809d8d3 100644 --- a/pkg/i2gw/providers/gce/gateway_converter_test.go +++ b/pkg/i2gw/providers/gce/gateway_converter_test.go @@ -35,20 +35,14 @@ import ( "sigs.k8s.io/gateway-api/apis/v1alpha2" ) -func Test_irToGateway(t *testing.T) { - testNamespace := "default" - testHost := "test.mydomain.com" - testServiceName := "test-service" - testGatewayName := "test-gateway" - testHTTPRouteName := "test-http-route" - - gPathPrefix := gatewayv1.PathMatchPathPrefix - saTypeClientIP := "CLIENT_IP" - testCookieTTLSec := int64(10) - saTypeCookie := "GENERATED_COOKIE" - testSecurityPolicy := "test-security-policy" +const ( + testGatewayName = "test-gateway" + testHTTPRouteName = "test-http-route" + testSaBackendPolicyName = testServiceName +) - testGateway := gatewayv1.Gateway{ +var ( + testGateway = gatewayv1.Gateway{ ObjectMeta: metav1.ObjectMeta{Name: testGatewayName, Namespace: testNamespace}, Spec: gatewayv1.GatewaySpec{ GatewayClassName: gceL7GlobalExternalManagedGatewayClass, @@ -61,7 +55,7 @@ func Test_irToGateway(t *testing.T) { }, } - testHTTPRoute := gatewayv1.HTTPRoute{ + testHTTPRoute = gatewayv1.HTTPRoute{ ObjectMeta: metav1.ObjectMeta{Name: testHTTPRouteName, Namespace: testNamespace}, Spec: gatewayv1.HTTPRouteSpec{ CommonRouteSpec: gatewayv1.CommonRouteSpec{ @@ -95,8 +89,11 @@ func Test_irToGateway(t *testing.T) { }, } - testSaBackendPolicyName := testServiceName - testSaGCPBackendPolicyCookie := gkegatewayv1.GCPBackendPolicy{ + testSaGCPBackendPolicyCookie = gkegatewayv1.GCPBackendPolicy{ + TypeMeta: metav1.TypeMeta{ + APIVersion: "networking.gke.io/v1", + Kind: "GCPBackendPolicy", + }, ObjectMeta: metav1.ObjectMeta{ Namespace: testNamespace, Name: testSaBackendPolicyName, @@ -115,13 +112,12 @@ func Test_irToGateway(t *testing.T) { }, }, } - testSaGCPBackendPolicyCookie.SetGroupVersionKind(GCPBackendPolicyGVK) - testSaGCPBackendPolicyCookieUnstructured, err := i2gw.CastToUnstructured(&testSaGCPBackendPolicyCookie) - if err != nil { - t.Errorf("Failed to generate unstructured GCP Backend Policy with Cookie-based session affinity feature: %v", err) - } - testSaGCPBackendPolicyClientIP := gkegatewayv1.GCPBackendPolicy{ + testSaGCPBackendPolicyClientIP = gkegatewayv1.GCPBackendPolicy{ + TypeMeta: metav1.TypeMeta{ + APIVersion: "networking.gke.io/v1", + Kind: "GCPBackendPolicy", + }, ObjectMeta: metav1.ObjectMeta{ Namespace: testNamespace, Name: testSaBackendPolicyName, @@ -139,13 +135,12 @@ func Test_irToGateway(t *testing.T) { }, }, } - testSaGCPBackendPolicyClientIP.SetGroupVersionKind(GCPBackendPolicyGVK) - testSaGCPBackendPolicyClientIPUnstructured, err := i2gw.CastToUnstructured(&testSaGCPBackendPolicyClientIP) - if err != nil { - t.Errorf("Failed to generate unstructured GCP Backend Policy with ClientIP-based session affinity feature: %v", err) - } - testSpGCPBackendPolicy := gkegatewayv1.GCPBackendPolicy{ + testSpGCPBackendPolicy = gkegatewayv1.GCPBackendPolicy{ + TypeMeta: metav1.TypeMeta{ + APIVersion: "networking.gke.io/v1", + Kind: "GCPBackendPolicy", + }, ObjectMeta: metav1.ObjectMeta{ Namespace: testNamespace, Name: testSaBackendPolicyName, @@ -161,11 +156,22 @@ func Test_irToGateway(t *testing.T) { }, }, } - testSpGCPBackendPolicy.SetGroupVersionKind(GCPBackendPolicyGVK) +) + +func Test_irToGateway(t *testing.T) { + testSaGCPBackendPolicyCookieUnstructured, err := i2gw.CastToUnstructured(&testSaGCPBackendPolicyCookie) + if err != nil { + t.Errorf("Failed to generate unstructured GCP Backend Policy with Cookie-based session affinity feature: %v", err) + } + testSaGCPBackendPolicyClientIPUnstructured, err := i2gw.CastToUnstructured(&testSaGCPBackendPolicyClientIP) + if err != nil { + t.Errorf("Failed to generate unstructured GCP Backend Policy with ClientIP-based session affinity feature: %v", err) + } testSpGCPBackendPolicyUnstructured, err := i2gw.CastToUnstructured(&testSpGCPBackendPolicy) if err != nil { t.Errorf("Failed to generate unstructured GCP Backend Policy with Security Policy feature: %v", err) } + testCases := []struct { name string ir intermediate.IR diff --git a/pkg/i2gw/providers/gce/ir_converter_test.go b/pkg/i2gw/providers/gce/ir_converter_test.go index 25a75c36..68176970 100644 --- a/pkg/i2gw/providers/gce/ir_converter_test.go +++ b/pkg/i2gw/providers/gce/ir_converter_test.go @@ -38,26 +38,31 @@ import ( gatewayv1 "sigs.k8s.io/gateway-api/apis/v1" ) -func Test_convertToIR(t *testing.T) { - testNamespace := "default" - testHost := "test.mydomain.com" - testServiceName := "test-service" - testBackendConfigName := "test-backendconfig" - iPrefix := networkingv1.PathTypePrefix - implSpecificPathType := networkingv1.PathTypeImplementationSpecific +const ( + testNamespace = "default" + testHost = "test.mydomain.com" + testServiceName = "test-service" + testBackendConfigName = "test-backendconfig" + + saTypeClientIP = "CLIENT_IP" + saTypeCookie = "GENERATED_COOKIE" +) - gPathPrefix := gatewayv1.PathMatchPathPrefix - gExact := gatewayv1.PathMatchExact +var ( + // These variables are referenced by pointers. + testSecurityPolicy = "test-security-policy" + testCookieTTLSec = int64(10) + iPrefix = networkingv1.PathTypePrefix + gPathPrefix = gatewayv1.PathMatchPathPrefix + gExact = gatewayv1.PathMatchExact + implSpecificPathType = networkingv1.PathTypeImplementationSpecific +) +func Test_convertToIR(t *testing.T) { extIngClassIngressName := "gce-ingress-class" intIngClassIngressName := "gce-internal-ingress-class" noIngClassIngressName := "no-ingress-class" - saTypeClientIP := "CLIENT_IP" - testCookieTTLSec := int64(10) - saTypeCookie := "GENERATED_COOKIE" - testSecurityPolicy := "test-security-policy" - testExtIngress := getTestIngress(testNamespace, extIngClassIngressName, testServiceName, true) testIntIngress := getTestIngress(testNamespace, intIngClassIngressName, testServiceName, false)