From db5c930d8f45c8ee80771cd05e7d3e582f1a8f30 Mon Sep 17 00:00:00 2001 From: Kevin Phoenix Date: Wed, 17 Jan 2024 12:57:23 -0700 Subject: [PATCH] Update tests to pass or skip on macos --- pyproject.toml | 4 +++- python/tests/conftest.py | 14 ++++++++++++++ python/tests/test_busybox.py | 5 +++++ python/tests/test_executor.py | 7 +++++-- python/tests/test_inject.py | 17 +++++++++++------ python/tests/test_localenvironment.py | 5 +++++ python/tests/test_qemu_executor.py | 4 ++++ python/tests/test_target_local.py | 4 ++++ python/tests/test_target_serialization.py | 9 +++++---- 9 files changed, 56 insertions(+), 13 deletions(-) create mode 100644 python/tests/conftest.py diff --git a/pyproject.toml b/pyproject.toml index dff3362..ec4951e 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -38,6 +38,7 @@ sdist-generator = "cargo" [tool.pytest.ini_options] addopts = "--tb=native --cov=binharness --cov-report lcov:.lcov --cov-report term-missing" +markers = ["linux", "darwin", "win32"] [tool.coverage.run] branch = true @@ -65,7 +66,8 @@ ignore = [ ignore-init-module-imports = true line-length = 88 -[tool.ruff.isort] +[tool.ruff.lint.isort] +known-first-party = ["binharness"] required-imports = ["from __future__ import annotations"] [tool.ruff.per-file-ignores] diff --git a/python/tests/conftest.py b/python/tests/conftest.py new file mode 100644 index 0000000..26edd93 --- /dev/null +++ b/python/tests/conftest.py @@ -0,0 +1,14 @@ +from __future__ import annotations + +import sys + +import pytest + +ALL = {"darwin", "linux", "win32"} + + +def pytest_runtest_setup(item) -> None: # noqa: ANN001 + supported_platforms = ALL.intersection(mark.name for mark in item.iter_markers()) + plat = sys.platform + if supported_platforms and plat not in supported_platforms: + pytest.skip(f"cannot run on platform {plat}") diff --git a/python/tests/test_busybox.py b/python/tests/test_busybox.py index a54ad99..d0e55e9 100644 --- a/python/tests/test_busybox.py +++ b/python/tests/test_busybox.py @@ -1,9 +1,12 @@ from __future__ import annotations +import pytest + from binharness.common.busybox import BusyboxInjection from binharness.localenvironment import LocalEnvironment +@pytest.mark.linux() def test_busybox_injection() -> None: env = LocalEnvironment() busybox_injection = BusyboxInjection() @@ -22,6 +25,7 @@ def test_busybox_injection() -> None: assert stdout == b"one\n" +@pytest.mark.linux() def test_busbox_injection_mktemp() -> None: env = LocalEnvironment() busybox_injection = BusyboxInjection() @@ -30,6 +34,7 @@ def test_busbox_injection_mktemp() -> None: assert busybox_injection.mktemp(directory=True).is_dir() +@pytest.mark.linux() def test_nc_interaction() -> None: env = LocalEnvironment() busybox = BusyboxInjection() diff --git a/python/tests/test_executor.py b/python/tests/test_executor.py index 88021f8..96c97e7 100644 --- a/python/tests/test_executor.py +++ b/python/tests/test_executor.py @@ -3,6 +3,7 @@ from pathlib import Path import pytest + from binharness.common.busybox import BusyboxShellExecutor from binharness.localenvironment import LocalEnvironment from binharness.types.executor import ( @@ -12,18 +13,20 @@ from binharness.types.target import Target +@pytest.mark.linux() def test_busybox_injection_without_install() -> None: env = LocalEnvironment() - target = Target(env, Path("/bin/true")) + target = Target(env, Path("/usr/bin/true")) busybox_shell = BusyboxShellExecutor() with pytest.raises(InjectionNotInstalledError): assert busybox_shell.run_target(target) +@pytest.mark.linux() def test_busybox_injection_different_environment() -> None: env1 = LocalEnvironment() env2 = LocalEnvironment() - target = Target(env1, Path("/bin/true")) + target = Target(env1, Path("/usr/bin/true")) busybox_shell = BusyboxShellExecutor() busybox_shell.install(env2) with pytest.raises(ExecutorEnvironmentMismatchError): diff --git a/python/tests/test_inject.py b/python/tests/test_inject.py index 84db91b..a0e4e6c 100644 --- a/python/tests/test_inject.py +++ b/python/tests/test_inject.py @@ -3,6 +3,7 @@ from pathlib import Path import pytest + from binharness.localenvironment import LocalEnvironment from binharness.types.injection import ( ExecutableInjection, @@ -12,17 +13,19 @@ ) +@pytest.mark.linux() def test_inject_true() -> None: env = LocalEnvironment() - true_injection = Injection(Path("/bin/true"), None) + true_injection = Injection(Path("/usr/bin/true"), None) true_injection.install(env) assert true_injection.env_path is not None assert (true_injection.env_path / "true").is_file() +@pytest.mark.linux() def test_inject_true_executable() -> None: env = LocalEnvironment() - true_injection = ExecutableInjection(Path("true"), Path("/bin/true")) + true_injection = ExecutableInjection(Path("true"), Path("/usr/bin/true")) true_injection.install(env) assert true_injection.env_path is not None assert true_injection.env_path.is_dir() @@ -32,23 +35,25 @@ def test_inject_true_executable() -> None: assert true_injection.run().wait() == 0 +@pytest.mark.linux() def test_inject_true_executable_twice() -> None: env = LocalEnvironment() - true_injection = ExecutableInjection(Path("true"), Path("/bin/true")) + true_injection = ExecutableInjection(Path("true"), Path("/usr/bin/true")) true_injection.install(env) with pytest.raises(InjectionAlreadyInstalledError): true_injection.install(env) +@pytest.mark.linux() def test_inject_two_true_executables() -> None: env = LocalEnvironment() - true_injection_1 = ExecutableInjection(Path("true"), Path("/bin/true")) + true_injection_1 = ExecutableInjection(Path("true"), Path("/usr/bin/true")) true_injection_1.install(env) - true_injection_2 = ExecutableInjection(Path("true"), Path("/bin/true")) + true_injection_2 = ExecutableInjection(Path("true"), Path("/usr/bin/true")) true_injection_2.install(env) def test_executable_injection_no_install() -> None: - true_injection = ExecutableInjection(Path("true"), Path("/bin/true")) + true_injection = ExecutableInjection(Path("true"), Path("/usr/bin/true")) with pytest.raises(InjectionNotInstalledError): true_injection.run() diff --git a/python/tests/test_localenvironment.py b/python/tests/test_localenvironment.py index 821f228..00d7c70 100644 --- a/python/tests/test_localenvironment.py +++ b/python/tests/test_localenvironment.py @@ -3,6 +3,8 @@ import pathlib import tempfile +import pytest + from binharness.common.busybox import BusyboxInjection from binharness.localenvironment import LocalEnvironment @@ -35,6 +37,7 @@ def test_get_tempdir() -> None: assert env.get_tempdir() == pathlib.Path(tempfile.gettempdir()) +@pytest.mark.linux() def test_stdout() -> None: env = LocalEnvironment() busybox = BusyboxInjection() @@ -43,6 +46,7 @@ def test_stdout() -> None: assert proc.stdout.read() == b"hello\n" +@pytest.mark.linux() def test_stderr() -> None: env = LocalEnvironment() busybox = BusyboxInjection() @@ -51,6 +55,7 @@ def test_stderr() -> None: assert proc.stderr.read() == b"hello\n" +@pytest.mark.linux() def test_process_poll() -> None: env = LocalEnvironment() busybox = BusyboxInjection() diff --git a/python/tests/test_qemu_executor.py b/python/tests/test_qemu_executor.py index dcceecc..323883a 100644 --- a/python/tests/test_qemu_executor.py +++ b/python/tests/test_qemu_executor.py @@ -2,10 +2,13 @@ from pathlib import Path +import pytest + from binharness import LocalEnvironment, Target from binharness.common.qemu import QemuExecutor +@pytest.mark.linux() def test_run_true() -> None: env = LocalEnvironment() target = Target(env, Path("/bin/true")) @@ -14,6 +17,7 @@ def test_run_true() -> None: assert qemu.run_target(target).wait() == 0 +@pytest.mark.linux() def test_run_strace() -> None: env = LocalEnvironment() target = Target(env, Path("/bin/true")) diff --git a/python/tests/test_target_local.py b/python/tests/test_target_local.py index cc8c9b0..e550ade 100644 --- a/python/tests/test_target_local.py +++ b/python/tests/test_target_local.py @@ -2,12 +2,15 @@ from pathlib import Path +import pytest + from binharness import LocalEnvironment from binharness.common.busybox import BusyboxShellExecutor from binharness.types.executor import NullExecutor from binharness.types.target import Target +@pytest.mark.linux() def test_run_target() -> None: env = LocalEnvironment() target = Target(env, Path("true")) @@ -16,6 +19,7 @@ def test_run_target() -> None: assert proc.wait() == 0 +@pytest.mark.linux() def test_run_target_busybox() -> None: env = LocalEnvironment() target = Target(env, Path("true")) diff --git a/python/tests/test_target_serialization.py b/python/tests/test_target_serialization.py index addb1ce..4101b84 100644 --- a/python/tests/test_target_serialization.py +++ b/python/tests/test_target_serialization.py @@ -5,6 +5,7 @@ from pathlib import Path import pytest + from binharness.localenvironment import LocalEnvironment from binharness.serialize import TargetImportError, export_target, import_target from binharness.types.executor import NullExecutor @@ -13,7 +14,7 @@ def test_local_target_export() -> None: env = LocalEnvironment() - target = Target(env, Path("/bin/true")) + target = Target(env, Path("/usr/bin/true")) with tempfile.TemporaryDirectory() as raw_tmpdir: tmpdir = Path(raw_tmpdir) export_target(target, tmpdir / "test_export.tar.gz") @@ -21,7 +22,7 @@ def test_local_target_export() -> None: def test_local_target_import() -> None: env = LocalEnvironment() - target = Target(env, Path("/bin/true")) + target = Target(env, Path("/usr/bin/true")) with tempfile.TemporaryDirectory() as raw_tmpdir: tmpdir = Path(raw_tmpdir) export_target(target, tmpdir / "test_export.tar.gz") @@ -33,7 +34,7 @@ def test_local_target_import() -> None: def test_local_target_import_without_metadata() -> None: env = LocalEnvironment() - target = Target(env, Path("/bin/true")) + target = Target(env, Path("/usr/bin/true")) with tempfile.TemporaryDirectory() as raw_tmpdir: tmpdir = Path(raw_tmpdir) export_target(target, tmpdir / "test_export.tar.gz") @@ -50,7 +51,7 @@ def test_local_target_import_without_metadata() -> None: def test_local_target_import_invalid_metadata_archive() -> None: env = LocalEnvironment() - target = Target(env, Path("/bin/true")) + target = Target(env, Path("/usr/bin/true")) with tempfile.TemporaryDirectory() as raw_tmpdir: tmpdir = Path(raw_tmpdir) export_target(target, tmpdir / "test_export.tar.gz")