|
| 1 | +"""Real-runtime integration tests for `vp version`. |
| 2 | +
|
| 3 | +These tests are intended for dedicated CI jobs that prepare concrete host |
| 4 | +runtime scenarios. They are skipped unless ``VP_REAL_RUNTIME_SCENARIO`` is set. |
| 5 | +""" |
| 6 | + |
| 7 | +from __future__ import annotations |
| 8 | + |
| 9 | +import os |
| 10 | +import subprocess |
| 11 | +import sys |
| 12 | +from pathlib import Path |
| 13 | + |
| 14 | +import pytest |
| 15 | + |
| 16 | +SCENARIO = os.environ.get("VP_REAL_RUNTIME_SCENARIO") |
| 17 | + |
| 18 | +if SCENARIO is None: |
| 19 | + pytest.skip( |
| 20 | + "requires dedicated runtime integration CI", |
| 21 | + allow_module_level=True, |
| 22 | + ) |
| 23 | + |
| 24 | +ROOT = Path(__file__).resolve().parents[1] |
| 25 | +SRC = ROOT / "src" |
| 26 | + |
| 27 | + |
| 28 | +def _run_vp(tmp_path: Path, *args: str) -> subprocess.CompletedProcess[str]: |
| 29 | + env = os.environ.copy() |
| 30 | + env["VP_CONFIG_DIR"] = str(tmp_path / "global-config") |
| 31 | + env["PYTHONPATH"] = ( |
| 32 | + f"{SRC}{os.pathsep}{env['PYTHONPATH']}" |
| 33 | + if "PYTHONPATH" in env and env["PYTHONPATH"] |
| 34 | + else str(SRC) |
| 35 | + ) |
| 36 | + env.pop("VP_CONTAINER_RUNTIME", None) |
| 37 | + return subprocess.run( |
| 38 | + [sys.executable, "-m", "vibepod.cli", *args], |
| 39 | + cwd=tmp_path, |
| 40 | + capture_output=True, |
| 41 | + text=True, |
| 42 | + check=False, |
| 43 | + env=env, |
| 44 | + timeout=60, |
| 45 | + ) |
| 46 | + |
| 47 | + |
| 48 | +def _assert_ok(result: subprocess.CompletedProcess[str]) -> None: |
| 49 | + assert result.returncode == 0, ( |
| 50 | + f"stdout:\n{result.stdout}\n" |
| 51 | + f"stderr:\n{result.stderr}" |
| 52 | + ) |
| 53 | + |
| 54 | + |
| 55 | +def _runtime_parts(output: str) -> tuple[str, str]: |
| 56 | + for line in output.splitlines(): |
| 57 | + if not line.startswith("Runtime:"): |
| 58 | + continue |
| 59 | + _, runtime_name, runtime_version = line.split(maxsplit=2) |
| 60 | + return runtime_name, runtime_version |
| 61 | + raise AssertionError(f"Missing Runtime line in output:\n{output}") |
| 62 | + |
| 63 | + |
| 64 | +def _assert_runtime( |
| 65 | + result: subprocess.CompletedProcess[str], |
| 66 | + *, |
| 67 | + runtime_name: str, |
| 68 | +) -> None: |
| 69 | + _assert_ok(result) |
| 70 | + assert "VibePod CLI:" in result.stdout |
| 71 | + assert "Python:" in result.stdout |
| 72 | + actual_runtime, actual_version = _runtime_parts(result.stdout) |
| 73 | + assert actual_runtime == runtime_name |
| 74 | + assert actual_version not in {"unknown", "unavailable"} |
| 75 | + |
| 76 | + |
| 77 | +def _assert_unavailable(result: subprocess.CompletedProcess[str]) -> None: |
| 78 | + _assert_ok(result) |
| 79 | + assert _runtime_parts(result.stdout) == ("unknown", "unavailable") |
| 80 | + |
| 81 | + |
| 82 | +def test_version_with_docker_only(tmp_path: Path) -> None: |
| 83 | + if SCENARIO != "docker-only": |
| 84 | + pytest.skip("scenario mismatch") |
| 85 | + |
| 86 | + result = _run_vp(tmp_path, "version") |
| 87 | + |
| 88 | + _assert_runtime(result, runtime_name="docker") |
| 89 | + |
| 90 | + |
| 91 | +def test_version_with_podman_only(tmp_path: Path) -> None: |
| 92 | + if SCENARIO != "podman-only": |
| 93 | + pytest.skip("scenario mismatch") |
| 94 | + |
| 95 | + result = _run_vp(tmp_path, "version") |
| 96 | + |
| 97 | + _assert_runtime(result, runtime_name="podman") |
| 98 | + |
| 99 | + |
| 100 | +def test_version_with_no_runtime_available(tmp_path: Path) -> None: |
| 101 | + if SCENARIO != "none": |
| 102 | + pytest.skip("scenario mismatch") |
| 103 | + |
| 104 | + result = _run_vp(tmp_path, "version") |
| 105 | + |
| 106 | + _assert_unavailable(result) |
| 107 | + |
| 108 | + |
| 109 | +def test_version_with_both_runtimes_defaults_to_docker_non_interactive( |
| 110 | + tmp_path: Path, |
| 111 | +) -> None: |
| 112 | + if SCENARIO != "both-auto": |
| 113 | + pytest.skip("scenario mismatch") |
| 114 | + |
| 115 | + result = _run_vp(tmp_path, "version") |
| 116 | + |
| 117 | + _assert_runtime(result, runtime_name="docker") |
| 118 | + |
| 119 | + |
| 120 | +def test_version_uses_saved_docker_default_when_both_runtimes_are_available( |
| 121 | + tmp_path: Path, |
| 122 | +) -> None: |
| 123 | + if SCENARIO != "both-default-docker": |
| 124 | + pytest.skip("scenario mismatch") |
| 125 | + |
| 126 | + set_result = _run_vp(tmp_path, "config", "runtime", "docker") |
| 127 | + result = _run_vp(tmp_path, "version") |
| 128 | + |
| 129 | + _assert_ok(set_result) |
| 130 | + assert "Set default container runtime to 'docker'" in set_result.stdout |
| 131 | + _assert_runtime(result, runtime_name="docker") |
| 132 | + |
| 133 | + |
| 134 | +def test_version_uses_saved_podman_default_when_both_runtimes_are_available( |
| 135 | + tmp_path: Path, |
| 136 | +) -> None: |
| 137 | + if SCENARIO != "both-default-podman": |
| 138 | + pytest.skip("scenario mismatch") |
| 139 | + |
| 140 | + set_result = _run_vp(tmp_path, "config", "runtime", "podman") |
| 141 | + result = _run_vp(tmp_path, "version") |
| 142 | + |
| 143 | + _assert_ok(set_result) |
| 144 | + assert "Set default container runtime to 'podman'" in set_result.stdout |
| 145 | + _assert_runtime(result, runtime_name="podman") |
| 146 | + |
| 147 | + |
| 148 | +def test_version_reflects_runtime_switch_command(tmp_path: Path) -> None: |
| 149 | + if SCENARIO != "both-switch": |
| 150 | + pytest.skip("scenario mismatch") |
| 151 | + |
| 152 | + first_set_result = _run_vp(tmp_path, "config", "runtime", "docker") |
| 153 | + first_version_result = _run_vp(tmp_path, "version") |
| 154 | + second_set_result = _run_vp(tmp_path, "config", "runtime", "podman") |
| 155 | + second_version_result = _run_vp(tmp_path, "version") |
| 156 | + |
| 157 | + _assert_ok(first_set_result) |
| 158 | + _assert_runtime(first_version_result, runtime_name="docker") |
| 159 | + _assert_ok(second_set_result) |
| 160 | + _assert_runtime(second_version_result, runtime_name="podman") |
0 commit comments