|
| 1 | +from unittest.mock import MagicMock |
| 2 | + |
1 | 3 | import pytest |
2 | 4 |
|
3 | 5 | from metaflow.plugins.kubernetes.kube_utils import ( |
4 | 6 | KubernetesException, |
5 | 7 | validate_kube_labels, |
6 | 8 | parse_kube_keyvalue_list, |
7 | 9 | ) |
| 10 | +from metaflow.plugins.kubernetes.kubernetes_decorator import KubernetesDecorator |
8 | 11 |
|
9 | 12 |
|
10 | 13 | @pytest.mark.parametrize( |
@@ -95,3 +98,132 @@ def test_kubernetes_parse_keyvalue_list(items, requires_both, expected): |
95 | 98 | def test_kubernetes_parse_keyvalue_list(items, requires_both): |
96 | 99 | with pytest.raises(KubernetesException): |
97 | 100 | parse_kube_keyvalue_list(items, requires_both) |
| 101 | + |
| 102 | + |
| 103 | +class TestKubernetesDecoratorLocalMode: |
| 104 | + """Tests for issue #2588: @kubernetes should be ignored for local runs.""" |
| 105 | + |
| 106 | + def _make_decorator(self, statically_defined=True, **attrs): |
| 107 | + deco = KubernetesDecorator( |
| 108 | + attributes=attrs or None, statically_defined=statically_defined |
| 109 | + ) |
| 110 | + return deco |
| 111 | + |
| 112 | + def test_static_decorator_enters_local_mode_with_local_datastore(self): |
| 113 | + """When @kubernetes is in source code and datastore is local, enter local mode.""" |
| 114 | + deco = self._make_decorator(statically_defined=True) |
| 115 | + flow_datastore = MagicMock() |
| 116 | + flow_datastore.TYPE = "local" |
| 117 | + # step_init should not raise; it should silently enter local mode |
| 118 | + deco.step_init( |
| 119 | + flow=MagicMock(), |
| 120 | + graph=MagicMock(), |
| 121 | + step="my_step", |
| 122 | + decos=[], |
| 123 | + environment=MagicMock(), |
| 124 | + flow_datastore=flow_datastore, |
| 125 | + logger=MagicMock(), |
| 126 | + ) |
| 127 | + assert deco._local_mode is True |
| 128 | + |
| 129 | + def test_dynamic_decorator_raises_with_local_datastore(self): |
| 130 | + """When @kubernetes is added via --with and datastore is local, raise error.""" |
| 131 | + deco = self._make_decorator(statically_defined=False) |
| 132 | + flow_datastore = MagicMock() |
| 133 | + flow_datastore.TYPE = "local" |
| 134 | + with pytest.raises(KubernetesException, match="--datastore=s3"): |
| 135 | + deco.step_init( |
| 136 | + flow=MagicMock(), |
| 137 | + graph=MagicMock(), |
| 138 | + step="my_step", |
| 139 | + decos=[], |
| 140 | + environment=MagicMock(), |
| 141 | + flow_datastore=flow_datastore, |
| 142 | + logger=MagicMock(), |
| 143 | + ) |
| 144 | + |
| 145 | + def test_local_mode_skips_runtime_step_cli(self): |
| 146 | + """In local mode, runtime_step_cli should not redirect to kubernetes.""" |
| 147 | + deco = self._make_decorator(statically_defined=True) |
| 148 | + deco._local_mode = True |
| 149 | + cli_args = MagicMock() |
| 150 | + cli_args.commands = ["step"] |
| 151 | + deco.runtime_step_cli( |
| 152 | + cli_args, retry_count=0, max_user_code_retries=3, ubf_context=None |
| 153 | + ) |
| 154 | + # commands should NOT have been changed to ["kubernetes", "step"] |
| 155 | + assert cli_args.commands == ["step"] |
| 156 | + |
| 157 | + def test_local_mode_skips_package_init(self): |
| 158 | + """In local mode, package_init should be a no-op (no kubernetes import needed).""" |
| 159 | + deco = self._make_decorator(statically_defined=True) |
| 160 | + deco._local_mode = True |
| 161 | + # Should not raise even if kubernetes package is not installed |
| 162 | + deco.package_init( |
| 163 | + flow=MagicMock(), step_name="my_step", environment=MagicMock() |
| 164 | + ) |
| 165 | + |
| 166 | + def test_local_mode_skips_runtime_init(self): |
| 167 | + """In local mode, runtime_init should be a no-op.""" |
| 168 | + deco = self._make_decorator(statically_defined=True) |
| 169 | + deco._local_mode = True |
| 170 | + deco.runtime_init( |
| 171 | + flow=MagicMock(), graph=MagicMock(), package=MagicMock(), run_id="123" |
| 172 | + ) |
| 173 | + # Should not set self.flow etc. |
| 174 | + assert not hasattr(deco, "flow") |
| 175 | + |
| 176 | + def test_local_mode_skips_runtime_task_created(self): |
| 177 | + """In local mode, runtime_task_created should be a no-op.""" |
| 178 | + deco = self._make_decorator(statically_defined=True) |
| 179 | + deco._local_mode = True |
| 180 | + # Should not raise even though flow_datastore and package are not set |
| 181 | + deco.runtime_task_created( |
| 182 | + task_datastore=MagicMock(), |
| 183 | + task_id="1", |
| 184 | + split_index=None, |
| 185 | + input_paths=[], |
| 186 | + is_cloned=False, |
| 187 | + ubf_context=None, |
| 188 | + ) |
| 189 | + |
| 190 | + def test_s3_datastore_does_not_enter_local_mode(self): |
| 191 | + """With S3 datastore, even a static decorator should NOT enter local mode.""" |
| 192 | + deco = self._make_decorator(statically_defined=True) |
| 193 | + flow_datastore = MagicMock() |
| 194 | + flow_datastore.TYPE = "s3" |
| 195 | + # This will proceed with normal K8s setup - it will fail at QoS |
| 196 | + # validation or other checks, but should NOT set _local_mode |
| 197 | + try: |
| 198 | + deco.step_init( |
| 199 | + flow=MagicMock(), |
| 200 | + graph=MagicMock(), |
| 201 | + step="my_step", |
| 202 | + decos=[], |
| 203 | + environment=MagicMock(), |
| 204 | + flow_datastore=flow_datastore, |
| 205 | + logger=MagicMock(), |
| 206 | + ) |
| 207 | + except Exception: |
| 208 | + pass # Expected to fail on later validation |
| 209 | + assert deco._local_mode is False |
| 210 | + |
| 211 | + |
| 212 | +class TestKubernetesDecoratorPriorityClass: |
| 213 | + """Tests for issue #1752: priority_class option for @kubernetes.""" |
| 214 | + |
| 215 | + def test_priority_class_default_is_from_config(self): |
| 216 | + """priority_class should default to KUBERNETES_PRIORITY_CLASS config value.""" |
| 217 | + from metaflow.metaflow_config import KUBERNETES_PRIORITY_CLASS |
| 218 | + |
| 219 | + deco = KubernetesDecorator() |
| 220 | + assert deco.attributes["priority_class"] == KUBERNETES_PRIORITY_CLASS |
| 221 | + |
| 222 | + def test_priority_class_can_be_set(self): |
| 223 | + """priority_class should be settable via decorator attributes.""" |
| 224 | + deco = KubernetesDecorator(attributes={"priority_class": "high-priority"}) |
| 225 | + assert deco.attributes["priority_class"] == "high-priority" |
| 226 | + |
| 227 | + def test_priority_class_in_defaults(self): |
| 228 | + """priority_class should be in the decorator defaults.""" |
| 229 | + assert "priority_class" in KubernetesDecorator.defaults |
0 commit comments