Skip to content

Commit

Permalink
Configure eventually timeouts in e2e tests
Browse files Browse the repository at this point in the history
Instead of specifying cutom timeouts in `Eventually`, configure the
default eventually timeout and polling interval consistently across
e2e suites.

Co-authored-by: Georgi Sabev <georgethebeatle@gmail.com>
  • Loading branch information
danail-branekov and georgethebeatle committed Oct 13, 2023
1 parent 5f0689c commit ad386f0
Show file tree
Hide file tree
Showing 5 changed files with 56 additions and 36 deletions.
4 changes: 2 additions & 2 deletions tests/crds/crds_suite_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package crds_test

import (
"testing"
"time"

"code.cloudfoundry.org/korifi/tests/helpers"

Expand All @@ -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")
}

Expand Down
51 changes: 28 additions & 23 deletions tests/crds/crds_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package crds_test

import (
"fmt"

. "code.cloudfoundry.org/korifi/controllers/controllers/workloads/testutils"
"code.cloudfoundry.org/korifi/tests/helpers"

Expand Down Expand Up @@ -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() {
Expand All @@ -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(
Expand All @@ -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(
Expand Down Expand Up @@ -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))
})

Expand All @@ -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))
Expand Down
2 changes: 1 addition & 1 deletion tests/e2e/e2e_suite_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)))
})
Expand Down
30 changes: 23 additions & 7 deletions tests/helpers/eventually.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
5 changes: 2 additions & 3 deletions tests/smoke/smoke_suite_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import (
"fmt"
"strings"
"testing"
"time"

korifiv1alpha1 "code.cloudfoundry.org/korifi/controllers/api/v1alpha1"
"code.cloudfoundry.org/korifi/tests/helpers"
Expand Down Expand Up @@ -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")
}

Expand Down

0 comments on commit ad386f0

Please sign in to comment.