Skip to content

Commit

Permalink
chore: refactor wait.Poll* to wait.PollUntilContext
Browse files Browse the repository at this point in the history
Signed-off-by: Nico Berlee <[email protected]>
  • Loading branch information
nberlee committed Jul 10, 2024
1 parent bf9d115 commit 1e2c02d
Show file tree
Hide file tree
Showing 25 changed files with 120 additions and 96 deletions.
18 changes: 9 additions & 9 deletions e2e/framework/helper.go
Original file line number Diff line number Diff line change
Expand Up @@ -66,8 +66,8 @@ func ExpectNamespace(namespace string) {
kubeClient, err := kube.NewKubeHelper()
ExpectNoErrorWithOffset(1, err)

err = wait.PollImmediate(time.Second, time.Minute*2, func() (done bool, err error) {
ns, err := kubeClient.Client().KubeClient().CoreV1().Namespaces().Get(context.TODO(), namespace, metav1.GetOptions{})
err = wait.PollUntilContextTimeout(context.TODO(), time.Second, time.Minute*2, true, func(ctx context.Context) (done bool, err error) {
ns, err := kubeClient.Client().KubeClient().CoreV1().Namespaces().Get(ctx, namespace, metav1.GetOptions{})
if err != nil {
return false, nil
}
Expand All @@ -81,7 +81,7 @@ func ExpectRemoteFileContents(imageSelector string, namespace string, filePath s
kubeClient, err := kube.NewKubeHelper()
ExpectNoErrorWithOffset(1, err)

err = wait.PollImmediate(time.Second, time.Minute*2, func() (done bool, err error) {
err = wait.PollUntilContextTimeout(context.TODO(), time.Second, time.Minute*2, true, func(_ context.Context) (done bool, err error) {
out, err := kubeClient.ExecByImageSelector(imageSelector, namespace, []string{"cat", filePath})
if err != nil {
return false, nil
Expand All @@ -94,7 +94,7 @@ func ExpectRemoteFileContents(imageSelector string, namespace string, filePath s

func ExpectLocalCurlContents(urlString string, contents string) {
client := resty.New()
err := wait.PollImmediate(time.Second, time.Minute*2, func() (done bool, err error) {
err := wait.PollUntilContextTimeout(context.TODO(), time.Second, time.Minute*2, true, func(_ context.Context) (done bool, err error) {
resp, _ := client.R().
EnableTrace().
Get(urlString)
Expand All @@ -106,7 +106,7 @@ func ExpectLocalCurlContents(urlString string, contents string) {
func ExpectContainerNameAndImageEqual(namespace, deploymentName, containerImage, containerName string) {
kubeClient, err := kube.NewKubeHelper()
ExpectNoErrorWithOffset(1, err)
err = wait.PollImmediate(time.Second, time.Minute*2, func() (done bool, err error) {
err = wait.PollUntilContextTimeout(context.TODO(), time.Second, time.Minute*2, true, func(_ context.Context) (done bool, err error) {
deploy, err := kubeClient.RawClient().AppsV1().Deployments(namespace).Get(context.TODO(),
deploymentName, metav1.GetOptions{})
if err != nil {
Expand All @@ -121,7 +121,7 @@ func ExpectContainerNameAndImageEqual(namespace, deploymentName, containerImage,
func ExpectRemoteCurlContents(imageSelector string, namespace string, urlString string, contents string) {
kubeClient, err := kube.NewKubeHelper()
ExpectNoErrorWithOffset(1, err)
err = wait.PollImmediate(time.Second, time.Minute*2, func() (done bool, err error) {
err = wait.PollUntilContextTimeout(context.TODO(), time.Second, time.Minute*2, true, func(_ context.Context) (done bool, err error) {
out, err := kubeClient.ExecByImageSelector(imageSelector, namespace, []string{"curl", urlString})
if err != nil {
return false, nil
Expand All @@ -137,7 +137,7 @@ func ExpectRemoteFileNotFound(imageSelector string, namespace string, filePath s

fileExists := "file exists"
fileNotFound := "file not found"
err = wait.PollImmediate(time.Second, time.Minute*2, func() (done bool, err error) {
err = wait.PollUntilContextTimeout(context.TODO(), time.Second, time.Minute*2, true, func(_ context.Context) (done bool, err error) {
test := []string{"sh", "-c", fmt.Sprintf("test -e %s && echo %s || echo %s", filePath, fileExists, fileNotFound)}
out, err := kubeClient.ExecByImageSelector(imageSelector, namespace, test)
if err != nil {
Expand All @@ -159,7 +159,7 @@ func ExpectRemoteContainerFileContents(labelSelector, container string, namespac
kubeClient, err := kube.NewKubeHelper()
ExpectNoErrorWithOffset(1, err)

err = wait.PollImmediate(time.Second, time.Minute*2, func() (done bool, err error) {
err = wait.PollUntilContextTimeout(context.TODO(), time.Second, time.Minute*2, true, func(_ context.Context) (done bool, err error) {
out, err := kubeClient.ExecByContainer(labelSelector, container, namespace, []string{"cat", filePath})
if err != nil {
return false, nil
Expand All @@ -182,7 +182,7 @@ func ExpectLocalFileContainSubstringImmediately(filePath string, contents string
}

func ExpectLocalFileContents(filePath string, contents string) {
err := wait.PollImmediate(time.Second, time.Minute*2, func() (done bool, err error) {
err := wait.PollUntilContextTimeout(context.TODO(), time.Second, time.Minute*2, true, func(_ context.Context) (done bool, err error) {
out, err := os.ReadFile(filePath)
if err != nil {
if !os.IsNotExist(err) {
Expand Down
8 changes: 4 additions & 4 deletions e2e/tests/dependencies/dependencies.go
Original file line number Diff line number Diff line change
Expand Up @@ -492,8 +492,8 @@ dep2dep2wait

// wait until a pod has started
var pods *corev1.PodList
err = wait.Poll(time.Second, time.Minute, func() (done bool, err error) {
pods, err = kubeClient.RawClient().CoreV1().Pods(ns).List(context.TODO(), metav1.ListOptions{LabelSelector: selector.ReplacedLabel})
err = wait.PollUntilContextTimeout(context.TODO(), time.Second, time.Minute, false, func(ctx context.Context) (done bool, err error) {
pods, err = kubeClient.RawClient().CoreV1().Pods(ns).List(ctx, metav1.ListOptions{LabelSelector: selector.ReplacedLabel})
if err != nil {
return false, err
}
Expand All @@ -515,8 +515,8 @@ dep2dep2wait
framework.ExpectNoError(err)

// wait until all pods are killed
err = wait.Poll(time.Second, time.Minute, func() (done bool, err error) {
pods, err = kubeClient.RawClient().CoreV1().Pods(ns).List(context.TODO(), metav1.ListOptions{LabelSelector: selector.ReplacedLabel})
err = wait.PollUntilContextTimeout(context.TODO(), time.Second, time.Minute, false, func(ctx context.Context) (done bool, err error) {
pods, err = kubeClient.RawClient().CoreV1().Pods(ns).List(ctx, metav1.ListOptions{LabelSelector: selector.ReplacedLabel})
if err != nil {
return false, err
}
Expand Down
11 changes: 6 additions & 5 deletions e2e/tests/devspacehelper/inject.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,11 @@ package devspacehelper

import (
"context"
"github.com/onsi/ginkgo/v2"
"os"
"time"

"github.com/onsi/ginkgo/v2"

"github.com/loft-sh/devspace/cmd"
"github.com/loft-sh/devspace/cmd/flags"
"github.com/loft-sh/devspace/e2e/framework"
Expand Down Expand Up @@ -65,8 +66,8 @@ var _ = DevSpaceDescribe("devspacehelper", func() {

// wait until nginx pod is reachable
var pods *corev1.PodList
err = wait.Poll(time.Second, time.Minute, func() (done bool, err error) {
pods, err = kubeClient.RawClient().CoreV1().Pods(ns).List(context.TODO(), metav1.ListOptions{LabelSelector: "app=curl-container"})
err = wait.PollUntilContextTimeout(context.TODO(), time.Second, time.Minute, false, func(ctx context.Context) (done bool, err error) {
pods, err = kubeClient.RawClient().CoreV1().Pods(ns).List(ctx, metav1.ListOptions{LabelSelector: "app=curl-container"})
if err != nil {
return false, err
} else if len(pods.Items) == 0 || len(pods.Items[0].Status.ContainerStatuses) == 0 {
Expand Down Expand Up @@ -116,8 +117,8 @@ var _ = DevSpaceDescribe("devspacehelper", func() {

// wait until nginx pod is reachable
var pods *corev1.PodList
err = wait.Poll(time.Second, time.Minute, func() (done bool, err error) {
pods, err = kubeClient.RawClient().CoreV1().Pods(ns).List(context.TODO(), metav1.ListOptions{LabelSelector: "app=non-curl-container"})
err = wait.PollUntilContextTimeout(context.TODO(), time.Second, time.Minute, false, func(ctx context.Context) (done bool, err error) {
pods, err = kubeClient.RawClient().CoreV1().Pods(ns).List(ctx, metav1.ListOptions{LabelSelector: "app=non-curl-container"})
if err != nil {
return false, err
}
Expand Down
11 changes: 6 additions & 5 deletions e2e/tests/hooks/hooks.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,12 @@ package hooks

import (
"context"
"github.com/onsi/ginkgo/v2"
"os"
"sync"
"time"

"github.com/onsi/ginkgo/v2"

"github.com/loft-sh/devspace/cmd"
"github.com/loft-sh/devspace/cmd/flags"
"github.com/loft-sh/devspace/e2e/framework"
Expand Down Expand Up @@ -147,7 +148,7 @@ var _ = DevSpaceDescribe("hooks", func() {

// Read the 'once' hook output
onceOutput1 := ""
err = wait.PollImmediate(time.Second, time.Minute*2, func() (done bool, err error) {
err = wait.PollUntilContextTimeout(context.TODO(), time.Second, time.Minute*2, true, func(_ context.Context) (done bool, err error) {
onceOutput1, err = kubeClient.ExecByImageSelector("node", ns, []string{"cat", "/app/once.out"})
if err != nil {
return false, nil
Expand All @@ -159,7 +160,7 @@ var _ = DevSpaceDescribe("hooks", func() {

// Read the 'each' hook output
eachOutput1 := ""
err = wait.PollImmediate(time.Second, time.Minute*2, func() (done bool, err error) {
err = wait.PollUntilContextTimeout(context.TODO(), time.Second, time.Minute*2, true, func(_ context.Context) (done bool, err error) {
eachOutput1, err = kubeClient.ExecByImageSelector("node", ns, []string{"cat", "/app/each.out"})
if err != nil {
return false, nil
Expand Down Expand Up @@ -196,7 +197,7 @@ var _ = DevSpaceDescribe("hooks", func() {

// Wait for 'each' hook output to change
eachOutput2 := ""
err = wait.PollImmediate(time.Second, time.Minute*2, func() (done bool, err error) {
err = wait.PollUntilContextTimeout(context.TODO(), time.Second, time.Minute*2, true, func(_ context.Context) (done bool, err error) {
eachOutput2, err = kubeClient.ExecByImageSelector("node", ns, []string{"cat", "/app/each.out"})
if err != nil {
return false, nil
Expand All @@ -208,7 +209,7 @@ var _ = DevSpaceDescribe("hooks", func() {

// Read the 'once' hook output again
onceOutput2 := ""
err = wait.PollImmediate(time.Second, time.Minute*2, func() (done bool, err error) {
err = wait.PollUntilContextTimeout(context.TODO(), time.Second, time.Minute*2, true, func(_ context.Context) (done bool, err error) {
onceOutput2, err = kubeClient.ExecByImageSelector("node", ns, []string{"cat", "/app/once.out"})
if err != nil {
return false, nil
Expand Down
4 changes: 2 additions & 2 deletions e2e/tests/proxycommands/proxycommands.go
Original file line number Diff line number Diff line change
Expand Up @@ -78,8 +78,8 @@ var _ = DevSpaceDescribe("proxyCommands", func() {

// Get the expected Pod hostname
var pods *corev1.PodList
err = wait.Poll(time.Second, time.Minute, func() (done bool, err error) {
pods, err = kubeClient.RawClient().CoreV1().Pods(ns).List(context.TODO(), metav1.ListOptions{LabelSelector: "app.kubernetes.io/component=test"})
err = wait.PollUntilContextTimeout(context.TODO(), time.Second, time.Minute, false, func(ctx context.Context) (done bool, err error) {
pods, err = kubeClient.RawClient().CoreV1().Pods(ns).List(ctx, metav1.ListOptions{LabelSelector: "app.kubernetes.io/component=test"})
if err != nil {
return false, err
}
Expand Down
27 changes: 14 additions & 13 deletions e2e/tests/replacepods/replacepods.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,12 @@ package replacepods

import (
"context"
"github.com/onsi/ginkgo/v2"
"os"
"strings"
"time"

"github.com/onsi/ginkgo/v2"

"github.com/loft-sh/devspace/cmd"
"github.com/loft-sh/devspace/cmd/flags"
"github.com/loft-sh/devspace/e2e/framework"
Expand Down Expand Up @@ -115,8 +116,8 @@ var _ = DevSpaceDescribe("replacepods", func() {

// wait until a pod has started
var pods *corev1.PodList
err = wait.Poll(time.Second, time.Minute*3, func() (done bool, err error) {
pods, err = kubeClient.RawClient().CoreV1().Pods(ns).List(context.TODO(), metav1.ListOptions{LabelSelector: selector.ReplacedLabel})
err = wait.PollUntilContextTimeout(context.TODO(), time.Second, time.Minute*3, false, func(ctx context.Context) (done bool, err error) {
pods, err = kubeClient.RawClient().CoreV1().Pods(ns).List(ctx, metav1.ListOptions{LabelSelector: selector.ReplacedLabel})
if err != nil {
return false, err
}
Expand Down Expand Up @@ -156,8 +157,8 @@ var _ = DevSpaceDescribe("replacepods", func() {
framework.ExpectEqual(len(list.Items), 1)

// wait until a pod has started
err = wait.Poll(time.Second, time.Minute*3, func() (done bool, err error) {
pods, err = kubeClient.RawClient().CoreV1().Pods(ns).List(context.TODO(), metav1.ListOptions{LabelSelector: selector.ReplacedLabel})
err = wait.PollUntilContextTimeout(context.TODO(), time.Second, time.Minute*3, false, func(ctx context.Context) (done bool, err error) {
pods, err = kubeClient.RawClient().CoreV1().Pods(ns).List(ctx, metav1.ListOptions{LabelSelector: selector.ReplacedLabel})
if err != nil {
return false, err
}
Expand All @@ -179,8 +180,8 @@ var _ = DevSpaceDescribe("replacepods", func() {
framework.ExpectNoError(err)

// wait until all pods are killed
err = wait.Poll(time.Second, time.Minute*3, func() (done bool, err error) {
pods, err = kubeClient.RawClient().CoreV1().Pods(ns).List(context.TODO(), metav1.ListOptions{LabelSelector: selector.ReplacedLabel})
err = wait.PollUntilContextTimeout(context.TODO(), time.Second, time.Minute*3, false, func(ctx context.Context) (done bool, err error) {
pods, err = kubeClient.RawClient().CoreV1().Pods(ns).List(ctx, metav1.ListOptions{LabelSelector: selector.ReplacedLabel})
if err != nil {
return false, err
}
Expand Down Expand Up @@ -222,8 +223,8 @@ var _ = DevSpaceDescribe("replacepods", func() {

// wait until a pod has started
var pods *corev1.PodList
err = wait.Poll(time.Second, time.Minute, func() (done bool, err error) {
pods, err = kubeClient.RawClient().CoreV1().Pods(ns).List(context.TODO(), metav1.ListOptions{LabelSelector: selector.ReplacedLabel})
err = wait.PollUntilContextTimeout(context.TODO(), time.Second, time.Minute, false, func(ctx context.Context) (done bool, err error) {
pods, err = kubeClient.RawClient().CoreV1().Pods(ns).List(ctx, metav1.ListOptions{LabelSelector: selector.ReplacedLabel})
if err != nil {
return false, err
}
Expand Down Expand Up @@ -258,8 +259,8 @@ var _ = DevSpaceDescribe("replacepods", func() {
framework.ExpectEqual(len(list.Items), 1)

// wait until a pod has started
err = wait.Poll(time.Second, time.Minute, func() (done bool, err error) {
pods, err = kubeClient.RawClient().CoreV1().Pods(ns).List(context.TODO(), metav1.ListOptions{LabelSelector: selector.ReplacedLabel})
err = wait.PollUntilContextTimeout(context.TODO(), time.Second, time.Minute, false, func(ctx context.Context) (done bool, err error) {
pods, err = kubeClient.RawClient().CoreV1().Pods(ns).List(ctx, metav1.ListOptions{LabelSelector: selector.ReplacedLabel})
if err != nil {
return false, err
}
Expand Down Expand Up @@ -312,8 +313,8 @@ var _ = DevSpaceDescribe("replacepods", func() {
framework.ExpectNoError(err)

// wait until all pods are killed
err = wait.Poll(time.Second, time.Minute, func() (done bool, err error) {
pods, err = kubeClient.RawClient().CoreV1().Pods(ns).List(context.TODO(), metav1.ListOptions{LabelSelector: selector.ReplacedLabel})
err = wait.PollUntilContextTimeout(context.TODO(), time.Second, time.Minute, false, func(ctx context.Context) (done bool, err error) {
pods, err = kubeClient.RawClient().CoreV1().Pods(ns).List(ctx, metav1.ListOptions{LabelSelector: selector.ReplacedLabel})
if err != nil {
return false, err
}
Expand Down
12 changes: 6 additions & 6 deletions e2e/tests/sync/sync.go
Original file line number Diff line number Diff line change
Expand Up @@ -211,7 +211,7 @@ var _ = DevSpaceDescribe("sync", func() {
}()

// wait until files were synced
err = wait.PollImmediate(time.Second, time.Minute*2, func() (done bool, err error) {
err = wait.PollUntilContextTimeout(context.TODO(), time.Second, time.Minute*2, true, func(_ context.Context) (done bool, err error) {
out, err := kubeClient.ExecByImageSelector("node", ns, []string{"cat", "/app/file1.txt"})
if err != nil {
return false, nil
Expand All @@ -236,7 +236,7 @@ var _ = DevSpaceDescribe("sync", func() {
framework.ExpectNoError(err)

// wait for sync
err = wait.PollImmediate(time.Second, time.Minute*2, func() (done bool, err error) {
err = wait.PollUntilContextTimeout(context.TODO(), time.Second, time.Minute*2, true, func(_ context.Context) (done bool, err error) {
out, err := kubeClient.ExecByImageSelector("node", ns, []string{"cat", "/app/file3.txt"})
if err != nil {
return false, nil
Expand All @@ -252,7 +252,7 @@ var _ = DevSpaceDescribe("sync", func() {
framework.ExpectEqual(os.IsNotExist(err), true)

// check if file was downloaded through after hook
err = wait.PollImmediate(time.Second, time.Minute, func() (done bool, err error) {
err = wait.PollUntilContextTimeout(context.TODO(), time.Second, time.Minute, true, func(_ context.Context) (done bool, err error) {
out, err := os.ReadFile(filepath.Join(tempDir, "file5.txt"))
if err != nil {
if !os.IsNotExist(err) {
Expand Down Expand Up @@ -311,7 +311,7 @@ var _ = DevSpaceDescribe("sync", func() {
}()

// wait until files were synced
err = wait.PollImmediate(time.Second, time.Minute*2, func() (done bool, err error) {
err = wait.PollUntilContextTimeout(context.TODO(), time.Second, time.Minute*2, true, func(_ context.Context) (done bool, err error) {
out, err := kubeClient.ExecByImageSelector("node", ns, []string{"cat", "/watch/app/file1.txt"})
if err != nil {
return false, nil
Expand All @@ -335,7 +335,7 @@ var _ = DevSpaceDescribe("sync", func() {
err = os.WriteFile(filepath.Join(tempDir, "/project1/app/file3.txt"), []byte(payload1), 0666)
framework.ExpectNoError(err)

err = wait.PollImmediate(time.Second, time.Minute*2, func() (done bool, err error) {
err = wait.PollUntilContextTimeout(context.TODO(), time.Second, time.Minute*2, true, func(_ context.Context) (done bool, err error) {
out, err := kubeClient.ExecByImageSelector("node", ns, []string{"cat", "/watch/app/file3.txt"})
if err != nil {
return false, nil
Expand All @@ -350,7 +350,7 @@ var _ = DevSpaceDescribe("sync", func() {
err = os.WriteFile(filepath.Join(tempDir, "/project2/file4.txt"), []byte(payload2), 0666)
framework.ExpectNoError(err)

err = wait.PollImmediate(time.Second, time.Minute*2, func() (done bool, err error) {
err = wait.PollUntilContextTimeout(context.TODO(), time.Second, time.Minute*2, true, func(_ context.Context) (done bool, err error) {
out, err := kubeClient.ExecByImageSelector("node", ns, []string{"cat", "/watch/app/file4.txt"})
if err != nil {
return false, nil
Expand Down
19 changes: 10 additions & 9 deletions e2e/tests/terminal/terminal.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,12 @@ package terminal
import (
"bytes"
"context"
"os"
"path/filepath"
"strings"
"sync"
"time"

"github.com/loft-sh/devspace/cmd"
"github.com/loft-sh/devspace/cmd/flags"
"github.com/loft-sh/devspace/e2e/framework"
Expand All @@ -13,11 +19,6 @@ import (
kerrors "k8s.io/apimachinery/pkg/api/errors"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/util/wait"
"os"
"path/filepath"
"strings"
"sync"
"time"
)

var _ = DevSpaceDescribe("terminal", func() {
Expand Down Expand Up @@ -126,7 +127,7 @@ sleep 1000000

// wait until we get the first hostnames
var podName string
err = wait.PollImmediate(time.Second, time.Minute*3, func() (done bool, err error) {
err = wait.PollUntilContextTimeout(context.TODO(), time.Second, time.Minute*3, true, func(_ context.Context) (done bool, err error) {
lines := strings.Split(buffer.String(), "\n")
if len(lines) <= 1 {
return false, nil
Expand All @@ -142,8 +143,8 @@ sleep 1000000
framework.ExpectNoError(err)

// wait until pod is terminated
err = wait.PollImmediate(time.Second, time.Minute*3, func() (done bool, err error) {
_, err = kubeClient.RawClient().CoreV1().Pods(ns).Get(context.TODO(), podName, metav1.GetOptions{})
err = wait.PollUntilContextTimeout(context.TODO(), time.Second, time.Minute*3, true, func(ctx context.Context) (done bool, err error) {
_, err = kubeClient.RawClient().CoreV1().Pods(ns).Get(ctx, podName, metav1.GetOptions{})
if err != nil {
if kerrors.IsNotFound(err) {
return true, nil
Expand All @@ -157,7 +158,7 @@ sleep 1000000
framework.ExpectNoError(err)

// get new pod name
err = wait.PollImmediate(time.Second, time.Minute*3, func() (done bool, err error) {
err = wait.PollUntilContextTimeout(context.TODO(), time.Second, time.Minute*3, true, func(_ context.Context) (done bool, err error) {
lines := strings.Split(buffer.String(), "\n")
if len(lines) <= 1 {
return false, nil
Expand Down
3 changes: 2 additions & 1 deletion helper/util/restart_linux.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
package util

import (
"context"
"fmt"
"os"
"strconv"
Expand Down Expand Up @@ -79,7 +80,7 @@ func (*containerRestarter) RestartContainer() error {
if err != nil {
return nil
}
err = wait.PollImmediate(time.Second, 5*time.Second, func() (done bool, err error) {
err = wait.PollUntilContextTimeout(context.TODO(), time.Second, 5*time.Second, true, func(_ context.Context) (done bool, err error) {
_, err = os.Stat(procPath)
return os.IsNotExist(err), nil
})
Expand Down
Loading

0 comments on commit 1e2c02d

Please sign in to comment.