Skip to content

Commit

Permalink
Move test variables as const and var.
Browse files Browse the repository at this point in the history
  • Loading branch information
sawsa307 committed Sep 19, 2024
1 parent 696e1eb commit 906c909
Show file tree
Hide file tree
Showing 2 changed files with 101 additions and 98 deletions.
82 changes: 44 additions & 38 deletions pkg/i2gw/providers/gce/gateway_converter_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,33 +35,27 @@ 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,
Listeners: []gatewayv1.Listener{{
Name: "test-mydomain-com-http",
Port: 80,
Protocol: gatewayv1.HTTPProtocolType,
Hostname: ptrTo(gatewayv1.Hostname(testHost)),
Hostname: common.PtrTo(gatewayv1.Hostname(testHost)),
}},
},
}

testHTTPRoute := gatewayv1.HTTPRoute{
testHTTPRoute = gatewayv1.HTTPRoute{
ObjectMeta: metav1.ObjectMeta{Name: testHTTPRouteName, Namespace: testNamespace},
Spec: gatewayv1.HTTPRouteSpec{
CommonRouteSpec: gatewayv1.CommonRouteSpec{
Expand All @@ -75,8 +69,8 @@ func Test_irToGateway(t *testing.T) {
Matches: []gatewayv1.HTTPRouteMatch{
{
Path: &gatewayv1.HTTPPathMatch{
Type: &gPathPrefix,
Value: ptrTo("/"),
Type: common.PtrTo(gPathPrefix),
Value: common.PtrTo("/"),
},
},
},
Expand All @@ -85,7 +79,7 @@ func Test_irToGateway(t *testing.T) {
BackendRef: gatewayv1.BackendRef{
BackendObjectReference: gatewayv1.BackendObjectReference{
Name: gatewayv1.ObjectName(testServiceName),
Port: ptrTo(gatewayv1.PortNumber(80)),
Port: common.PtrTo(gatewayv1.PortNumber(80)),
},
},
},
Expand All @@ -95,17 +89,20 @@ 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,
},
Spec: gkegatewayv1.GCPBackendPolicySpec{
Default: &gkegatewayv1.GCPBackendPolicyConfig{
SessionAffinity: &gkegatewayv1.SessionAffinityConfig{
Type: ptrTo("GENERATED_COOKIE"),
CookieTTLSec: &testCookieTTLSec,
Type: common.PtrTo("GENERATED_COOKIE"),
CookieTTLSec: common.PtrTo(testCookieTTLSec),
},
},
TargetRef: v1alpha2.NamespacedPolicyTargetReference{
Expand All @@ -115,21 +112,20 @@ 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,
},
Spec: gkegatewayv1.GCPBackendPolicySpec{
Default: &gkegatewayv1.GCPBackendPolicyConfig{
SessionAffinity: &gkegatewayv1.SessionAffinityConfig{
Type: ptrTo("CLIENT_IP"),
Type: common.PtrTo("CLIENT_IP"),
},
},
TargetRef: v1alpha2.NamespacedPolicyTargetReference{
Expand All @@ -139,20 +135,19 @@ 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,
},
Spec: gkegatewayv1.GCPBackendPolicySpec{
Default: &gkegatewayv1.GCPBackendPolicyConfig{
SecurityPolicy: &testSecurityPolicy,
SecurityPolicy: common.PtrTo(testSecurityPolicy),
},
TargetRef: v1alpha2.NamespacedPolicyTargetReference{
Group: "",
Expand All @@ -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
Expand Down Expand Up @@ -226,7 +232,7 @@ func Test_irToGateway(t *testing.T) {
Gce: &intermediate.GceServiceIR{
SessionAffinity: &intermediate.SessionAffinityConfig{
AffinityType: saTypeCookie,
CookieTTLSec: &testCookieTTLSec,
CookieTTLSec: common.PtrTo(testCookieTTLSec),
},
},
},
Expand Down
Loading

0 comments on commit 906c909

Please sign in to comment.