|
1 | 1 | from pathlib import Path |
2 | 2 | from unittest.mock import MagicMock, patch |
3 | 3 |
|
4 | | -from airflow.utils.context import Context |
| 4 | +import pytest |
5 | 5 | from pendulum import datetime |
6 | 6 |
|
7 | 7 | from cosmos.operators.kubernetes import ( |
|
12 | 12 | DbtTestKubernetesOperator, |
13 | 13 | ) |
14 | 14 |
|
| 15 | +from airflow.utils.context import Context, context_merge |
| 16 | +from airflow.models import TaskInstance |
| 17 | + |
| 18 | +try: |
| 19 | + from airflow.providers.cncf.kubernetes.utils.pod_manager import OnFinishAction |
| 20 | + |
| 21 | + module_available = True |
| 22 | +except ImportError: |
| 23 | + module_available = False |
| 24 | + |
15 | 25 |
|
16 | 26 | def test_dbt_kubernetes_operator_add_global_flags() -> None: |
17 | 27 | dbt_kube_operator = DbtKubernetesBaseOperator( |
@@ -103,6 +113,113 @@ def test_dbt_kubernetes_build_command(): |
103 | 113 | ] |
104 | 114 |
|
105 | 115 |
|
| 116 | +@pytest.mark.parametrize( |
| 117 | + "additional_kwargs,expected_results", |
| 118 | + [ |
| 119 | + ({"on_success_callback": None, "is_delete_operator_pod": True}, (1, 1, True, "delete_pod")), |
| 120 | + ( |
| 121 | + {"on_success_callback": (lambda **kwargs: None), "is_delete_operator_pod": False}, |
| 122 | + (2, 1, False, "keep_pod"), |
| 123 | + ), |
| 124 | + ( |
| 125 | + {"on_success_callback": [(lambda **kwargs: None), (lambda **kwargs: None)], "is_delete_operator_pod": None}, |
| 126 | + (3, 1, True, "delete_pod"), |
| 127 | + ), |
| 128 | + ( |
| 129 | + {"on_failure_callback": None, "is_delete_operator_pod": True, "on_finish_action": "keep_pod"}, |
| 130 | + (1, 1, True, "delete_pod"), |
| 131 | + ), |
| 132 | + ( |
| 133 | + { |
| 134 | + "on_failure_callback": (lambda **kwargs: None), |
| 135 | + "is_delete_operator_pod": None, |
| 136 | + "on_finish_action": "delete_pod", |
| 137 | + }, |
| 138 | + (1, 2, True, "delete_pod"), |
| 139 | + ), |
| 140 | + ( |
| 141 | + { |
| 142 | + "on_failure_callback": [(lambda **kwargs: None), (lambda **kwargs: None)], |
| 143 | + "is_delete_operator_pod": None, |
| 144 | + "on_finish_action": "delete_succeeded_pod", |
| 145 | + }, |
| 146 | + (1, 3, False, "delete_succeeded_pod"), |
| 147 | + ), |
| 148 | + ({"is_delete_operator_pod": None, "on_finish_action": "keep_pod"}, (1, 1, False, "keep_pod")), |
| 149 | + ({}, (1, 1, True, "delete_pod")), |
| 150 | + ], |
| 151 | +) |
| 152 | +@pytest.mark.skipif( |
| 153 | + not module_available, reason="Kubernetes module `airflow.providers.cncf.kubernetes.utils.pod_manager` not available" |
| 154 | +) |
| 155 | +def test_dbt_test_kubernetes_operator_constructor(additional_kwargs, expected_results): |
| 156 | + test_operator = DbtTestKubernetesOperator( |
| 157 | + on_warning_callback=(lambda **kwargs: None), **additional_kwargs, **base_kwargs |
| 158 | + ) |
| 159 | + |
| 160 | + print(additional_kwargs, test_operator.__dict__) |
| 161 | + |
| 162 | + assert isinstance(test_operator.on_success_callback, list) |
| 163 | + assert isinstance(test_operator.on_failure_callback, list) |
| 164 | + assert test_operator._handle_warnings in test_operator.on_success_callback |
| 165 | + assert test_operator._cleanup_pod in test_operator.on_failure_callback |
| 166 | + assert len(test_operator.on_success_callback) == expected_results[0] |
| 167 | + assert len(test_operator.on_failure_callback) == expected_results[1] |
| 168 | + assert test_operator.is_delete_operator_pod_original == expected_results[2] |
| 169 | + assert test_operator.on_finish_action_original == OnFinishAction(expected_results[3]) |
| 170 | + |
| 171 | + |
| 172 | +class FakePodManager: |
| 173 | + def read_pod_logs(self, pod, container): |
| 174 | + assert pod == "pod" |
| 175 | + assert container == "base" |
| 176 | + log_string = """ |
| 177 | +19:48:25 Concurrency: 4 threads (target='target') |
| 178 | +19:48:25 |
| 179 | +19:48:25 1 of 2 START test dbt_utils_accepted_range_table_col__12__0 ................... [RUN] |
| 180 | +19:48:25 2 of 2 START test unique_table__uuid .......................................... [RUN] |
| 181 | +19:48:27 1 of 2 WARN 252 dbt_utils_accepted_range_table_col__12__0 ..................... [WARN 117 in 1.83s] |
| 182 | +19:48:27 2 of 2 PASS unique_table__uuid ................................................ [PASS in 1.85s] |
| 183 | +19:48:27 |
| 184 | +19:48:27 Finished running 2 tests, 1 hook in 0 hours 0 minutes and 12.86 seconds (12.86s). |
| 185 | +19:48:27 |
| 186 | +19:48:27 Completed with 1 warning: |
| 187 | +19:48:27 |
| 188 | +19:48:27 Warning in test dbt_utils_accepted_range_table_col__12__0 (models/ads/ads.yaml) |
| 189 | +19:48:27 Got 252 results, configured to warn if >0 |
| 190 | +19:48:27 |
| 191 | +19:48:27 compiled Code at target/compiled/model/models/table/table.yaml/dbt_utils_accepted_range_table_col__12__0.sql |
| 192 | +19:48:27 |
| 193 | +19:48:27 Done. PASS=1 WARN=1 ERROR=0 SKIP=0 TOTAL=2 |
| 194 | +""" |
| 195 | + return (log.encode("utf-8") for log in log_string.split("\n")) |
| 196 | + |
| 197 | + |
| 198 | +@pytest.mark.skipif( |
| 199 | + not module_available, reason="Kubernetes module `airflow.providers.cncf.kubernetes.utils.pod_manager` not available" |
| 200 | +) |
| 201 | +def test_dbt_test_kubernetes_operator_handle_warnings_and_cleanup_pod(): |
| 202 | + def on_warning_callback(context: Context): |
| 203 | + assert context["test_names"] == ["dbt_utils_accepted_range_table_col__12__0"] |
| 204 | + assert context["test_results"] == ["Got 252 results, configured to warn if >0"] |
| 205 | + |
| 206 | + def cleanup(pod: str, remote_pod: str): |
| 207 | + assert pod == remote_pod |
| 208 | + |
| 209 | + test_operator = DbtTestKubernetesOperator( |
| 210 | + is_delete_operator_pod=True, on_warning_callback=on_warning_callback, **base_kwargs |
| 211 | + ) |
| 212 | + task_instance = TaskInstance(test_operator) |
| 213 | + task_instance.task.pod_manager = FakePodManager() |
| 214 | + task_instance.task.pod = task_instance.task.remote_pod = "pod" |
| 215 | + task_instance.task.cleanup = cleanup |
| 216 | + |
| 217 | + context = Context() |
| 218 | + context_merge(context, task_instance=task_instance) |
| 219 | + |
| 220 | + test_operator._handle_warnings(context) |
| 221 | + |
| 222 | + |
106 | 223 | @patch("airflow.providers.cncf.kubernetes.operators.pod.KubernetesPodOperator.hook") |
107 | 224 | def test_created_pod(test_hook): |
108 | 225 | test_hook.is_in_cluster = False |
|
0 commit comments