Skip to content

Commit

Permalink
Update tests to pass or skip on macos
Browse files Browse the repository at this point in the history
  • Loading branch information
twizmwazin committed Jan 17, 2024
1 parent 9788f3c commit dd7ca42
Show file tree
Hide file tree
Showing 9 changed files with 56 additions and 13 deletions.
4 changes: 3 additions & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ sdist-generator = "cargo"

[tool.pytest.ini_options]
addopts = "--tb=native --cov=binharness --cov-report lcov:.lcov --cov-report term-missing --mypy"
markers = ["linux", "darwin", "win32"]

[tool.coverage.run]
branch = true
Expand Down Expand Up @@ -66,7 +67,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]
Expand Down
14 changes: 14 additions & 0 deletions python/tests/conftest.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
from __future__ import annotations

import sys

import pytest

ALL = {"darwin", "linux", "win32"}


def pytest_runtest_setup(item: pytest.Item) -> None:
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}")
5 changes: 5 additions & 0 deletions python/tests/test_busybox.py
Original file line number Diff line number Diff line change
@@ -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()
Expand All @@ -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()
Expand All @@ -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()
Expand Down
7 changes: 5 additions & 2 deletions python/tests/test_executor.py
Original file line number Diff line number Diff line change
Expand Up @@ -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 (
Expand All @@ -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):
Expand Down
17 changes: 11 additions & 6 deletions python/tests/test_inject.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
from pathlib import Path

import pytest

from binharness.localenvironment import LocalEnvironment
from binharness.types.injection import (
ExecutableInjection,
Expand All @@ -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()
Expand All @@ -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()
5 changes: 5 additions & 0 deletions python/tests/test_localenvironment.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
import pathlib
import tempfile

import pytest

from binharness.common.busybox import BusyboxInjection
from binharness.localenvironment import LocalEnvironment

Expand Down Expand Up @@ -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()
Expand All @@ -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()
Expand All @@ -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()
Expand Down
4 changes: 4 additions & 0 deletions python/tests/test_qemu_executor.py
Original file line number Diff line number Diff line change
Expand Up @@ -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"))
Expand All @@ -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"))
Expand Down
4 changes: 4 additions & 0 deletions python/tests/test_target_local.py
Original file line number Diff line number Diff line change
Expand Up @@ -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"))
Expand All @@ -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"))
Expand Down
9 changes: 5 additions & 4 deletions python/tests/test_target_serialization.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -13,15 +14,15 @@

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")


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")
Expand All @@ -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")
Expand All @@ -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")
Expand Down

0 comments on commit dd7ca42

Please sign in to comment.