Skip to content

Commit

Permalink
workaroud for the flaky oidc e2e test
Browse files Browse the repository at this point in the history
Signed-off-by: Huabing Zhao <[email protected]>
  • Loading branch information
zhaohuabing committed Nov 1, 2024
1 parent 7285dda commit e5f3897
Showing 1 changed file with 21 additions and 8 deletions.
29 changes: 21 additions & 8 deletions test/e2e/tests/oidc.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import (
corev1 "k8s.io/api/core/v1"
"k8s.io/apimachinery/pkg/types"
"k8s.io/apimachinery/pkg/util/wait"
"sigs.k8s.io/controller-runtime/pkg/client"
gwapiv1 "sigs.k8s.io/gateway-api/apis/v1"
gwapiv1a2 "sigs.k8s.io/gateway-api/apis/v1alpha2"
gwhttp "sigs.k8s.io/gateway-api/conformance/utils/http"
Expand Down Expand Up @@ -119,7 +120,7 @@ func testOIDC(t *testing.T, suite *suite.ConformanceTestSuite) {
WaitForPods(t, suite.Client, ns, map[string]string{"job-name": "setup-keycloak"}, corev1.PodSucceeded, podInitialized)

// Initialize the test OIDC client that will keep track of the state of the OIDC login process
client, err := NewOIDCTestClient(
oidcClient, err := NewOIDCTestClient(
WithLoggingOptions(t.Log, true),
// Map the application and keycloak cluster DNS name to the gateway address
WithCustomAddressMappings(map[string]string{
Expand All @@ -135,13 +136,25 @@ func testOIDC(t *testing.T, suite *suite.ConformanceTestSuite) {

// Send a request to the http route with OIDC configured.
// It will be redirected to the keycloak login page
res, err := client.Get(testURL, true)
require.NoError(t, err, "Failed to get the login page")
require.Equal(t, 200, res.StatusCode, "Expected 200 OK")
res, err := oidcClient.Get(testURL, true)
if err != nil {
tlog.Logf(t, "failed to get the login page: %v", err)
return false, nil
}
if res.StatusCode != http.StatusOK {
tlog.Logf(t, "Failed to get the login page, expected 200 OK, got %d", res.StatusCode)
return false, nil
}

// Parse the response body to get the URL where the login page would post the user-entered credentials
if err := client.ParseLoginForm(res.Body, keyCloakLoginFormID); err != nil {
if err := oidcClient.ParseLoginForm(res.Body, keyCloakLoginFormID); err != nil {
tlog.Logf(t, "failed to parse login form: %v", err)
// restart the envoy proxy to recover from the error, this is a workaround for the flaky test
// TODO: we should investigate the root cause of the flakiness and remove this workaround
proxyLabel := map[string]string{"gateway.envoyproxy.io/owning-gateway-name": "same-namespace"}
err := suite.Client.DeleteAllOf(context.TODO(), &corev1.Pod{}, client.MatchingLabels(proxyLabel), client.InNamespace("envoy-gateway-system"))
require.NoError(t, err)
WaitForPods(t, suite.Client, "envoy-gateway-system", proxyLabel, corev1.PodSucceeded, podInitialized)
return false, nil
}

Expand All @@ -153,7 +166,7 @@ func testOIDC(t *testing.T, suite *suite.ConformanceTestSuite) {

// Submit the login form to the IdP.
// This will authenticate and redirect back to the application
res, err := client.Login(map[string]string{"username": username, "password": password, "credentialId": ""})
res, err := oidcClient.Login(map[string]string{"username": username, "password": password, "credentialId": ""})
require.NoError(t, err, "Failed to login to the IdP")

// Verify that we get the expected response from the application
Expand All @@ -163,14 +176,14 @@ func testOIDC(t *testing.T, suite *suite.ConformanceTestSuite) {
require.Contains(t, string(body), "infra-backend-v1", "Expected response from the application")

// Verify that we can access the application without logging in again
res, err = client.Get(testURL, false)
res, err = oidcClient.Get(testURL, false)
require.NoError(t, err)
require.Equal(t, http.StatusOK, res.StatusCode)
require.Contains(t, string(body), "infra-backend-v1", "Expected response from the application")

// Verify that we can logout
// Note: OAuth2 filter just clears its cookies and does not log out from the IdP.
res, err = client.Get(logoutURL, false)
res, err = oidcClient.Get(logoutURL, false)
require.NoError(t, err)
require.Equal(t, http.StatusFound, res.StatusCode)

Expand Down

0 comments on commit e5f3897

Please sign in to comment.