diff --git a/test/e2e/test_operator_logs.go b/test/e2e/test_operator_logs.go index 8f109481f..23f30f2e9 100644 --- a/test/e2e/test_operator_logs.go +++ b/test/e2e/test_operator_logs.go @@ -144,7 +144,7 @@ func TestOperatorLogs(t *testing.T) { }() t.Log("deploying a GatewayClass resource") - gatewayClass := helpers.GenerateGatewayClass(nil) + gatewayClass := helpers.MustGenerateGatewayClass(t) gatewayClass, err = clients.GatewayClient.GatewayV1().GatewayClasses().Create(ctx, gatewayClass, metav1.CreateOptions{}) require.NoError(t, err) cleaner.Add(gatewayClass) diff --git a/test/helpers/generators.go b/test/helpers/generators.go index 5cff98ad4..261eeb0c6 100644 --- a/test/helpers/generators.go +++ b/test/helpers/generators.go @@ -19,8 +19,17 @@ import ( "github.com/kong/gateway-operator/pkg/vars" ) -// GenerateGatewayClass generates the default GatewayClass to be used in tests -func GenerateGatewayClass(parametersRef *gatewayv1.ParametersReference) *gatewayv1.GatewayClass { +// MustGenerateGatewayClass generates the default GatewayClass to be used in tests +func MustGenerateGatewayClass(t *testing.T, parametersRefs ...gatewayv1.ParametersReference) *gatewayv1.GatewayClass { + t.Helper() + + if len(parametersRefs) > 1 { + require.Fail(t, "only one ParametersReference is allowed") + } + var parametersRef *gatewayv1.ParametersReference + if len(parametersRefs) == 1 { + parametersRef = ¶metersRefs[0] + } gatewayClass := &gatewayv1.GatewayClass{ ObjectMeta: metav1.ObjectMeta{ Name: uuid.NewString(), @@ -184,6 +193,8 @@ func GenerateHTTPRoute(namespace string, gatewayName, serviceName string, opts . // MustGenerateTLSSecret generates a TLS secret to be used in tests func MustGenerateTLSSecret(t *testing.T, namespace, secretName string, hosts []string) *corev1.Secret { + t.Helper() + var serverKey, serverCert bytes.Buffer require.NoError(t, generateRSACert(hosts, &serverKey, &serverCert), "failed to generate RSA certificate") diff --git a/test/helpers/http.go b/test/helpers/http.go index 49bffe243..bba64f85a 100644 --- a/test/helpers/http.go +++ b/test/helpers/http.go @@ -11,11 +11,14 @@ import ( "time" "github.com/stretchr/testify/assert" + "github.com/stretchr/testify/require" corev1 "k8s.io/api/core/v1" ) // MustCreateHTTPClient creates an HTTP client with the given TLS secret and host func MustCreateHTTPClient(t *testing.T, tlsSecret *corev1.Secret, host string) *http.Client { + t.Helper() + httpClient, err := createHTTPClient(tlsSecret, host) assert.NoError(t, err) return httpClient @@ -77,8 +80,10 @@ func createTLSClientConfig(tlsSecret *corev1.Secret, server string) (*tls.Config // MustBuildRequest creates an HTTP request with the given method, URL, and host func MustBuildRequest(t *testing.T, ctx context.Context, method, url, host string) *http.Request { + t.Helper() + req, err := http.NewRequestWithContext(ctx, method, url, nil) - assert.NoError(t, err) + require.NoError(t, err) if host != "" { req.Host = host } diff --git a/test/integration/suite.go b/test/integration/suite.go index 49d0fb319..1a3b55ae7 100644 --- a/test/integration/suite.go +++ b/test/integration/suite.go @@ -28,12 +28,6 @@ import ( "github.com/kong/gateway-operator/test/helpers/certificate" ) -func init() { - var err error - sharedHTTPClient, err = helpers.CreateHTTPClient(nil, "") - exitOnErr(err) -} - // ----------------------------------------------------------------------------- // Testing Vars - Environment Overrideable // ----------------------------------------------------------------------------- @@ -67,10 +61,9 @@ func addTestsToTestSuite(tests ...func(*testing.T)) { } var ( - ctx context.Context - env environments.Environment - clients testutils.K8sClients - sharedHTTPClient *http.Client + ctx context.Context + env environments.Environment + clients testutils.K8sClients ) // GetCtx returns the context used by the test suite. @@ -183,7 +176,9 @@ func TestMain( }() <-startedChan - exitOnErr(testutils.BuildMTLSCredentials(GetCtx(), GetClients().K8sClient, sharedHTTPClient)) + httpClient, err := helpers.CreateHTTPClient(nil, "") + exitOnErr(err) + exitOnErr(testutils.BuildMTLSCredentials(GetCtx(), GetClients().K8sClient, httpClient)) // Wait for webhook server in controller to be ready after controller started. if webhookEnabled { diff --git a/test/integration/test_aigateway.go b/test/integration/test_aigateway.go index d3267f2a8..da1a8fcca 100644 --- a/test/integration/test_aigateway.go +++ b/test/integration/test_aigateway.go @@ -83,7 +83,7 @@ func TestAIGatewayCreation(t *testing.T) { cleaner.Add(gatewayConfiguration) t.Log("deploying a GatewayClass resource, [", &gatewayConfiguration.Name, "]") - gatewayClass := helpers.GenerateGatewayClass(nil) + gatewayClass := helpers.MustGenerateGatewayClass(t) gatewayClass.Spec.ParametersRef = &gatewayv1.ParametersReference{ Group: gatewayv1.Group("gateway-operator.konghq.com"), Kind: gatewayv1.Kind("GatewayConfiguration"), diff --git a/test/integration/test_gateway.go b/test/integration/test_gateway.go index 123e8ce34..a971dde5b 100644 --- a/test/integration/test_gateway.go +++ b/test/integration/test_gateway.go @@ -36,7 +36,7 @@ func TestGatewayEssentials(t *testing.T) { namespace, cleaner := helpers.SetupTestEnv(t, GetCtx(), GetEnv()) t.Log("deploying a GatewayClass resource") - gatewayClass := helpers.GenerateGatewayClass(nil) + gatewayClass := helpers.MustGenerateGatewayClass(t) gatewayClass, err := GetClients().GatewayClient.GatewayV1().GatewayClasses().Create(GetCtx(), gatewayClass, metav1.CreateOptions{}) require.NoError(t, err) cleaner.Add(gatewayClass) @@ -197,7 +197,7 @@ func TestGatewayMultiple(t *testing.T) { gatewayV1Client := GetClients().GatewayClient.GatewayV1() t.Log("deploying a GatewayClass resource") - gatewayClass := helpers.GenerateGatewayClass(nil) + gatewayClass := helpers.MustGenerateGatewayClass(t) gatewayClass, err := gatewayV1Client.GatewayClasses().Create(GetCtx(), gatewayClass, metav1.CreateOptions{}) require.NoError(t, err) cleaner.Add(gatewayClass) @@ -340,6 +340,8 @@ func TestGatewayMultiple(t *testing.T) { t.Log("verifying connectivity to the HTTPRoute") + httpClient, err := helpers.CreateHTTPClient(nil, "") + require.NoError(t, err) require.Eventually(t, func() bool { url := fmt.Sprintf("http://%s%s", gatewayOneIPAddress, pathOne) bad := fmt.Sprintf("http://%s%s", gatewayOneIPAddress, pathTwo) @@ -347,7 +349,7 @@ func TestGatewayMultiple(t *testing.T) { if err != nil { return false } - resp, err := sharedHTTPClient.Do(req) + resp, err := httpClient.Do(req) if err != nil { return false } @@ -356,7 +358,7 @@ func TestGatewayMultiple(t *testing.T) { if err != nil { return false } - badResp, err := sharedHTTPClient.Do(badReq) + badResp, err := httpClient.Do(badReq) if err != nil { return false } @@ -371,7 +373,7 @@ func TestGatewayMultiple(t *testing.T) { if err != nil { return false } - resp, err := sharedHTTPClient.Do(req) + resp, err := httpClient.Do(req) if err != nil { return false } @@ -380,7 +382,7 @@ func TestGatewayMultiple(t *testing.T) { if err != nil { return false } - badResp, err := sharedHTTPClient.Do(badReq) + badResp, err := httpClient.Do(badReq) if err != nil { return false } @@ -464,7 +466,7 @@ func TestGatewayWithMultipleListeners(t *testing.T) { namespace, cleaner := helpers.SetupTestEnv(t, ctx, env) t.Log("deploying a GatewayClass resource") - gatewayClass := helpers.GenerateGatewayClass(nil) + gatewayClass := helpers.MustGenerateGatewayClass(t) gatewayClass, err := clients.GatewayClient.GatewayV1().GatewayClasses().Create(ctx, gatewayClass, metav1.CreateOptions{}) require.NoError(t, err) cleaner.Add(gatewayClass) @@ -538,7 +540,7 @@ func TestScalingDataPlaneThroughGatewayConfiguration(t *testing.T) { require.NoError(t, err) cleaner.Add(gatewayConfig) - gatewayClass := helpers.GenerateGatewayClass(nil) + gatewayClass := helpers.MustGenerateGatewayClass(t) gatewayClass.Spec.ParametersRef = &gatewayv1.ParametersReference{ Group: "gateway-operator.konghq.com", Kind: "GatewayConfiguration", @@ -670,7 +672,7 @@ func TestGatewayDataPlaneNetworkPolicy(t *testing.T) { cleaner.Add(gatewayConfig) t.Log("deploying a GatewayClass resource") - gatewayClass := helpers.GenerateGatewayClass(nil) + gatewayClass := helpers.MustGenerateGatewayClass(t) gatewayClass.Spec.ParametersRef = &gatewayv1.ParametersReference{ Group: "gateway-operator.konghq.com", Kind: "GatewayConfiguration", @@ -895,7 +897,7 @@ func TestGatewayProvisionDataPlaneFail(t *testing.T) { t.Log("creating a Gateway and verify that it does not get Programmed") t.Log("deploying a GatewayClass resource") - gatewayClass := helpers.GenerateGatewayClass(nil) + gatewayClass := helpers.MustGenerateGatewayClass(t) gatewayClass, err = GetClients().GatewayClient.GatewayV1().GatewayClasses().Create(GetCtx(), gatewayClass, metav1.CreateOptions{}) require.NoError(t, err) cleaner.Add(gatewayClass) diff --git a/test/integration/test_httproute.go b/test/integration/test_httproute.go index 545cbc9d0..d5607780a 100644 --- a/test/integration/test_httproute.go +++ b/test/integration/test_httproute.go @@ -30,7 +30,7 @@ func TestHTTPRoute(t *testing.T) { require.NoError(t, err) cleaner.Add(gatewayConfig) - gatewayClass := helpers.GenerateGatewayClass(&gatewayv1.ParametersReference{ + gatewayClass := helpers.MustGenerateGatewayClass(t, gatewayv1.ParametersReference{ Group: gatewayv1.Group(operatorv1beta1.SchemeGroupVersion.Group), Kind: gatewayv1.Kind("GatewayConfiguration"), Namespace: (*gatewayv1.Namespace)(&gatewayConfig.Namespace), @@ -97,20 +97,23 @@ func TestHTTPRoute(t *testing.T) { waitTick = time.Second ) - // route to /test path of service httpbin should receive a 200 OK response. + httpClient, err := helpers.CreateHTTPClient(nil, "") + require.NoError(t, err) + + t.Log("route to /test path of service httpbin should receive a 200 OK response") request := helpers.MustBuildRequest(t, GetCtx(), http.MethodGet, "http://"+gatewayIPAddress+"/test", "") require.Eventually( t, - testutils.GetResponseBodyContains(t, clients, sharedHTTPClient, request, "httpbin.org"), + testutils.GetResponseBodyContains(t, clients, httpClient, request, "httpbin.org"), httpRouteAccessTimeout, time.Second, ) - // route to /test/1234 path of service httpbin should receive a 404 OK response. + t.Log("route to /test/1234 path of service httpbin should receive a 404 OK response") request = helpers.MustBuildRequest(t, GetCtx(), http.MethodGet, "http://"+gatewayIPAddress+"/test/1234", "") require.Eventually( t, - testutils.GetResponseBodyContains(t, clients, sharedHTTPClient, request, "

