-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest_aws-profile.sh
575 lines (500 loc) · 14.9 KB
/
test_aws-profile.sh
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
#!/usr/bin/env bats -v
#
# Tests for aws-profile.
#
# Usage:
# ./test_aws-profile.bats
# bats ./test_aws-profile.bats
#
# Requirements:
#
# apt install bats jq python3 python3-pip
# pip3 install awscli
# source install
#
#
# Helper functions
#
function run_aws_profile_add() {
run bash -c "aws-profile add $1 << EOF
$2
$3
$4
$5
$6
EOF
"
}
function clean_profiles() {
export AWS_PROFILE=""
rm -rf ~/.aws/
return 0
}
#
# Test setup and teardown
#
setup() {
clean_profiles
}
teardown() {
clean_profiles
}
#
# Test aws-profile use
#
@test "Test command usage: aws-profile use without profile-name" {
run aws-profile use
[ "$status" -eq 1 ]
[[ "${lines[@]}" == *"Usage"* ]]
}
@test "Test command usage: aws-profile use" {
run aws-profile use non-existing
[ "${status}" -eq 1 ]
[[ "${lines[@]}" == *"does not exist!"* ]]
}
@test "Test command usage: aws-profile use existing" {
run_aws_profile_add default AWS_ACCESS_KEY_ID AWS_SECRET_ACCESS_KEY REGION FORMAT AWS_SESSION_TOKEN
run aws-profile use default
[ "${status}" -eq 0 ]
[[ "${lines[@]}" == *"profile"*"default"* ]]
[[ "${lines[@]}" == *"access_key"* ]]
[[ "${lines[@]}" == *"secret_key"* ]]
[[ "${lines[@]}" == *"region"* ]]
}
@test "Test command usage: aws-profile use existing shows override warning" {
run_aws_profile_add default AWS_ACCESS_KEY_ID AWS_SECRET_ACCESS_KEY REGION FORMAT AWS_SESSION_TOKEN
export AWS_DEFAULT_REGION=TEST
run aws-profile use default
[ "${status}" -eq 0 ]
[[ "${lines[@]}" == *"The following variable(s) are set and might override the ones set in the profile: 'AWS_DEFAULT_REGION'"* ]]
[[ "${lines[@]}" == *"profile"*"default"* ]]
[[ "${lines[@]}" == *"access_key"* ]]
[[ "${lines[@]}" == *"secret_key"* ]]
[[ "${lines[@]}" == *"region"* ]]
}
#
# Test aws-profile help
#
@test "Test command usage: aws-profile help" {
run aws-profile help
[ "${status}" -eq 0 ]
[[ "${lines[@]}" == *"Usage"* ]]
}
#
# Test aws-profile add
#
@test "Test command usage: aws-profile add without profile name" {
run aws-profile add
[ "${status}" -eq 1 ]
}
@test "Test command usage: aws-profile add with profile name" {
run_aws_profile_add default AWS_ACCESS_KEY_ID AWS_SECRET_ACCESS_KEY REGION FORMAT AWS_SESSION_TOKEN
[ "${status}" -eq 0 ]
# Test whether aws config file has correct content
expected_aws_config="[default]
region = REGION
output = FORMAT"
actual_content=$(cat ~/.aws/config)
[ "$actual_content" == "$expected_aws_config" ]
# Test whether aws credentials file has correct content
actual_content=$(cat ~/.aws/credentials)
expected_aws_credentials="[default]
aws_access_key_id = AWS_ACCESS_KEY_ID
aws_secret_access_key = AWS_SECRET_ACCESS_KEY
aws_session_token = AWS_SESSION_TOKEN"
[ "$actual_content" == "$expected_aws_credentials" ]
}
@test "Test command usage: aws-profile add multiple" {
run_aws_profile_add p1 AAKI1 ASAK1 R1 F1 S1
run_aws_profile_add p2 AAKI2 ASAK2 R2 F2 S2
run_aws_profile_add p3 AAKI3 ASAK3 R3 F3 S3
# Test whether aws config file has correct content
expected_content="[profile p1]
region = R1
output = F1
[profile p2]
region = R2
output = F2
[profile p3]
region = R3
output = F3"
actual_content=$(cat ~/.aws/config)
[ "$actual_content" == "$expected_content" ]
# Test whether aws credentials file has correct content
actual_content=$(cat ~/.aws/credentials)
expected_content="[p1]
aws_access_key_id = AAKI1
aws_secret_access_key = ASAK1
aws_session_token = S1
[p2]
aws_access_key_id = AAKI2
aws_secret_access_key = ASAK2
aws_session_token = S2
[p3]
aws_access_key_id = AAKI3
aws_secret_access_key = ASAK3
aws_session_token = S3"
[ "$actual_content" == "$expected_content" ]
}
@test "Test command usage: aws-profile add with already existing profile name" {
run_aws_profile_add default AWS_ACCESS_KEY_ID AWS_SECRET_ACCESS_KEY REGION FORMAT AWS_SESSION_TOKEN
run_aws_profile_add default AWS_ACCESS_KEY_ID AWS_SECRET_ACCESS_KEY REGION FORMAT AWS_SESSION_TOKEN
[ "${status}" -eq 1 ]
[[ "${lines[@]}" == *"already exists!"* ]]
}
# TODO: add without session token
#
# Test aws-profile current
#
@test "Test command usage: aws-profile current without profile selected" {
run aws-profile current
[ "${status}" -eq 1 ]
[[ "${lines[@]}" == *"No profile selected"* ]]
}
@test "Test command usage: aws-profile current after profile selected" {
run_aws_profile_add default AWS_ACCESS_KEY_ID AWS_SECRET_ACCESS_KEY REGION FORMAT AWS_SESSION_TOKEN
aws-profile use default
aws-profile current
[[ -f ~/.aws-profile/current ]]
[ "$?" -eq 0 ]
[ "${AWS_PROFILE}" == "default" ]
}
#
# Test aws-profile export
#
@test "Test command usage: aws-profile export not existing profile" {
run aws-profile export --format bash not-existing
[ "${status}" -eq 1 ]
[[ "${lines[@]}" == *"does not exist!"* ]]
}
@test "Test command usage: aws-profile export invalid format" {
run_aws_profile_add default AWS_ACCESS_KEY_ID AWS_SECRET_ACCESS_KEY REGION FORMAT AWS_SESSION_TOKEN
run aws-profile export --format invalid-format default
[ "${status}" -eq 1 ]
[[ "${lines[@]}" == *"Invalid format!"* ]]
}
@test "Test command usage: aws-profile export without profile selected" {
run_aws_profile_add default AWS_ACCESS_KEY_ID AWS_SECRET_ACCESS_KEY REGION FORMAT AWS_SESSION_TOKEN
run aws-profile export --format bash
[ "${status}" -eq 1 ]
[[ "${lines[@]}" == *"No profile name specified!"* ]]
}
@test "Test command usage: aws-profile export currently used profile" {
run_aws_profile_add p1 AWS_ACCESS_KEY_ID AWS_SECRET_ACCESS_KEY REGION FORMAT AWS_SESSION_TOKEN
aws-profile use p1 &>/dev/null
actual_content=$(aws-profile export --format bash)
expected_output="export AWS_ACCESS_KEY_ID='AWS_ACCESS_KEY_ID'
export AWS_SECRET_ACCESS_KEY='AWS_SECRET_ACCESS_KEY'
export AWS_DEFAULT_REGION='REGION'
export AWS_DEFAULT_OUTPUT='FORMAT'
export AWS_SESSION_TOKEN='AWS_SESSION_TOKEN'"
[ "$actual_content" == "$expected_output" ]
}
@test "Test command usage: aws-profile export specified profile" {
run_aws_profile_add p1 AWS_ACCESS_KEY_ID AWS_SECRET_ACCESS_KEY REGION FORMAT AWS_SESSION_TOKEN
actual_content=$(aws-profile export --format bash p1)
expected_output="export AWS_ACCESS_KEY_ID='AWS_ACCESS_KEY_ID'
export AWS_SECRET_ACCESS_KEY='AWS_SECRET_ACCESS_KEY'
export AWS_DEFAULT_REGION='REGION'
export AWS_DEFAULT_OUTPUT='FORMAT'
export AWS_SESSION_TOKEN='AWS_SESSION_TOKEN'"
[ "$actual_content" == "$expected_output" ]
}
@test "Test command usage: aws-profile export json" {
run_aws_profile_add p1 AWS_ACCESS_KEY_ID AWS_SECRET_ACCESS_KEY REGION FORMAT AWS_SESSION_TOKEN
actual_content=$(aws-profile export --format json p1)
expected_output='{
"Credentials": {
"SessionToken": "AWS_SESSION_TOKEN",
"AccessKeyId": "AWS_SECRET_ACCESS_KEY",
"SecretAccessKey": "AWS_SECRET_ACCESS_KEY"
},
"Region": "REGION",
"Output": "FORMAT"
}'
[ "$actual_content" == "$expected_output" ]
}
#
# Test aws-profile import
#
@test "Test command usage: aws-profile import usage" {
run aws-profile import
[ "${status}" -eq 1 ]
[[ "${lines[@]}" == *"Usage"* ]]
}
@test "Test command usage: aws-profile import no file" {
run aws-profile import default
[ "${status}" -eq 1 ]
[[ "${lines[@]}" == *"Usage"* ]]
}
@test "Test command usage: aws-profile import not existing file" {
credentials_file=/tmp/not-existing.json
run aws-profile import default "${credentials_file}"
[ "${status}" -eq 1 ]
[[ "${lines[@]}" == *"does not exist!"* ]]
}
@test "Test command usage: aws-profile import abort when existing profile" {
run_aws_profile_add default AWS_ACCESS_KEY_ID AWS_SECRET_ACCESS_KEY REGION FORMAT AWS_SESSION_TOKEN
credentials_file=/tmp/dummy.json
echo '{
"Credentials": {
"SessionToken": "AWS_SESSION_TOKEN",
"AccessKeyId": "AWS_ACCESS_KEY_ID",
"SecretAccessKey": "AWS_SECRET_ACCESS_KEY"
},
"Region": "REGION",
"Output": "FORMAT"
}' > "${credentials_file}"
run aws-profile import default "${credentials_file}" < <(echo 'n')
[ "${status}" -eq 1 ]
[[ "${lines[@]}" == *"Aborted"* ]]
}
@test "Test command usage: aws-profile import existing profile" {
run_aws_profile_add default AWS_ACCESS_KEY_ID AWS_SECRET_ACCESS_KEY REGION FORMAT AWS_SESSION_TOKEN
credentials_file=/tmp/dummy.json
echo '{
"Credentials": {
"SessionToken": "SessionToken",
"AccessKeyId": "AccessKeyId",
"SecretAccessKey": "SecretAccessKey"
},
"Region": "Region",
"Output": "Output"
}' > "${credentials_file}"
run aws-profile import default "${credentials_file}" < <(echo 'y')
# Test whether aws config file has correct content
expected_aws_config="[default]
region = Region
output = Output"
actual_content=$(cat ~/.aws/config)
[ "$actual_content" == "$expected_aws_config" ]
# Test whether aws credentials file has correct content
actual_content=$(cat ~/.aws/credentials)
expected_aws_credentials="[default]
aws_access_key_id = AccessKeyId
aws_secret_access_key = SecretAccessKey
aws_session_token = SessionToken"
rm -f "${credentials_file}"
}
@test "Test command usage: aws-profile import" {
credentials_file=/tmp/aws-credentials.json
echo '{
"Credentials": {
"SessionToken": "AWS_SESSION_TOKEN",
"AccessKeyId": "AWS_ACCESS_KEY_ID",
"SecretAccessKey": "AWS_SECRET_ACCESS_KEY"
},
"Region": "REGION",
"Output": "FORMAT"
}' > "${credentials_file}"
run aws-profile import default "${credentials_file}"
# Test whether aws config file has correct content
expected_aws_config="[default]
region = REGION
output = FORMAT"
actual_content=$(cat ~/.aws/config)
[ "$actual_content" == "$expected_aws_config" ]
# Test whether aws credentials file has correct content
actual_content=$(cat ~/.aws/credentials)
expected_aws_credentials="[default]
aws_access_key_id = AWS_ACCESS_KEY_ID
aws_secret_access_key = AWS_SECRET_ACCESS_KEY
aws_session_token = AWS_SESSION_TOKEN"
rm -f "${credentials_file}"
}
# TODO:
# - file can not be parsed
#
# Test aws-profile list
#
@test "Test command usage: aws-profile list empty" {
run aws-profile list
[ "${status}" -eq 0 ]
[ -z "${lines[@]}" ]
}
@test "Test command usage: aws-profile list multiple" {
run_aws_profile_add p1 AWS_ACCESS_KEY_ID AWS_SECRET_ACCESS_KEY REGION FORMAT AWS_SESSION_TOKEN
run_aws_profile_add p2 AWS_ACCESS_KEY_ID AWS_SECRET_ACCESS_KEY REGION FORMAT AWS_SESSION_TOKEN
actual_content=$(aws-profile list)
[ "${status}" -eq 0 ]
expected_content="p1
p2"
[ "$actual_content" == "$expected_content" ]
}
#
# Test aws-profile reload
#
@test "Test command usage: aws-profile reload" {
mkdir -p ~/.aws
echo "[default]" > ~/.aws/config
echo "[default]
aws_access_key_id = AWS_ACCESS_KEY_ID
aws_secret_access_key = AWS_SECRET_ACCESS_KEY" > ~/.aws/credentials
aws-profile reload
actual_content=$(aws-profile list)
expected_content="default"
[ "$actual_content" == "$expected_content" ]
}
#
# Test aws-profile remove
#
@test "Test command usage: aws-profile remove first" {
run_aws_profile_add p1 AAKI1 ASAK1 R1 F1 S1
run_aws_profile_add p2 AAKI2 ASAK2 R2 F2 S2
run_aws_profile_add p3 AAKI3 ASAK3 R3 F3 S3
run aws-profile remove p1 << EOF
Y
EOF
# Test whether aws config file has correct content
expected_content="[profile p2]
region = R2
output = F2
[profile p3]
region = R3
output = F3"
actual_content=$(cat ~/.aws/config)
[ "$actual_content" == "$expected_content" ]
# Test whether aws credentials file has correct content
actual_content=$(cat ~/.aws/credentials)
expected_content="[p2]
aws_access_key_id = AAKI2
aws_secret_access_key = ASAK2
aws_session_token = S2
[p3]
aws_access_key_id = AAKI3
aws_secret_access_key = ASAK3
aws_session_token = S3"
[ "$actual_content" == "$expected_content" ]
}
@test "Test command usage: aws-profile remove not existing profile" {
run aws-profile remove default
[ "${status}" -eq 1 ]
[[ "${lines[@]}" == *"does not exist!"* ]]
}
@test "Test command usage: aws-profile remove middle" {
run_aws_profile_add p1 AAKI1 ASAK1 R1 F1 S1
run_aws_profile_add p2 AAKI2 ASAK2 R2 F2 S2
run_aws_profile_add p3 AAKI3 ASAK3 R3 F3 S3
run aws-profile remove p2 << EOF
Y
EOF
# Test whether aws config file has correct content
expected_content="[profile p1]
region = R1
output = F1
[profile p3]
region = R3
output = F3"
actual_content=$(cat ~/.aws/config)
[ "$actual_content" == "$expected_content" ]
# Test whether aws credentials file has correct content
actual_content=$(cat ~/.aws/credentials)
expected_content="[p1]
aws_access_key_id = AAKI1
aws_secret_access_key = ASAK1
aws_session_token = S1
[p3]
aws_access_key_id = AAKI3
aws_secret_access_key = ASAK3
aws_session_token = S3"
[ "$actual_content" == "$expected_content" ]
}
@test "Test command usage: aws-profile remove last" {
run_aws_profile_add p1 AAKI1 ASAK1 R1 F1 S1
run_aws_profile_add p2 AAKI2 ASAK2 R2 F2 S2
run_aws_profile_add p3 AAKI3 ASAK3 R3 F3 S3
run aws-profile remove p3 << EOF
Y
EOF
# Test whether aws config file has correct content
expected_content="[profile p1]
region = R1
output = F1
[profile p2]
region = R2
output = F2"
actual_content=$(cat ~/.aws/config)
[ "$actual_content" == "$expected_content" ]
# Test whether aws credentials file has correct content
actual_content=$(cat ~/.aws/credentials)
expected_content="[p1]
aws_access_key_id = AAKI1
aws_secret_access_key = ASAK1
aws_session_token = S1
[p2]
aws_access_key_id = AAKI2
aws_secret_access_key = ASAK2
aws_session_token = S2"
[ "$actual_content" == "$expected_content" ]
}
#
# Test aws-profile show
#
@test "Test command usage: aws-profile show not existing" {
run aws-profile show not-existing
[ "${status}" -eq 1 ]
[[ "${lines[@]}" == *"does not exist!"* ]]
}
@test "Test command usage: aws-profile show existing" {
run_aws_profile_add p1 AAKI1 ASAK1 R1 F1 S1
run aws-profile show p1
[ "${status}" -eq 0 ]
[[ "${lines[@]}" == *"profile"*"p1"* ]]
[[ "${lines[@]}" == *"access_key"* ]]
[[ "${lines[@]}" == *"secret_key"* ]]
[[ "${lines[@]}" == *"region"* ]]
}
@test "Test command usage: aws-profile show current" {
run_aws_profile_add p1 AAKI1 ASAK1 R1 F1 S1
run_aws_profile_add p2 AAKI2 ASAK2 R2 F2 S2
run_aws_profile_add p3 AAKI3 ASAK3 R3 F3 S3
aws-profile use p2
run aws-profile show
[ "${status}" -eq 0 ]
[[ "${lines[@]}" == *"profile"*"p2"* ]]
[[ "${lines[@]}" == *"access_key"* ]]
[[ "${lines[@]}" == *"secret_key"* ]]
[[ "${lines[@]}" == *"region"* ]]
}
#
# Test aws-profile update
#
@test "Test command usage: aws-profile update" {
run_aws_profile_add p1 1a 1b 1c 1d 1e
run_aws_profile_add p2 2a 2b 2c 2d 2e
run_aws_profile_add p3 3a 3b 3c 3d 3e
run aws-profile update p2 << EOF
2f
2g
2h
2i
2j
EOF
# Test whether aws config file has correct content
expected_content="[profile p1]
region = 1c
output = 1d
[profile p2]
region = 2h
output = 2i
[profile p3]
region = 3c
output = 3d"
actual_content=$(cat ~/.aws/config)
[ "$actual_content" == "$expected_content" ]
# Test whether aws credentials file has correct content
actual_content=$(cat ~/.aws/credentials)
expected_content="[p1]
aws_access_key_id = 1a
aws_secret_access_key = 1b
aws_session_token = 1e
[p2]
aws_access_key_id = 2f
aws_secret_access_key = 2g
aws_session_token = 2j
[p3]
aws_access_key_id = 3a
aws_secret_access_key = 3b
aws_session_token = 3e"
[ "$actual_content" == "$expected_content" ]
}