forked from gruntwork-io/bash-commons
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathaws-cli.bats
190 lines (147 loc) · 4.05 KB
/
aws-cli.bats
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
#!/usr/bin/env bats
source "$BATS_TEST_DIRNAME/../modules/bash-commons/src/aws.sh"
load "test-helper"
load "aws-helper"
function setup {
start_moto
}
function teardown {
stop_moto
}
@test "aws_get_instance_tags empty" {
run aws_get_instance_tags "fake-id" "us-east-1"
assert_success
local -r expected=$(cat <<END_HEREDOC
{
"Tags": []
}
END_HEREDOC
)
assert_output_json "$expected"
}
@test "aws_get_instance_tags non-empty" {
local -r tag_key="foo"
local -r tag_value="bar"
local instance_id
instance_id=$(create_mock_instance_with_tags "$tag_key" "$tag_value")
run aws_get_instance_tags "$instance_id" "us-east-1"
assert_success
local -r expected=$(cat <<END_HEREDOC
{
"Tags": [
{
"ResourceType": "instance",
"ResourceId": "$instance_id",
"Value": "$tag_value",
"Key": "$tag_key"
}
]
}
END_HEREDOC
)
assert_output_json "$expected"
}
@test "aws_get_instance_tag_val non-empty" {
local -r tag_key="foo"
local -r tag_value="bar"
local instance_id
instance_id=$(create_mock_instance_with_tags "$tag_key" "$tag_value")
run aws_get_instance_tag_val "$tag_key" "$instance_id" "us-east-1"
assert_success
local -r expected="$tag_value"
assert_output "$expected"
}
@test "aws_describe_asg empty" {
run aws_describe_asg "fake-asg-name" "us-east-1"
assert_success
local -r expected=$(cat <<END_HEREDOC
{
"AutoScalingGroups": []
}
END_HEREDOC
)
assert_output_json "$expected"
}
@test "aws_describe_asg non-empty" {
local -r asg_name="foo"
local -r min_size=1
local -r max_size=3
local -r region="us-east-1"
local -r azs="${region}a"
create_mock_asg "$asg_name" "$min_size" "$max_size" "$azs"
run aws_describe_asg "$asg_name" "$region"
assert_success
local actual_asg_name
actual_asg_name=$(echo "$output" | jq -r '.AutoScalingGroups[0].AutoScalingGroupName')
assert_equal "$asg_name" "$actual_asg_name"
local actual_min_size
actual_min_size=$(echo "$output" | jq -r '.AutoScalingGroups[0].MinSize')
assert_equal "$min_size" "$actual_min_size"
local actual_max_size
actual_max_size=$(echo "$output" | jq -r '.AutoScalingGroups[0].MaxSize')
assert_equal "$max_size" "$actual_max_size"
}
@test "aws_describe_instances_in_asg empty" {
run aws_describe_instances_in_asg "fake-asg-name" "us-east-1"
assert_success
local -r expected=$(cat <<END_HEREDOC
{
"Reservations": []
}
END_HEREDOC
)
assert_output_json "$expected"
}
@test "aws_describe_instances_in_asg non-empty" {
local -r asg_name="foo"
local -r min_size=1
local -r max_size=3
local -r region="us-east-1"
local -r azs="${region}a"
create_mock_asg "$asg_name" "$min_size" "$max_size" "$azs"
run aws_describe_instances_in_asg "$asg_name" "$region"
assert_success
local num_instances
num_instances=$(echo "$output" | jq -r '.Reservations | length')
assert_greater_than "$num_instances" 0
}
@test "aws_get_instances_with_tag empty" {
run aws_get_instances_with_tag "Name" "Value" "us-east-1"
assert_success
local -r expected=$(cat <<END_HEREDOC
{
"Reservations": []
}
END_HEREDOC
)
assert_output_json "$expected"
}
@test "aws_get_instances_with_tag non-empty" {
local -r tag_key="Name"
local -r tag_value="Value"
local -r region="us-east-1"
create_mock_instance_with_tags "$tag_key" "$tag_value"
run aws_get_instances_with_tag "$tag_key" "$tag_value" "$region"
assert_success
local num_instances
num_instances=$(echo "$output" | jq -r '.Reservations | length')
assert_greater_than "$num_instances" 0
}
@test "configure_imdsv2_ttl_with_minimal_ttl" {
local -r ttl_value=1
returned_ttl=$(configure_imdsv2_ttl "$ttl_value")
assert_success
assert_equal "$ttl_value" "$returned_ttl"
}
@test "configure_imdsv2_ttl_with_maximum_ttl" {
local -r ttl_value=21600
returned_ttl=$(configure_imdsv2_ttl "$ttl_value")
assert_success
assert_equal "$ttl_value" "$returned_ttl"
}
@test "configure_imdsv2_ttl_with_excessively_high_ttl" {
local -r ttl_value=310000
returned_ttl=$(configure_imdsv2_ttl "$ttl_value")
assert_success
assert_equal "$returned_ttl" "21600"
}