diff --git a/tests/loaders/test_cb.py b/tests/loaders/test_cb.py index 7faa46852..e0abddeb4 100644 --- a/tests/loaders/test_cb.py +++ b/tests/loaders/test_cb.py @@ -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) diff --git a/tests/plugins/os/windows/test_registry.py b/tests/plugins/os/windows/test_registry.py index e200e17e6..f55b931cc 100644 --- a/tests/plugins/os/windows/test_registry.py +++ b/tests/plugins/os/windows/test_registry.py @@ -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 = [ @@ -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"] == [ @@ -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"] == [ @@ -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() @@ -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"] == [ diff --git a/tests/test_plugin.py b/tests/test_plugin.py index 7e04e338f..e109d9ddf 100644 --- a/tests/test_plugin.py +++ b/tests/test_plugin.py @@ -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 @@ -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""" @@ -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: diff --git a/tests/test_target.py b/tests/test_target.py index 2500bf594..1f74bf713 100644 --- a/tests/test_target.py +++ b/tests/test_target.py @@ -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 @@ -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) @@ -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)