forked from algolia/sup3rS3cretMes5age
-
Notifications
You must be signed in to change notification settings - Fork 0
/
vault_test.go
91 lines (75 loc) · 2.01 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
package main
import (
"log"
"os"
"testing"
"github.com/hashicorp/vault/api"
dockertest "gopkg.in/ory-am/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.Run("vault", "latest", []string{"VAULT_ADDR", "VAULT_DEV_ROOT_TOKEN_ID=rootsecret"})
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("rootsecret")
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(), 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(), 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")
}
}