forked from hashicorp/vault
-
Notifications
You must be signed in to change notification settings - Fork 1
/
testing.go
84 lines (72 loc) · 1.83 KB
/
testing.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
package logical
import (
"reflect"
"testing"
"time"
"github.com/hashicorp/vault/helper/logformat"
log "github.com/mgutz/logxi/v1"
)
// TestRequest is a helper to create a purely in-memory Request struct.
func TestRequest(t *testing.T, op Operation, path string) *Request {
return &Request{
Operation: op,
Path: path,
Data: make(map[string]interface{}),
Storage: new(InmemStorage),
}
}
// TestStorage is a helper that can be used from unit tests to verify
// the behavior of a Storage impl.
func TestStorage(t *testing.T, s Storage) {
keys, err := s.List("")
if err != nil {
t.Fatalf("list error: %s", err)
}
if len(keys) > 0 {
t.Fatalf("should have no keys to start: %#v", keys)
}
entry := &StorageEntry{Key: "foo", Value: []byte("bar")}
if err := s.Put(entry); err != nil {
t.Fatalf("put error: %s", err)
}
actual, err := s.Get("foo")
if err != nil {
t.Fatalf("get error: %s", err)
}
if !reflect.DeepEqual(actual, entry) {
t.Fatalf("wrong value. Expected: %#v\nGot: %#v", entry, actual)
}
keys, err = s.List("")
if err != nil {
t.Fatalf("list error: %s", err)
}
if !reflect.DeepEqual(keys, []string{"foo"}) {
t.Fatalf("bad keys: %#v", keys)
}
if err := s.Delete("foo"); err != nil {
t.Fatalf("put error: %s", err)
}
keys, err = s.List("")
if err != nil {
t.Fatalf("list error: %s", err)
}
if len(keys) > 0 {
t.Fatalf("should have no keys to start: %#v", keys)
}
}
func TestSystemView() *StaticSystemView {
defaultLeaseTTLVal := time.Hour * 24
maxLeaseTTLVal := time.Hour * 24 * 2
return &StaticSystemView{
DefaultLeaseTTLVal: defaultLeaseTTLVal,
MaxLeaseTTLVal: maxLeaseTTLVal,
}
}
func TestBackendConfig() *BackendConfig {
bc := &BackendConfig{
Logger: logformat.NewVaultLogger(log.LevelTrace),
System: TestSystemView(),
}
bc.Logger.SetLevel(log.LevelTrace)
return bc
}