From 62c801b4e933b5ad50f8795f66da53795d77e593 Mon Sep 17 00:00:00 2001 From: Cagri Yonca Date: Wed, 6 Nov 2024 15:18:48 +0100 Subject: [PATCH] fix(pubsub): flaky tests Signed-off-by: Cagri Yonca --- tests/agent/test_host.py | 8 +++++++- tests/conftest.py | 11 +++++++++++ 2 files changed, 18 insertions(+), 1 deletion(-) diff --git a/tests/agent/test_host.py b/tests/agent/test_host.py index cff710620..567ec706a 100644 --- a/tests/agent/test_host.py +++ b/tests/agent/test_host.py @@ -64,7 +64,7 @@ def test_has_options(self) -> None: assert isinstance(self.agent.options, StandardOptions) def test_agent_default_log_level(self) -> None: - assert self.agent.options.log_level == logging.WARNING + assert self.agent.options.log_level == logging.WARN def test_agent_instana_debug(self) -> None: os.environ["INSTANA_DEBUG"] = "asdf" @@ -76,6 +76,7 @@ def test_agent_instana_service_name(self) -> None: self.agent.options = StandardOptions() assert self.agent.options.service_name == "greycake" + @pytest.mark.original @patch.object(requests.Session, "put") def test_announce_is_successful( self, @@ -104,6 +105,7 @@ def test_announce_is_successful( assert "agentUuid" in payload assert test_agent_uuid == payload["agentUuid"] + @pytest.mark.original @patch.object(requests.Session, "put") def test_announce_fails_with_non_200( self, @@ -129,6 +131,7 @@ def test_announce_fails_with_non_200( assert "response status code" in caplog.messages[0] assert "is NOT 200" in caplog.messages[0] + @pytest.mark.original @patch.object(requests.Session, "put") def test_announce_fails_with_non_json( self, @@ -153,6 +156,7 @@ def test_announce_fails_with_non_json( assert len(caplog.records) == 1 assert "response is not JSON" in caplog.messages[0] + @pytest.mark.original @patch.object(requests.Session, "put") def test_announce_fails_with_empty_list_json( self, @@ -177,6 +181,7 @@ def test_announce_fails_with_empty_list_json( assert len(caplog.records) == 1 assert "payload has no fields" in caplog.messages[0] + @pytest.mark.original @patch.object(requests.Session, "put") def test_announce_fails_with_missing_pid( self, @@ -202,6 +207,7 @@ def test_announce_fails_with_missing_pid( assert len(caplog.records) == 1 assert "response payload has no pid" in caplog.messages[0] + @pytest.mark.original @patch.object(requests.Session, "put") def test_announce_fails_with_missing_uuid( self, diff --git a/tests/conftest.py b/tests/conftest.py index 4c7d7c65c..775a66414 100644 --- a/tests/conftest.py +++ b/tests/conftest.py @@ -222,3 +222,14 @@ def announce_sensor(monkeypatch, request) -> None: monkeypatch.setattr(TheMachine, "announce_sensor", TheMachine.announce_sensor) else: monkeypatch.setattr(TheMachine, "announce_sensor", always_true) + + +@pytest.fixture(autouse=True) +def announce(monkeypatch, request) -> None: + """Always return `True` for `Host.announce()`""" + if "original" in request.keywords: + # If using the `@pytest.mark.original` marker before the test function, + # uses the original HostAgent.announce() + monkeypatch.setattr(HostAgent, "announce", HostAgent.announce) + else: + monkeypatch.setattr(HostAgent, "announce", always_true)