Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 10 additions & 11 deletions e2e/e2e_suite_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package e2e

import (
"context"
"os"
"runtime"
"testing"
Expand Down Expand Up @@ -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) {
Expand All @@ -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)
Expand Down
19 changes: 12 additions & 7 deletions e2e/framework/ssh_agent.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package framework

import (
"bytes"
"context"
"errors"
"fmt"
"io"
Expand All @@ -12,6 +13,7 @@ import (
"time"

"al.essio.dev/pkg/shellescape"
"k8s.io/apimachinery/pkg/util/wait"
)

type testLogger interface {
Expand Down Expand Up @@ -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 {
Expand Down
27 changes: 27 additions & 0 deletions e2e/framework/ssh_agent_test.go
Original file line number Diff line number Diff line change
@@ -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))
}
Loading