-
Notifications
You must be signed in to change notification settings - Fork 14.4k
/
test_batch.py
286 lines (249 loc) · 11.3 KB
/
test_batch.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
# 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 unittest import mock
import pytest
from airflow.exceptions import AirflowException, TaskDeferred
from airflow.providers.amazon.aws.hooks.batch_client import BatchClientHook
from airflow.providers.amazon.aws.sensors.batch import (
BatchComputeEnvironmentSensor,
BatchJobQueueSensor,
BatchSensor,
)
from airflow.providers.amazon.aws.triggers.batch import BatchJobTrigger
TASK_ID = "batch_job_sensor"
JOB_ID = "8222a1c2-b246-4e19-b1b8-0039bb4407c0"
AWS_REGION = "eu-west-1"
ENVIRONMENT_NAME = "environment_name"
JOB_QUEUE = "job_queue"
@pytest.fixture(scope="module")
def batch_sensor() -> BatchSensor:
return BatchSensor(
task_id="batch_job_sensor",
job_id=JOB_ID,
)
@pytest.fixture(scope="module")
def deferrable_batch_sensor() -> BatchSensor:
return BatchSensor(task_id="task", job_id=JOB_ID, region_name=AWS_REGION, deferrable=True)
class TestBatchSensor:
@mock.patch.object(BatchClientHook, "get_job_description")
def test_poke_on_success_state(self, mock_get_job_description, batch_sensor: BatchSensor):
mock_get_job_description.return_value = {"status": "SUCCEEDED"}
assert batch_sensor.poke({}) is True
mock_get_job_description.assert_called_once_with(JOB_ID)
@mock.patch.object(BatchClientHook, "get_job_description")
def test_poke_on_failure_state(self, mock_get_job_description, batch_sensor: BatchSensor):
mock_get_job_description.return_value = {"status": "FAILED"}
with pytest.raises(AirflowException, match="Batch sensor failed. AWS Batch job status: FAILED"):
batch_sensor.poke({})
mock_get_job_description.assert_called_once_with(JOB_ID)
@mock.patch.object(BatchClientHook, "get_job_description")
def test_poke_on_invalid_state(self, mock_get_job_description, batch_sensor: BatchSensor):
mock_get_job_description.return_value = {"status": "INVALID"}
with pytest.raises(AirflowException, match="Batch sensor failed. AWS Batch job status: INVALID"):
batch_sensor.poke({})
mock_get_job_description.assert_called_once_with(JOB_ID)
@pytest.mark.parametrize("job_status", ["SUBMITTED", "PENDING", "RUNNABLE", "STARTING", "RUNNING"])
@mock.patch.object(BatchClientHook, "get_job_description")
def test_poke_on_intermediate_state(
self, mock_get_job_description, job_status, batch_sensor: BatchSensor
):
print(job_status)
mock_get_job_description.return_value = {"status": job_status}
assert batch_sensor.poke({}) is False
mock_get_job_description.assert_called_once_with(JOB_ID)
def test_execute_in_deferrable_mode(self, deferrable_batch_sensor: BatchSensor):
"""
Asserts that a task is deferred and a BatchSensorTrigger will be fired
when the BatchSensor is executed in deferrable mode.
"""
with pytest.raises(TaskDeferred) as exc:
deferrable_batch_sensor.execute({})
assert isinstance(exc.value.trigger, BatchJobTrigger), "Trigger is not a BatchJobTrigger"
def test_execute_failure_in_deferrable_mode(self, deferrable_batch_sensor: BatchSensor):
"""Tests that an AirflowException is raised in case of error event"""
with pytest.raises(AirflowException):
deferrable_batch_sensor.execute_complete(context={}, event={"status": "failure"})
@pytest.mark.parametrize(
"state",
(
BatchClientHook.FAILURE_STATE,
"unknown_state",
),
)
@mock.patch.object(BatchClientHook, "get_job_description")
def test_fail_poke(self, mock_get_job_description, state):
mock_get_job_description.return_value = {"status": state}
batch_sensor = BatchSensor(task_id="batch_job_sensor", job_id=JOB_ID)
with pytest.raises(AirflowException):
batch_sensor.poke({})
@pytest.fixture(scope="module")
def batch_compute_environment_sensor() -> BatchComputeEnvironmentSensor:
return BatchComputeEnvironmentSensor(
task_id="test_batch_compute_environment_sensor",
compute_environment=ENVIRONMENT_NAME,
)
class TestBatchComputeEnvironmentSensor:
@mock.patch.object(BatchClientHook, "client")
def test_poke_no_environment(
self, mock_batch_client, batch_compute_environment_sensor: BatchComputeEnvironmentSensor
):
mock_batch_client.describe_compute_environments.return_value = {"computeEnvironments": []}
with pytest.raises(AirflowException) as ctx:
batch_compute_environment_sensor.poke({})
mock_batch_client.describe_compute_environments.assert_called_once_with(
computeEnvironments=[ENVIRONMENT_NAME],
)
assert "not found" in str(ctx.value)
@mock.patch.object(BatchClientHook, "client")
def test_poke_valid(
self, mock_batch_client, batch_compute_environment_sensor: BatchComputeEnvironmentSensor
):
mock_batch_client.describe_compute_environments.return_value = {
"computeEnvironments": [{"status": "VALID"}]
}
assert batch_compute_environment_sensor.poke({}) is True
mock_batch_client.describe_compute_environments.assert_called_once_with(
computeEnvironments=[ENVIRONMENT_NAME],
)
@mock.patch.object(BatchClientHook, "client")
def test_poke_running(
self, mock_batch_client, batch_compute_environment_sensor: BatchComputeEnvironmentSensor
):
mock_batch_client.describe_compute_environments.return_value = {
"computeEnvironments": [
{
"status": "CREATING",
}
]
}
assert batch_compute_environment_sensor.poke({}) is False
mock_batch_client.describe_compute_environments.assert_called_once_with(
computeEnvironments=[ENVIRONMENT_NAME],
)
@mock.patch.object(BatchClientHook, "client")
def test_poke_invalid(
self, mock_batch_client, batch_compute_environment_sensor: BatchComputeEnvironmentSensor
):
mock_batch_client.describe_compute_environments.return_value = {
"computeEnvironments": [
{
"status": "INVALID",
}
]
}
with pytest.raises(AirflowException) as ctx:
batch_compute_environment_sensor.poke({})
mock_batch_client.describe_compute_environments.assert_called_once_with(
computeEnvironments=[ENVIRONMENT_NAME],
)
assert "AWS Batch compute environment failed" in str(ctx.value)
@pytest.mark.parametrize(
"compute_env, error_message",
(
(
[{"status": "unknown_status"}],
"AWS Batch compute environment failed. AWS Batch compute environment status:",
),
([], "AWS Batch compute environment"),
),
)
@mock.patch.object(BatchClientHook, "client")
def test_fail_poke(
self,
mock_batch_client,
batch_compute_environment_sensor: BatchComputeEnvironmentSensor,
compute_env,
error_message,
):
mock_batch_client.describe_compute_environments.return_value = {"computeEnvironments": compute_env}
with pytest.raises(AirflowException, match=error_message):
batch_compute_environment_sensor.poke({})
@pytest.fixture(scope="module")
def batch_job_queue_sensor() -> BatchJobQueueSensor:
return BatchJobQueueSensor(
task_id="test_batch_job_queue_sensor",
job_queue=JOB_QUEUE,
)
class TestBatchJobQueueSensor:
@mock.patch.object(BatchClientHook, "client")
def test_poke_no_queue(self, mock_batch_client, batch_job_queue_sensor: BatchJobQueueSensor):
mock_batch_client.describe_job_queues.return_value = {"jobQueues": []}
with pytest.raises(AirflowException) as ctx:
batch_job_queue_sensor.poke({})
mock_batch_client.describe_job_queues.assert_called_once_with(
jobQueues=[JOB_QUEUE],
)
assert "not found" in str(ctx.value)
@mock.patch.object(BatchClientHook, "client")
def test_poke_no_queue_with_treat_non_existing_as_deleted(
self, mock_batch_client, batch_job_queue_sensor: BatchJobQueueSensor
):
batch_job_queue_sensor.treat_non_existing_as_deleted = True
mock_batch_client.describe_job_queues.return_value = {"jobQueues": []}
assert batch_job_queue_sensor.poke({}) is True
mock_batch_client.describe_job_queues.assert_called_once_with(
jobQueues=[JOB_QUEUE],
)
@mock.patch.object(BatchClientHook, "client")
def test_poke_valid(self, mock_batch_client, batch_job_queue_sensor: BatchJobQueueSensor):
mock_batch_client.describe_job_queues.return_value = {"jobQueues": [{"status": "VALID"}]}
assert batch_job_queue_sensor.poke({}) is True
mock_batch_client.describe_job_queues.assert_called_once_with(
jobQueues=[JOB_QUEUE],
)
@mock.patch.object(BatchClientHook, "client")
def test_poke_running(self, mock_batch_client, batch_job_queue_sensor: BatchJobQueueSensor):
mock_batch_client.describe_job_queues.return_value = {
"jobQueues": [
{
"status": "CREATING",
}
]
}
assert batch_job_queue_sensor.poke({}) is False
mock_batch_client.describe_job_queues.assert_called_once_with(
jobQueues=[JOB_QUEUE],
)
@mock.patch.object(BatchClientHook, "client")
def test_poke_invalid(self, mock_batch_client, batch_job_queue_sensor: BatchJobQueueSensor):
mock_batch_client.describe_job_queues.return_value = {
"jobQueues": [
{
"status": "INVALID",
}
]
}
with pytest.raises(AirflowException) as ctx:
batch_job_queue_sensor.poke({})
mock_batch_client.describe_job_queues.assert_called_once_with(
jobQueues=[JOB_QUEUE],
)
assert "AWS Batch job queue failed" in str(ctx.value)
@pytest.mark.parametrize("job_queue", ([], [{"status": "UNKNOWN_STATUS"}]))
@mock.patch.object(BatchClientHook, "client")
def test_fail_poke(
self,
mock_batch_client,
batch_job_queue_sensor: BatchJobQueueSensor,
job_queue,
):
mock_batch_client.describe_job_queues.return_value = {"jobQueues": job_queue}
batch_job_queue_sensor.treat_non_existing_as_deleted = False
message = "AWS Batch job queue"
with pytest.raises(AirflowException, match=message):
batch_job_queue_sensor.poke({})