|
| 1 | +# Copyright (C) 2026 Vates SAS |
| 2 | +# |
| 3 | +# This program is free software: you can redistribute it and/or modify |
| 4 | +# it under the terms of the GNU General Public License as published by |
| 5 | +# the Free Software Foundation, either version 3 of the License, or |
| 6 | +# (at your option) any later version. |
| 7 | +# This program is distributed in the hope that it will be useful, |
| 8 | +# but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 9 | +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 10 | +# GNU General Public License for more details. |
| 11 | +# |
| 12 | +# You should have received a copy of the GNU General Public License |
| 13 | +# along with this program. If not, see <https://www.gnu.org/licenses/>. |
| 14 | + |
| 15 | +from pathlib import Path |
| 16 | +from unittest.mock import patch |
| 17 | + |
| 18 | +import pytest |
| 19 | + |
| 20 | +from xcp_storage.config.platform import get_exec_path, get_os_ids |
| 21 | + |
| 22 | +from xcp_storage.typing import Final, Iterator, Tuple |
| 23 | + |
| 24 | +# ============================================================================== |
| 25 | + |
| 26 | +@pytest.fixture(autouse=True) |
| 27 | +def clear_os_ids_cache() -> Iterator[None]: |
| 28 | + get_os_ids.cache_clear() |
| 29 | + yield |
| 30 | + get_os_ids.cache_clear() |
| 31 | + |
| 32 | +@pytest.fixture |
| 33 | +def os_release_path(tmp_path: Path) -> Iterator[Path]: |
| 34 | + path = tmp_path / "os-release" |
| 35 | + with patch("xcp_storage.config.platform._OS_RELEASE_PATH", str(path)): |
| 36 | + yield path |
| 37 | + |
| 38 | +# ------------------------------------------------------------------------------ |
| 39 | + |
| 40 | +class TestGetOsIds: |
| 41 | + @pytest.mark.parametrize(("content", "expected"), [ |
| 42 | + ("ID=alpine\n", ("alpine",)), |
| 43 | + ('NAME="Ubuntu"\nID=ubuntu\nID_LIKE=debian\n', ("ubuntu", "debian")), |
| 44 | + ('ID="rhel"\nID_LIKE="fedora centos"\n', ("rhel", "fedora", "centos")), |
| 45 | + ("NAME=Foo\n", ()), |
| 46 | + ]) |
| 47 | + def test_parsing(self, os_release_path: Path, content: str, expected: Tuple[str, ...]) -> None: |
| 48 | + os_release_path.write_text(content) |
| 49 | + assert get_os_ids() == expected |
| 50 | + |
| 51 | + def test_without_os_release_file(self, os_release_path: Path) -> None: |
| 52 | + assert not os_release_path.exists() |
| 53 | + assert get_os_ids() == () |
| 54 | + |
| 55 | + def test_os_release_file_is_cached(self, os_release_path: Path) -> None: |
| 56 | + os_release_path.write_text("ID=alpine\n") |
| 57 | + expected = get_os_ids() |
| 58 | + os_release_path.write_text("ID=ubuntu\n") |
| 59 | + assert get_os_ids() == expected |
| 60 | + |
| 61 | +# ------------------------------------------------------------------------------ |
| 62 | + |
| 63 | +class TestGetExecPath: |
| 64 | + DEFAULT_PATH: Final = "/opt/default/tool" |
| 65 | + ALT_PATH: Final = "/opt/alt/tool" |
| 66 | + BY_OS_ID: Final = {"debian": ALT_PATH} |
| 67 | + |
| 68 | + def test_with_mapping_id_match(self, os_release_path: Path) -> None: |
| 69 | + os_release_path.write_text("ID=debian\nID_LIKE=unknown\n") |
| 70 | + assert get_exec_path(self.DEFAULT_PATH, self.BY_OS_ID) == self.ALT_PATH |
| 71 | + |
| 72 | + def test_with_mapping_id_like_match(self, os_release_path: Path) -> None: |
| 73 | + os_release_path.write_text("ID=ubuntu\nID_LIKE=debian\n") |
| 74 | + assert get_exec_path(self.DEFAULT_PATH, self.BY_OS_ID) == self.ALT_PATH |
| 75 | + |
| 76 | + def test_without_mapping_match(self, os_release_path: Path) -> None: |
| 77 | + os_release_path.write_text("ID=alpine\n") |
| 78 | + assert get_exec_path(self.DEFAULT_PATH, self.BY_OS_ID) == self.DEFAULT_PATH |
| 79 | + |
| 80 | + def test_without_os_release_file(self, os_release_path: Path) -> None: |
| 81 | + assert not os_release_path.exists() |
| 82 | + assert get_exec_path(self.DEFAULT_PATH, self.BY_OS_ID) == self.DEFAULT_PATH |
0 commit comments