From ad386f093df6dadf3499212284cf7ed66ad36cc7 Mon Sep 17 00:00:00 2001 From: Danail Branekov Date: Fri, 13 Oct 2023 08:37:05 +0000 Subject: [PATCH] Configure eventually timeouts in e2e tests Instead of specifying cutom timeouts in `Eventually`, configure the default eventually timeout and polling interval consistently across e2e suites. Co-authored-by: Georgi Sabev --- tests/crds/crds_suite_test.go | 4 +-- tests/crds/crds_test.go | 51 ++++++++++++++++++--------------- tests/e2e/e2e_suite_test.go | 2 +- tests/helpers/eventually.go | 30 ++++++++++++++----- tests/smoke/smoke_suite_test.go | 5 ++-- 5 files changed, 56 insertions(+), 36 deletions(-) diff --git a/tests/crds/crds_suite_test.go b/tests/crds/crds_suite_test.go index cd44e5523..deb59285d 100644 --- a/tests/crds/crds_suite_test.go +++ b/tests/crds/crds_suite_test.go @@ -2,7 +2,6 @@ package crds_test import ( "testing" - "time" "code.cloudfoundry.org/korifi/tests/helpers" @@ -15,7 +14,8 @@ import ( func TestCrds(t *testing.T) { RegisterFailHandler(Fail) - SetDefaultEventuallyTimeout(10 * time.Second) + SetDefaultEventuallyTimeout(helpers.EventuallyTimeout()) + SetDefaultEventuallyPollingInterval(helpers.EventuallyPollingInterval()) RunSpecs(t, "CRDs Suite") } diff --git a/tests/crds/crds_test.go b/tests/crds/crds_test.go index 0b24420e2..a4e42ef09 100644 --- a/tests/crds/crds_test.go +++ b/tests/crds/crds_test.go @@ -1,6 +1,8 @@ package crds_test import ( + "fmt" + . "code.cloudfoundry.org/korifi/controllers/controllers/workloads/testutils" "code.cloudfoundry.org/korifi/tests/helpers" @@ -38,11 +40,12 @@ var _ = Describe("Using the k8s API directly", Ordered, func() { }) AfterAll(func() { - deleteOrg := helpers.Kubectl("delete", "--ignore-not-found=true", "-n="+rootNamespace, "cforg", orgGUID) - deleteRoleBinding := helpers.Kubectl("delete", "--ignore-not-found=true", "-n="+rootNamespace, "rolebinding", bindingName) - - Eventually(deleteOrg, "60s").Should(Exit(0), "deleteOrg") - Eventually(deleteRoleBinding, "20s").Should(Exit(0), "deleteRoleBinging") + Eventually( + helpers.Kubectl("delete", "--ignore-not-found=true", "-n="+rootNamespace, "cforg", orgGUID), + ).Should(Exit(0)) + Eventually( + helpers.Kubectl("delete", "--ignore-not-found=true", "-n="+rootNamespace, "rolebinding", bindingName), + ).Should(Exit(0)) }) It("can create a CFOrg", func() { @@ -59,7 +62,6 @@ var _ = Describe("Using the k8s API directly", Ordered, func() { Eventually( helpers.Kubectl("wait", "--for=condition=ready", "-n="+rootNamespace, "cforg/"+orgGUID), - "20s", ).Should(Exit(0)) Eventually( @@ -81,7 +83,6 @@ var _ = Describe("Using the k8s API directly", Ordered, func() { Eventually( helpers.Kubectl("wait", "--for=condition=ready", "-n="+orgGUID, "cfspace/"+spaceGUID), - "20s", ).Should(Exit(0)) Eventually( @@ -117,7 +118,6 @@ var _ = Describe("Using the k8s API directly", Ordered, func() { Eventually( cf.Cf("push", PrefixedGUID("crds-test-app"), "-p", "../assets/dorifi", "--no-start"), // This could be any app - "20s", ).Should(Exit(0)) }) @@ -141,39 +141,44 @@ var _ = Describe("Using the k8s API directly", Ordered, func() { `, rootNamespace, propagatedBindingName, cfUser, rootNamespace) Eventually(applyCFAdminRoleBinding).Should(Exit(0)) - Eventually(func() int { - return helpers.Kubectl("get", "rolebinding/"+propagatedBindingName, "-n", rootNamespace).Wait().ExitCode() - }, "20s").Should(BeNumerically("==", 0)) + Eventually( + helpers.Kubectl("get", "rolebinding/"+propagatedBindingName, "-n", rootNamespace), + ).Should(Exit(0)) - Eventually(func() int { - return helpers.Kubectl("get", "rolebinding/"+propagatedBindingName, "-n", orgGUID).Wait().ExitCode() - }, "20s").Should(BeNumerically("==", 0)) + Eventually( + helpers.Kubectl("get", "rolebinding/"+propagatedBindingName, "-n", orgGUID), + ).Should(Exit(0)) - Eventually(func() int { - return helpers.Kubectl("get", "rolebinding/"+propagatedBindingName, "-n", spaceGUID).Wait().ExitCode() - }, "20s").Should(BeNumerically("==", 0)) + Eventually( + helpers.Kubectl("get", "rolebinding/"+propagatedBindingName, "-n", spaceGUID), + ).Should(Exit(0)) }) It("can delete the cf-admin rolebinding", func() { Eventually( helpers.Kubectl("delete", "--ignore-not-found=true", "-n="+rootNamespace, "rolebinding/"+propagatedBindingName), - "20s", ).Should(Exit(0)) - Eventually(helpers.Kubectl("wait", "--for=delete", "rolebinding/"+propagatedBindingName, "-n", rootNamespace, "--timeout=60s"), "60s").Should(Exit(0)) + Eventually( + helpers.Kubectl("wait", "--for=delete", "rolebinding/"+propagatedBindingName, "-n", rootNamespace, fmt.Sprintf("--timeout=%s", helpers.EventuallyTimeout())), + ).Should(Exit(0)) - Eventually(helpers.Kubectl("wait", "--for=delete", "rolebinding/"+propagatedBindingName, "-n", orgGUID, "--timeout=60s"), "60s").Should(Exit(0)) + Eventually( + helpers.Kubectl("wait", "--for=delete", "rolebinding/"+propagatedBindingName, "-n", orgGUID, fmt.Sprintf("--timeout=%s", helpers.EventuallyTimeout())), + ).Should(Exit(0)) - Eventually(helpers.Kubectl("wait", "--for=delete", "rolebinding/"+propagatedBindingName, "-n", spaceGUID, "--timeout=60s"), "60s").Should(Exit(0)) + Eventually( + helpers.Kubectl("wait", "--for=delete", "rolebinding/"+propagatedBindingName, "-n", spaceGUID, fmt.Sprintf("--timeout=%s", helpers.EventuallyTimeout())), + ).Should(Exit(0)) }) It("can delete the space", func() { - Eventually(helpers.Kubectl("delete", "--ignore-not-found=true", "-n="+orgGUID, "cfspace/"+spaceGUID), "120s").Should(Exit(0)) + Eventually(helpers.Kubectl("delete", "--ignore-not-found=true", "-n="+orgGUID, "cfspace/"+spaceGUID)).Should(Exit(0)) Eventually(helpers.Kubectl("wait", "--for=delete", "namespace/"+spaceGUID)).Should(Exit(0)) }) It("can delete the org", func() { - Eventually(helpers.Kubectl("delete", "--ignore-not-found=true", "-n="+rootNamespace, "cforgs/"+orgGUID), "120s").Should(Exit(0)) + Eventually(helpers.Kubectl("delete", "--ignore-not-found=true", "-n="+rootNamespace, "cforgs/"+orgGUID)).Should(Exit(0)) Eventually(helpers.Kubectl("wait", "--for=delete", "cforg/"+orgGUID, "-n", rootNamespace)).Should(Exit(0)) Eventually(helpers.Kubectl("wait", "--for=delete", "namespace/"+orgGUID)).Should(Exit(0)) diff --git a/tests/e2e/e2e_suite_test.go b/tests/e2e/e2e_suite_test.go index b534898b5..8c6fd596e 100644 --- a/tests/e2e/e2e_suite_test.go +++ b/tests/e2e/e2e_suite_test.go @@ -366,7 +366,7 @@ var _ = SynchronizedBeforeSuite(func() []byte { adminClient = makeTokenClient(sharedSetup.AdminServiceAccountToken) SetDefaultEventuallyTimeout(helpers.EventuallyTimeout()) - SetDefaultEventuallyPollingInterval(2 * time.Second) + SetDefaultEventuallyPollingInterval(helpers.EventuallyPollingInterval()) logf.SetLogger(zap.New(zap.WriteTo(GinkgoWriter), zap.UseDevMode(true))) }) diff --git a/tests/helpers/eventually.go b/tests/helpers/eventually.go index 3b7d22a74..44096c95e 100644 --- a/tests/helpers/eventually.go +++ b/tests/helpers/eventually.go @@ -5,23 +5,39 @@ import ( "strconv" "time" - . "github.com/onsi/gomega" //lint:ignore ST1001 this is a test file + . "github.com/onsi/ginkgo/v2" //lint:ignore ST1001 this is a test file + . "github.com/onsi/gomega" //lint:ignore ST1001 this is a test file ) func EventuallyShouldHold(condition func(g Gomega)) { + GinkgoHelper() + Eventually(condition).WithTimeout(EventuallyTimeout()).Should(Succeed()) Consistently(condition).Should(Succeed()) } func EventuallyTimeout() time.Duration { - eventuallyTimeout := 4 * time.Minute - eventuallyTimeoutSecondsString := os.Getenv("E2E_EVENTUALLY_TIMEOUT_SECONDS") + GinkgoHelper() + + return getDuration("E2E_EVENTUALLY_TIMEOUT_SECONDS", 4*60) +} + +func EventuallyPollingInterval() time.Duration { + GinkgoHelper() + + return getDuration("E2E_EVENTUALLY_POLLING_INTERVAL_SECONDS", 2) +} + +func getDuration(envName string, defaultDurationSeconds int) time.Duration { + GinkgoHelper() + + durationSecondsString := os.Getenv(envName) - if eventuallyTimeoutSecondsString != "" { - eventuallyTimeoutSeconds, err := strconv.Atoi(eventuallyTimeoutSecondsString) + if durationSecondsString != "" { + durationSeconds, err := strconv.Atoi(durationSecondsString) Expect(err).NotTo(HaveOccurred()) - eventuallyTimeout = time.Duration(eventuallyTimeoutSeconds) * time.Second + return time.Duration(durationSeconds) * time.Second } - return eventuallyTimeout + return time.Duration(defaultDurationSeconds) * time.Second } diff --git a/tests/smoke/smoke_suite_test.go b/tests/smoke/smoke_suite_test.go index ea68316e0..282e284f8 100644 --- a/tests/smoke/smoke_suite_test.go +++ b/tests/smoke/smoke_suite_test.go @@ -6,7 +6,6 @@ import ( "fmt" "strings" "testing" - "time" korifiv1alpha1 "code.cloudfoundry.org/korifi/controllers/api/v1alpha1" "code.cloudfoundry.org/korifi/tests/helpers" @@ -58,8 +57,8 @@ func TestSmoke(t *testing.T) { }) }, })) - SetDefaultEventuallyTimeout(5 * time.Minute) - SetDefaultEventuallyPollingInterval(5 * time.Second) + SetDefaultEventuallyTimeout(helpers.EventuallyTimeout()) + SetDefaultEventuallyPollingInterval(helpers.EventuallyPollingInterval()) RunSpecs(t, "Smoke Tests Suite") }