forked from aws/aws-sdk-go-v2
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcredential_cache_test.go
301 lines (267 loc) · 6.77 KB
/
credential_cache_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
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
package aws
import (
"context"
"fmt"
"math/rand"
"sync"
"sync/atomic"
"testing"
"time"
"github.com/aws/aws-sdk-go-v2/internal/sdk"
)
type stubCredentialsProvider struct {
creds Credentials
expires time.Time
err error
onInvalidate func(*stubCredentialsProvider)
}
func (s *stubCredentialsProvider) Retrieve(ctx context.Context) (Credentials, error) {
creds := s.creds
creds.Source = "stubCredentialsProvider"
creds.CanExpire = !s.expires.IsZero()
creds.Expires = s.expires
return creds, s.err
}
func (s *stubCredentialsProvider) Invalidate() {
s.onInvalidate(s)
}
func TestCredentialsCache_Cache(t *testing.T) {
expect := Credentials{
AccessKeyID: "key",
SecretAccessKey: "secret",
CanExpire: true,
Expires: time.Now().Add(10 * time.Minute),
}
var called bool
p := NewCredentialsCache(CredentialsProviderFunc(func(ctx context.Context) (Credentials, error) {
if called {
t.Fatalf("expect provider.Retrieve to only be called once")
}
called = true
return expect, nil
}))
for i := 0; i < 2; i++ {
creds, err := p.Retrieve(context.Background())
if err != nil {
t.Fatalf("expect no error, got %v", err)
}
if e, a := expect, creds; e != a {
t.Errorf("expect %v credential, got %v", e, a)
}
}
}
func TestCredentialsCache_Expires(t *testing.T) {
orig := sdk.NowTime
defer func() { sdk.NowTime = orig }()
var mockTime time.Time
sdk.NowTime = func() time.Time { return mockTime }
cases := []struct {
Creds func() Credentials
Called int
}{
{
Called: 2,
Creds: func() Credentials {
return Credentials{
AccessKeyID: "key",
SecretAccessKey: "secret",
CanExpire: true,
Expires: mockTime.Add(5),
}
},
},
{
Called: 1,
Creds: func() Credentials {
return Credentials{
AccessKeyID: "key",
SecretAccessKey: "secret",
}
},
},
{
Called: 6,
Creds: func() Credentials {
return Credentials{
AccessKeyID: "key",
SecretAccessKey: "secret",
CanExpire: true,
Expires: mockTime,
}
},
},
}
for _, c := range cases {
var called int
p := NewCredentialsCache(CredentialsProviderFunc(func(ctx context.Context) (Credentials, error) {
called++
return c.Creds(), nil
}))
p.Retrieve(context.Background())
p.Retrieve(context.Background())
p.Retrieve(context.Background())
mockTime = mockTime.Add(10)
p.Retrieve(context.Background())
p.Retrieve(context.Background())
p.Retrieve(context.Background())
if e, a := c.Called, called; e != a {
t.Errorf("expect %v called, got %v", e, a)
}
}
}
func TestCredentialsCache_ExpireTime(t *testing.T) {
orig := sdk.NowTime
defer func() { sdk.NowTime = orig }()
var mockTime time.Time
sdk.NowTime = func() time.Time { return mockTime }
cases := map[string]struct {
ExpireTime time.Time
ExpiryWindow time.Duration
JitterFrac float64
Validate func(t *testing.T, v time.Time)
}{
"no expire window": {
Validate: func(t *testing.T, v time.Time) {
t.Helper()
if e, a := mockTime, v; !e.Equal(a) {
t.Errorf("expect %v, got %v", e, a)
}
},
},
"expire window": {
ExpireTime: mockTime.Add(100),
ExpiryWindow: 50,
Validate: func(t *testing.T, v time.Time) {
t.Helper()
if e, a := mockTime.Add(50), v; !e.Equal(a) {
t.Errorf("expect %v, got %v", e, a)
}
},
},
"expire window with jitter": {
ExpireTime: mockTime.Add(100),
JitterFrac: 0.5,
ExpiryWindow: 50,
Validate: func(t *testing.T, v time.Time) {
t.Helper()
max := mockTime.Add(75)
min := mockTime.Add(50)
if v.Before(min) {
t.Errorf("expect %v to be before %s", v, min)
}
if v.After(max) {
t.Errorf("expect %v to be after %s", v, max)
}
},
},
"no expire window with jitter": {
ExpireTime: mockTime,
JitterFrac: 0.5,
Validate: func(t *testing.T, v time.Time) {
t.Helper()
if e, a := mockTime, v; !e.Equal(a) {
t.Errorf("expect %v, got %v", e, a)
}
},
},
}
for name, tt := range cases {
t.Run(name, func(t *testing.T) {
p := NewCredentialsCache(CredentialsProviderFunc(func(ctx context.Context) (Credentials, error) {
return Credentials{
AccessKeyID: "accessKey",
SecretAccessKey: "secretKey",
CanExpire: true,
Expires: tt.ExpireTime,
}, nil
}), func(options *CredentialsCacheOptions) {
options.ExpiryWindow = tt.ExpiryWindow
options.ExpiryWindowJitterFrac = tt.JitterFrac
})
credentials, err := p.Retrieve(context.Background())
if err != nil {
t.Fatalf("expect no error, got %v", err)
}
tt.Validate(t, credentials.Expires)
})
}
}
func TestCredentialsCache_Error(t *testing.T) {
p := NewCredentialsCache(CredentialsProviderFunc(func(ctx context.Context) (Credentials, error) {
return Credentials{}, fmt.Errorf("failed")
}))
creds, err := p.Retrieve(context.Background())
if err == nil {
t.Fatalf("expect error, not none")
}
if e, a := "failed", err.Error(); e != a {
t.Errorf("expect %q, got %q", e, a)
}
if e, a := (Credentials{}), creds; e != a {
t.Errorf("expect empty credentials, got %v", a)
}
}
func TestCredentialsCache_Race(t *testing.T) {
expect := Credentials{
AccessKeyID: "key",
SecretAccessKey: "secret",
}
var called bool
p := NewCredentialsCache(CredentialsProviderFunc(func(ctx context.Context) (Credentials, error) {
time.Sleep(time.Duration(rand.Intn(10)) * time.Millisecond)
if called {
t.Fatalf("expect provider.Retrieve only called once")
}
called = true
return expect, nil
}))
var wg sync.WaitGroup
wg.Add(100)
for i := 0; i < 100; i++ {
go func() {
time.Sleep(time.Duration(rand.Intn(10)) * time.Millisecond)
creds, err := p.Retrieve(context.Background())
if err != nil {
t.Errorf("expect no error, got %v", err)
}
if e, a := expect, creds; e != a {
t.Errorf("expect %v, got %v", e, a)
}
wg.Done()
}()
}
wg.Wait()
}
type stubConcurrentProvider struct {
called uint32
done chan struct{}
}
func (s *stubConcurrentProvider) Retrieve(ctx context.Context) (Credentials, error) {
atomic.AddUint32(&s.called, 1)
<-s.done
return Credentials{
AccessKeyID: "AKIAIOSFODNN7EXAMPLE",
SecretAccessKey: "wJalrXUtnFEMI/K7MDENG/bPxRfiCYEXAMPLEKEY",
}, nil
}
func TestCredentialsCache_RetrieveConcurrent(t *testing.T) {
stub := &stubConcurrentProvider{
done: make(chan struct{}),
}
provider := NewCredentialsCache(stub)
var wg sync.WaitGroup
wg.Add(2)
for i := 0; i < 2; i++ {
go func() {
provider.Retrieve(context.Background())
wg.Done()
}()
}
// Validates that a single call to Retrieve is shared between two calls to
// Retrieve method call.
stub.done <- struct{}{}
wg.Wait()
if e, a := uint32(1), atomic.LoadUint32(&stub.called); e != a {
t.Errorf("expected %v, got %v", e, a)
}
}