-
Notifications
You must be signed in to change notification settings - Fork 1
/
vault_test.go
101 lines (85 loc) · 2.38 KB
/
vault_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
92
93
94
95
96
97
98
99
100
101
package main
import (
"log"
"os"
"testing"
"github.com/hashicorp/vault/api"
dockertest "github.com/ory/dockertest/v3"
)
var c *api.Client
func TestMain(m *testing.M) {
// uses a sensible default on windows (tcp/http) and linux/osx (socket)
pool, err := dockertest.NewPool("")
if err != nil {
log.Fatalf("Could not connect to docker: %s", err)
}
// pulls an image, creates a container based on it and runs it
resource, err := pool.RunWithOptions(&dockertest.RunOptions{
Repository: "hashicorp/vault",
Tag: "latest",
Env: []string{
"VAULT_ADDR=http://localhost:8200",
"VAULT_API_ADDR=http://0.0.0.0:8200",
"VAULT_DEV_ROOT_TOKEN_ID=root",
},
CapAdd: []string{"IPC_LOCK"},
// Cmd: []string{"vault", "server", "-dev-tls", "-dev-listen-address=0.0.0.0:8200"},
})
if err != nil {
log.Fatalf("Could not start resource: %s", err)
}
// exponential backoff-retry, because the application in the container might not be ready to accept connections yet
if err := pool.Retry(func() error {
var err error
c, err = api.NewClient(api.DefaultConfig())
if err != nil {
return err
}
c.SetToken("root")
err = c.SetAddress("http://127.0.0.1:" + resource.GetPort("8200/tcp"))
if err != nil {
return err
}
_, err = c.Sys().Health()
return err
}); err != nil {
log.Fatalf("Could not connect to docker: %s", err)
}
code := m.Run()
// You can't defer this because os.Exit doesn't care for defer
if err := pool.Purge(resource); err != nil {
log.Fatalf("could not purge resource: %s", err)
}
os.Exit(code)
}
// func TestStoreAndGet(t *testing.T) {
// v := newVault(c.Address(), "test/", c.Token())
// secret := "my secret"
// token, err := v.Store(secret)
// if err != nil {
// t.Fatalf("no error expected, got %v", err)
// }
// msg, err := v.Get(token)
// if err != nil {
// t.Fatalf("no error expected, got %v", err)
// }
// if msg != secret {
// t.Fatalf("expected message %s, got: %s", secret, msg)
// }
// }
// func TestMsgCanOnlyBeAccessedOnce(t *testing.T) {
// v := newVault(c.Address(), "test/", c.Token())
// secret := "my secret"
// token, err := v.Store(secret)
// if err != nil {
// t.Fatalf("no error expected, got %v", err)
// }
// _, err = v.Get(token)
// if err != nil {
// t.Fatalf("no error expected, got %v", err)
// }
// _, err = v.Get(token)
// if err == nil {
// t.Fatal("error expected, got nil")
// }
// }