From 8253c2b6d3792394a40fc5774f3ef686a6811dbc Mon Sep 17 00:00:00 2001 From: Varsha GS Date: Fri, 4 Oct 2024 14:27:41 +0530 Subject: [PATCH 1/3] fix(ci): flaky tests - Always return `True` for `HostAgent.is_agent_listening()` Signed-off-by: Varsha GS --- tests/agent/test_host.py | 2 +- tests/conftest.py | 11 +++++++++++ tests/platforms/test_host.py | 5 +++-- 3 files changed, 15 insertions(+), 3 deletions(-) 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..1c76cc79 100644 --- a/tests/conftest.py +++ b/tests/conftest.py @@ -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() From e0fe546f4adf8d545cd0618ac2d28d654ff8f373 Mon Sep 17 00:00:00 2001 From: Varsha GS Date: Fri, 4 Oct 2024 14:50:07 +0530 Subject: [PATCH 2/3] fix(ci): fix warnings by registering the pytest marker - ``` /workspace/python-sensor/tests/agent/test_host.py:125: PytestUnknownMarkWarning: Unknown pytest.mark.original - is this a typo? You can register custom marks to avoid this warning - for details, see https://docs.pytest.org/en/stable/how-to/mark.html @pytest.mark.original ``` Signed-off-by: Varsha GS --- pytest.ini | 2 ++ 1 file changed, 2 insertions(+) diff --git a/pytest.ini b/pytest.ini index 30d6f4d7..ad0ec74b 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` \ No newline at end of file From a497c0a399e4e6c83b5b050c287a136d0a9f0c2f Mon Sep 17 00:00:00 2001 From: Varsha GS Date: Fri, 4 Oct 2024 15:07:06 +0530 Subject: [PATCH 3/3] fix(ci): fix warnings on `always_true()` Signed-off-by: Varsha GS --- pytest.ini | 2 +- tests/conftest.py | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/pytest.ini b/pytest.ini index ad0ec74b..e79dc7de 100644 --- a/pytest.ini +++ b/pytest.ini @@ -8,4 +8,4 @@ testpaths = tests tests_aws markers = - original: mark test to use the original method instead of the mocked ones under `conftest.py` \ No newline at end of file + original: mark test to use the original method instead of the mocked ones under `conftest.py` diff --git a/tests/conftest.py b/tests/conftest.py index 1c76cc79..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