Skip to content

Commit 6c43fdb

Browse files
committed
fix(test): Wait for Vault server in watch tests
Fix flaky tests by polling Vault's /v1/sys/health endpoint. This ensures the server is ready before tests attempt to connect, preventing a race condition during Vault dev server startup. Signed-off-by: Ville Vesilehto <ville@vesilehto.fi>
1 parent 1adb94d commit 6c43fdb

File tree

1 file changed

+26
-0
lines changed

1 file changed

+26
-0
lines changed

watch/watch_test.go

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,11 +7,13 @@ import (
77
"encoding/json"
88
"io"
99
"log"
10+
"net/http"
1011
"os"
1112
"os/exec"
1213
"path/filepath"
1314
"strings"
1415
"testing"
16+
"time"
1517

1618
dep "github.com/hashicorp/consul-template/dependency"
1719
"github.com/hashicorp/vault/api"
@@ -79,6 +81,30 @@ func newTestVault() *vaultServer {
7981
if err := cmd.Start(); err != nil {
8082
panic("vault failed to start: " + err.Error())
8183
}
84+
85+
// Wait for Vault to become available
86+
client := &http.Client{Timeout: 1 * time.Second}
87+
healthURL := vaultAddr + "/v1/sys/health"
88+
startTime := time.Now()
89+
timeout := 30 * time.Second
90+
91+
for {
92+
if time.Since(startTime) > timeout {
93+
panic("timed out waiting for vault dev server to start")
94+
}
95+
96+
resp, err := client.Get(healthURL)
97+
if err == nil && resp.StatusCode == http.StatusOK {
98+
resp.Body.Close()
99+
break
100+
}
101+
if resp != nil {
102+
resp.Body.Close()
103+
}
104+
105+
time.Sleep(200 * time.Millisecond)
106+
}
107+
82108
return &vaultServer{
83109
cmd: cmd,
84110
}

0 commit comments

Comments
 (0)