Skip to content

Commit

Permalink
Fix some issues with tests not being independent from other tests (fo…
Browse files Browse the repository at this point in the history
…x-it#526)

These are due to the tests (inadvertently) changing global state.

(DIS-2971)
  • Loading branch information
pyrco authored and Zawadidone committed Apr 5, 2024
1 parent 775ab0b commit 734e4d4
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 16 deletions.
13 changes: 8 additions & 5 deletions tests/loaders/test_cb.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,15 @@

@pytest.fixture
def mock_cbc_sdk(monkeypatch: pytest.MonkeyPatch) -> Iterator[MagicMock]:
try:
del sys.modules["dissect.target.loader"]
except KeyError:
pass

with monkeypatch.context() as m:
# The references to cbc_sdk properties in the cb loader will point to the MagicMock created
# for the first test function that runs.
# Thus we need to delete the cb loader module to force it to be reimported when used in a
# new test so the MagickMock used in the cb module will be the one created for the test
# function that is running.
if "dissect.target.loaders.cb" in sys.modules:
m.delitem(sys.modules, "dissect.target.loaders.cb")

mock_cbc_sdk = MagicMock()
m.setitem(sys.modules, "cbc_sdk", mock_cbc_sdk)
m.setitem(sys.modules, "cbc_sdk.errors", mock_cbc_sdk.errors)
Expand Down
10 changes: 5 additions & 5 deletions tests/plugins/os/windows/test_registry.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ def test_missing_hives(fs_win: VirtualFilesystem, caplog: LogCaptureFixture) ->
target = Target()
target.filesystems.add(fs_win)

caplog.set_level(logging.DEBUG)
caplog.set_level(logging.DEBUG, target.log.name)
target.apply()

expected = [
Expand All @@ -31,7 +31,7 @@ def test_missing_hives(fs_win: VirtualFilesystem, caplog: LogCaptureFixture) ->
def test_missing_user_hives(fs_win: VirtualFilesystem, target_win_users: Target, caplog: LogCaptureFixture) -> None:
fs_win.makedirs("Users/John")

caplog.set_level(logging.DEBUG)
caplog.set_level(logging.DEBUG, target_win_users.log.name)
target_win_users.registry.load_user_hives()

assert [record.message for record in caplog.records if record.filename == "registry.py"] == [
Expand All @@ -47,7 +47,7 @@ def test_empty_hives(fs_win: VirtualFilesystem, caplog: LogCaptureFixture) -> No
target = Target()
target.filesystems.add(fs_win)

caplog.set_level(logging.WARNING)
caplog.set_level(logging.WARNING, target.log.name)
target.apply()

assert [record.message for record in caplog.records if record.filename == "registry.py"] == [
Expand All @@ -65,7 +65,7 @@ def test_empty_hives_skip_warning(fs_win: VirtualFilesystem, caplog: LogCaptureF
target = Target()
target.filesystems.add(fs_win)

caplog.set_level(logging.WARNING)
caplog.set_level(logging.WARNING, target.log.name)

with patch("dissect.target.plugins.os.windows.registry.RegfHive"):
target.apply()
Expand All @@ -77,7 +77,7 @@ def test_empty_user_hives(fs_win: VirtualFilesystem, target_win_users: Target, c
fs_win.map_file_fh("Users/John/ntuser.dat", BytesIO())
fs_win.map_file_fh("Users/John/AppData/Local/Microsoft/Windows/usrclass.dat", BytesIO())

caplog.set_level(logging.WARNING)
caplog.set_level(logging.WARNING, target_win_users.log.name)
target_win_users.registry.load_user_hives()

assert [record.message for record in caplog.records if record.filename == "registry.py"] == [
Expand Down
8 changes: 4 additions & 4 deletions tests/test_plugin.py
Original file line number Diff line number Diff line change
Expand Up @@ -444,7 +444,7 @@ def test_os_plugin_property_methods(target_bare: Target, method_name: str) -> No
getattr(os_plugin, method_name)


class TestOS1(OSPlugin):
class MockOS1(OSPlugin):
@export(property=True)
def hostname(self) -> Optional[str]:
pass
Expand All @@ -470,7 +470,7 @@ def architecture(self) -> Optional[str]:
pass


class TestOS2(OSPlugin):
class MockOS2(OSPlugin):
@export(property=True)
def hostname(self) -> Optional[str]:
"""Test docstring hostname"""
Expand Down Expand Up @@ -505,8 +505,8 @@ def architecture(self) -> Optional[str]:
@pytest.mark.parametrize(
"subclass, replaced",
[
(TestOS1, True),
(TestOS2, False),
(MockOS1, True),
(MockOS2, False),
],
)
def test_os_plugin___init_subclass__(subclass: type[OSPlugin], replaced: bool) -> None:
Expand Down
5 changes: 3 additions & 2 deletions tests/test_target.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import io
import urllib.parse
from collections import defaultdict
from pathlib import Path
from typing import Iterator
from unittest.mock import MagicMock, Mock, PropertyMock, call, patch
Expand Down Expand Up @@ -377,7 +378,7 @@ def test_target_set_event_callback() -> None:
mock_callback2 = MagicMock()

class MockTarget(Target):
pass
event_callbacks = defaultdict(set)

MockTarget.set_event_callback(event_type=None, event_callback=mock_callback1)
MockTarget.set_event_callback(event_type=None, event_callback=mock_callback2)
Expand Down Expand Up @@ -405,7 +406,7 @@ def test_target_send_event() -> None:
mock_callback2 = MagicMock()

class MockTarget(Target):
pass
event_callbacks = defaultdict(set)

MockTarget.set_event_callback(event_type=None, event_callback=mock_callback1)
MockTarget.set_event_callback(event_type=Event.FUNC_EXEC, event_callback=mock_callback2)
Expand Down

0 comments on commit 734e4d4

Please sign in to comment.