Skip to content

Commit e42a153

Browse files
committed
test: dynamic vault addr for watch test
Signed-off-by: Ville Vesilehto <[email protected]>
1 parent 1e1eb21 commit e42a153

File tree

1 file changed

+13
-2
lines changed

1 file changed

+13
-2
lines changed

watch/watch_test.go

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@ import (
88
"encoding/json"
99
"fmt"
1010
"io"
11+
"log"
12+
"net"
1113
"net/http"
1214
"os"
1315
"os/exec"
@@ -21,14 +23,14 @@ import (
2123
)
2224

2325
const (
24-
vaultAddr = "http://127.0.0.1:8200"
2526
vaultToken = "a_token"
2627
)
2728

2829
var (
2930
testVault *vaultServer
3031
testClients *dep.ClientSet
3132
tokenRoleId string
33+
vaultAddr string
3234
)
3335

3436
func TestMain(m *testing.M) {
@@ -37,7 +39,15 @@ func TestMain(m *testing.M) {
3739

3840
// sub-main so I can use defer
3941
func main(m *testing.M) int {
40-
//log.SetOutput(io.Discard)
42+
log.SetOutput(io.Discard)
43+
// Find a free port for the Vault server
44+
listener, err := net.Listen("tcp", "127.0.0.1:0")
45+
if err != nil {
46+
panic(fmt.Sprintf("failed to find free port for vault: %v", err))
47+
}
48+
vaultAddr = fmt.Sprintf("http://%s", listener.Addr().String())
49+
listener.Close()
50+
4151
testVault = newTestVault()
4252
defer func() { testVault.Stop() }()
4353

@@ -94,6 +104,7 @@ func newTestVault() *vaultServer {
94104
args := []string{
95105
"server", "-dev", "-dev-root-token-id", vaultToken,
96106
"-dev-no-store-token",
107+
"-dev-listen-address", strings.TrimPrefix(vaultAddr, "http://"),
97108
}
98109
cmd := exec.Command("vault", args...)
99110
var stdout, stderr bytes.Buffer

0 commit comments

Comments
 (0)