-
Notifications
You must be signed in to change notification settings - Fork 25
/
test_s3_sensors.py
364 lines (314 loc) · 13.6 KB
/
test_s3_sensors.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
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
import unittest
from datetime import timedelta
from typing import Any, List
from unittest import mock
import pytest
from airflow.exceptions import AirflowException, AirflowSkipException, TaskDeferred
from airflow.models import DAG, DagRun, TaskInstance
from airflow.models.variable import Variable
from airflow.utils import timezone
from parameterized import parameterized
from astronomer.providers.amazon.aws.sensors.s3 import (
S3KeySensorAsync,
S3KeySizeSensorAsync,
S3KeysUnchangedSensorAsync,
S3PrefixSensorAsync,
)
from astronomer.providers.amazon.aws.triggers.s3 import (
S3KeysUnchangedTrigger,
S3KeyTrigger,
)
MODULE = "astronomer.providers.amazon.aws.sensors.s3"
class TestS3KeySensorAsync:
@mock.patch(f"{MODULE}.S3KeySensorAsync.defer")
@mock.patch(f"{MODULE}.S3KeySensorAsync.poke", return_value=True)
def test_finish_before_deferred(self, mock_poke, mock_defer, context):
"""Assert task is not deferred when it receives a finish status before deferring"""
sensor = S3KeySensorAsync(task_id="s3_key_sensor", bucket_key="file_in_bucket")
sensor.execute(context)
assert not mock_defer.called
@mock.patch(f"{MODULE}.S3KeySensorAsync.poke", return_value=False)
def test_bucket_name_none_and_bucket_key_as_relative_path(self, mock_poke, context):
"""
Test if exception is raised when bucket_name is None
and bucket_key is provided with one of the two keys as relative path rather than s3:// url.
"""
sensor = S3KeySensorAsync(task_id="s3_key_sensor", bucket_key="file_in_bucket")
with pytest.raises(TaskDeferred):
sensor.execute(context)
@mock.patch(f"{MODULE}.S3KeySensorAsync.poke", return_value=False)
@mock.patch("astronomer.providers.amazon.aws.hooks.s3.S3HookAsync.get_head_object")
def test_bucket_name_none_and_bucket_key_is_list_and_contain_relative_path(
self, mock_head_object, mock_poke, context
):
"""
Test if exception is raised when bucket_name is None
and bucket_key is provided with one of the two keys as relative path rather than s3:// url.
:return:
"""
mock_head_object.return_value = {"ContentLength": 0}
sensor = S3KeySensorAsync(
task_id="s3_key_sensor", bucket_key=["s3://test_bucket/file", "file_in_bucket"]
)
with pytest.raises(TaskDeferred):
sensor.execute(context)
@mock.patch(f"{MODULE}.S3KeySensorAsync.poke", return_value=False)
def test_bucket_name_provided_and_bucket_key_is_s3_url(self, mock_poke, context):
"""
Test if exception is raised when bucket_name is provided
while bucket_key is provided as a full s3:// url.
:return:
"""
op = S3KeySensorAsync(
task_id="s3_key_sensor", bucket_key="s3://test_bucket/file", bucket_name="test_bucket"
)
with pytest.raises(TaskDeferred):
op.execute(context)
@parameterized.expand(
[
["s3://bucket/key", None],
["key", "bucket"],
]
)
@mock.patch(f"{MODULE}.S3KeySensorAsync.poke", return_value=False)
@mock.patch("airflow.providers.amazon.aws.sensors.s3.S3Hook")
def test_s3_key_sensor_async(self, key, bucket, mock_hook, mock_poke):
"""
Asserts that a task is deferred and an S3KeyTrigger will be fired
when the S3KeySensorAsync is executed.
"""
mock_hook.check_for_key.return_value = False
sensor = S3KeySensorAsync(
task_id="s3_key_sensor_async",
bucket_key=key,
bucket_name=bucket,
)
with pytest.raises(TaskDeferred) as exc:
sensor.execute(context=None)
assert isinstance(exc.value.trigger, S3KeyTrigger), "Trigger is not a S3KeyTrigger"
@parameterized.expand(
[
["s3://bucket/key", None],
["key", "bucket"],
]
)
@mock.patch(f"{MODULE}.S3KeySensorAsync.poke", return_value=False)
@mock.patch("airflow.providers.amazon.aws.sensors.s3.S3Hook")
def test_s3_key_sensor_execute_complete_success(self, key, bucket, mock_poke, mock_hook):
"""
Asserts that a task is completed with success status.
"""
mock_hook.check_for_key.return_value = False
sensor = S3KeySensorAsync(
task_id="s3_key_sensor_async",
bucket_key=key,
bucket_name=bucket,
)
assert sensor.execute_complete(context={}, event={"status": "success"}) is None
@parameterized.expand(
[
["key", "bucket"],
]
)
@mock.patch(f"{MODULE}.S3KeySensorAsync.poke", return_value=False)
def test_s3_key_sensor_execute_complete_success_with_keys(self, key, bucket, mock_poke):
"""
Asserts that a task is completed with success status and check function
"""
def check_fn(files: List[Any]) -> bool:
return all(f.get("Size", 0) > 0 for f in files)
sensor = S3KeySensorAsync(
task_id="s3_key_sensor_async",
bucket_key=key,
bucket_name=bucket,
check_fn=check_fn,
)
assert (
sensor.execute_complete(context={}, event={"status": "running", "files": [{"Size": 10}]}) is None
)
@parameterized.expand(
[
["s3://bucket/key", None],
["key", "bucket"],
]
)
@mock.patch(f"{MODULE}.S3KeySensorAsync.poke", return_value=False)
@mock.patch("airflow.providers.amazon.aws.sensors.s3.S3Hook")
def test_s3_key_sensor_execute_complete_error(self, key, bucket, mock_hook, mock_poke):
"""
Asserts that a task is completed with error status.
"""
mock_hook.check_for_key.return_value = False
sensor = S3KeySensorAsync(
task_id="s3_key_sensor_async",
bucket_key=key,
bucket_name=bucket,
)
with pytest.raises(AirflowException):
sensor.execute_complete(
context={}, event={"status": "error", "message": "mocked error", "soft_fail": False}
)
@parameterized.expand(
[
["s3://bucket/key", None],
["key", "bucket"],
]
)
@mock.patch(f"{MODULE}.S3KeySensorAsync.poke", return_value=False)
@mock.patch("airflow.providers.amazon.aws.sensors.s3.S3Hook")
@mock.patch.object(S3KeySensorAsync, "defer")
@mock.patch("astronomer.providers.amazon.aws.sensors.s3.S3KeyTrigger")
def test_s3_key_sensor_async_with_mock_defer(
self, key, bucket, mock_trigger, mock_defer, mock_hook, mock_poke
):
"""
Asserts that a task is deferred and an S3KeyTrigger will be fired
when the S3KeySensorAsync is executed.
"""
mock_hook.check_for_key.return_value = False
sensor = S3KeySensorAsync(
task_id="s3_key_sensor_async",
bucket_key=key,
bucket_name=bucket,
)
sensor.execute(context=None)
mock_defer.assert_called()
mock_defer.assert_called_once_with(
timeout=timedelta(days=7), trigger=mock_trigger.return_value, method_name="execute_complete"
)
@mock.patch(f"{MODULE}.S3KeySensorAsync.poke", return_value=False)
@mock.patch("airflow.providers.amazon.aws.sensors.s3.S3Hook.check_for_key")
def test_parse_bucket_key_from_jinja(self, mock_check, mock_poke):
mock_check.return_value = False
Variable.set("test_bucket_key", "s3://bucket/key")
execution_date = timezone.datetime(2020, 1, 1)
dag = DAG("test_s3_key", start_date=execution_date)
op = S3KeySensorAsync(
task_id="s3_key_sensor",
bucket_key="s3://bucket/key",
bucket_name=None,
dag=dag,
)
dag_run = DagRun(dag_id=dag.dag_id, execution_date=execution_date, run_id="test")
ti = TaskInstance(task=op)
ti.dag_run = dag_run
context = ti.get_template_context()
ti.render_templates(context)
assert op.bucket_key == ["s3://bucket/key"]
assert op.bucket_name is None
@mock.patch(f"{MODULE}.S3KeySensorAsync.poke", return_value=False)
@mock.patch("airflow.providers.amazon.aws.sensors.s3.S3Hook")
def test_s3_key_sensor_with_wildcard_async(self, mock_hook, mock_poke, context):
"""
Asserts that a task with wildcard=True is deferred and an S3KeyTrigger will be fired
when the S3KeySensorAsync is executed.
"""
mock_hook.check_for_key.return_value = False
sensor = S3KeySensorAsync(
task_id="s3_key_sensor_async", bucket_key="s3://test_bucket/file", wildcard_match=True
)
with pytest.raises(TaskDeferred) as exc:
sensor.execute(context)
assert isinstance(exc.value.trigger, S3KeyTrigger), "Trigger is not a S3KeyTrigger"
def test_soft_fail(self):
"""Raise AirflowSkipException in case soft_fail is true"""
sensor = S3KeySensorAsync(
task_id="s3_key_sensor_async", bucket_key="key", bucket_name="bucket", soft_fail=True
)
with pytest.raises(AirflowSkipException):
sensor.execute_complete(
context={}, event={"status": "error", "message": "mocked error", "soft_fail": True}
)
@pytest.mark.parametrize(
"soft_fail,exception",
[
(True, AirflowSkipException),
(False, Exception),
],
)
@mock.patch(f"{MODULE}.S3KeySensorAsync.poke")
def test_execute_handle_exception(self, mock_poke, soft_fail, exception):
mock_poke.side_effect = Exception()
sensor = S3KeySensorAsync(
task_id="s3_key_sensor_async", bucket_key="key", bucket_name="bucket", soft_fail=soft_fail
)
with pytest.raises(exception):
sensor.execute(context={})
class TestS3KeysUnchangedSensorAsync:
@mock.patch(f"{MODULE}.S3KeysUnchangedSensorAsync.defer")
@mock.patch(f"{MODULE}.S3KeysUnchangedSensorAsync.poke", return_value=True)
def test_s3_keys_unchanged_sensor_async_finish_before_deferred(self, mock_poke, mock_defer, context):
"""Assert task is not deferred when it receives a finish status before deferring"""
S3KeysUnchangedSensorAsync(
task_id="s3_keys_unchanged_sensor", bucket_name="test_bucket", prefix="test"
)
assert not mock_defer.called
@mock.patch(f"{MODULE}.S3KeysUnchangedSensorAsync.poke", return_value=False)
@mock.patch("airflow.providers.amazon.aws.sensors.s3.S3Hook")
def test_s3_keys_unchanged_sensor_check_trigger_instance(self, mock_hook, mock_poke, context):
"""
Asserts that a task is deferred and an S3KeysUnchangedTrigger will be fired
when the S3KeysUnchangedSensorAsync is executed.
"""
mock_hook.check_for_key.return_value = False
sensor = S3KeysUnchangedSensorAsync(
task_id="s3_keys_unchanged_sensor", bucket_name="test_bucket", prefix="test"
)
with pytest.raises(TaskDeferred) as exc:
sensor.execute(context)
assert isinstance(
exc.value.trigger, S3KeysUnchangedTrigger
), "Trigger is not a S3KeysUnchangedTrigger"
@parameterized.expand([["bucket", "test"]])
@mock.patch(f"{MODULE}.S3KeysUnchangedSensorAsync.poke", return_value=False)
@mock.patch("airflow.providers.amazon.aws.sensors.s3.S3Hook")
def test_s3_keys_unchanged_sensor_execute_complete_success(self, bucket, prefix, mock_hook, mock_poke):
"""
Asserts that a task completed with success status
"""
mock_hook.check_for_key.return_value = False
sensor = S3KeysUnchangedSensorAsync(
task_id="s3_keys_unchanged_sensor",
bucket_name=bucket,
prefix=prefix,
)
assert sensor.execute_complete(context={}, event={"status": "success"}) is None
@parameterized.expand([["bucket", "test"]])
@mock.patch(f"{MODULE}.S3KeysUnchangedSensorAsync.poke", return_value=False)
@mock.patch("airflow.providers.amazon.aws.sensors.s3.S3Hook")
def test_s3_keys_unchanged_sensor_execute_complete_error(self, bucket, prefix, mock_hook, mock_poke):
"""
Asserts that a task is completed with error.
"""
mock_hook.check_for_key.return_value = False
sensor = S3KeysUnchangedSensorAsync(
task_id="s3_keys_unchanged_sensor",
bucket_name=bucket,
prefix=prefix,
)
with pytest.raises(AirflowException):
sensor.execute_complete(context={}, event={"status": "error", "message": "Mocked error"})
@mock.patch(f"{MODULE}.S3KeysUnchangedSensorAsync.poke", return_value=False)
def test_s3_keys_unchanged_sensor_raise_value_error(self, mock_poke):
"""
Test if the S3KeysUnchangedTrigger raises Value error for negative inactivity_period.
"""
with pytest.raises(ValueError):
S3KeysUnchangedSensorAsync(
task_id="s3_keys_unchanged_sensor",
bucket_name="test_bucket",
prefix="test",
inactivity_period=-100,
)
class TestS3KeySizeSensorAsync(unittest.TestCase):
def test_deprecation_warnings_generated(self):
with pytest.warns(expected_warning=DeprecationWarning):
S3KeySizeSensorAsync(task_id="s3_size_sensor", bucket_key="s3://test_bucket/file")
class TestS3PrefixSensorAsync(unittest.TestCase):
def test_deprecation_warnings_generated(self):
with pytest.warns(expected_warning=DeprecationWarning):
S3PrefixSensorAsync(
task_id="check_s3_prefix_sensor",
bucket_name="test_bucket",
prefix="test",
)