|
| 1 | +"""Tests for @resources vs @kubernetes decorator precedence logic. |
| 2 | +
|
| 3 | +The merge logic in KubernetesDecorator.step_init works as follows: |
| 4 | +- If @kubernetes attribute is at its default AND @resources explicitly sets a value |
| 5 | + → use the @resources value (even if lower than the @kubernetes default). |
| 6 | +- If both explicitly set → take max. |
| 7 | +- If both at default → take max (which equals default). |
| 8 | +""" |
| 9 | + |
| 10 | +K8S_DEFAULTS = {"cpu": "1", "memory": "4096", "disk": "10240"} |
| 11 | +RESOURCES_DEFAULTS = {"cpu": "1", "memory": "4096"} |
| 12 | + |
| 13 | + |
| 14 | +def _merge(k, k8s_val, resources_val): |
| 15 | + """Replicate the merge logic from KubernetesDecorator.step_init.""" |
| 16 | + k8s_is_default = float(k8s_val or 0) == float(K8S_DEFAULTS.get(k) or 0) |
| 17 | + resources_is_default = k in RESOURCES_DEFAULTS and float( |
| 18 | + resources_val or 0 |
| 19 | + ) == float(RESOURCES_DEFAULTS.get(k) or 0) |
| 20 | + |
| 21 | + if k8s_is_default and not resources_is_default: |
| 22 | + return str(float(resources_val or 0)) |
| 23 | + else: |
| 24 | + return str(max(float(k8s_val or 0), float(resources_val or 0))) |
| 25 | + |
| 26 | + |
| 27 | +def test_resources_lower_than_k8s_default(): |
| 28 | + """@resources(memory=256) with @kubernetes default (4096) → resources wins.""" |
| 29 | + result = _merge("memory", K8S_DEFAULTS["memory"], "256") |
| 30 | + assert result == "256.0" |
| 31 | + |
| 32 | + |
| 33 | +def test_resources_higher_than_k8s_default(): |
| 34 | + """@resources(memory=8192) with @kubernetes default (4096) → resources wins.""" |
| 35 | + result = _merge("memory", K8S_DEFAULTS["memory"], "8192") |
| 36 | + assert result == "8192.0" |
| 37 | + |
| 38 | + |
| 39 | +def test_both_explicit_resources_higher(): |
| 40 | + """@kubernetes(memory=2048) explicit with @resources(memory=8192) → max wins.""" |
| 41 | + result = _merge("memory", "2048", "8192") |
| 42 | + assert result == "8192.0" |
| 43 | + |
| 44 | + |
| 45 | +def test_both_explicit_k8s_higher(): |
| 46 | + """@kubernetes(memory=8192) explicit with @resources(memory=2048) → max wins.""" |
| 47 | + result = _merge("memory", "8192", "2048") |
| 48 | + assert result == "8192.0" |
| 49 | + |
| 50 | + |
| 51 | +def test_both_at_default(): |
| 52 | + """Both at default → stays at default (4096).""" |
| 53 | + result = _merge("memory", K8S_DEFAULTS["memory"], RESOURCES_DEFAULTS["memory"]) |
| 54 | + assert result == "4096.0" |
0 commit comments