|
3 | 3 |
|
4 | 4 | from __future__ import annotations |
5 | 5 |
|
| 6 | +import importlib.util |
| 7 | +import subprocess |
6 | 8 | import sys |
7 | 9 | import tempfile |
8 | 10 | from pathlib import Path |
| 11 | +from unittest import mock |
9 | 12 |
|
| 13 | +import pytest |
10 | 14 | import yaml |
11 | 15 |
|
12 | 16 | sys.path.insert(0, str(Path(__file__).resolve().parent)) |
|
28 | 32 | "ubuntu:24.04@" |
29 | 33 | "sha256:561618e2c15bf2397621dd04f96926663a3b5616c189cf7e38db7e82f5c538ea" |
30 | 34 | ) |
| 35 | +PYTHON_IMAGE = ( |
| 36 | + "python:3.12-alpine@" |
| 37 | + "sha256:c4634f578a412db396771b61b064c6e546c9d6414c7fb5b1b05d5871f1885f7b" |
| 38 | +) |
| 39 | + |
| 40 | + |
| 41 | +@pytest.mark.parametrize("check_path", ["platform_config.py", "system/hbm_memory_exposure.py"]) |
| 42 | +@pytest.mark.parametrize("override", [None, "registry.example/python@" + PYTHON_IMAGE.split("@")[1]]) |
| 43 | +def test_python_host_checks_use_pinned_default_and_preserve_override(check_path, override): |
| 44 | + spec = importlib.util.spec_from_file_location("host_check", SCRIPT.parent / "checks" / check_path) |
| 45 | + module = importlib.util.module_from_spec(spec) |
| 46 | + spec.loader.exec_module(module) |
| 47 | + node = {"name": "gpu-0"} |
| 48 | + fanout = mock.Mock() |
| 49 | + fanout.k8s_gpu_nodes.return_value = [node] |
| 50 | + fanout.fan_out_k8s.side_effect = lambda worker, **kwargs: worker(node) |
| 51 | + env = {} if override is None else {"CLUSTERMAX_AUDIT_K8S_HOST_CHECK_IMAGE": override} |
| 52 | + with ( |
| 53 | + mock.patch.dict(module.os.environ, env, clear=True), |
| 54 | + mock.patch.object(module, "load_fanout", return_value=fanout), |
| 55 | + mock.patch.object(module, "kubectl", return_value=subprocess.CompletedProcess([], 0, "{}")), |
| 56 | + mock.patch.object(module, "run_k8s_host_check") as run_host, |
| 57 | + ): |
| 58 | + if check_path == "platform_config.py": |
| 59 | + module.run_k8s_check([node]) |
| 60 | + else: |
| 61 | + module.run_k8s_check() |
| 62 | + namespace, selected_node, image = run_host.call_args.args |
| 63 | + manifest = module.pod_manifest(namespace, selected_node, image, "test-pod") |
| 64 | + assert manifest["spec"]["containers"][0]["image"] == (override or PYTHON_IMAGE) |
31 | 65 |
|
32 | 66 |
|
33 | 67 | def _applied_manifest(function_name: str, setup: str) -> dict: |
|
0 commit comments