diff --git a/pytest.ini b/pytest.ini index 30d6f4d7..e79dc7de 100644 --- a/pytest.ini +++ b/pytest.ini @@ -7,3 +7,5 @@ pythonpath = src testpaths = tests tests_aws +markers = + original: mark test to use the original method instead of the mocked ones under `conftest.py` diff --git a/tests/agent/test_host.py b/tests/agent/test_host.py index b345ce28..ec130aa2 100644 --- a/tests/agent/test_host.py +++ b/tests/agent/test_host.py @@ -122,7 +122,7 @@ def test_get_from_structure(): agent.announce_data = AnnounceData(pid=1234, agentUuid="value") assert agent.get_from_structure() == {"e": 1234, "h": "value"} - +@pytest.mark.original def test_is_agent_listening( caplog: LogCaptureFixture, ): diff --git a/tests/conftest.py b/tests/conftest.py index b2a81955..a6acf871 100644 --- a/tests/conftest.py +++ b/tests/conftest.py @@ -134,7 +134,7 @@ def context(span: InstanaSpan) -> Context: return set_span_in_context(span) -def always_true(_: object) -> bool: +def always_true(_: object, *args: object, **kwargs: object) -> bool: return True @@ -185,3 +185,14 @@ def prepare_and_report_data(monkeypatch, request): ) else: monkeypatch.setattr(BaseCollector, "prepare_and_report_data", always_true) + +# Mocking HostAgent.is_agent_listening() +@pytest.fixture(autouse=True) +def is_agent_listening(monkeypatch, request) -> None: + """Always return `True` for `HostAgent.is_agent_listening()`""" + if "original" in request.keywords: + # If using the `@pytest.mark.original` marker before the test function, + # uses the original HostAgent.is_agent_listening() + monkeypatch.setattr(HostAgent, "is_agent_listening", HostAgent.is_agent_listening) + else: + monkeypatch.setattr(HostAgent, "is_agent_listening", always_true) diff --git a/tests/platforms/test_host.py b/tests/platforms/test_host.py index 1eb07125..fcfc80a9 100644 --- a/tests/platforms/test_host.py +++ b/tests/platforms/test_host.py @@ -4,6 +4,7 @@ import os import logging import unittest +import pytest from mock import MagicMock, patch import requests @@ -230,7 +231,7 @@ def test_announce_fails_with_missing_uuid(self, mock_requests_session_put): self.assertEqual(len(log.records), 1) self.assertIn('response payload has no agentUuid', log.output[0]) - + @pytest.mark.original @patch.object(requests.Session, "get") def test_agent_connection_attempt(self, mock_requests_session_get): mock_response = MagicMock() @@ -248,7 +249,7 @@ def test_agent_connection_attempt(self, mock_requests_session_get): self.assertTrue(result) self.assertIn(msg, log.output[0]) - + @pytest.mark.original @patch.object(requests.Session, "get") def test_agent_connection_attempt_fails_with_404(self, mock_requests_session_get): mock_response = MagicMock()