|
| 1 | +# SPDX-License-Identifier: Apache-2.0 |
| 2 | +# Copyright 2026 Ague Samuel Amen |
| 3 | +# |
| 4 | +# Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | +# you may not use this file except in compliance with the License. |
| 6 | +# You may obtain a copy of the License at |
| 7 | +# |
| 8 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | +# |
| 10 | +# Unless required by applicable law or agreed to in writing, software |
| 11 | +# distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | +# See the License for the specific language governing permissions and |
| 14 | +# limitations under the License. |
| 15 | + |
| 16 | +"""Headless system dependency installation tests.""" |
| 17 | + |
| 18 | +from __future__ import annotations |
| 19 | + |
| 20 | + |
| 21 | +def test_install_system_packages_headless_linux_uses_noninteractive_sudo( |
| 22 | + monkeypatch, |
| 23 | +) -> None: |
| 24 | + """Headless mode should run package install commands without Qt widgets.""" |
| 25 | + import Core.sys_deps as sys_deps |
| 26 | + |
| 27 | + executed: list[list[str]] = [] |
| 28 | + |
| 29 | + class _Result: |
| 30 | + def __init__(self, returncode: int = 0) -> None: |
| 31 | + self.returncode = returncode |
| 32 | + self.stdout = "" |
| 33 | + self.stderr = "" |
| 34 | + |
| 35 | + monkeypatch.setattr("platform.system", lambda: "Linux") |
| 36 | + monkeypatch.setattr(sys_deps.shutil, "which", lambda name: "/usr/bin/apt" if name == "apt" else None) |
| 37 | + monkeypatch.setattr(sys_deps.os, "geteuid", lambda: 1000, raising=False) |
| 38 | + |
| 39 | + def _fake_run(cmd, **_kwargs): |
| 40 | + executed.append(list(cmd)) |
| 41 | + return _Result(0) |
| 42 | + |
| 43 | + monkeypatch.setattr(sys_deps.subprocess, "run", _fake_run) |
| 44 | + |
| 45 | + assert sys_deps.install_system_packages(["cloc"], gui=None) is True |
| 46 | + assert executed == [ |
| 47 | + ["sudo", "-n", "apt-get", "-o", "Acquire::Retries=3", "update"], |
| 48 | + [ |
| 49 | + "sudo", |
| 50 | + "-n", |
| 51 | + "apt-get", |
| 52 | + "-o", |
| 53 | + "Dpkg::Options::=--force-confdef", |
| 54 | + "-o", |
| 55 | + "Dpkg::Options::=--force-confnew", |
| 56 | + "-o", |
| 57 | + "Acquire::Retries=3", |
| 58 | + "install", |
| 59 | + "-y", |
| 60 | + "--no-install-recommends", |
| 61 | + "cloc", |
| 62 | + ], |
| 63 | + ] |
| 64 | + |
| 65 | + |
| 66 | +def test_install_system_packages_headless_does_not_use_qt_manager(monkeypatch) -> None: |
| 67 | + """Headless mode must bypass the Qt-based SysDependencyManager path.""" |
| 68 | + import Core.sys_deps as sys_deps |
| 69 | + |
| 70 | + monkeypatch.setattr("platform.system", lambda: "Linux") |
| 71 | + monkeypatch.setattr(sys_deps.shutil, "which", lambda name: "/usr/bin/dnf" if name == "dnf" else None) |
| 72 | + monkeypatch.setattr(sys_deps.os, "geteuid", lambda: 0, raising=False) |
| 73 | + monkeypatch.setattr( |
| 74 | + sys_deps, |
| 75 | + "SysDependencyManager", |
| 76 | + lambda _gui: (_ for _ in ()).throw(AssertionError("Qt manager should not be used in headless mode")), |
| 77 | + ) |
| 78 | + |
| 79 | + class _Result: |
| 80 | + def __init__(self, returncode: int = 0) -> None: |
| 81 | + self.returncode = returncode |
| 82 | + self.stdout = "" |
| 83 | + self.stderr = "" |
| 84 | + |
| 85 | + monkeypatch.setattr(sys_deps.subprocess, "run", lambda *_a, **_k: _Result(0)) |
| 86 | + |
| 87 | + assert sys_deps.install_system_packages(["cloc"], gui=None) is True |
| 88 | + |
| 89 | + |
| 90 | +def test_install_system_packages_headless_retries_transient_failures(monkeypatch) -> None: |
| 91 | + """Headless install should retry command steps before failing.""" |
| 92 | + import Core.sys_deps as sys_deps |
| 93 | + |
| 94 | + attempts = {"count": 0} |
| 95 | + |
| 96 | + class _Result: |
| 97 | + def __init__(self, returncode: int) -> None: |
| 98 | + self.returncode = returncode |
| 99 | + self.stdout = "" |
| 100 | + self.stderr = "" |
| 101 | + |
| 102 | + monkeypatch.setattr("platform.system", lambda: "Linux") |
| 103 | + monkeypatch.setattr( |
| 104 | + sys_deps.shutil, |
| 105 | + "which", |
| 106 | + lambda name: "/usr/bin/dnf" if name == "dnf" else None, |
| 107 | + ) |
| 108 | + monkeypatch.setattr(sys_deps.os, "geteuid", lambda: 0, raising=False) |
| 109 | + monkeypatch.setattr(sys_deps.time, "sleep", lambda *_a, **_k: None) |
| 110 | + |
| 111 | + def _fake_run(_cmd, **_kwargs): |
| 112 | + attempts["count"] += 1 |
| 113 | + if attempts["count"] == 1: |
| 114 | + return _Result(1) |
| 115 | + return _Result(0) |
| 116 | + |
| 117 | + monkeypatch.setattr(sys_deps.subprocess, "run", _fake_run) |
| 118 | + |
| 119 | + assert sys_deps.install_system_packages(["cloc"], gui=None) is True |
| 120 | + assert attempts["count"] == 2 |
0 commit comments