-
Notifications
You must be signed in to change notification settings - Fork 0
/
setup_test.go
96 lines (85 loc) · 2.05 KB
/
setup_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
package main
import (
"context"
"flag"
"os"
"testing"
"github.com/retailnext/vault-init/pkgs/objects"
"github.com/retailnext/vault-init/pkgs/vault"
"github.com/stretchr/testify/assert"
"github.com/urfave/cli/v2"
)
func TestInitVault(t *testing.T) {
initoutFile := "./testinitout.txt"
initOut := `
{"keys":[],"keys_base64":[],"recovery_keys":["35a24","1ac7","ac0f5"],"recovery_keys_base64":["AOk","dK3rH","rPA"],"root_token":"root-token"}
`
if err := os.WriteFile(initoutFile, []byte(initOut), 0666); err != nil {
t.Fatal(err)
}
defer os.Remove(initoutFile)
ctx := context.Background()
vserver, _, err := vault.StartTestDevVaultInTest(t, ctx)
if err != nil {
t.Fatal(err)
}
// setup clients
vaultAddr, err := vserver.HttpHostAddress(ctx)
if err != nil {
t.Fatal(err)
}
set := flag.NewFlagSet("test", 0)
set.String("vault-addr", vaultAddr, "doc")
set.String("initout", initoutFile, "doc")
cliCtx := cli.NewContext(nil, set, nil)
mainClients, err := SetupClients(cliCtx)
if err != nil {
t.Fatal(err)
}
// initVault
err = mainClients.InitVault()
if err != nil {
t.Fatal(err)
}
vaultToken := mainClients.Clients.VaultClient.GetToken()
assert.Equal(t, "root-token", vaultToken)
}
func TestExecutePostTasks(t *testing.T) {
postTasks := `
- type: policy
task:
name: admin
policy_content: |
path "sys/leases/*"
{
capabilities = ["create", "read", "update", "delete", "list", "sudo"]
}
`
expectedPolicy := `path "sys/leases/*"
{
capabilities = ["create", "read", "update", "delete", "list", "sudo"]
}
`
ctx := context.Background()
_, vclient, err := vault.StartTestDevVaultInTest(t, ctx)
if err != nil {
t.Fatal(err)
}
mainClients := &MainClients{}
mainClients.Clients = &objects.Clients{
VaultClient: vclient,
}
err = mainClients.SetupPostTasks([]byte(postTasks))
if err != nil {
t.Fatal(err)
}
err = mainClients.ExecutePostTasks()
if err != nil {
t.Fatal(err)
}
actualPolicy, err := vclient.GetPolicy("admin")
if err != nil {
t.Fatal()
}
assert.Equal(t, expectedPolicy, actualPolicy)
}