Skip to content

Commit

Permalink
GatewayAPI e2e tests
Browse files Browse the repository at this point in the history
Signed-off-by: Jan Wozniak <wozniak.jan@gmail.com>
  • Loading branch information
wozniakjan committed May 27, 2024
1 parent 30e7571 commit c4e5571
Show file tree
Hide file tree
Showing 2 changed files with 276 additions and 0 deletions.
236 changes: 236 additions & 0 deletions tests/checks/httproute_in_app_namespace/httproute_in_app_namespace.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,236 @@
//go:build e2e
// +build e2e

package ingress_in_app_namespace_test

import (
"fmt"
"testing"

"github.com/stretchr/testify/assert"
"k8s.io/client-go/kubernetes"

. "github.com/kedacore/http-add-on/tests/helper"
)

const (
testName = "ingress-in-app-namespace-test"
)

var (
testNamespace = fmt.Sprintf("%s-ns", testName)
deploymentName = fmt.Sprintf("%s-deployment", testName)
serviceName = fmt.Sprintf("%s-service", testName)
httprouteName = fmt.Sprintf("%s-httproute", testName)
httpScaledObjectName = fmt.Sprintf("%s-http-so", testName)
referenceGrantName = fmt.Sprintf("%s-rg", testName)
gatewayHost = "http://envoy-gateway.envoy-gateway-system.svc.cluster.local"
host = testName
minReplicaCount = 0
maxReplicaCount = 1
)

type templateData struct {
TestNamespace string
DeploymentName string
ServiceName string
HTTPRouteName string
ReferenceGrantName string
KEDANamespace string
GatewayHost string
HTTPScaledObjectName string
Host string
MinReplicas int
MaxReplicas int
}

const (
httprouteTemplate = `
apiVersion: gateway.networking.k8s.io/v1
kind: HTTPRoute
metadata:
name: {{.HTTPRouteName}}
namespace: {{.TestNamespace}}
spec:
parentRefs:
- name: eg
namespace: envoy-gateway-system
hostnames:
- {{.Host}}
rules:
- backendRefs:
- kind: Service
name: {{ .ServiceName }}
namespace: keda
port: 8080
matches:
- path:
type: PathPrefix
value: /
`

referenceGrantTemplate = `
apiVersion: gateway.networking.k8s.io/v1
kind: ReferenceGrant
metadata:
name: {{.ReferenceGrantName}}
namespace: {{.TestNamespace}}
spec:
from:
- group: gateway.networking.k8s.io
kind: HTTPRoute
namespace: {{.TestNamespace}}
to:
- group: ""
kind: Service
name: keda-http-add-on-interceptor-proxy
`
serviceTemplate = `
apiVersion: v1
kind: Service
metadata:
name: {{.ServiceName}}
namespace: {{.TestNamespace}}
labels:
app: {{.DeploymentName}}
spec:
ports:
- port: 8080
targetPort: http
protocol: TCP
name: http
selector:
app: {{.DeploymentName}}
`

deploymentTemplate = `
apiVersion: apps/v1
kind: Deployment
metadata:
name: {{.DeploymentName}}
namespace: {{.TestNamespace}}
labels:
app: {{.DeploymentName}}
spec:
replicas: 0
selector:
matchLabels:
app: {{.DeploymentName}}
template:
metadata:
labels:
app: {{.DeploymentName}}
spec:
containers:
- name: {{.DeploymentName}}
image: registry.k8s.io/e2e-test-images/agnhost:2.45
args:
- netexec
ports:
- name: http
containerPort: 8080
protocol: TCP
readinessProbe:
httpGet:
path: /
port: http
`

loadJobTemplate = `
apiVersion: batch/v1
kind: Job
metadata:
name: generate-request
namespace: {{.TestNamespace}}
spec:
template:
spec:
containers:
- name: curl-client
image: curlimages/curl
imagePullPolicy: Always
command: ["curl", "-H", "Host: {{.Host}}", "{{.GatewayHost}}"]
restartPolicy: Never
activeDeadlineSeconds: 600
backoffLimit: 5
`

httpScaledObjectTemplate = `
kind: HTTPScaledObject
apiVersion: http.keda.sh/v1alpha1
metadata:
name: {{.HTTPScaledObjectName}}
namespace: {{.TestNamespace}}
spec:
hosts:
- {{.Host}}
targetPendingRequests: 100
scaledownPeriod: 10
scaleTargetRef:
deployment: {{.DeploymentName}}
service: {{.ServiceName}}
port: 8080
replicas:
min: {{ .MinReplicas }}
max: {{ .MaxReplicas }}
`
)