Not Found

"), + testutils.GetResponseBodyContains(t, clients, httpClient, request, "

Not Found

"), httpRouteAccessTimeout, time.Second, ) @@ -126,7 +129,7 @@ func TestHTTPRouteWithTLS(t *testing.T) { require.NoError(t, err) cleaner.Add(gatewayConfig) - gatewayClass := helpers.GenerateGatewayClass(&gatewayv1.ParametersReference{ + gatewayClass := helpers.MustGenerateGatewayClass(t, gatewayv1.ParametersReference{ Group: gatewayv1.Group(operatorv1beta1.SchemeGroupVersion.Group), Kind: gatewayv1.Kind("GatewayConfiguration"), Namespace: (*gatewayv1.Namespace)(&gatewayConfig.Namespace), @@ -217,7 +220,7 @@ func TestHTTPRouteWithTLS(t *testing.T) { httpClient := helpers.MustCreateHTTPClient(t, secret, host) - // route to /test path of service httpbin should receive a 200 OK response. + t.Log("route to /test path of service httpbin should receive a 200 OK response") request := helpers.MustBuildRequest(t, GetCtx(), http.MethodGet, "https://"+gatewayIPAddress+"/test", host) require.Eventually( t, @@ -225,7 +228,7 @@ func TestHTTPRouteWithTLS(t *testing.T) { httpRouteAccessTimeout, time.Second, ) - // route to /test/1234 path of service httpbin should receive a 404 OK response. + t.Log("route to /test/1234 path of service httpbin should receive a 404 OK response") request = helpers.MustBuildRequest(t, GetCtx(), http.MethodGet, "https://"+gatewayIPAddress+"/test/1234", host) require.Eventually( t, diff --git a/test/integration/test_ingress.go b/test/integration/test_ingress.go index 17e07a33f..09f702692 100644 --- a/test/integration/test_ingress.go +++ b/test/integration/test_ingress.go @@ -182,8 +182,10 @@ func TestIngressEssentials(t *testing.T) { }, testutils.DefaultIngressWait, testutils.WaitIngressTick) t.Log("waiting for routes from Ingress to be operational") + httpClient, err := helpers.CreateHTTPClient(nil, "") + require.NoError(t, err) require.Eventually(t, func() bool { - resp, err := sharedHTTPClient.Get(fmt.Sprintf("%s/%s-httpbin", proxyURL, strings.ToLower(t.Name()))) + resp, err := httpClient.Get(fmt.Sprintf("%s/%s-httpbin", proxyURL, strings.ToLower(t.Name()))) if err != nil { t.Logf("WARNING: error while waiting for %s: %v", proxyURL, err) return false @@ -262,7 +264,7 @@ func TestIngressEssentials(t *testing.T) { t.Log("waiting for routes from Ingress to be operational after reintroducing ingress class annotation") require.Eventually(t, func() bool { - resp, err := sharedHTTPClient.Get(fmt.Sprintf("%s/%s-httpbin", proxyURL, strings.ToLower(t.Name()))) + resp, err := httpClient.Get(fmt.Sprintf("%s/%s-httpbin", proxyURL, strings.ToLower(t.Name()))) if err != nil { t.Logf("WARNING: error while waiting for %s: %v", proxyURL, err) return false diff --git a/test/integration/utils.go b/test/integration/utils.go index 281d4c978..b7090cdd0 100644 --- a/test/integration/utils.go +++ b/test/integration/utils.go @@ -9,6 +9,7 @@ import ( "testing" "github.com/kong/kubernetes-testing-framework/pkg/clusters" + "github.com/stretchr/testify/require" corev1 "k8s.io/api/core/v1" metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" "k8s.io/apimachinery/pkg/types" @@ -16,6 +17,7 @@ import ( "github.com/kong/gateway-operator/pkg/consts" "github.com/kong/gateway-operator/pkg/utils/kubernetes/resources" + "github.com/kong/gateway-operator/test/helpers" ) // Expect404WithNoRouteFunc is used to check whether a given URL responds @@ -23,6 +25,9 @@ import ( func Expect404WithNoRouteFunc(t *testing.T, ctx context.Context, url string) func() bool { t.Helper() + httpClient, err := helpers.CreateHTTPClient(nil, "") + require.NoError(t, err) + return func() bool { t.Logf("verifying connectivity to the dataplane %v", url) @@ -31,7 +36,7 @@ func Expect404WithNoRouteFunc(t *testing.T, ctx context.Context, url string) fun t.Logf("failed creating request for %s: %v", url, err) return false } - resp, err := sharedHTTPClient.Do(req) + resp, err := httpClient.Do(req) if err != nil { t.Logf("failed issuing HTTP GET for %s: %v", url, err) return false