Skip to content

Commit

Permalink
address review comments
Browse files Browse the repository at this point in the history
Signed-off-by: Mattia Lavacca <[email protected]>
  • Loading branch information
mlavacca committed Apr 22, 2024
1 parent f5f4aff commit 0789bef
Show file tree
Hide file tree
Showing 9 changed files with 60 additions and 37 deletions.
2 changes: 1 addition & 1 deletion test/e2e/test_operator_logs.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
15 changes: 13 additions & 2 deletions test/helpers/generators.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 = &parametersRefs[0]
}
gatewayClass := &gatewayv1.GatewayClass{
ObjectMeta: metav1.ObjectMeta{
Name: uuid.NewString(),
Expand Down Expand Up @@ -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")

Expand Down
7 changes: 6 additions & 1 deletion test/helpers/http.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
}
Expand Down
17 changes: 6 additions & 11 deletions test/integration/suite.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
// -----------------------------------------------------------------------------
Expand Down Expand Up @@ -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.
Expand Down Expand Up @@ -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 {
Expand Down
2 changes: 1 addition & 1 deletion test/integration/test_aigateway.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"),
Expand Down
22 changes: 12 additions & 10 deletions test/integration/test_gateway.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -340,14 +340,16 @@ 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)
req, err := http.NewRequestWithContext(GetCtx(), http.MethodGet, url, nil)
if err != nil {
return false
}
resp, err := sharedHTTPClient.Do(req)
resp, err := httpClient.Do(req)
if err != nil {
return false
}
Expand All @@ -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
}
Expand All @@ -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
}
Expand All @@ -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
}
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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",
Expand Down Expand Up @@ -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",
Expand Down Expand Up @@ -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)
Expand Down
19 changes: 11 additions & 8 deletions test/integration/test_httproute.go
Original file line number Diff line number Diff line change
Expand Up @@ -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),
Expand Down Expand Up @@ -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, "<title>httpbin.org</title>"),
testutils.GetResponseBodyContains(t, clients, httpClient, request, "<title>httpbin.org</title>"),
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, "<h1>Not Found</h1>"),
testutils.GetResponseBodyContains(t, clients, httpClient, request, "<h1>Not Found</h1>"),
httpRouteAccessTimeout,
time.Second,
)
Expand All @@ -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),
Expand Down Expand Up @@ -217,15 +220,15 @@ 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,
testutils.GetResponseBodyContains(t, clients, httpClient, request, "<title>httpbin.org</title>"),
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,
Expand Down
6 changes: 4 additions & 2 deletions test/integration/test_ingress.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down
7 changes: 6 additions & 1 deletion test/integration/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,20 +9,25 @@ 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"
kubernetesclient "k8s.io/client-go/kubernetes"

"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
// with 404 and a standard Kong no route message.
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)

Expand All @@ -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
Expand Down

0 comments on commit 0789bef

Please sign in to comment.