From 40082c4748e5a0c12f414a9b711ceddeea1cccbd Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Grzegorz=20Burzy=C5=84ski?= Date: Mon, 29 May 2023 15:03:55 +0200 Subject: [PATCH] tests: delete migrations job before upgrade in e2e test --- test/e2e/features_test.go | 1 - test/e2e/helpers_test.go | 3 +++ test/e2e/upgrade_test.go | 32 +++++++++++++++++++++++++++++--- 3 files changed, 32 insertions(+), 4 deletions(-) diff --git a/test/e2e/features_test.go b/test/e2e/features_test.go index 0dc76f5a25..4c7353f97b 100644 --- a/test/e2e/features_test.go +++ b/test/e2e/features_test.go @@ -71,7 +71,6 @@ nodes: ` validationWebhookName = "kong-validation-webhook" kongNamespace = "kong" - admissionScriptPath = "../../hack/deploy-admission-controller.sh" ) // openssl req -new -x509 -nodes -newkey ec:<(openssl ecparam -name secp384r1) -keyout cert.key -out cert.crt -days 3650 -subj '/CN=first.example/' diff --git a/test/e2e/helpers_test.go b/test/e2e/helpers_test.go index 40758da839..b54850a9c1 100644 --- a/test/e2e/helpers_test.go +++ b/test/e2e/helpers_test.go @@ -81,6 +81,9 @@ const ( // proxyContainerName is the name of the proxy container in all manifests variants. proxyContainerName = "proxy" + + // migrationsJobName is the name of the migrations job in postgres manifests variant. + migrationsJobName = "kong-migrations" ) // setupE2ETest builds a testing environment for the E2E test. It also sets up the environment's teardown and test diff --git a/test/e2e/upgrade_test.go b/test/e2e/upgrade_test.go index a30769d7a1..b443e804ff 100644 --- a/test/e2e/upgrade_test.go +++ b/test/e2e/upgrade_test.go @@ -3,10 +3,14 @@ package e2e import ( + "context" "fmt" "testing" + "github.com/kong/kubernetes-testing-framework/pkg/environments" + "github.com/samber/lo" "github.com/stretchr/testify/require" + metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" "github.com/kong/kubernetes-ingress-controller/v2/test/internal/helpers" ) @@ -25,16 +29,33 @@ const ( func TestDeployAndUpgradeAllInOneDBLESS(t *testing.T) { fromManifestURL := fmt.Sprintf(dblessURLTemplate, upgradeTestFromTag) toManifestPath := dblessPath - testManifestsUpgrade(t, fromManifestURL, toManifestPath) + testManifestsUpgrade(t, fromManifestURL, toManifestPath, nil) } func TestDeployAndUpgradeAllInOnePostgres(t *testing.T) { fromManifestURL := fmt.Sprintf(postgresURLTemplate, upgradeTestFromTag) toManifestPath := postgresPath - testManifestsUpgrade(t, fromManifestURL, toManifestPath) + + // Injecting a beforeUpgradeHook to delete the old migrations job before the upgrade. This is necessary because it's + // not allowed to modify the existing job's spec. + beforeUpgradeHook := func(ctx context.Context, t *testing.T, env environments.Environment) { + err := env.Cluster().Client().BatchV1().Jobs(namespace).Delete(ctx, migrationsJobName, metav1.DeleteOptions{ + PropagationPolicy: lo.ToPtr(metav1.DeletePropagationBackground), + }) + require.NoError(t, err, "failed to delete old migrations job before upgrade") + } + testManifestsUpgrade(t, fromManifestURL, toManifestPath, beforeUpgradeHook) } -func testManifestsUpgrade(t *testing.T, fromManifestURL string, toManifestPath string) { +// beforeUpgradeFn is a function that is run before the upgrade to clean up any resources that may interfere with the upgrade. +type beforeUpgradeFn func(ctx context.Context, t *testing.T, env environments.Environment) + +func testManifestsUpgrade( + t *testing.T, + fromManifestURL string, + toManifestPath string, + beforeUpgradeHook beforeUpgradeFn, +) { t.Parallel() httpClient := helpers.RetryableHTTPClient(helpers.DefaultHTTPClient()) @@ -52,6 +73,11 @@ func testManifestsUpgrade(t *testing.T, fromManifestURL string, toManifestPath s deployIngress(ctx, t, env) verifyIngress(ctx, t, env) + if beforeUpgradeHook != nil { + t.Log("running before upgrade hook") + beforeUpgradeHook(ctx, t, env) + } + t.Logf("deploying target version of kong manifests: %s", toManifestPath) manifest := getTestManifest(t, toManifestPath) deployKong(ctx, t, env, manifest)