mirror of
https://github.com/kuhyx/steam-backlog-enforcer.git
synced 2026-11-11 21:33:49 +01:00
A total gaming block on 2026-07-04 purged the Steam client (packages and
all data dirs, per total_block_purge_log.json) and it was never
reinstalled. The enforcer kept trying to drive it anyway: every start
called ensure_steam_debug_port() -> _launch_steam_with_debug(), waited
45s for a CDP port no process would ever open, raised, and exited 1 into
Restart=always. That ran to 1050+ restarts.
Each attempt also left a zombie behind: _run_as_user() launches
fire-and-forget via Popen and never reaps, so a launch that dies on exec
lingers as a dead process still named "steam". The focus-mode daemon
scans /proc for process names and read that zombie as a live Steam,
entering GAMING mode and killing the user's browsers every few seconds.
- steam_is_installed(): probe the real /usr/bin/steam. Deliberately not
shutil.which("steam") - a launcher wrapper on $PATH keeps `which`
truthy long after the package is gone, which is precisely how a
deleted Steam went on looking installed.
- SteamUnavailableError (a RuntimeError, so existing handlers still
catch it) raised on both "not installed" and CDP timeout, and caught
at the _enforce_hide_games boundary: no library to hide is not fatal,
the store block and install guard still work.
- Guard _enforce_loop_iteration and game_install._ensure_steam_running,
whose 15s sleep was the source of the observed flapping interval.
- do_enforce falls through to the idle loop rather than returning:
returning exits the process, which under Restart=always is the same
crash loop by another name.
- _reap_spawned() retires exited launches so no zombie named "steam"
outlives them.
Verified live: NRestarts 1050 -> 0, service active/running for 60s+ with
zero errors and zero steam zombies, logging "Steam is not installed -
nothing to enforce."
Also removed the orphaned /usr/local/bin/steam wrapper, a leftover of
the purge that pointed at the deleted binary. Its body was:
#!/bin/sh
exec /usr/bin/steam -cef-disable-gpu -cef-disable-gpu-compositing "$@"
Restore those flags if a Steam reinstall brings back GPU trouble.
Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_013rN2usTS3pYLJqeVg2ZpgR
299 lines
10 KiB
Python
299 lines
10 KiB
Python
"""Tests for game_install module."""
|
|
|
|
from __future__ import annotations
|
|
|
|
import os
|
|
from typing import TYPE_CHECKING
|
|
from unittest.mock import MagicMock, patch
|
|
|
|
import pytest
|
|
|
|
from steam_backlog_enforcer.game_install import (
|
|
_assert_not_real_steam,
|
|
_echo,
|
|
_ensure_steam_running,
|
|
_get_real_user,
|
|
_get_uid_gid_for_user,
|
|
_trigger_steam_install,
|
|
is_game_installed,
|
|
)
|
|
|
|
if TYPE_CHECKING:
|
|
from collections.abc import Iterator
|
|
from pathlib import Path
|
|
|
|
|
|
PKG = "steam_backlog_enforcer.game_install"
|
|
|
|
|
|
class TestAssertNotRealSteam:
|
|
"""Tests for the _assert_not_real_steam safety guard."""
|
|
|
|
def test_allows_tmp_path(self, tmp_path: Path) -> None:
|
|
"""Non-Steam paths pass through without raising."""
|
|
_assert_not_real_steam(tmp_path / "appmanifest_440.acf")
|
|
|
|
def test_raises_when_real_steam_not_redirected(self, tmp_path: Path) -> None:
|
|
"""Raises when path is under real Steam and STEAMAPPS_PATH is real."""
|
|
real = tmp_path / "real_steam"
|
|
real.mkdir()
|
|
fake_manifest = real / "appmanifest_440.acf"
|
|
fake_manifest.touch()
|
|
with (
|
|
patch(f"{PKG}._REAL_STEAMAPPS", real),
|
|
patch(f"{PKG}.STEAMAPPS_PATH", real),
|
|
pytest.raises(RuntimeError, match="SAFETY"),
|
|
):
|
|
_assert_not_real_steam(fake_manifest)
|
|
|
|
def test_allows_when_steamapps_redirected(self, tmp_path: Path) -> None:
|
|
"""No raise when STEAMAPPS_PATH differs from _REAL_STEAMAPPS."""
|
|
real = tmp_path / "real_steam"
|
|
real.mkdir()
|
|
fake_manifest = real / "appmanifest_440.acf"
|
|
fake_manifest.touch()
|
|
redirected = tmp_path / "fake_steam"
|
|
redirected.mkdir()
|
|
with (
|
|
patch(f"{PKG}._REAL_STEAMAPPS", real),
|
|
patch(f"{PKG}.STEAMAPPS_PATH", redirected),
|
|
):
|
|
_assert_not_real_steam(fake_manifest)
|
|
|
|
def test_noop_outside_pytest(self, tmp_path: Path) -> None:
|
|
"""In production (no PYTEST_CURRENT_TEST) the guard is a no-op."""
|
|
real = tmp_path / "real_steam"
|
|
real.mkdir()
|
|
fake_manifest = real / "appmanifest_440.acf"
|
|
fake_manifest.touch()
|
|
env = {k: v for k, v in os.environ.items() if k != "PYTEST_CURRENT_TEST"}
|
|
with (
|
|
patch.dict(os.environ, env, clear=True),
|
|
patch(f"{PKG}._REAL_STEAMAPPS", real),
|
|
patch(f"{PKG}.STEAMAPPS_PATH", real),
|
|
):
|
|
_assert_not_real_steam(fake_manifest)
|
|
|
|
|
|
class TestEcho:
|
|
"""Tests for _echo."""
|
|
|
|
def test_default(self, capsys: pytest.CaptureFixture[str]) -> None:
|
|
_echo("hello")
|
|
assert capsys.readouterr().out == "hello\n"
|
|
|
|
def test_custom_end(self, capsys: pytest.CaptureFixture[str]) -> None:
|
|
_echo("hi", end="")
|
|
assert capsys.readouterr().out == "hi"
|
|
|
|
def test_empty(self, capsys: pytest.CaptureFixture[str]) -> None:
|
|
_echo()
|
|
assert capsys.readouterr().out == "\n"
|
|
|
|
def test_flush(self, capsys: pytest.CaptureFixture[str]) -> None:
|
|
_echo("x", flush=True)
|
|
assert capsys.readouterr().out == "x\n"
|
|
|
|
|
|
class TestTriggerSteamInstall:
|
|
"""Tests for _trigger_steam_install."""
|
|
|
|
def test_success(self) -> None:
|
|
with patch("steam_backlog_enforcer.game_install.subprocess.run") as mock_run:
|
|
result = _trigger_steam_install(440, "TF2")
|
|
assert result is True
|
|
mock_run.assert_called_once()
|
|
|
|
def test_file_not_found(self) -> None:
|
|
with patch(
|
|
"steam_backlog_enforcer.game_install.subprocess.run",
|
|
side_effect=FileNotFoundError,
|
|
):
|
|
result = _trigger_steam_install(440, "TF2")
|
|
assert result is False
|
|
|
|
def test_os_error(self) -> None:
|
|
with patch(
|
|
"steam_backlog_enforcer.game_install.subprocess.run",
|
|
side_effect=OSError,
|
|
):
|
|
result = _trigger_steam_install(440, "TF2")
|
|
assert result is False
|
|
|
|
def test_timeout(self) -> None:
|
|
import subprocess
|
|
|
|
with patch(
|
|
"steam_backlog_enforcer.game_install.subprocess.run",
|
|
side_effect=subprocess.TimeoutExpired("cmd", 15),
|
|
):
|
|
result = _trigger_steam_install(440, "TF2")
|
|
assert result is False
|
|
|
|
|
|
class TestGetRealUser:
|
|
"""Tests for _get_real_user."""
|
|
|
|
def test_sudo_user(self) -> None:
|
|
with patch.dict(os.environ, {"SUDO_USER": "alice", "USER": "root"}):
|
|
assert _get_real_user() == "alice"
|
|
|
|
def test_regular_user(self) -> None:
|
|
with patch.dict(os.environ, {"USER": "bob"}, clear=False):
|
|
env = os.environ.copy()
|
|
env.pop("SUDO_USER", None)
|
|
with patch.dict(os.environ, env, clear=True):
|
|
assert _get_real_user() == "bob"
|
|
|
|
|
|
class TestGetUidGid:
|
|
"""Tests for _get_uid_gid_for_user."""
|
|
|
|
def test_known_user(self) -> None:
|
|
mock_pw = MagicMock()
|
|
mock_pw.pw_uid = 1001
|
|
mock_pw.pw_gid = 1001
|
|
with patch(
|
|
"steam_backlog_enforcer.game_install.pwd.getpwnam",
|
|
return_value=mock_pw,
|
|
):
|
|
assert _get_uid_gid_for_user("alice") == (1001, 1001)
|
|
|
|
def test_unknown_user(self) -> None:
|
|
with patch(
|
|
"steam_backlog_enforcer.game_install.pwd.getpwnam",
|
|
side_effect=KeyError,
|
|
):
|
|
assert _get_uid_gid_for_user("nobody") == (1000, 1000)
|
|
|
|
|
|
class TestIsGameInstalled:
|
|
"""Tests for is_game_installed."""
|
|
|
|
def test_installed(self, tmp_path: Path) -> None:
|
|
manifest = tmp_path / "appmanifest_440.acf"
|
|
manifest.touch()
|
|
with patch("steam_backlog_enforcer.game_install.STEAMAPPS_PATH", tmp_path):
|
|
assert is_game_installed(440) is True
|
|
|
|
def test_not_installed(self, tmp_path: Path) -> None:
|
|
with patch("steam_backlog_enforcer.game_install.STEAMAPPS_PATH", tmp_path):
|
|
assert is_game_installed(440) is False
|
|
|
|
|
|
class TestEnsureSteamRunning:
|
|
"""Tests for _ensure_steam_running."""
|
|
|
|
@pytest.fixture(autouse=True)
|
|
def _steam_present(self) -> Iterator[None]:
|
|
"""Pretend Steam is installed for every test in this class.
|
|
|
|
Without it the function returns before any launch, which on a test
|
|
machine without Steam would make the launch assertions vacuous. The
|
|
absent case is covered by test_skips_when_steam_absent.
|
|
"""
|
|
with patch(f"{PKG}.steam_is_installed", return_value=True):
|
|
yield
|
|
|
|
def test_skips_when_steam_absent(self) -> None:
|
|
"""With Steam uninstalled, do not try to launch a missing client.
|
|
|
|
Regression guard: this path used to exec a dead launcher wrapper and
|
|
then sleep 15s, leaving a zombie named "steam" behind each time.
|
|
"""
|
|
with (
|
|
patch(f"{PKG}.steam_is_installed", return_value=False),
|
|
patch(f"{PKG}.subprocess.Popen") as mock_popen,
|
|
patch(f"{PKG}.time.sleep") as mock_sleep,
|
|
):
|
|
_ensure_steam_running()
|
|
|
|
mock_popen.assert_not_called()
|
|
mock_sleep.assert_not_called()
|
|
|
|
def test_already_running(self) -> None:
|
|
mock_result = MagicMock(returncode=0)
|
|
with patch(
|
|
"steam_backlog_enforcer.game_install.subprocess.run",
|
|
return_value=mock_result,
|
|
):
|
|
_ensure_steam_running()
|
|
|
|
def test_not_running_starts_as_non_root(self) -> None:
|
|
mock_result = MagicMock(returncode=1)
|
|
with (
|
|
patch(
|
|
"steam_backlog_enforcer.game_install.subprocess.run",
|
|
return_value=mock_result,
|
|
),
|
|
patch("steam_backlog_enforcer.game_install.subprocess.Popen") as mock_popen,
|
|
patch(
|
|
"steam_backlog_enforcer.game_install.os.geteuid",
|
|
return_value=1000,
|
|
),
|
|
patch("steam_backlog_enforcer.game_install.time.sleep"),
|
|
):
|
|
_ensure_steam_running()
|
|
mock_popen.assert_called_once()
|
|
|
|
def test_not_running_starts_as_root(self) -> None:
|
|
mock_result = MagicMock(returncode=1)
|
|
mock_pw = MagicMock()
|
|
mock_pw.pw_uid = 1000
|
|
mock_pw.pw_gid = 1000
|
|
with (
|
|
patch(
|
|
"steam_backlog_enforcer.game_install.subprocess.run",
|
|
return_value=mock_result,
|
|
),
|
|
patch("steam_backlog_enforcer.game_install.subprocess.Popen") as mock_popen,
|
|
patch(
|
|
"steam_backlog_enforcer.game_install.os.geteuid",
|
|
return_value=0,
|
|
),
|
|
patch(
|
|
"steam_backlog_enforcer.game_install._get_real_user",
|
|
return_value="alice",
|
|
),
|
|
patch(
|
|
"steam_backlog_enforcer.game_install._get_uid_gid_for_user",
|
|
return_value=(1000, 1000),
|
|
),
|
|
patch("steam_backlog_enforcer.game_install.time.sleep"),
|
|
):
|
|
_ensure_steam_running()
|
|
mock_popen.assert_called_once()
|
|
|
|
def test_pgrep_not_found(self) -> None:
|
|
with (
|
|
patch(
|
|
"steam_backlog_enforcer.game_install.subprocess.run",
|
|
side_effect=FileNotFoundError,
|
|
),
|
|
patch("steam_backlog_enforcer.game_install.subprocess.Popen"),
|
|
patch(
|
|
"steam_backlog_enforcer.game_install.os.geteuid",
|
|
return_value=1000,
|
|
),
|
|
patch("steam_backlog_enforcer.game_install.time.sleep"),
|
|
):
|
|
_ensure_steam_running()
|
|
|
|
def test_steam_executable_not_found(self) -> None:
|
|
mock_result = MagicMock(returncode=1)
|
|
with (
|
|
patch(
|
|
"steam_backlog_enforcer.game_install.subprocess.run",
|
|
return_value=mock_result,
|
|
),
|
|
patch(
|
|
"steam_backlog_enforcer.game_install.subprocess.Popen",
|
|
side_effect=FileNotFoundError,
|
|
),
|
|
patch(
|
|
"steam_backlog_enforcer.game_install.os.geteuid",
|
|
return_value=1000,
|
|
),
|
|
):
|
|
_ensure_steam_running()
|