forked from mch1307/vaultlib
-
Notifications
You must be signed in to change notification settings - Fork 2
/
integration_test.go
91 lines (75 loc) · 2.36 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, longLivedRoleID, longLivedSecretID string
var vaultVersion string = *flag.String("vaultVersion", "1.0.1", "provide vault version to be tested against")
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 execCommand(name string, args ...string) ([]byte, error) {
cmd := exec.Command(name, args...)
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")
return cmd.Output()
}
func prepareVault() {
err := startVault(vaultVersion)
if err != nil {
log.Fatalf("Error in initVaultDev.sh %v", err)
}
out, err := execCommand("./vault", "read", "-field=role_id", "auth/approle/role/my-role/role-id")
if err != nil {
log.Fatalf("error getting role id %v %v", err, out)
}
vaultRoleID = string(out)
out, err = execCommand("./vault", "write", "-field=secret_id", "-f", "auth/approle/role/my-role/secret-id")
if err != nil {
log.Fatalf("error getting secret id %v", err)
}
vaultSecretID = string(out)
out, err = execCommand("./vault", "read", "-field=role_id", "auth/approle/role/no-kv/role-id")
if err != nil {
log.Fatalf("error getting role id %v %v", err, out)
}
noKVRoleID = string(out)
out, err = execCommand("./vault", "write", "-field=secret_id", "-f", "auth/approle/role/no-kv/secret-id")
if err != nil {
log.Fatalf("error getting secret id %v", err)
}
noKVSecretID = string(out)
out, err = execCommand("./vault", "read", "-field=role_id", "auth/approle/role/long-lived/role-id")
if err != nil {
log.Fatalf("error getting role id %v %v", err, out)
}
longLivedRoleID = string(out)
out, err = execCommand("./vault", "write", "-field=secret_id", "-f", "auth/approle/role/long-lived/secret-id")
if err != nil {
log.Fatalf("error getting secret id %v", err)
}
longLivedSecretID = string(out)
os.Unsetenv("VAULT_TOKEN")
fmt.Println("Vault initialized successfully")
}
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
}