From 0639ceb410f6b2ebce988c4e5a7c886fba87820b Mon Sep 17 00:00:00 2001 From: Samuel K Date: Fri, 16 Oct 2026 18:33:00 -0600 Subject: [PATCH] refactor(e2e): use wait.PollUntilContextTimeout in suite and ssh agent helper --- e2e/e2e_suite_test.go | 21 ++++++++++----------- e2e/framework/ssh_agent.go | 19 ++++++++++++------- e2e/framework/ssh_agent_test.go | 27 +++++++++++++++++++++++++++ 3 files changed, 49 insertions(+), 18 deletions(-) create mode 100644 e2e/framework/ssh_agent_test.go diff --git a/e2e/e2e_suite_test.go b/e2e/e2e_suite_test.go index 27b318b232..dd956526ac 100644 --- a/e2e/e2e_suite_test.go +++ b/e2e/e2e_suite_test.go @@ -1,6 +1,7 @@ package e2e import ( + "context" "os" "runtime" "testing" @@ -43,6 +44,7 @@ import ( _ "github.com/devsy-org/devsy/e2e/tests/upgrade" "github.com/onsi/ginkgo/v2" "github.com/onsi/gomega" + "k8s.io/apimachinery/pkg/util/wait" ) func TestMain(m *testing.M) { @@ -67,17 +69,14 @@ func TestRunE2ETests(t *testing.T) { go framework.ServeAgent() // wait for http server to be up and running (max 30s) - deadline := time.After(30 * time.Second) - for { - select { - case <-deadline: - t.Fatal("timeout waiting for DEVSY_AGENT_URL to be set after 30s") - default: - } - time.Sleep(time.Second) - if os.Getenv("DEVSY_AGENT_URL") != "" { - break - } + err := wait.PollUntilContextTimeout( + context.Background(), time.Second, 30*time.Second, true, + func(context.Context) (bool, error) { + return os.Getenv("DEVSY_AGENT_URL") != "", nil + }, + ) + if err != nil { + t.Fatal("timeout waiting for DEVSY_AGENT_URL to be set after 30s") } } gomega.RegisterFailHandler(ginkgo.Fail) diff --git a/e2e/framework/ssh_agent.go b/e2e/framework/ssh_agent.go index 7010ceda06..6e03bf7631 100644 --- a/e2e/framework/ssh_agent.go +++ b/e2e/framework/ssh_agent.go @@ -2,6 +2,7 @@ package framework import ( "bytes" + "context" "errors" "fmt" "io" @@ -12,6 +13,7 @@ import ( "time" "al.essio.dev/pkg/shellescape" + "k8s.io/apimachinery/pkg/util/wait" ) type testLogger interface { @@ -108,14 +110,17 @@ func startSSHAgentProcess(sockPath string) (*exec.Cmd, *bytes.Buffer, error) { // waitForAgentSocket polls until the agent's socket file appears so subsequent // ssh-add invocations have something to connect to. func waitForAgentSocket(sockPath string) error { - deadline := time.Now().Add(2 * time.Second) - for time.Now().Before(deadline) { - if _, statErr := os.Stat(sockPath); statErr == nil { - return nil - } - time.Sleep(25 * time.Millisecond) + err := wait.PollUntilContextTimeout( + context.Background(), 25*time.Millisecond, 2*time.Second, true, + func(context.Context) (bool, error) { + _, statErr := os.Stat(sockPath) + return statErr == nil, nil + }, + ) + if err != nil { + return fmt.Errorf("ssh-agent did not bind socket %s within 2s: %w", sockPath, err) } - return fmt.Errorf("ssh-agent did not bind socket %s within 2s", sockPath) + return nil } func addKeyToAgent(sockPath, keyPath string) error { diff --git a/e2e/framework/ssh_agent_test.go b/e2e/framework/ssh_agent_test.go new file mode 100644 index 0000000000..1a66e69b07 --- /dev/null +++ b/e2e/framework/ssh_agent_test.go @@ -0,0 +1,27 @@ +package framework + +import ( + "os" + "path/filepath" + "testing" + "time" + + "github.com/stretchr/testify/require" +) + +func TestWaitForAgentSocket_ReturnsWhenSocketAppears(t *testing.T) { + sockPath := filepath.Join(t.TempDir(), "agent.sock") + + go func() { + time.Sleep(50 * time.Millisecond) + _ = os.WriteFile(sockPath, nil, 0o600) + }() + + require.NoError(t, waitForAgentSocket(sockPath)) +} + +func TestWaitForAgentSocket_ErrorWhenSocketNeverAppears(t *testing.T) { + sockPath := filepath.Join(t.TempDir(), "missing.sock") + + require.Error(t, waitForAgentSocket(sockPath)) +}