|
| 1 | +"""Tests for ``PodTemplate.with_termination_grace_period`` — the helper that sets |
| 2 | +``terminationGracePeriodSeconds`` on a pod template without needing the ``kubernetes`` package. |
| 3 | +""" |
| 4 | + |
| 5 | +import copy |
| 6 | +import pathlib |
| 7 | +from datetime import timedelta |
| 8 | + |
| 9 | +import pytest |
| 10 | +from kubernetes.client import V1Container, V1PodSpec |
| 11 | + |
| 12 | +import flyte |
| 13 | +from flyte._internal.runtime.task_serde import get_proto_task |
| 14 | +from flyte._pod import PodTemplate |
| 15 | +from flyte.models import SerializationContext |
| 16 | + |
| 17 | + |
| 18 | +def _grace_seconds(task): |
| 19 | + """Serialize a task and return the pod spec's terminationGracePeriodSeconds (or None).""" |
| 20 | + ctx = SerializationContext(project="p", domain="d", org="o", version="v1", root_dir=pathlib.Path.cwd()) |
| 21 | + proto = get_proto_task(task, ctx) |
| 22 | + if not proto.HasField("k8s_pod"): |
| 23 | + return None |
| 24 | + pod_spec = proto.k8s_pod.pod_spec # google.protobuf.Struct: supports `in` / `[]`, not `.get` |
| 25 | + return pod_spec["terminationGracePeriodSeconds"] if "terminationGracePeriodSeconds" in pod_spec else None |
| 26 | + |
| 27 | + |
| 28 | +# --------------------------------------------------------------------------- # |
| 29 | +# PodTemplate.with_termination_grace_period |
| 30 | +# --------------------------------------------------------------------------- # |
| 31 | + |
| 32 | + |
| 33 | +class TestWithTerminationGracePeriod: |
| 34 | + def test_synthesizes_pod_spec_with_primary_no_kubernetes_needed(self): |
| 35 | + # Starting from a bare PodTemplate(), the helper builds the pod spec + primary container |
| 36 | + # internally, so the caller never touches kubernetes.client. |
| 37 | + pt = PodTemplate().with_termination_grace_period(42) |
| 38 | + assert pt.pod_spec.termination_grace_period_seconds == 42 |
| 39 | + assert any(c.name == "primary" for c in pt.pod_spec.containers) |
| 40 | + |
| 41 | + def test_int_seconds(self): |
| 42 | + assert PodTemplate().with_termination_grace_period(30).pod_spec.termination_grace_period_seconds == 30 |
| 43 | + |
| 44 | + def test_timedelta(self): |
| 45 | + pt = PodTemplate().with_termination_grace_period(timedelta(minutes=2)) |
| 46 | + assert pt.pod_spec.termination_grace_period_seconds == 120 |
| 47 | + |
| 48 | + def test_zero_is_kept(self): |
| 49 | + # 0 is a meaningful k8s value (immediate kill), not "unset". |
| 50 | + assert PodTemplate().with_termination_grace_period(0).pod_spec.termination_grace_period_seconds == 0 |
| 51 | + |
| 52 | + def test_custom_primary_container_name(self): |
| 53 | + pt = PodTemplate(primary_container_name="worker").with_termination_grace_period(10) |
| 54 | + assert any(c.name == "worker" for c in pt.pod_spec.containers) |
| 55 | + assert pt.pod_spec.termination_grace_period_seconds == 10 |
| 56 | + |
| 57 | + def test_preserves_existing_pod_spec_fields(self): |
| 58 | + base = PodTemplate(pod_spec=V1PodSpec(containers=[V1Container(name="primary", image="img")], hostname="h")) |
| 59 | + pt = base.with_termination_grace_period(99) |
| 60 | + assert pt.pod_spec.termination_grace_period_seconds == 99 |
| 61 | + assert pt.pod_spec.containers[0].image == "img" |
| 62 | + assert pt.pod_spec.hostname == "h" |
| 63 | + |
| 64 | + def test_does_not_mutate_original(self): |
| 65 | + base = PodTemplate(pod_spec=V1PodSpec(containers=[V1Container(name="primary")])) |
| 66 | + snapshot = copy.deepcopy(base) |
| 67 | + base.with_termination_grace_period(99) |
| 68 | + assert base == snapshot |
| 69 | + |
| 70 | + def test_reapplying_overwrites(self): |
| 71 | + pt = PodTemplate().with_termination_grace_period(30).with_termination_grace_period(60) |
| 72 | + assert pt.pod_spec.termination_grace_period_seconds == 60 |
| 73 | + |
| 74 | + def test_composes_with_capability_helpers(self): |
| 75 | + pt = PodTemplate().allow_fuse().with_termination_grace_period(60) |
| 76 | + primary = next(c for c in pt.pod_spec.containers if c.name == "primary") |
| 77 | + assert pt.pod_spec.termination_grace_period_seconds == 60 |
| 78 | + assert primary.resources.requests["smarter-devices/fuse"] == "1" # allow_fuse survived |
| 79 | + |
| 80 | + def test_bool_rejected(self): |
| 81 | + with pytest.raises(TypeError, match="not bool"): |
| 82 | + PodTemplate().with_termination_grace_period(True) |
| 83 | + |
| 84 | + def test_negative_rejected(self): |
| 85 | + with pytest.raises(ValueError, match="non-negative"): |
| 86 | + PodTemplate().with_termination_grace_period(-1) |
| 87 | + |
| 88 | + def test_wrong_type_rejected(self): |
| 89 | + with pytest.raises(TypeError): |
| 90 | + PodTemplate().with_termination_grace_period("30") |
| 91 | + |
| 92 | + def test_none_rejected(self): |
| 93 | + with pytest.raises(TypeError): |
| 94 | + PodTemplate().with_termination_grace_period(None) |
| 95 | + |
| 96 | + |
| 97 | +# --------------------------------------------------------------------------- # |
| 98 | +# End-to-end: the helper output flows through task serialization |
| 99 | +# --------------------------------------------------------------------------- # |
| 100 | + |
| 101 | + |
| 102 | +env = flyte.TaskEnvironment( |
| 103 | + name="tgp_env", |
| 104 | + pod_template=PodTemplate().with_termination_grace_period(timedelta(minutes=10)), |
| 105 | +) |
| 106 | + |
| 107 | + |
| 108 | +@env.task |
| 109 | +async def env_task() -> int: |
| 110 | + return 1 |
| 111 | + |
| 112 | + |
| 113 | +env_plain = flyte.TaskEnvironment(name="tgp_plain") |
| 114 | + |
| 115 | + |
| 116 | +@env_plain.task |
| 117 | +async def plain_task() -> int: |
| 118 | + return 1 |
| 119 | + |
| 120 | + |
| 121 | +@env_plain.task(pod_template=PodTemplate().with_termination_grace_period(300)) |
| 122 | +async def decorated_task() -> int: |
| 123 | + return 1 |
| 124 | + |
| 125 | + |
| 126 | +class TestSerialization: |
| 127 | + def test_environment_pod_template(self): |
| 128 | + assert _grace_seconds(env_task) == 600 |
| 129 | + |
| 130 | + def test_no_pod_template_uses_container_path(self): |
| 131 | + assert _grace_seconds(plain_task) is None |
| 132 | + ctx = SerializationContext(project="p", domain="d", org="o", version="v1", root_dir=pathlib.Path.cwd()) |
| 133 | + assert get_proto_task(plain_task, ctx).HasField("container") |
| 134 | + |
| 135 | + def test_decorator_pod_template(self): |
| 136 | + assert _grace_seconds(decorated_task) == 300 |
| 137 | + |
| 138 | + def test_override_pod_template(self): |
| 139 | + overridden = plain_task.override(pod_template=PodTemplate().with_termination_grace_period(15)) |
| 140 | + assert _grace_seconds(overridden) == 15 |
0 commit comments