-
Notifications
You must be signed in to change notification settings - Fork 10
/
onthelambda_test.go
136 lines (124 loc) · 3.15 KB
/
onthelambda_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
131
132
133
134
135
136
package onthelambda
import (
vault "github.com/hashicorp/vault/api"
"testing"
"time"
)
func Test_isExpired(t *testing.T) {
tests := []struct {
ttl string
expired bool
}{
// expirationWindow is hardcoded to be 10 seconds
{ttl: "200h", expired: false},
{ttl: "6m", expired: false},
{ttl: "4m", expired: false},
{ttl: "11s", expired: false},
{ttl: "9s", expired: true},
{ttl: "0s", expired: true},
{ttl: "10h", expired: false},
}
for _, test := range tests {
ttl, _ := time.ParseDuration(test.ttl)
tokenExpiration = time.Now().Add(ttl)
rv := isExpired()
if rv != test.expired {
t.Errorf("Expiration check failed! With %s left, got '%t', expected '%t'.", test.ttl, rv, test.expired)
}
}
}
func Test_shouldRenew(t *testing.T) {
tests := []struct {
ttl string
renew bool
}{
// renewalWindow is hardcoded to be 300 seconds
{ttl: "200h", renew: false},
{ttl: "6m", renew: false},
{ttl: "4m", renew: true},
{ttl: "11s", renew: true},
{ttl: "9s", renew: true},
{ttl: "0s", renew: true},
{ttl: "10h", renew: false},
}
for _, test := range tests {
ttl, _ := time.ParseDuration(test.ttl)
tokenExpiration = time.Now().Add(ttl)
rv := shouldRenew()
if rv != test.renew {
t.Errorf("Renewal check failed! With %s left, got '%t', expected '%t'.", test.ttl, rv, test.renew)
}
}
}
func Test_parseToken(t *testing.T) {
tests := []struct {
secret *vault.Secret
token string
ttlStr string
renewable bool
roughExpiration time.Time
}{
{
secret: &vault.Secret{
Auth: &vault.SecretAuth{
ClientToken: "banana",
Renewable: true,
LeaseDuration: 3600,
},
},
token: "banana",
ttlStr: "1h",
renewable: true,
roughExpiration: time.Now().Add(time.Hour),
},
{
secret: &vault.Secret{
Auth: &vault.SecretAuth{
ClientToken: "apple",
Renewable: false,
LeaseDuration: 1800,
},
},
token: "apple",
ttlStr: "30m",
renewable: false,
roughExpiration: time.Now().Add(time.Duration(30) * time.Minute),
},
{
secret: &vault.Secret{
Auth: &vault.SecretAuth{
ClientToken: "plum",
Renewable: true,
LeaseDuration: 7200,
},
},
token: "plum",
ttlStr: "2h",
renewable: true,
roughExpiration: time.Now().Add(time.Duration(2) * time.Hour),
},
}
for _, test := range tests {
ttl, _ := time.ParseDuration(test.ttlStr)
err := parseToken(test.secret)
if err != nil {
t.Errorf("Failed to parse token from secret %#v", test.secret)
continue
}
if token != test.token {
t.Errorf("Token mismatch. Got %q, expected %q.", token, test.token)
}
if tokenIsRenewable != test.renewable {
t.Errorf("IsRenewable mismatch. Got %t, expected %t.", tokenIsRenewable, test.renewable)
}
if tokenTTL != ttl {
t.Errorf("TTL mismatch. Got %q, expected %q.", tokenTTL, ttl)
}
if tokenExpiration.Before(test.roughExpiration.Add(-time.Second)) {
t.Errorf("Expiration is too early!")
}
if tokenExpiration.After(test.roughExpiration.Add(time.Second)) {
t.Errorf("Expiration is too late!")
}
}
}