| 1 |
#!/usr/bin/env python3 |
| 2 |
# SPDX-License-Identifier: GPL-3.0-or-later |
| 3 |
# Copyright (c) 2008-2026 Andrew Ziem. |
| 4 |
# |
| 5 |
# This work is licensed under the terms of the GNU GPL, version 3 or |
| 6 |
# later. See the COPYING file in the top-level directory. |
| 7 |
|
| 8 |
""" |
| 9 |
Utility script to discover and run tests marked for sudo execution. |
| 10 |
Tests are marked with the @also_with_sudo decorator. |
| 11 |
|
| 12 |
Usage: |
| 13 |
make tests-with-sudo |
| 14 |
""" |
| 15 |
|
| 16 |
import inspect |
| 17 |
import os |
| 18 |
import shlex |
| 19 |
import shutil |
| 20 |
import subprocess |
| 21 |
import sys |
| 22 |
import unittest |
| 23 |
from importlib import import_module |
| 24 |
|
| 25 |
# Add parent directory to path for imports |
| 26 |
sys.path.insert(0, os.path.dirname(os.path.dirname(os.path.abspath(__file__)))) |
| 27 |
|
| 28 |
# Import common test utilities |
| 29 |
from tests import common |
| 30 |
from bleachbit import IS_POSIX |
| 31 |
|
| 32 |
|
| 33 |
def discover_sudo_tests(): |
| 34 |
"""Discover all test methods marked with @also_with_sudo decorator""" |
| 35 |
sudo_tests = [] |
| 36 |
|
| 37 |
# Import test modules to scan for decorated tests |
| 38 |
test_modules = [ |
| 39 |
'tests.TestCLI', |
| 40 |
'tests.TestGeneral', |
| 41 |
'tests.TestUnix', |
| 42 |
] |
| 43 |
|
| 44 |
for module_name in test_modules: |
| 45 |
try: |
| 46 |
module = import_module(module_name) |
| 47 |
|
| 48 |
# Find all test classes in the module |
| 49 |
for name, obj in inspect.getmembers(module): |
| 50 |
if inspect.isclass(obj) and issubclass(obj, unittest.TestCase): |
| 51 |
# Find all test methods in the class |
| 52 |
for method_name, method in inspect.getmembers(obj): |
| 53 |
if (method_name.startswith('test_') and |
| 54 |
hasattr(method, '_also_with_sudo')): |
| 55 |
test_path = f"{module_name}.{name}.{method_name}" |
| 56 |
sudo_tests.append(test_path) |
| 57 |
|
| 58 |
except ImportError as e: |
| 59 |
print(f"Warning: Could not import {module_name}: {e}") |
| 60 |
|
| 61 |
return sudo_tests |
| 62 |
|
| 63 |
|
| 64 |
def _build_test_command(test_list): |
| 65 |
"""Return command list to run unittest (optionally under coverage).""" |
| 66 |
coverage_runner = os.environ.get('BLEACHBIT_COVERAGE_RUNNER') or '' |
| 67 |
coverage_file = os.environ.get('BLEACHBIT_COVERAGE_FILE') or '' |
| 68 |
|
| 69 |
if coverage_runner.strip(): |
| 70 |
runner_parts = shlex.split(coverage_runner) |
| 71 |
if runner_parts and not os.path.isabs(runner_parts[0]): |
| 72 |
absolute = shutil.which(runner_parts[0]) |
| 73 |
if absolute: |
| 74 |
runner_parts[0] = absolute |
| 75 |
else: |
| 76 |
runner_parts = [sys.executable] |
| 77 |
|
| 78 |
return runner_parts + ['-m', 'unittest'] + test_list + ['-v'], coverage_file |
| 79 |
|
| 80 |
|
| 81 |
def run_sudo_tests(test_list): |
| 82 |
"""Run a list of tests with sudo privileges""" |
| 83 |
if not test_list: |
| 84 |
print("No tests marked with @requires_sudo found") |
| 85 |
return True |
| 86 |
|
| 87 |
print(f"Found {len(test_list)} tests marked for sudo execution:") |
| 88 |
for test in test_list: |
| 89 |
print(f" - {test}") |
| 90 |
|
| 91 |
print(f"\n{'=' * 60}") |
| 92 |
print("Running sudo tests...") |
| 93 |
print('=' * 60) |
| 94 |
|
| 95 |
base_cmd, coverage_file = _build_test_command(test_list) |
| 96 |
|
| 97 |
if IS_POSIX and os.geteuid() == 0: |
| 98 |
sudo_uid = os.getenv('SUDO_UID') |
| 99 |
if sudo_uid and sudo_uid != '0': |
| 100 |
print("\nDetected EUID=0 with SUDO_UID preserved; skipping nested sudo.") |
| 101 |
cmd = base_cmd |
| 102 |
else: |
| 103 |
print("\nERROR: Do not run tests/test_with_sudo.py with sudo or as root.") |
| 104 |
print("Run it unprivileged; it will invoke sudo internally.") |
| 105 |
print("Try: make tests-with-sudo or python3 tests/test_with_sudo.py") |
| 106 |
return False |
| 107 |
else: |
| 108 |
cmd = ['sudo', '-E'] + base_cmd |
| 109 |
|
| 110 |
try: |
| 111 |
with common.set_temporary_env('COVERAGE_FILE', coverage_file or None): |
| 112 |
result = subprocess.run(cmd, check=False) |
| 113 |
return result.returncode == 0 |
| 114 |
except Exception as e: |
| 115 |
print(f"Error running sudo tests: {e}") |
| 116 |
return False |
| 117 |
|
| 118 |
|
| 119 |
def main(): |
| 120 |
"""Discover and run all tests marked with @also_with_sudo""" |
| 121 |
sudo_tests = discover_sudo_tests() |
| 122 |
|
| 123 |
if not sudo_tests: |
| 124 |
print("No tests marked with @also_with_sudo found") |
| 125 |
sys.exit(0) |
| 126 |
|
| 127 |
if run_sudo_tests(sudo_tests): |
| 128 |
print("\nAll tests with sudo passed") |
| 129 |
sys.exit(0) |
| 130 |
else: |
| 131 |
print("\nSome tests with sudo failed") |
| 132 |
sys.exit(1) |
| 133 |
|
| 134 |
|
| 135 |
if __name__ == '__main__': |
| 136 |
main() |