func TestCheck(t *testing.T) {
// setup
t.Log("--- setting up ---")
// Create kubernetes resources
kc := GetKubernetesClient(t)
data, templates := getTemplateData()
CreateKubernetesResources(t, kc, testNamespace, data, templates)

assert.True(t, WaitForDeploymentReplicaReadyCount(t, kc, deploymentName, testNamespace, minReplicaCount, 6, 10),
"replica count should be %d after 1 minutes", minReplicaCount)
assert.True(t, WaitForIngressReady(t, kc, httprouteName, testNamespace, 12, 10),
"ingress should be ready after 2 minutes")

testScaleOut(t, kc, data)
testScaleIn(t, kc, data)

// cleanup
DeleteKubernetesResources(t, testNamespace, data, templates)
}

func testScaleOut(t *testing.T, kc *kubernetes.Clientset, data templateData) {
t.Log("--- testing scale out ---")

KubectlApplyWithTemplate(t, data, "loadJobTemplate", loadJobTemplate)

assert.True(t, WaitForDeploymentReplicaReadyCount(t, kc, deploymentName, testNamespace, maxReplicaCount, 6, 10),
"replica count should be %d after 1 minutes", maxReplicaCount)
}

func testScaleIn(t *testing.T, kc *kubernetes.Clientset, data templateData) {
t.Log("--- testing scale out ---")

KubectlDeleteWithTemplate(t, data, "loadJobTemplate", loadJobTemplate)
assert.True(t, WaitForDeploymentReplicaReadyCount(t, kc, deploymentName, testNamespace, minReplicaCount, 12, 10),
"replica count should be %d after 2 minutes", minReplicaCount)
}

func getTemplateData() (templateData, []Template) {
return templateData{
TestNamespace: testNamespace,
DeploymentName: deploymentName,
ServiceName: serviceName,
HTTPRouteName: httprouteName,
ReferenceGrantName: referenceGrantName,
KEDANamespace: KEDANamespace,
HTTPScaledObjectName: httpScaledObjectName,
GatewayHost: gatewayHost,
Host: host,
MinReplicas: minReplicaCount,
MaxReplicas: maxReplicaCount,
}, []Template{
{Name: "deploymentTemplate", Config: deploymentTemplate},
{Name: "serviceTemplate", Config: serviceTemplate},
{Name: "httprouteTemplate", Config: httprouteTemplate},
{Name: "httpScaledObjectTemplate", Config: httpScaledObjectTemplate},
{Name: "referenceGrantTemplate", Config: referenceGrantTemplate},
}
}
40 changes: 40 additions & 0 deletions tests/utils/setup_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,32 @@ spec:
name: prometheus
protocol: TCP
`

gatewayClass = `
apiVersion: gateway.networking.k8s.io/v1
kind: GatewayClass
metadata:
name: eg
spec:
controllerName: gateway.envoyproxy.io/gatewayclass-controller
`

gateway = `
apiVersion: gateway.networking.k8s.io/v1
kind: Gateway
metadata:
name: eg
namespace: envoy-gateway-system
spec:
gatewayClassName: eg
listeners:
- name: http
protocol: HTTP
port: 80
allowedRoutes:
namespaces:
from: All
`
)

func TestVerifyCommands(t *testing.T) {
Expand Down Expand Up @@ -149,6 +175,20 @@ func TestSetupIngress(t *testing.T) {
require.NoErrorf(t, err, "cannot install ingress - %s", err)
}

func TestSetupEnvoyGateway(t *testing.T) {
KubeClient = GetKubernetesClient(t)
_, err := ExecuteCommand("helm version")
require.NoErrorf(t, err, "helm is not installed - %s", err)

_, err = ExecuteCommand("helm install eg oci://docker.io/envoyproxy/gateway-helm --version v1.0.1 -n envoy-gateway-system --create-namespace")
require.NoErrorf(t, err, "cannot install envoy gateway - %s", err)

assert.True(t, WaitForDeploymentReplicaReadyCount(t, KubeClient, "envoy-gateway", "envoy-gateway-system", 1, 30, 6))

KubectlApplyWithTemplate(t, nil, "gatewayClass", gatewayClass)
KubectlApplyWithTemplate(t, nil, "gateway", gateway)
}

func TestSetupKEDA(t *testing.T) {
_, err := ExecuteCommand("helm version")
require.NoErrorf(t, err, "helm is not installed - %s", err)
Expand Down

0 comments on commit c4e5571

Please sign in to comment.