-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathpath_config_lease_test.go
More file actions
executable file
·148 lines (130 loc) · 3.19 KB
/
path_config_lease_test.go
File metadata and controls
executable file
·148 lines (130 loc) · 3.19 KB
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
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
package openstack
import (
"context"
"testing"
"time"
"github.com/hashicorp/vault/sdk/logical"
)
func TestConfigLease_ReadEmpty(t *testing.T) {
t.Parallel()
b, reqStorage := getTestBackend(t)
resp, err := b.HandleRequest(context.Background(), &logical.Request{
Operation: logical.ReadOperation,
Path: leaseConfigKey,
Storage: reqStorage,
})
if err != nil {
t.Fatalf("unexpected error: %v", err)
}
if resp != nil {
t.Fatal("expected nil response for empty lease config")
}
}
func TestConfigLease_CreateAndRead(t *testing.T) {
t.Parallel()
b, reqStorage := getTestBackend(t)
// Create lease config
resp, err := b.HandleRequest(context.Background(), &logical.Request{
Operation: logical.UpdateOperation,
Path: leaseConfigKey,
Data: map[string]interface{}{
"ttl": int64(3600),
},
Storage: reqStorage,
})
if err != nil {
t.Fatalf("unexpected error on create: %v", err)
}
if resp != nil && resp.IsError() {
t.Fatalf("unexpected error response: %v", resp.Error())
}
// Read and verify lease config
resp, err = b.HandleRequest(context.Background(), &logical.Request{
Operation: logical.ReadOperation,
Path: leaseConfigKey,
Storage: reqStorage,
})
if err != nil {
t.Fatalf("unexpected error on read: %v", err)
}
if resp == nil {
t.Fatal("expected response")
}
if resp.IsError() {
t.Fatalf("unexpected error response: %v", resp.Error())
}
if resp.Data["ttl"] != int64(3600) {
t.Errorf("ttl = %v, expected %v", resp.Data["ttl"], int64(3600))
}
}
func TestConfigLease_Update(t *testing.T) {
t.Parallel()
b, reqStorage := getTestBackend(t)
// Create initial lease config
_, err := b.HandleRequest(context.Background(), &logical.Request{
Operation: logical.UpdateOperation,
Path: leaseConfigKey,
Data: map[string]interface{}{
"ttl": int64(60),
},
Storage: reqStorage,
})
if err != nil {
t.Fatalf("unexpected error on create: %v", err)
}
// Update lease config
_, err = b.HandleRequest(context.Background(), &logical.Request{
Operation: logical.UpdateOperation,
Path: leaseConfigKey,
Data: map[string]interface{}{
"ttl": int64(120),
},
Storage: reqStorage,
})
if err != nil {
t.Fatalf("unexpected error on update: %v", err)
}
// Verify update
resp, err := b.HandleRequest(context.Background(), &logical.Request{
Operation: logical.ReadOperation,
Path: leaseConfigKey,
Storage: reqStorage,
})
if err != nil {
t.Fatalf("unexpected error on read: %v", err)
}
if resp.Data["ttl"] != int64(120) {
t.Errorf("ttl = %v, expected %v", resp.Data["ttl"], int64(120))
}
}
func TestLeaseConfig_Struct(t *testing.T) {
t.Parallel()
tests := []struct {
name string
config configLease
expected time.Duration
}{
{
name: "zero TTL",
config: configLease{TTL: 0},
expected: 0,
},
{
name: "one hour TTL",
config: configLease{TTL: time.Hour},
expected: time.Hour,
},
{
name: "custom TTL",
config: configLease{TTL: 30 * time.Minute},
expected: 30 * time.Minute,
},
}
for _, tc := range tests {
t.Run(tc.name, func(t *testing.T) {
if tc.config.TTL != tc.expected {
t.Errorf("TTL = %v, expected %v", tc.config.TTL, tc.expected)
}
})
}
}