|
| 1 | +from unittest.mock import MagicMock, patch |
| 2 | + |
| 3 | +import pytest |
| 4 | + |
| 5 | +from metaflow.plugins.kubernetes.kubernetes_decorator import KubernetesDecorator |
| 6 | + |
| 7 | + |
| 8 | +def test_priority_class_in_defaults(): |
| 9 | + """priority_class key exists in KubernetesDecorator.defaults.""" |
| 10 | + assert "priority_class" in KubernetesDecorator.defaults |
| 11 | + |
| 12 | + |
| 13 | +def test_priority_class_default_from_config(): |
| 14 | + """Default value comes from KUBERNETES_PRIORITY_CLASS config variable.""" |
| 15 | + from metaflow.metaflow_config import KUBERNETES_PRIORITY_CLASS |
| 16 | + |
| 17 | + assert KubernetesDecorator.defaults["priority_class"] == KUBERNETES_PRIORITY_CLASS |
| 18 | + |
| 19 | + |
| 20 | +def test_priority_class_attribute_set_via_decorator(): |
| 21 | + """Explicit priority_class value is preserved in attributes.""" |
| 22 | + deco = KubernetesDecorator(attributes={"priority_class": "high-priority"}) |
| 23 | + assert deco.attributes["priority_class"] == "high-priority" |
| 24 | + |
| 25 | + |
| 26 | +def test_priority_class_flows_to_pod_spec(): |
| 27 | + """priority_class kwarg becomes priorityClassName in V1PodSpec.""" |
| 28 | + from metaflow.plugins.kubernetes.kubernetes_job import KubernetesJob |
| 29 | + |
| 30 | + mock_client = MagicMock() |
| 31 | + # The client.get() returns the kubernetes.client module mock |
| 32 | + k8s_module = MagicMock() |
| 33 | + mock_client.get.return_value = k8s_module |
| 34 | + |
| 35 | + kwargs = dict( |
| 36 | + use_tmpfs=False, |
| 37 | + tmpfs_size=None, |
| 38 | + tmpfs_path="/metaflow_temp", |
| 39 | + shared_memory=None, |
| 40 | + qos="Burstable", |
| 41 | + cpu="1", |
| 42 | + memory="4096", |
| 43 | + disk="10240", |
| 44 | + gpu=None, |
| 45 | + gpu_vendor="nvidia", |
| 46 | + image="python:3.9", |
| 47 | + image_pull_policy="IfNotPresent", |
| 48 | + image_pull_secrets=[], |
| 49 | + command=["echo", "hello"], |
| 50 | + step_name="my_step", |
| 51 | + namespace="default", |
| 52 | + timeout_in_seconds=300, |
| 53 | + annotations={}, |
| 54 | + labels={}, |
| 55 | + port=None, |
| 56 | + secrets=[], |
| 57 | + node_selector=None, |
| 58 | + tolerations=[], |
| 59 | + persistent_volume_claims=None, |
| 60 | + priority_class="batch-high", |
| 61 | + service_account="default", |
| 62 | + security_context={}, |
| 63 | + ) |
| 64 | + job = KubernetesJob(client=mock_client, **kwargs) |
| 65 | + job.create_job_spec() |
| 66 | + |
| 67 | + # V1PodSpec should have been called with priority_class_name="batch-high" |
| 68 | + k8s_module.V1PodSpec.assert_called_once() |
| 69 | + call_kwargs = k8s_module.V1PodSpec.call_args |
| 70 | + assert call_kwargs[1]["priority_class_name"] == "batch-high" |
| 71 | + |
| 72 | + |
| 73 | +def test_priority_class_none_passes_none_to_pod_spec(): |
| 74 | + """When priority_class is None, priorityClassName is None in V1PodSpec.""" |
| 75 | + from metaflow.plugins.kubernetes.kubernetes_job import KubernetesJob |
| 76 | + |
| 77 | + mock_client = MagicMock() |
| 78 | + k8s_module = MagicMock() |
| 79 | + mock_client.get.return_value = k8s_module |
| 80 | + |
| 81 | + kwargs = dict( |
| 82 | + use_tmpfs=False, |
| 83 | + tmpfs_size=None, |
| 84 | + tmpfs_path="/metaflow_temp", |
| 85 | + shared_memory=None, |
| 86 | + qos="Burstable", |
| 87 | + cpu="1", |
| 88 | + memory="4096", |
| 89 | + disk="10240", |
| 90 | + gpu=None, |
| 91 | + gpu_vendor="nvidia", |
| 92 | + image="python:3.9", |
| 93 | + image_pull_policy="IfNotPresent", |
| 94 | + image_pull_secrets=[], |
| 95 | + command=["echo", "hello"], |
| 96 | + step_name="my_step", |
| 97 | + namespace="default", |
| 98 | + timeout_in_seconds=300, |
| 99 | + annotations={}, |
| 100 | + labels={}, |
| 101 | + port=None, |
| 102 | + secrets=[], |
| 103 | + node_selector=None, |
| 104 | + tolerations=[], |
| 105 | + persistent_volume_claims=None, |
| 106 | + priority_class=None, |
| 107 | + service_account="default", |
| 108 | + security_context={}, |
| 109 | + ) |
| 110 | + job = KubernetesJob(client=mock_client, **kwargs) |
| 111 | + job.create_job_spec() |
| 112 | + |
| 113 | + call_kwargs = k8s_module.V1PodSpec.call_args |
| 114 | + assert call_kwargs[1]["priority_class_name"] is None |
0 commit comments