/
main_test.go
110 lines (92 loc) · 2.59 KB
/
main_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
102
103
104
105
106
107
108
109
110
package vaku2
import (
"errors"
"net"
"testing"
"github.com/hashicorp/go-hclog"
"github.com/hashicorp/vault/api"
"github.com/hashicorp/vault/http"
"github.com/hashicorp/vault/vault"
"github.com/stretchr/testify/assert"
)
var (
// errInject is used when injecting errors in tests
errInject = errors.New("injected error")
)
// When tests are looping over kvMountVersions and the path is noMountPrefix they will not prefix
// the path with the mount version to allow testing on a nonexistent mount.
var noMountPrefix = "nomount"
// kvMountVersions lists the types of kv mounts for vault. There are currently two k/v mount types
// and vaku supports both. Tests should run against each version and return the same results.
var kvMountVersions = []string{"1", "2"}
// seeds holds the canonical secret seeds for every test.
var seeds = map[string]map[string]interface{}{
"test/foo": {
"value": "bar",
},
"test/value": {
"fizz": "buzz",
"foo": "bar",
},
"test/fizz": {
"fizz": "buzz",
"foo": "bar",
},
"test/HToOeKKD": {
"3zqxVbJY": "TvOjGxvC",
},
"test/inner/WKNC3muM": {
"IY1C148K": "JxBfEt91",
"iwVzPqbY": "0NH9GlR1",
},
"test/inner/A2xlzTfE": {
"Eg5ljS7t": "BHRMKjj1",
"quqr32S5": "pcidzSMW",
},
"test/inner/again/inner/UCrt6sZT": {
"Eg5ljS7t": "6F1B5nBg",
"quqr32S5": "81iY4HAN",
"r6R0JUzX": "rs1mCRB5",
},
}
// testServer creates a new inmem Vault server and returns a seeded client that points to it.
func testServer(t *testing.T) (net.Listener, *api.Client) {
t.Helper()
core, _, token := vault.TestCoreUnsealedWithConfig(t, &vault.CoreConfig{Logger: hclog.NewNullLogger()})
ln, addr := http.TestServer(t, core)
client, err := api.NewClient(api.DefaultConfig())
assert.NoError(t, err)
client.SetToken(token)
client.SetAddress(addr)
for _, ver := range kvMountVersions {
err = client.Sys().Mount(ver+"/", &api.MountInput{
Type: "kv",
Options: map[string]string{
"version": ver,
},
})
assert.NoError(t, err)
for path, secret := range seeds {
_, err := client.Logical().Write(PathJoin(ver, path), secret)
assert.NoError(t, err)
}
}
return ln, client
}
// errLogical implements logical and injects ouputs
type errLogical struct {
secret *api.Secret
err error
}
func (e *errLogical) List(path string) (*api.Secret, error) {
return e.secret, e.err
}
func (e *errLogical) Read(path string) (*api.Secret, error) {
return e.secret, e.err
}
func (e *errLogical) Write(path string, data map[string]interface{}) (*api.Secret, error) {
return e.secret, e.err
}
func (e *errLogical) Delete(path string) (*api.Secret, error) {
return e.secret, e.err
}