-
Notifications
You must be signed in to change notification settings - Fork 14.5k
/
test_rds.py
345 lines (290 loc) · 16.1 KB
/
test_rds.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
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
#
# Licensed to the Apache Software Foundation (ASF) under one
# or more contributor license agreements. See the NOTICE file
# distributed with this work for additional information
# regarding copyright ownership. The ASF licenses this file
# to you under the Apache License, Version 2.0 (the
# "License"); you may not use this file except in compliance
# with the License. You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing,
# software distributed under the License is distributed on an
# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
# KIND, either express or implied. See the License for the
# specific language governing permissions and limitations
# under the License.
from __future__ import annotations
from typing import TYPE_CHECKING, Generator
from unittest.mock import patch
import pytest
from moto import mock_aws
from airflow.exceptions import AirflowException, AirflowNotFoundException
from airflow.providers.amazon.aws.hooks.rds import RdsHook
if TYPE_CHECKING:
from mypy_boto3_rds.type_defs import DBSnapshotTypeDef
@pytest.fixture
def rds_hook() -> Generator[RdsHook, None, None]:
"""Returns an RdsHook whose underlying connection is mocked with moto"""
with mock_aws():
yield RdsHook(aws_conn_id="aws_default", region_name="us-east-1")
@pytest.fixture
def db_instance_id(rds_hook: RdsHook) -> str:
"""Creates an RDS DB instance and returns its id"""
response = rds_hook.conn.create_db_instance(
DBInstanceIdentifier="testrdshook-db-instance",
DBInstanceClass="db.t4g.micro",
Engine="postgres",
AllocatedStorage=20,
MasterUsername="testrdshook",
MasterUserPassword="testrdshook",
)
return response["DBInstance"]["DBInstanceIdentifier"]
@pytest.fixture
def db_cluster_id(rds_hook: RdsHook) -> str:
"""Creates an RDS DB cluster and returns its id"""
response = rds_hook.conn.create_db_cluster(
DBClusterIdentifier="testrdshook-db-cluster",
Engine="postgres",
MasterUsername="testrdshook",
MasterUserPassword="testrdshook",
DBClusterInstanceClass="db.t4g.micro",
AllocatedStorage=20,
)
return response["DBCluster"]["DBClusterIdentifier"]
@pytest.fixture
def db_snapshot(rds_hook: RdsHook, db_instance_id: str) -> DBSnapshotTypeDef:
"""
Creates a mock DB instance snapshot and returns the DBSnapshot dict from the boto response object.
https://boto3.amazonaws.com/v1/documentation/api/latest/reference/services/rds.html#RDS.Client.create_db_snapshot
"""
response = rds_hook.conn.create_db_snapshot(
DBSnapshotIdentifier="testrdshook-db-instance-snapshot", DBInstanceIdentifier=db_instance_id
)
return response["DBSnapshot"]
@pytest.fixture
def db_snapshot_id(db_snapshot: dict) -> str:
return db_snapshot["DBSnapshotIdentifier"]
@pytest.fixture
def db_snapshot_arn(db_snapshot: dict) -> str:
return db_snapshot["DBSnapshotArn"]
@pytest.fixture
def db_cluster_snapshot(rds_hook: RdsHook, db_cluster_id: str):
"""
Creates a mock DB cluster snapshot and returns the DBClusterSnapshot dict from the boto response object.
https://boto3.amazonaws.com/v1/documentation/api/latest/reference/services/rds.html#RDS.Client.create_db_cluster_snapshot
"""
response = rds_hook.conn.create_db_cluster_snapshot(
DBClusterSnapshotIdentifier="testrdshook-db-cluster-snapshot", DBClusterIdentifier=db_cluster_id
)
return response["DBClusterSnapshot"]
@pytest.fixture
def db_cluster_snapshot_id(db_cluster_snapshot) -> str:
return db_cluster_snapshot["DBClusterSnapshotIdentifier"]
@pytest.fixture
def export_task_id(rds_hook: RdsHook, db_snapshot_arn: str) -> str:
response = rds_hook.conn.start_export_task(
ExportTaskIdentifier="testrdshook-export-task",
SourceArn=db_snapshot_arn,
S3BucketName="test",
IamRoleArn="test",
KmsKeyId="test",
)
return response["ExportTaskIdentifier"]
@pytest.fixture
def event_subscription_name(rds_hook: RdsHook, db_instance_id: str) -> str:
"""Creates an mock RDS event subscription and returns its name"""
response = rds_hook.conn.create_event_subscription(
SubscriptionName="testrdshook-event-subscription",
SnsTopicArn="test",
SourceType="db-instance",
SourceIds=[db_instance_id],
Enabled=True,
)
return response["EventSubscription"]["CustSubscriptionId"]
class TestRdsHook:
# For testing, set the delay between status checks to 0 so that we aren't sleeping during tests,
# and max_attempts to 1 so that we don't retry unless required.
waiter_args = {"check_interval": 0, "max_attempts": 1}
def test_conn_attribute(self):
hook = RdsHook(aws_conn_id="aws_default", region_name="us-east-1")
assert hasattr(hook, "conn")
assert hook.conn.__class__.__name__ == "RDS"
conn = hook.conn
assert conn is hook.conn # Cached property
assert conn is hook.get_conn() # Same object as returned by `conn` property
def test_get_db_instance_state(self, rds_hook: RdsHook, db_instance_id: str):
response = rds_hook.conn.describe_db_instances(DBInstanceIdentifier=db_instance_id)
state_expected = response["DBInstances"][0]["DBInstanceStatus"]
state_actual = rds_hook.get_db_instance_state(db_instance_id)
assert state_actual == state_expected
def test_wait_for_db_instance_state_boto_waiters(self, rds_hook: RdsHook, db_instance_id: str):
"""Checks that the DB instance waiter uses AWS boto waiters where possible"""
for state in ("available", "deleted"):
with patch.object(rds_hook.conn, "get_waiter") as mock:
rds_hook.wait_for_db_instance_state(db_instance_id, target_state=state, **self.waiter_args)
mock.assert_called_once_with(f"db_instance_{state}")
mock.return_value.wait.assert_called_once_with(
DBInstanceIdentifier=db_instance_id,
WaiterConfig={
"MaxAttempts": self.waiter_args["max_attempts"],
},
)
def test_wait_for_db_instance_state_custom_waiter(self, rds_hook: RdsHook, db_instance_id: str):
"""Checks that the DB instance waiter uses custom wait logic when AWS boto waiters aren't available"""
with patch.object(rds_hook, "_wait_for_state") as mock:
rds_hook.wait_for_db_instance_state(db_instance_id, target_state="stopped", **self.waiter_args)
mock.assert_called_once()
with patch.object(rds_hook, "get_db_instance_state", return_value="stopped") as mock:
rds_hook.wait_for_db_instance_state(db_instance_id, target_state="stopped", **self.waiter_args)
mock.assert_called_once_with(db_instance_id)
def test_get_db_cluster_state(self, rds_hook: RdsHook, db_cluster_id: str):
response = rds_hook.conn.describe_db_clusters(DBClusterIdentifier=db_cluster_id)
state_expected = response["DBClusters"][0]["Status"]
state_actual = rds_hook.get_db_cluster_state(db_cluster_id)
assert state_actual == state_expected
def test_wait_for_db_cluster_state_boto_waiters(self, rds_hook: RdsHook, db_cluster_id: str):
"""Checks that the DB cluster waiter uses AWS boto waiters where possible"""
for state in ("available", "deleted"):
with patch.object(rds_hook.conn, "get_waiter") as mock:
rds_hook.wait_for_db_cluster_state(db_cluster_id, target_state=state, **self.waiter_args)
mock.assert_called_once_with(f"db_cluster_{state}")
mock.return_value.wait.assert_called_once_with(
DBClusterIdentifier=db_cluster_id,
WaiterConfig={
"Delay": self.waiter_args["check_interval"],
"MaxAttempts": self.waiter_args["max_attempts"],
},
)
def test_wait_for_db_cluster_state_custom_waiter(self, rds_hook: RdsHook, db_cluster_id: str):
"""Checks that the DB cluster waiter uses custom wait logic when AWS boto waiters aren't available"""
with patch.object(rds_hook, "_wait_for_state") as mock_wait_for_state:
rds_hook.wait_for_db_cluster_state(db_cluster_id, target_state="stopped", **self.waiter_args)
mock_wait_for_state.assert_called_once()
with patch.object(rds_hook, "get_db_cluster_state", return_value="stopped") as mock:
rds_hook.wait_for_db_cluster_state(db_cluster_id, target_state="stopped", **self.waiter_args)
mock.assert_called_once_with(db_cluster_id)
def test_get_db_snapshot_state(self, rds_hook: RdsHook, db_snapshot_id: str):
response = rds_hook.conn.describe_db_snapshots(DBSnapshotIdentifier=db_snapshot_id)
state_expected = response["DBSnapshots"][0]["Status"]
state_actual = rds_hook.get_db_snapshot_state(db_snapshot_id)
assert state_actual == state_expected
def test_get_db_snapshot_state_not_found(self, rds_hook: RdsHook):
with pytest.raises(AirflowNotFoundException):
rds_hook.get_db_snapshot_state("does_not_exist")
def test_wait_for_db_snapshot_state_boto_waiters(self, rds_hook: RdsHook, db_snapshot_id: str):
"""Checks that the DB snapshot waiter uses AWS boto waiters where possible"""
for state in ("available", "deleted", "completed"):
with patch.object(rds_hook.conn, "get_waiter") as mock:
rds_hook.wait_for_db_snapshot_state(db_snapshot_id, target_state=state, **self.waiter_args)
mock.assert_called_once_with(f"db_snapshot_{state}")
mock.return_value.wait.assert_called_once_with(
DBSnapshotIdentifier=db_snapshot_id,
WaiterConfig={
"Delay": self.waiter_args["check_interval"],
"MaxAttempts": self.waiter_args["max_attempts"],
},
)
def test_wait_for_db_snapshot_state_custom_waiter(self, rds_hook: RdsHook, db_snapshot_id: str):
"""Checks that the DB snapshot waiter uses custom wait logic when AWS boto waiters aren't available"""
with patch.object(rds_hook, "_wait_for_state") as mock:
rds_hook.wait_for_db_snapshot_state(db_snapshot_id, target_state="canceled", **self.waiter_args)
mock.assert_called_once()
with patch.object(rds_hook, "get_db_snapshot_state", return_value="canceled") as mock:
rds_hook.wait_for_db_snapshot_state(db_snapshot_id, target_state="canceled", **self.waiter_args)
mock.assert_called_once_with(db_snapshot_id)
def test_get_db_cluster_snapshot_state(self, rds_hook: RdsHook, db_cluster_snapshot_id: str):
response = rds_hook.conn.describe_db_cluster_snapshots(
DBClusterSnapshotIdentifier=db_cluster_snapshot_id
)
state_expected = response["DBClusterSnapshots"][0]["Status"]
state_actual = rds_hook.get_db_cluster_snapshot_state(db_cluster_snapshot_id)
assert state_actual == state_expected
def test_get_db_cluster_snapshot_state_not_found(self, rds_hook: RdsHook):
with pytest.raises(AirflowNotFoundException):
rds_hook.get_db_cluster_snapshot_state("does_not_exist")
def test_wait_for_db_cluster_snapshot_state_boto_waiters(
self, rds_hook: RdsHook, db_cluster_snapshot_id: str
):
"""Checks that the DB cluster snapshot waiter uses AWS boto waiters where possible"""
for state in ("available", "deleted"):
with patch.object(rds_hook.conn, "get_waiter") as mock:
rds_hook.wait_for_db_cluster_snapshot_state(
db_cluster_snapshot_id, target_state=state, **self.waiter_args
)
mock.assert_called_once_with(f"db_cluster_snapshot_{state}")
mock.return_value.wait.assert_called_once_with(
DBClusterSnapshotIdentifier=db_cluster_snapshot_id,
WaiterConfig={
"Delay": self.waiter_args["check_interval"],
"MaxAttempts": self.waiter_args["max_attempts"],
},
)
def test_wait_for_db_cluster_snapshot_state_custom_waiter(
self, rds_hook: RdsHook, db_cluster_snapshot_id: str
):
"""
Checks that the DB cluster snapshot waiter uses custom wait logic when AWS boto waiters
aren't available
"""
with patch.object(rds_hook, "_wait_for_state") as mock:
rds_hook.wait_for_db_cluster_snapshot_state(
db_cluster_snapshot_id, target_state="canceled", **self.waiter_args
)
mock.assert_called_once()
with patch.object(rds_hook, "get_db_cluster_snapshot_state", return_value="canceled") as mock:
rds_hook.wait_for_db_cluster_snapshot_state(
db_cluster_snapshot_id, target_state="canceled", **self.waiter_args
)
mock.assert_called_once_with(db_cluster_snapshot_id)
def test_get_export_task_state(self, rds_hook: RdsHook, export_task_id: str):
response = rds_hook.conn.describe_export_tasks(ExportTaskIdentifier=export_task_id)
state_expected = response["ExportTasks"][0]["Status"]
state_actual = rds_hook.get_export_task_state(export_task_id)
assert state_actual == state_expected
def test_get_export_task_state_not_found(self, rds_hook: RdsHook):
with pytest.raises(AirflowNotFoundException):
rds_hook.get_export_task_state("does_not_exist")
def test_wait_for_export_task_state(self, rds_hook: RdsHook, export_task_id: str):
"""
Checks that the export task waiter uses custom wait logic (no boto waiters exist for this resource)
"""
with patch.object(rds_hook, "_wait_for_state") as mock:
rds_hook.wait_for_export_task_state(export_task_id, target_state="complete", **self.waiter_args)
mock.assert_called_once()
with patch.object(rds_hook, "get_export_task_state", return_value="complete") as mock:
rds_hook.wait_for_export_task_state(export_task_id, target_state="complete", **self.waiter_args)
mock.assert_called_once_with(export_task_id)
def test_get_event_subscription_state(self, rds_hook: RdsHook, event_subscription_name: str):
response = rds_hook.conn.describe_event_subscriptions(SubscriptionName=event_subscription_name)
state_expected = response["EventSubscriptionsList"][0]["Status"]
state_actual = rds_hook.get_event_subscription_state(event_subscription_name)
assert state_actual == state_expected
def test_get_event_subscription_state_not_found(self, rds_hook: RdsHook):
with pytest.raises(AirflowNotFoundException):
rds_hook.get_event_subscription_state("does_not_exist")
def test_wait_for_event_subscription_state(self, rds_hook: RdsHook, event_subscription_name: str):
"""
Checks that the event subscription waiter uses custom wait logic (no boto waiters
exist for this resource)
"""
with patch.object(rds_hook, "_wait_for_state") as mock:
rds_hook.wait_for_event_subscription_state(
event_subscription_name, target_state="active", **self.waiter_args
)
mock.assert_called_once()
with patch.object(rds_hook, "get_event_subscription_state", return_value="active") as mock:
rds_hook.wait_for_event_subscription_state(
event_subscription_name, target_state="active", **self.waiter_args
)
mock.assert_called_once_with(event_subscription_name)
def test_wait_for_state(self, rds_hook: RdsHook):
def poke():
return "foo"
with pytest.raises(AirflowException, match="Max attempts exceeded"):
with patch("airflow.providers.amazon.aws.hooks.rds.time.sleep") as mock:
rds_hook._wait_for_state(poke, target_state="bar", check_interval=0, max_attempts=2)
# This next line should exist outside of the pytest.raises() context manager or else it won't
# get executed
mock.assert_called_once_with(0)