-
Notifications
You must be signed in to change notification settings - Fork 125
/
test_kubernetes.py
287 lines (249 loc) · 10.4 KB
/
test_kubernetes.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
from pathlib import Path
from unittest.mock import MagicMock, patch
import pytest
from airflow.models import TaskInstance
from airflow.utils.context import Context, context_merge
from pendulum import datetime
from cosmos.operators.kubernetes import (
DbtBuildKubernetesOperator,
DbtLSKubernetesOperator,
DbtRunKubernetesOperator,
DbtSeedKubernetesOperator,
DbtTestKubernetesOperator,
)
try:
from airflow.providers.cncf.kubernetes.utils.pod_manager import OnFinishAction
module_available = True
except ImportError:
module_available = False
@pytest.fixture()
def mock_kubernetes_execute():
with patch("cosmos.operators.kubernetes.KubernetesPodOperator.execute") as mock_execute:
yield mock_execute
@pytest.fixture()
def base_operator(mock_kubernetes_execute):
from cosmos.operators.kubernetes import DbtKubernetesBaseOperator
class ConcreteDbtKubernetesBaseOperator(DbtKubernetesBaseOperator):
base_cmd = ["cmd"]
return ConcreteDbtKubernetesBaseOperator
def test_dbt_kubernetes_operator_add_global_flags(base_operator) -> None:
dbt_kube_operator = base_operator(
conn_id="my_airflow_connection",
task_id="my-task",
image="my_image",
project_dir="my/dir",
vars={
"start_time": "{{ data_interval_start.strftime('%Y%m%d%H%M%S') }}",
"end_time": "{{ data_interval_end.strftime('%Y%m%d%H%M%S') }}",
},
no_version_check=True,
)
assert dbt_kube_operator.add_global_flags() == [
"--vars",
"end_time: '{{ data_interval_end.strftime(''%Y%m%d%H%M%S'') }}'\n"
"start_time: '{{ data_interval_start.strftime(''%Y%m%d%H%M%S'') }}'\n",
"--no-version-check",
]
@patch("cosmos.operators.kubernetes.DbtKubernetesBaseOperator.build_kube_args")
def test_dbt_kubernetes_operator_execute(mock_build_kube_args, base_operator, mock_kubernetes_execute):
"""Tests that the execute method call results in both the build_kube_args method and the kubernetes execute method being called."""
operator = base_operator(
conn_id="my_airflow_connection",
task_id="my-task",
image="my_image",
project_dir="my/dir",
)
operator.execute(context={})
# Assert that the build_kube_args method was called in the execution
mock_build_kube_args.assert_called_once()
# Assert that the kubernetes execute method was called in the execution
mock_kubernetes_execute.assert_called_once()
assert mock_kubernetes_execute.call_args.args[-1] == {}
@patch("cosmos.operators.base.context_to_airflow_vars")
def test_dbt_kubernetes_operator_get_env(p_context_to_airflow_vars: MagicMock, base_operator) -> None:
"""
If an end user passes in a
"""
dbt_kube_operator = base_operator(
conn_id="my_airflow_connection", task_id="my-task", image="my_image", project_dir="my/dir", append_env=False
)
dbt_kube_operator.env = {
"start_date": "20220101",
"end_date": "20220102",
"some_path": Path(__file__),
"retries": 3,
("tuple", "key"): "some_value",
}
p_context_to_airflow_vars.return_value = {"START_DATE": "2023-02-15 12:30:00"}
env = dbt_kube_operator.get_env(
Context(execution_date=datetime(2023, 2, 15, 12, 30)),
)
expected_env = {
"start_date": "20220101",
"end_date": "20220102",
"some_path": Path(__file__),
"START_DATE": "2023-02-15 12:30:00",
}
assert env == expected_env
base_kwargs = {
"conn_id": "my_airflow_connection",
"task_id": "my-task",
"image": "my_image",
"project_dir": "my/dir",
"vars": {
"start_time": "{{ data_interval_start.strftime('%Y%m%d%H%M%S') }}",
"end_time": "{{ data_interval_end.strftime('%Y%m%d%H%M%S') }}",
},
"no_version_check": True,
}
result_map = {
"ls": DbtLSKubernetesOperator(**base_kwargs),
"run": DbtRunKubernetesOperator(**base_kwargs),
"test": DbtTestKubernetesOperator(**base_kwargs),
"build": DbtBuildKubernetesOperator(**base_kwargs),
"seed": DbtSeedKubernetesOperator(**base_kwargs),
}
def test_dbt_kubernetes_build_command():
"""
Since we know that the KubernetesOperator is tested, we can just test that the
command is built correctly and added to the "arguments" parameter.
"""
for command_name, command_operator in result_map.items():
command_operator.build_kube_args(context=MagicMock(), cmd_flags=MagicMock())
assert command_operator.arguments == [
"dbt",
command_name,
"--vars",
"end_time: '{{ data_interval_end.strftime(''%Y%m%d%H%M%S'') }}'\n"
"start_time: '{{ data_interval_start.strftime(''%Y%m%d%H%M%S'') }}'\n",
"--no-version-check",
"--project-dir",
"my/dir",
]
@pytest.mark.parametrize(
"additional_kwargs,expected_results",
[
({"on_success_callback": None, "is_delete_operator_pod": True}, (1, 1, True, "delete_pod")),
(
{"on_success_callback": (lambda **kwargs: None), "is_delete_operator_pod": False},
(2, 1, False, "keep_pod"),
),
(
{"on_success_callback": [(lambda **kwargs: None), (lambda **kwargs: None)], "is_delete_operator_pod": None},
(3, 1, True, "delete_pod"),
),
(
{"on_failure_callback": None, "is_delete_operator_pod": True, "on_finish_action": "keep_pod"},
(1, 1, True, "delete_pod"),
),
(
{
"on_failure_callback": (lambda **kwargs: None),
"is_delete_operator_pod": None,
"on_finish_action": "delete_pod",
},
(1, 2, True, "delete_pod"),
),
(
{
"on_failure_callback": [(lambda **kwargs: None), (lambda **kwargs: None)],
"is_delete_operator_pod": None,
"on_finish_action": "delete_succeeded_pod",
},
(1, 3, False, "delete_succeeded_pod"),
),
({"is_delete_operator_pod": None, "on_finish_action": "keep_pod"}, (1, 1, False, "keep_pod")),
({}, (1, 1, True, "delete_pod")),
],
)
@pytest.mark.skipif(
not module_available, reason="Kubernetes module `airflow.providers.cncf.kubernetes.utils.pod_manager` not available"
)
def test_dbt_test_kubernetes_operator_constructor(additional_kwargs, expected_results):
test_operator = DbtTestKubernetesOperator(
on_warning_callback=(lambda **kwargs: None), **additional_kwargs, **base_kwargs
)
print(additional_kwargs, test_operator.__dict__)
assert isinstance(test_operator.on_success_callback, list)
assert isinstance(test_operator.on_failure_callback, list)
assert test_operator._handle_warnings in test_operator.on_success_callback
assert test_operator._cleanup_pod in test_operator.on_failure_callback
assert len(test_operator.on_success_callback) == expected_results[0]
assert len(test_operator.on_failure_callback) == expected_results[1]
assert test_operator.is_delete_operator_pod_original == expected_results[2]
assert test_operator.on_finish_action_original == OnFinishAction(expected_results[3])
class FakePodManager:
def read_pod_logs(self, pod, container):
assert pod == "pod"
assert container == "base"
log_string = """
19:48:25 Concurrency: 4 threads (target='target')
19:48:25
19:48:25 1 of 2 START test dbt_utils_accepted_range_table_col__12__0 ................... [RUN]
19:48:25 2 of 2 START test unique_table__uuid .......................................... [RUN]
19:48:27 1 of 2 WARN 252 dbt_utils_accepted_range_table_col__12__0 ..................... [WARN 117 in 1.83s]
19:48:27 2 of 2 PASS unique_table__uuid ................................................ [PASS in 1.85s]
19:48:27
19:48:27 Finished running 2 tests, 1 hook in 0 hours 0 minutes and 12.86 seconds (12.86s).
19:48:27
19:48:27 Completed with 1 warning:
19:48:27
19:48:27 Warning in test dbt_utils_accepted_range_table_col__12__0 (models/ads/ads.yaml)
19:48:27 Got 252 results, configured to warn if >0
19:48:27
19:48:27 compiled Code at target/compiled/model/models/table/table.yaml/dbt_utils_accepted_range_table_col__12__0.sql
19:48:27
19:48:27 Done. PASS=1 WARN=1 ERROR=0 SKIP=0 TOTAL=2
"""
return (log.encode("utf-8") for log in log_string.split("\n"))
@pytest.mark.skipif(
not module_available, reason="Kubernetes module `airflow.providers.cncf.kubernetes.utils.pod_manager` not available"
)
def test_dbt_test_kubernetes_operator_handle_warnings_and_cleanup_pod():
def on_warning_callback(context: Context):
assert context["test_names"] == ["dbt_utils_accepted_range_table_col__12__0"]
assert context["test_results"] == ["Got 252 results, configured to warn if >0"]
def cleanup(pod: str, remote_pod: str):
assert pod == remote_pod
test_operator = DbtTestKubernetesOperator(
is_delete_operator_pod=True, on_warning_callback=on_warning_callback, **base_kwargs
)
task_instance = TaskInstance(test_operator)
task_instance.task.pod_manager = FakePodManager()
task_instance.task.pod = task_instance.task.remote_pod = "pod"
task_instance.task.cleanup = cleanup
context = Context()
context_merge(context, task_instance=task_instance)
test_operator._handle_warnings(context)
def test_created_pod():
ls_kwargs = {"env_vars": {"FOO": "BAR"}, "namespace": "foo", "append_env": False}
ls_kwargs.update(base_kwargs)
ls_operator = DbtLSKubernetesOperator(**ls_kwargs)
ls_operator.hook = MagicMock()
ls_operator.hook.is_in_cluster = False
ls_operator.build_kube_args(context={}, cmd_flags=MagicMock())
pod_obj = ls_operator.build_pod_request_obj()
metadata = pod_obj.metadata
assert metadata.labels["airflow_kpo_in_cluster"] == "False"
assert metadata.namespace == "foo"
container = pod_obj.spec.containers[0]
assert container.env[0].name == "FOO"
assert container.env[0].value == "BAR"
assert container.env[0].value_from is None
assert container.image == "my_image"
expected_container_args = [
"dbt",
"ls",
"--vars",
"end_time: '{{ "
"data_interval_end.strftime(''%Y%m%d%H%M%S'') "
"}}'\n"
"start_time: '{{ "
"data_interval_start.strftime(''%Y%m%d%H%M%S'') "
"}}'\n",
"--no-version-check",
"--project-dir",
"my/dir",
]
assert container.args == expected_container_args
assert container.command == []