2016-08-14 01:33:48 +00:00
|
|
|
package testutil
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
2017-07-23 02:04:36 +00:00
|
|
|
"math/rand"
|
2016-08-14 01:33:48 +00:00
|
|
|
"os"
|
|
|
|
"os/exec"
|
2017-07-23 02:04:36 +00:00
|
|
|
"time"
|
2016-08-14 01:33:48 +00:00
|
|
|
|
|
|
|
"github.com/hashicorp/nomad/nomad/structs"
|
|
|
|
"github.com/hashicorp/nomad/nomad/structs/config"
|
|
|
|
vapi "github.com/hashicorp/vault/api"
|
2017-07-25 21:21:59 +00:00
|
|
|
"github.com/mitchellh/go-testing-interface"
|
2016-08-14 01:33:48 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
// TestVault is a test helper. It uses a fork/exec model to create a test Vault
|
|
|
|
// server instance in the background and can be initialized with policies, roles
|
|
|
|
// and backends mounted. The test Vault instances can be used to run a unit test
|
|
|
|
// and offers and easy API to tear itself down on test end. The only
|
|
|
|
// prerequisite is that the Vault binary is on the $PATH.
|
|
|
|
|
|
|
|
// TestVault wraps a test Vault server launched in dev mode, suitable for
|
|
|
|
// testing.
|
|
|
|
type TestVault struct {
|
2017-07-23 02:04:36 +00:00
|
|
|
cmd *exec.Cmd
|
2017-07-25 21:21:59 +00:00
|
|
|
t testing.T
|
2017-07-23 02:04:36 +00:00
|
|
|
waitCh chan error
|
2016-08-14 01:33:48 +00:00
|
|
|
|
|
|
|
Addr string
|
|
|
|
HTTPAddr string
|
|
|
|
RootToken string
|
|
|
|
Config *config.VaultConfig
|
|
|
|
Client *vapi.Client
|
|
|
|
}
|
|
|
|
|
|
|
|
// NewTestVault returns a new TestVault instance that has yet to be started
|
2017-07-25 21:21:59 +00:00
|
|
|
func NewTestVault(t testing.T) *TestVault {
|
2017-07-23 23:21:25 +00:00
|
|
|
for i := 10; i >= 0; i-- {
|
|
|
|
port := getPort()
|
|
|
|
token := structs.GenerateUUID()
|
|
|
|
bind := fmt.Sprintf("-dev-listen-address=127.0.0.1:%d", port)
|
|
|
|
http := fmt.Sprintf("http://127.0.0.1:%d", port)
|
|
|
|
root := fmt.Sprintf("-dev-root-token-id=%s", token)
|
|
|
|
|
2017-07-26 00:42:36 +00:00
|
|
|
cmd := exec.Command("vault", "server", "-dev", bind, root)
|
2017-07-23 23:21:25 +00:00
|
|
|
cmd.Stdout = os.Stdout
|
|
|
|
cmd.Stderr = os.Stderr
|
|
|
|
|
|
|
|
// Build the config
|
|
|
|
conf := vapi.DefaultConfig()
|
|
|
|
conf.Address = http
|
|
|
|
|
|
|
|
// Make the client and set the token to the root token
|
|
|
|
client, err := vapi.NewClient(conf)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("failed to build Vault API client: %v", err)
|
|
|
|
}
|
|
|
|
client.SetToken(token)
|
|
|
|
|
|
|
|
enable := true
|
|
|
|
tv := &TestVault{
|
|
|
|
cmd: cmd,
|
|
|
|
t: t,
|
|
|
|
Addr: bind,
|
|
|
|
HTTPAddr: http,
|
|
|
|
RootToken: token,
|
|
|
|
Client: client,
|
|
|
|
Config: &config.VaultConfig{
|
|
|
|
Enabled: &enable,
|
|
|
|
Token: token,
|
|
|
|
Addr: http,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := tv.cmd.Start(); err != nil {
|
|
|
|
tv.t.Fatalf("failed to start vault: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Start the waiter
|
|
|
|
tv.waitCh = make(chan error, 1)
|
|
|
|
go func() {
|
|
|
|
err := tv.cmd.Wait()
|
|
|
|
tv.waitCh <- err
|
|
|
|
}()
|
|
|
|
|
|
|
|
// Ensure Vault started
|
|
|
|
var startErr error
|
|
|
|
select {
|
|
|
|
case startErr = <-tv.waitCh:
|
|
|
|
case <-time.After(time.Duration(500*TestMultiplier()) * time.Millisecond):
|
|
|
|
}
|
|
|
|
|
|
|
|
if startErr != nil && i == 0 {
|
|
|
|
t.Fatalf("failed to start vault: %v", startErr)
|
|
|
|
} else if startErr != nil {
|
|
|
|
wait := time.Duration(rand.Int31n(2000)) * time.Millisecond
|
|
|
|
time.Sleep(wait)
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
waitErr := tv.waitForAPI()
|
|
|
|
if waitErr != nil && i == 0 {
|
|
|
|
t.Fatalf("failed to start vault: %v", waitErr)
|
|
|
|
} else if waitErr != nil {
|
|
|
|
wait := time.Duration(rand.Int31n(2000)) * time.Millisecond
|
|
|
|
time.Sleep(wait)
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
return tv
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// NewTestVaultDelayed returns a test Vault server that has not been started.
|
|
|
|
// Start must be called and it is the callers responsibility to deal with any
|
|
|
|
// port conflicts that may occur and retry accordingly.
|
|
|
|
func NewTestVaultDelayed(t testing.T) *TestVault {
|
2016-08-14 01:33:48 +00:00
|
|
|
port := getPort()
|
|
|
|
token := structs.GenerateUUID()
|
|
|
|
bind := fmt.Sprintf("-dev-listen-address=127.0.0.1:%d", port)
|
|
|
|
http := fmt.Sprintf("http://127.0.0.1:%d", port)
|
|
|
|
root := fmt.Sprintf("-dev-root-token-id=%s", token)
|
|
|
|
|
2017-07-26 00:42:36 +00:00
|
|
|
cmd := exec.Command("vault", "server", "-dev", bind, root)
|
2016-08-14 01:33:48 +00:00
|
|
|
cmd.Stdout = os.Stdout
|
|
|
|
cmd.Stderr = os.Stderr
|
|
|
|
|
|
|
|
// Build the config
|
|
|
|
conf := vapi.DefaultConfig()
|
|
|
|
conf.Address = http
|
|
|
|
|
|
|
|
// Make the client and set the token to the root token
|
|
|
|
client, err := vapi.NewClient(conf)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("failed to build Vault API client: %v", err)
|
|
|
|
}
|
2016-08-15 01:56:32 +00:00
|
|
|
client.SetToken(token)
|
2016-08-14 01:33:48 +00:00
|
|
|
|
2016-10-11 01:04:39 +00:00
|
|
|
enable := true
|
2016-08-14 01:33:48 +00:00
|
|
|
tv := &TestVault{
|
|
|
|
cmd: cmd,
|
|
|
|
t: t,
|
|
|
|
Addr: bind,
|
|
|
|
HTTPAddr: http,
|
2016-08-22 20:57:27 +00:00
|
|
|
RootToken: token,
|
2016-08-14 01:33:48 +00:00
|
|
|
Client: client,
|
|
|
|
Config: &config.VaultConfig{
|
2016-10-11 01:04:39 +00:00
|
|
|
Enabled: &enable,
|
2016-08-14 01:33:48 +00:00
|
|
|
Token: token,
|
|
|
|
Addr: http,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
return tv
|
|
|
|
}
|
|
|
|
|
|
|
|
// Start starts the test Vault server and waits for it to respond to its HTTP
|
|
|
|
// API
|
2017-07-23 23:21:25 +00:00
|
|
|
func (tv *TestVault) Start() error {
|
2016-08-14 01:33:48 +00:00
|
|
|
if err := tv.cmd.Start(); err != nil {
|
|
|
|
tv.t.Fatalf("failed to start vault: %v", err)
|
|
|
|
}
|
|
|
|
|
2017-07-23 02:04:36 +00:00
|
|
|
// Start the waiter
|
|
|
|
tv.waitCh = make(chan error, 1)
|
|
|
|
go func() {
|
|
|
|
err := tv.cmd.Wait()
|
|
|
|
tv.waitCh <- err
|
|
|
|
}()
|
|
|
|
|
|
|
|
// Ensure Vault started
|
|
|
|
select {
|
|
|
|
case err := <-tv.waitCh:
|
2017-07-23 23:21:25 +00:00
|
|
|
return err
|
2017-07-23 02:04:36 +00:00
|
|
|
case <-time.After(time.Duration(500*TestMultiplier()) * time.Millisecond):
|
|
|
|
}
|
|
|
|
|
2017-07-23 23:21:25 +00:00
|
|
|
return tv.waitForAPI()
|
2016-08-14 01:33:48 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Stop stops the test Vault server
|
|
|
|
func (tv *TestVault) Stop() {
|
2016-08-15 01:56:32 +00:00
|
|
|
if tv.cmd.Process == nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2016-08-14 01:33:48 +00:00
|
|
|
if err := tv.cmd.Process.Kill(); err != nil {
|
|
|
|
tv.t.Errorf("err: %s", err)
|
|
|
|
}
|
2017-07-23 02:04:36 +00:00
|
|
|
if tv.waitCh != nil {
|
|
|
|
<-tv.waitCh
|
|
|
|
}
|
2016-08-14 01:33:48 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// waitForAPI waits for the Vault HTTP endpoint to start
|
|
|
|
// responding. This is an indication that the agent has started.
|
2017-07-23 23:21:25 +00:00
|
|
|
func (tv *TestVault) waitForAPI() error {
|
|
|
|
var waitErr error
|
2016-08-14 01:33:48 +00:00
|
|
|
WaitForResult(func() (bool, error) {
|
|
|
|
inited, err := tv.Client.Sys().InitStatus()
|
|
|
|
if err != nil {
|
|
|
|
return false, err
|
|
|
|
}
|
|
|
|
return inited, nil
|
|
|
|
}, func(err error) {
|
2017-07-23 23:21:25 +00:00
|
|
|
waitErr = err
|
2016-08-14 01:33:48 +00:00
|
|
|
})
|
2017-07-23 23:21:25 +00:00
|
|
|
return waitErr
|
2016-08-14 01:33:48 +00:00
|
|
|
}
|
|
|
|
|
2017-07-23 02:04:36 +00:00
|
|
|
func getPort() int {
|
|
|
|
return 1030 + int(rand.Int31n(6440))
|
2016-08-14 01:33:48 +00:00
|
|
|
}
|
2017-01-27 23:06:01 +00:00
|
|
|
|
|
|
|
// VaultVersion returns the Vault version as a string or an error if it couldn't
|
|
|
|
// be determined
|
|
|
|
func VaultVersion() (string, error) {
|
|
|
|
cmd := exec.Command("vault", "version")
|
|
|
|
out, err := cmd.Output()
|
|
|
|
return string(out), err
|
|
|
|
}
|