-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathpath_checkout_sets_test.go
765 lines (717 loc) · 22.3 KB
/
path_checkout_sets_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
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
// Copyright (c) HashiCorp, Inc.
// SPDX-License-Identifier: MPL-2.0
package openldap
import (
"context"
"fmt"
"testing"
"time"
"github.com/hashicorp/vault/sdk/logical"
"github.com/stretchr/testify/require"
)
// The library of service accounts that can be checked out
// is a discrete set of features. This test suite provides
// end-to-end tests of these interrelated endpoints.
func TestCheckOut(t *testing.T) {
ctx := context.Background()
b, s := getBackend(false)
defer b.Cleanup(ctx)
req := &logical.Request{
Operation: logical.UpdateOperation,
Path: configPath,
Storage: s,
Data: map[string]interface{}{
"binddn": "euclid",
"password": "password",
"url": "ldap://ldap.forumsys.com:389",
"userdn": "cn=read-only-admin,dc=example,dc=com",
},
}
resp, err := b.HandleRequest(ctx, req)
if err != nil || (resp != nil && resp.IsError()) {
t.Fatalf("err: %v, resp: %v", err, resp.Error())
}
setName := "test-set"
ts := testSuite{
b: b,
s: s,
name: setName,
svcAccountNames: getTestSvcAccountNames(setName, 2),
}
// Exercise all set endpoints.
t.Run("write set", ts.WriteSet())
t.Run("read set", ts.ReadSet())
t.Run("read set status", ts.ReadSetStatus())
t.Run("write set toggle off", ts.WriteSetToggleOff())
t.Run("read set toggle off", ts.ReadSetToggleOff())
t.Run("write conflicting set", ts.WriteSetWithConflictingServiceAccounts())
t.Run("list sets", ts.ListSets([]string{ts.name}))
t.Run("delete set", ts.DeleteSet())
// Do some common updates on sets and ensure they work.
t.Run("write set", ts.WriteSet())
t.Run("add service account", ts.AddAnotherServiceAccount())
t.Run("remove service account", ts.RemoveServiceAccount())
t.Run("check initial status", ts.CheckInitialStatus())
t.Run("check out account", ts.PerformCheckOut())
t.Run("check updated status", ts.CheckUpdatedStatus())
t.Run("normal check in", ts.NormalCheckIn())
t.Run("return to initial status", ts.CheckInitialStatus())
t.Run("check out again", ts.PerformCheckOut())
t.Run("check updated status", ts.CheckUpdatedStatus())
t.Run("force check in", ts.ForceCheckIn())
t.Run("check all are available", ts.CheckInitialStatus())
}
func TestCheckOutHierarchicalPaths(t *testing.T) {
ctx := context.Background()
b, s := getBackend(false)
defer b.Cleanup(ctx)
req := &logical.Request{
Operation: logical.UpdateOperation,
Path: configPath,
Storage: s,
Data: map[string]interface{}{
"binddn": "euclid",
"password": "password",
"url": "ldap://ldap.forumsys.com:389",
"userdn": "cn=read-only-admin,dc=example,dc=com",
},
}
resp, err := b.HandleRequest(ctx, req)
if err != nil || (resp != nil && resp.IsError()) {
t.Fatalf("err: %v, resp: %v", err, resp.Error())
}
setNames := []string{"foo", "org/secure", "org/platform/dev", "org/platform/support"}
var tSuiteForList testSuite
for _, setName := range setNames {
ts := testSuite{
b: b,
s: s,
name: setName,
svcAccountNames: getTestSvcAccountNames(setName, 2),
}
// Exercise all set endpoints expect List Sets
t.Run("write set", ts.WriteSet())
t.Run("read set", ts.ReadSet())
t.Run("read set status", ts.ReadSetStatus())
t.Run("write set toggle off", ts.WriteSetToggleOff())
t.Run("read set toggle off", ts.ReadSetToggleOff())
t.Run("write conflicting set", ts.WriteSetWithConflictingServiceAccounts())
t.Run("delete set", ts.DeleteSet())
// Do some common updates on sets and ensure they work.
t.Run("write set", ts.WriteSet())
t.Run("add service account", ts.AddAnotherServiceAccount())
t.Run("remove service account", ts.RemoveServiceAccount())
t.Run("check initial status", ts.CheckInitialStatus())
t.Run("check out account", ts.PerformCheckOut())
t.Run("check updated status", ts.CheckUpdatedStatus())
t.Run("normal check in", ts.NormalCheckIn())
t.Run("return to initial status", ts.CheckInitialStatus())
t.Run("check out again", ts.PerformCheckOut())
t.Run("check updated status", ts.CheckUpdatedStatus())
t.Run("force check in", ts.ForceCheckIn())
t.Run("check all are available", ts.CheckInitialStatus())
// capture test suite so we can perform a listing on all the created sets
tSuiteForList = ts
}
// Reset the managed users to simulate startup memory state
b.managedUsers = make(map[string]struct{})
// Now finish the startup process by populating the managed users
b.loadManagedUsers(ctx, s)
for _, setName := range setNames {
ts := testSuite{
b: b,
s: s,
name: setName,
svcAccountNames: getTestSvcAccountNames(setName, 2),
}
t.Run("write conflicting set", ts.WriteSetWithConflictingServiceAccounts())
}
tests := []struct {
path string
expectedListResp []string
}{
{path: "org", expectedListResp: []string{"platform/", "secure"}},
{path: "org/platform", expectedListResp: []string{"dev", "support"}},
}
for _, tt := range tests {
// `LIST /library/:role_set`
// will return direct sub-keys split on "/" for each level (split)
t.Run("list sets hierarchy", tSuiteForList.ListSetsHierarchy(tt.path, tt.expectedListResp))
}
// `LIST /library`
// will return direct sub-keys split on "/" for the FIRST level (split) only
t.Run("list library hierarchy", tSuiteForList.ListSets([]string{"foo", "org/"}))
}
// TestCheckOutRaces executes a whole bunch of calls at once and only looks for
// races. Responses are ignored because they'll vary depending on execution order.
func TestCheckOutRaces(t *testing.T) {
if testing.Short() {
t.Skip("skipping check for races in the checkout system due to short flag")
}
ctx := context.Background()
b, s := getBackend(false)
defer b.Cleanup(ctx)
// Get 100 goroutines ready to go.
numParallel := 100
start := make(chan bool, 1)
end := make(chan bool, numParallel)
for i := 0; i < numParallel; i++ {
go func() {
<-start
b.HandleRequest(ctx, &logical.Request{
Operation: logical.CreateOperation,
Path: libraryPrefix + "test-set",
Storage: s,
Data: map[string]interface{}{
"service_account_names": []string{"[email protected]", "[email protected]"},
"ttl": "10h",
"max_ttl": "11h",
"disable_check_in_enforcement": true,
},
})
b.HandleRequest(ctx, &logical.Request{
Operation: logical.UpdateOperation,
Path: libraryPrefix + "test-set",
Storage: s,
Data: map[string]interface{}{
},
})
b.HandleRequest(ctx, &logical.Request{
Operation: logical.UpdateOperation,
Path: libraryPrefix + "test-set",
Storage: s,
Data: map[string]interface{}{
"service_account_names": []string{"[email protected]", "[email protected]"},
},
})
b.HandleRequest(ctx, &logical.Request{
Operation: logical.ReadOperation,
Path: libraryPrefix + "test-set",
Storage: s,
})
b.HandleRequest(ctx, &logical.Request{
Operation: logical.UpdateOperation,
Path: libraryPrefix + "test-set",
Storage: s,
Data: map[string]interface{}{
"service_account_names": []string{"[email protected]", "[email protected]"},
"ttl": "10h",
"disable_check_in_enforcement": false,
},
})
b.HandleRequest(ctx, &logical.Request{
Operation: logical.ReadOperation,
Path: libraryPrefix + "test-set",
Storage: s,
})
b.HandleRequest(ctx, &logical.Request{
Operation: logical.ReadOperation,
Path: libraryPrefix + "test-set/status",
Storage: s,
})
b.HandleRequest(ctx, &logical.Request{
Operation: logical.CreateOperation,
Path: libraryPrefix + "test-set2",
Storage: s,
Data: map[string]interface{}{
"service_account_names": "[email protected]",
},
})
b.HandleRequest(ctx, &logical.Request{
Operation: logical.ListOperation,
Path: libraryPrefix,
Storage: s,
})
b.HandleRequest(ctx, &logical.Request{
Operation: logical.DeleteOperation,
Path: libraryPrefix + "test-set",
Storage: s,
})
b.HandleRequest(ctx, &logical.Request{
Operation: logical.ReadOperation,
Path: libraryPrefix + "test-set/status",
Storage: s,
})
b.HandleRequest(ctx, &logical.Request{
Operation: logical.ReadOperation,
Path: libraryPrefix + "test-set/check-out",
Storage: s,
})
b.HandleRequest(ctx, &logical.Request{
Operation: logical.ReadOperation,
Path: libraryPrefix + "test-set/status",
Storage: s,
})
b.HandleRequest(ctx, &logical.Request{
Operation: logical.ReadOperation,
Path: libraryPrefix + "test-set/check-in",
Storage: s,
})
b.HandleRequest(ctx, &logical.Request{
Operation: logical.ReadOperation,
Path: libraryPrefix + "manage/test-set/check-in",
Storage: s,
})
end <- true
}()
}
// Start them all at once.
close(start)
// Wait for them all to finish.
timer := time.NewTimer(15 * time.Second)
for i := 0; i < numParallel; i++ {
select {
case <-timer.C:
t.Fatal("test took more than 15 seconds, may be deadlocked")
case <-end:
continue
}
}
}
type testSuite struct {
b logical.Backend
s logical.Storage
name string
svcAccountNames []string
}
func (ts testSuite) WriteSet() func(t *testing.T) {
return func(t *testing.T) {
req := &logical.Request{
Operation: logical.CreateOperation,
Path: libraryPrefix + ts.name,
Storage: ts.s,
Data: map[string]interface{}{
"service_account_names": ts.svcAccountNames,
"ttl": "10h",
"max_ttl": "11h",
"disable_check_in_enforcement": true,
},
}
resp, err := ts.b.HandleRequest(context.Background(), req)
if err != nil || (resp != nil && resp.IsError()) {
t.Fatalf("err: %v, resp: %v", err, resp.Error())
}
if resp != nil {
t.Fatalf("expected an empty response, got: %v", resp)
}
}
}
func (ts testSuite) AddAnotherServiceAccount() func(t *testing.T) {
newSvcAccountSet := append(ts.svcAccountNames, "[email protected]")
return func(t *testing.T) {
req := &logical.Request{
Operation: logical.UpdateOperation,
Path: libraryPrefix + ts.name,
Storage: ts.s,
Data: map[string]interface{}{
"service_account_names": newSvcAccountSet,
},
}
resp, err := ts.b.HandleRequest(context.Background(), req)
if err != nil || (resp != nil && resp.IsError()) {
t.Fatalf("err: %v, resp: %v", err, resp.Error())
}
if resp != nil {
t.Fatalf("expected an empty response, got: %v", resp)
}
}
}
func (ts testSuite) RemoveServiceAccount() func(t *testing.T) {
return func(t *testing.T) {
req := &logical.Request{
Operation: logical.UpdateOperation,
Path: libraryPrefix + ts.name,
Storage: ts.s,
Data: map[string]interface{}{
"service_account_names": ts.svcAccountNames,
},
}
resp, err := ts.b.HandleRequest(context.Background(), req)
if err != nil || (resp != nil && resp.IsError()) {
t.Fatalf("err: %v, resp: %v", err, resp.Error())
}
if resp != nil {
t.Fatalf("expected an empty response, got: %v", resp)
}
}
}
func (ts testSuite) ReadSet() func(t *testing.T) {
return func(t *testing.T) {
req := &logical.Request{
Operation: logical.ReadOperation,
Path: libraryPrefix + ts.name,
Storage: ts.s,
}
resp, err := ts.b.HandleRequest(context.Background(), req)
if err != nil || (resp != nil && resp.IsError()) {
t.Fatalf("err: %v, resp: %v", err, resp.Error())
}
if resp == nil {
t.Fatal("expected a response")
}
serviceAccountNames := resp.Data["service_account_names"].([]string)
if len(serviceAccountNames) != len(ts.svcAccountNames) {
t.Fatalf("expected %d", len(ts.svcAccountNames))
}
disableCheckInEnforcement := resp.Data["disable_check_in_enforcement"].(bool)
if !disableCheckInEnforcement {
t.Fatal("check-in enforcement should be disabled")
}
ttl := resp.Data["ttl"].(int64)
if ttl != 10*60*60 { // 10 hours
t.Fatal(ttl)
}
maxTTL := resp.Data["max_ttl"].(int64)
if maxTTL != 11*60*60 { // 11 hours
t.Fatal(maxTTL)
}
}
}
func (ts testSuite) WriteSetToggleOff() func(t *testing.T) {
return func(t *testing.T) {
req := &logical.Request{
Operation: logical.UpdateOperation,
Path: libraryPrefix + ts.name,
Storage: ts.s,
Data: map[string]interface{}{
"service_account_names": ts.svcAccountNames,
"ttl": "10h",
"disable_check_in_enforcement": false,
},
}
resp, err := ts.b.HandleRequest(context.Background(), req)
if err != nil || (resp != nil && resp.IsError()) {
t.Fatalf("err: %v, resp: %v", err, resp.Error())
}
if resp != nil {
t.Fatalf("expected an empty response, got: %v", resp)
}
}
}
func (ts testSuite) ReadSetToggleOff() func(t *testing.T) {
return func(t *testing.T) {
req := &logical.Request{
Operation: logical.ReadOperation,
Path: libraryPrefix + ts.name,
Storage: ts.s,
}
resp, err := ts.b.HandleRequest(context.Background(), req)
if err != nil || (resp != nil && resp.IsError()) {
t.Fatalf("err: %v, resp: %v", err, resp.Error())
}
if resp == nil {
t.Fatal("expected a response")
}
serviceAccountNames := resp.Data["service_account_names"].([]string)
if len(serviceAccountNames) != len(ts.svcAccountNames) {
t.Fatalf("expected %d", len(ts.svcAccountNames))
}
disableCheckInEnforcement := resp.Data["disable_check_in_enforcement"].(bool)
if disableCheckInEnforcement {
t.Fatal("check-in enforcement should be enabled")
}
}
}
func (ts testSuite) ReadSetStatus() func(t *testing.T) {
return func(t *testing.T) {
req := &logical.Request{
Operation: logical.ReadOperation,
Path: libraryPrefix + ts.name + "/status",
Storage: ts.s,
}
resp, err := ts.b.HandleRequest(context.Background(), req)
if err != nil || (resp != nil && resp.IsError()) {
t.Fatalf("err: %v, resp: %v", err, resp.Error())
}
if resp == nil {
t.Fatal("expected a response")
}
if len(resp.Data) != len(ts.svcAccountNames) {
t.Fatalf("expected %d service accounts in this set", len(ts.svcAccountNames))
}
for i := 0; i < len(ts.svcAccountNames); i++ {
n := ts.svcAccountNames[i]
if resp.Data[n] == nil {
t.Fatal("expected non-nil map")
}
testerStatus := resp.Data[n].(map[string]interface{})
if !testerStatus["available"].(bool) {
t.Fatal("should be available for checkout")
}
}
}
}
func (ts testSuite) WriteSetWithConflictingServiceAccounts() func(t *testing.T) {
existingSvcAcc := ts.svcAccountNames[0]
return func(t *testing.T) {
req := &logical.Request{
Operation: logical.CreateOperation,
Path: libraryPrefix + "test-set2",
Storage: ts.s,
Data: map[string]interface{}{
"service_account_names": existingSvcAcc,
},
}
resp, err := ts.b.HandleRequest(context.Background(), req)
if err != nil {
t.Fatal(err)
}
if resp == nil || !resp.IsError() {
t.Fatal("expected err response because we're adding a service account managed by another set")
}
}
}
func (ts testSuite) ListSets(expectedListResp []string) func(t *testing.T) {
return func(t *testing.T) {
req := &logical.Request{
Operation: logical.ListOperation,
Path: libraryPrefix,
Storage: ts.s,
}
resp, err := ts.b.HandleRequest(context.Background(), req)
if err != nil || (resp != nil && resp.IsError()) {
t.Fatalf("err: %v, resp: %v", err, resp.Error())
}
if resp == nil {
t.Fatal("expected a response")
}
if resp.Data["keys"] == nil {
t.Fatal("expected non-nil data")
}
listedKeys := resp.Data["keys"].([]string)
require.Equal(t, expectedListResp, listedKeys)
}
}
func (ts testSuite) ListSetsHierarchy(path string, expectedListResp []string) func(t *testing.T) {
return func(t *testing.T) {
req := &logical.Request{
Operation: logical.ListOperation,
Path: libraryPrefix + path + "/",
Storage: ts.s,
}
resp, err := ts.b.HandleRequest(context.Background(), req)
if err != nil || (resp != nil && resp.IsError()) {
t.Fatalf("err: %v, resp: %v", err, resp.Error())
}
if resp == nil {
t.Fatal("expected a response")
}
if resp.Data["keys"] == nil {
t.Fatal("expected non-nil data")
}
listedKeys := resp.Data["keys"].([]string)
require.Equal(t, expectedListResp, listedKeys)
}
}
func (ts testSuite) DeleteSet() func(t *testing.T) {
return func(t *testing.T) {
req := &logical.Request{
Operation: logical.DeleteOperation,
Path: libraryPrefix + ts.name,
Storage: ts.s,
}
resp, err := ts.b.HandleRequest(context.Background(), req)
if err != nil || (resp != nil && resp.IsError()) {
t.Fatalf("err: %v, resp: %v", err, resp.Error())
}
if resp != nil {
t.Fatalf("expected an empty response, got: %v", resp)
}
}
}
func (ts testSuite) CheckInitialStatus() func(t *testing.T) {
return func(t *testing.T) {
req := &logical.Request{
Operation: logical.ReadOperation,
Path: libraryPrefix + ts.name + "/status",
Storage: ts.s,
}
resp, err := ts.b.HandleRequest(context.Background(), req)
if err != nil || (resp != nil && resp.IsError()) {
t.Fatalf("err: %v, resp: %v", err, resp.Error())
}
if resp == nil {
t.Fatal("expected a response")
}
for _, svcAcc := range ts.svcAccountNames {
if resp.Data[svcAcc] == nil {
t.Fatal("expected map to not be nil")
}
tester1CheckOut := resp.Data[svcAcc].(map[string]interface{})
available := tester1CheckOut["available"].(bool)
if !available {
t.Fatalf("%s should be available", svcAcc)
}
}
}
}
func (ts testSuite) PerformCheckOut() func(t *testing.T) {
return func(t *testing.T) {
req := &logical.Request{
Operation: logical.UpdateOperation,
Path: libraryPrefix + ts.name + "/check-out",
Storage: ts.s,
}
resp, err := ts.b.HandleRequest(context.Background(), req)
if err != nil || (resp != nil && resp.IsError()) {
t.Fatalf("err: %v, resp: %v", err, resp.Error())
}
if resp == nil {
t.Fatal("expected a response")
}
if resp.Data == nil {
t.Fatal("expected resp data to not be nil")
}
if resp.Data["service_account_name"] == nil {
t.Fatal("expected string to be populated")
}
if resp.Data["service_account_name"].(string) == "" {
t.Fatal("service account name should be populated")
}
if resp.Data["password"].(string) == "" {
t.Fatal("password should be populated")
}
if !resp.Secret.Renewable {
t.Fatal("lease should be renewable")
}
if resp.Secret.TTL != time.Hour*10 {
t.Fatal("expected 10h TTL")
}
if resp.Secret.MaxTTL != time.Hour*11 {
t.Fatal("expected 11h TTL")
}
if resp.Secret.InternalData["service_account_name"].(string) == "" {
t.Fatal("internal service account name should not be empty")
}
}
}
func (ts testSuite) CheckUpdatedStatus() func(t *testing.T) {
return func(t *testing.T) {
req := &logical.Request{
Operation: logical.ReadOperation,
Path: libraryPrefix + ts.name + "/status",
Storage: ts.s,
}
resp, err := ts.b.HandleRequest(context.Background(), req)
if err != nil || (resp != nil && resp.IsError()) {
t.Fatalf("err: %v, resp: %v", err, resp.Error())
}
if resp == nil {
t.Fatal("expected a response")
}
if resp.Data == nil {
t.Fatal("expected data to not be nil")
}
if resp.Data[ts.svcAccountNames[0]] == nil {
t.Fatal("expected map to not be nil")
}
tester1CheckOut := resp.Data[ts.svcAccountNames[0]].(map[string]interface{})
tester1Available := tester1CheckOut["available"].(bool)
if resp.Data[ts.svcAccountNames[1]] == nil {
t.Fatal("expected map to not be nil")
}
tester2CheckOut := resp.Data[ts.svcAccountNames[1]].(map[string]interface{})
tester2Available := tester2CheckOut["available"].(bool)
if tester1Available && tester2Available {
t.Fatal("one of the testers should not be available")
}
}
}
func (ts testSuite) NormalCheckIn() func(t *testing.T) {
return func(t *testing.T) {
req := &logical.Request{
Operation: logical.UpdateOperation,
Path: libraryPrefix + ts.name + "/check-in",
Storage: ts.s,
}
resp, err := ts.b.HandleRequest(context.Background(), req)
if err != nil || (resp != nil && resp.IsError()) {
t.Fatalf("err: %v, resp: %v", err, resp.Error())
}
if resp == nil {
t.Fatal("expected a response")
}
checkIns := resp.Data["check_ins"].([]string)
if len(checkIns) != 1 {
t.Fatal("expected 1 check-in")
}
}
}
func (ts testSuite) ForceCheckIn() func(t *testing.T) {
return func(t *testing.T) {
req := &logical.Request{
Operation: logical.UpdateOperation,
Path: libraryManagePrefix + ts.name + "/check-in",
Storage: ts.s,
}
resp, err := ts.b.HandleRequest(context.Background(), req)
if err != nil || (resp != nil && resp.IsError()) {
t.Fatalf("err: %v, resp: %v", err, resp.Error())
}
if resp == nil {
t.Fatal("expected a response")
}
checkIns := resp.Data["check_ins"].([]string)
if len(checkIns) != 1 {
t.Fatal("expected 1 check-in")
}
}
}
func TestCheckOut_librarySet_Validate(t *testing.T) {
tests := []struct {
name string
set *librarySet
wantErr bool
}{
{
name: "valid library set",
set: &librarySet{
ServiceAccountNames: []string{"name1"},
TTL: time.Minute,
MaxTTL: 2 * time.Minute,
},
},
{
name: "invalid library set with empty list of service account names",
set: &librarySet{
ServiceAccountNames: []string{},
TTL: time.Minute,
MaxTTL: 2 * time.Minute,
},
wantErr: true,
},
{
name: "invalid library set with empty service account name",
set: &librarySet{
ServiceAccountNames: []string{""},
TTL: time.Minute,
MaxTTL: 2 * time.Minute,
},
wantErr: true,
},
{
name: "invalid library set with max TTL less than TTL",
set: &librarySet{
ServiceAccountNames: []string{"name1", "name2"},
TTL: 2 * time.Minute,
MaxTTL: time.Minute,
},
wantErr: true,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if err := tt.set.Validate(); (err != nil) != tt.wantErr {
t.Errorf("Validate() error = %v, wantErr %v", err, tt.wantErr)
}
})
}
}
func getTestSvcAccountNames(name string, count int) []string {
var s []string
for i := 0; i < count; i++ {
s = append(s, fmt.Sprintf("%s-tester-%[email protected]", name, i))
}
return s
}