-
Notifications
You must be signed in to change notification settings - Fork 10
/
integration_test.go
91 lines (77 loc) · 2.32 KB
/
integration_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
package vaultlib
import (
"flag"
"fmt"
"log"
"os"
"os/exec"
"testing"
)
var vaultRoleID, vaultSecretID, noKVRoleID, noKVSecretID string
var vaultVersion string
func init() {
flag.StringVar(&vaultVersion, "vaultVersion", "1.0.1", "provide vault version to be tested against")
flag.Parse()
}
func TestMain(m *testing.M) {
fmt.Println("Testing with Vault version", vaultVersion)
fmt.Println("TestMain: Preparing Vault server")
prepareVault()
ret := m.Run()
os.Exit(ret)
}
func prepareVault() {
err := startVault(vaultVersion)
if err != nil {
log.Fatalf("Error in initVaultDev.sh %v", err)
}
cmd := exec.Command("./vault", "read", "-field=role_id", "auth/approle/role/my-role/role-id")
cmd.Env = os.Environ()
cmd.Env = append(cmd.Env, "VAULT_TOKEN=my-dev-root-vault-token")
cmd.Env = append(cmd.Env, "VAULT_ADDR=http://localhost:8200")
out, err := cmd.Output()
if err != nil {
log.Fatalf("error getting role id %v %v", err, out)
}
vaultRoleID = string(out)
cmd = exec.Command("./vault", "write", "-field=secret_id", "-f", "auth/approle/role/my-role/secret-id")
cmd.Env = os.Environ()
cmd.Env = append(cmd.Env, "VAULT_TOKEN=my-dev-root-vault-token")
cmd.Env = append(cmd.Env, "VAULT_ADDR=http://localhost:8200")
out, err = cmd.Output()
if err != nil {
log.Fatalf("error getting secret id %v", err)
}
vaultSecretID = string(out)
cmd = exec.Command("./vault", "read", "-field=role_id", "auth/approle/role/no-kv/role-id")
cmd.Env = os.Environ()
cmd.Env = append(cmd.Env, "VAULT_TOKEN=my-dev-root-vault-token")
cmd.Env = append(cmd.Env, "VAULT_ADDR=http://localhost:8200")
out, err = cmd.Output()
if err != nil {
log.Fatalf("error getting role id %v %v", err, out)
}
noKVRoleID = string(out)
cmd = exec.Command("./vault", "write", "-field=secret_id", "-f", "auth/approle/role/no-kv/secret-id")
cmd.Env = os.Environ()
cmd.Env = append(cmd.Env, "VAULT_TOKEN=my-dev-root-vault-token")
cmd.Env = append(cmd.Env, "VAULT_ADDR=http://localhost:8200")
out, err = cmd.Output()
if err != nil {
log.Fatalf("error getting secret id %v", err)
}
noKVSecretID = string(out)
os.Unsetenv("VAULT_TOKEN")
}
func startVault(version string) error {
cmd := exec.Command("bash", "./test-files/initVaultDev.sh", version)
err := cmd.Start()
if err != nil {
return err
}
err = cmd.Wait()
if err != nil {
return err
}
return nil
}