-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathsys_test.go
130 lines (113 loc) · 2.96 KB
/
sys_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
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
// Copyright 2021 Outreach Corporation. All Rights Reserved.
package vault_client //nolint:revive // Why: We're using - in the name
import (
"context"
"testing"
"time"
"github.com/getoutreach/gobox/pkg/differs"
"github.com/google/go-cmp/cmp"
"github.com/mitchellh/mapstructure"
)
func TestClient_Health(t *testing.T) {
type args struct {
ctx context.Context
}
tests := []struct {
name string
args args
want map[string]interface{}
wantErr bool
}{
{
name: "should return basic health",
args: args{
ctx: context.Background(),
},
want: map[string]interface{}{
"Sealed": false,
"ClusterName": differs.AnyString(),
"ClusterID": differs.AnyString(),
"Initialized": true,
"PerformanceStandby": false,
"ReplicationDrMode": "disabled",
"ReplicationPerfMode": "",
"ServerTimeUtc": differs.Customf(func(o interface{}) bool {
v, ok := o.(int)
if !ok {
return false
}
ti := time.Unix(int64(v), 0)
return !ti.IsZero()
}),
"Standby": false,
"Version": differs.AnyString(),
},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
c, cleanup := createTestVaultServer(t, false)
defer cleanup()
oGot, err := c.Health(tt.args.ctx)
// convert to map[string]interface for cmp.Diff
got := make(map[string]interface{})
mapstructure.Decode(oGot, &got)
if (err != nil) != tt.wantErr {
t.Errorf("Client.Health() error = %v, wantErr %v", err, tt.wantErr)
return
}
if diff := cmp.Diff(tt.want, got, differs.Custom()); diff != "" {
t.Errorf("Client.Health(): %s", diff)
}
})
}
}
func TestClient_Initialize(t *testing.T) {
vc, cleanupFn := createTestVaultServer(t, true)
defer cleanupFn()
ctx := context.Background()
resp, err := vc.Initialize(ctx, &InitializeOptions{
SecretShares: 10,
SecretThreshold: 10,
})
if err != nil {
t.Errorf("Failed to initialize vault: Initialize() = %v", err)
return
}
// convert to map[string]interface for cmp.Diff
got := make(map[string]interface{})
mapstructure.Decode(resp, &got)
want := map[string]interface{}{
// check that keys is a []string and len(10)
"Keys": differs.Customf(func(o interface{}) bool {
v, ok := o.([]string)
if !ok {
return false
}
return len(v) == 10
}),
"RecoveryKeys": differs.Customf(func(o interface{}) bool {
_, ok := o.([]string)
return ok
}),
"RootToken": differs.AnyString(),
}
if diff := cmp.Diff(want, got, differs.Custom()); diff != "" {
t.Errorf("Initialize(): %s", diff)
return
}
}
func TestClient_CreateEngine(t *testing.T) {
vc, cleanupFn := createTestVaultServer(t, false)
defer cleanupFn()
ctx := context.Background()
// create the secret engine
if err := vc.CreateEngine(ctx, "deploy", &CreateEngineOptions{
Type: "kv",
Options: map[string]interface{}{
"version": 2,
},
}); err != nil {
t.Errorf("Failed to create a KV2 engine: CreateEngine() = %v", err)
}
}