-
Notifications
You must be signed in to change notification settings - Fork 0
/
test_aws.py
241 lines (213 loc) · 8.54 KB
/
test_aws.py
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
"""Test the AWS adapter."""
import datetime
import logging
from typing import Generator
from unittest.mock import Mock, patch
import pytest
from _pytest.logging import LogCaptureFixture
from botocore.exceptions import NoRegionError
from drode.adapters.aws import AWS, AutoscalerInfo, AWSConfigurationError, AWSStateError
@pytest.fixture(name="boto")
def boto_() -> Generator[Mock, None, None]:
"""Prepare the boto mock."""
boto_patch = patch("drode.adapters.aws.boto3", autospec=True)
boto = boto_patch.start()
yield boto
boto_patch.stop()
# W0613: boto is not used, but it is, as we're using it to initialize the patch on each
# test.
@pytest.fixture(name="aws")
def aws_(boto: Mock) -> AWS: # noqa: W0613
"""Prepare the AWS object to test."""
return AWS()
def test_check_config_happy_path(aws: AWS, caplog: LogCaptureFixture) -> None:
"""
Given: A correctly configured AWS adapter object.
When: Configuration is checked
Then: The user is informed of the correct state.
"""
aws.check_configuration() # act
assert ("drode.adapters.aws", logging.INFO, "AWS: OK") in caplog.record_tuples
def test_check_config_unauthorized_error(
aws: AWS, boto: Mock, caplog: LogCaptureFixture
) -> None:
"""
Given: An incorrectly configured AWS adapter object.
When: Configuration is checked.
Then: The user is informed of the incorrect state and an exception is raised.
"""
boto.client.side_effect = NoRegionError()
with pytest.raises(AWSConfigurationError):
aws.check_configuration()
assert ("drode.adapters.aws", logging.ERROR, "AWS: KO") in caplog.record_tuples
def test_get_autoscaling_returns_instances_info(aws: AWS, boto: Mock) -> None:
"""
Given: An AWS adapter.
When: Using the get_autoscaling_group method.
Then: The information of the autoscaling group and it's associated resources is
returned.
"""
boto = boto.client.return_value
boto.describe_auto_scaling_groups.return_value = {
"AutoScalingGroups": [
{
"AutoScalingGroupARN": "autoscaling_arn",
"AutoScalingGroupName": "production_autoscaling_group_name",
"AvailabilityZones": ["us-west-1a", "us-west-1b", "us-west-1c"],
"CreatedTime": datetime.datetime(2020, 5, 19, 16, 8, 26, 535000),
"DefaultCooldown": 300,
"DesiredCapacity": 2,
"EnabledMetrics": [],
"HealthCheckGracePeriod": 300,
"HealthCheckType": "ELB",
"Instances": [
{
"AvailabilityZone": "us-west-1d",
"HealthStatus": "Healthy",
"InstanceId": "i-xxxxxxxxxxxxxxxxx",
"LaunchConfigurationName": "old-launch-config-name",
"LifecycleState": "InService",
"ProtectedFromScaleIn": False,
},
],
"LaunchConfigurationName": "launch-config-name",
"LoadBalancerNames": [],
"MaxSize": 10,
"MinSize": 2,
"NewInstancesProtectedFromScaleIn": False,
"ServiceLinkedRoleARN": "servicelinkedrolearn",
"SuspendedProcesses": [],
"TargetGroupARNs": ["target_group_arn"],
"TerminationPolicies": ["Default"],
}
],
"ResponseMetadata": {"HTTPStatusCode": 200},
}
# ECE001: Expression is too complex (7.5 > 7). It's the way the API is defined.
boto.describe_instances.return_value = { # noqa: ECE001
"Reservations": [
{
"Groups": [],
"Instances": [
{
"InstanceType": "t2.medium",
"LaunchTime": datetime.datetime(2020, 6, 8, 11, 29, 27),
"PrivateIpAddress": "192.168.1.13",
"State": {"Code": 16, "Name": "running"},
}
],
}
],
"ResponseMetadata": {"HTTPStatusCode": 200},
}
desired_result = AutoscalerInfo(
template="launch-config-name",
instances=[
{
"Instance": "i-xxxxxxxxxxxxxxxxx",
"IP": "192.168.1.13",
"Status": "Healthy/InService",
"Created": "2020-06-08T11:29",
"Template": "old-launch-config-name",
}
],
)
result = aws.get_autoscaling_group("production_autoscaling_group_name")
assert result == desired_result
def test_get_autoscaling_handles_unexistent(aws: AWS, boto: Mock) -> None:
"""
Given: An AWS adapter.
When: Using the get_autoscaling_group on an inexistent autoscaling group.
Then: An exception is raised.
"""
boto = boto.client.return_value
boto.describe_auto_scaling_groups.return_value = {
"AutoScalingGroups": [],
"ResponseMetadata": {"HTTPStatusCode": 200},
}
with pytest.raises(AWSStateError) as error:
aws.get_autoscaling_group("inexistent_autoscaling_group")
assert "There are no autoscaling groups named inexistent_autoscaling_group" in str(
error.value
)
def test_get_autoscaling_handles_launch_templates(aws: AWS, boto: Mock) -> None:
"""
Given: An AWS adapter and an existing autoscaling group using launch templates.
When: Using the get_autoscaling_group.
Then: The information of the launch template is returned
"""
boto = boto.client.return_value
boto.describe_auto_scaling_groups.return_value = {
"AutoScalingGroups": [
{
"AutoScalingGroupARN": "autoscaling_arn",
"AutoScalingGroupName": "production_autoscaling_group_name",
"AvailabilityZones": ["us-west-1a", "us-west-1b", "us-west-1c"],
"CreatedTime": datetime.datetime(2020, 5, 19, 16, 8, 26, 535000),
"DefaultCooldown": 300,
"DesiredCapacity": 2,
"EnabledMetrics": [],
"HealthCheckGracePeriod": 300,
"HealthCheckType": "ELB",
"Instances": [
{
"AvailabilityZone": "us-west-1d",
"HealthStatus": "Healthy",
"InstanceId": "i-xxxxxxxxxxxxxxxxx",
"LaunchTemplate": {
"LaunchTemplateId": "lt-xxxxxxxxxxxxxxxxx",
"LaunchTemplateName": "old-launch-template-name",
"Version": "1",
},
"LifecycleState": "InService",
"ProtectedFromScaleIn": False,
},
],
"LaunchTemplate": {
"LaunchTemplateId": "lt-xxxxxxxxxxxxxxxxx",
"LaunchTemplateName": "launch-template-name",
"Version": "1",
},
"LoadBalancerNames": [],
"MaxSize": 10,
"MinSize": 2,
"NewInstancesProtectedFromScaleIn": False,
"ServiceLinkedRoleARN": "servicelinkedrolearn",
"SuspendedProcesses": [],
"TargetGroupARNs": ["target_group_arn"],
"TerminationPolicies": ["Default"],
}
],
"ResponseMetadata": {"HTTPStatusCode": 200},
}
# ECE001: Expression is too complex (7.5 > 7). It's the way the API is defined.
boto.describe_instances.return_value = { # noqa: ECE001
"Reservations": [
{
"Groups": [],
"Instances": [
{
"InstanceType": "t2.medium",
"LaunchTime": datetime.datetime(2020, 6, 8, 11, 29, 27),
"PrivateIpAddress": "192.168.1.13",
"State": {"Code": 16, "Name": "running"},
}
],
}
],
"ResponseMetadata": {"HTTPStatusCode": 200},
}
desired_result = AutoscalerInfo(
template="launch-template-name:1",
instances=[
{
"Instance": "i-xxxxxxxxxxxxxxxxx",
"IP": "192.168.1.13",
"Status": "Healthy/InService",
"Created": "2020-06-08T11:29",
"Template": "old-launch-template-name:1",
}
],
)
result = aws.get_autoscaling_group("production_autoscaling_group_name")
assert result == desired_result