From b2684e208a4957c4ca2bd0889da967608ed4543c Mon Sep 17 00:00:00 2001 From: Cagri Yonca Date: Wed, 6 Nov 2024 15:18:48 +0100 Subject: [PATCH 1/2] 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) From ae289fcf38267c7cd45f7e6169000daebb20c511 Mon Sep 17 00:00:00 2001 From: Cagri Yonca Date: Mon, 16 Dec 2024 09:55:55 +0100 Subject: [PATCH 2/2] enhancement: removed duplicated code Signed-off-by: Cagri Yonca --- tests/agent/test_host.py | 2 +- tests/platforms/__init__.py | 0 tests/platforms/test_host.py | 233 ----------------------------------- 3 files changed, 1 insertion(+), 234 deletions(-) delete mode 100644 tests/platforms/__init__.py delete mode 100644 tests/platforms/test_host.py diff --git a/tests/agent/test_host.py b/tests/agent/test_host.py index 567ec706a..845eae784 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.WARN + assert self.agent.options.log_level == logging.WARNING def test_agent_instana_debug(self) -> None: os.environ["INSTANA_DEBUG"] = "asdf" diff --git a/tests/platforms/__init__.py b/tests/platforms/__init__.py deleted file mode 100644 index e69de29bb..000000000 diff --git a/tests/platforms/test_host.py b/tests/platforms/test_host.py deleted file mode 100644 index 75cea793f..000000000 --- a/tests/platforms/test_host.py +++ /dev/null @@ -1,233 +0,0 @@ -# (c) Copyright IBM Corp. 2021 -# (c) Copyright Instana Inc. 2020 - -import logging -import os -from typing import Generator - -import pytest -import requests -from mock import MagicMock, patch - -from instana.fsm import Discovery -from instana.options import StandardOptions -from instana.singletons import get_agent - - -class TestHost: - @pytest.fixture(autouse=True) - def _resource(self) -> Generator[None, None, None]: - self.agent = get_agent() - self.span_processor = None - self.agent.options = StandardOptions() - pass - variable_names = ( - "AWS_EXECUTION_ENV", - "INSTANA_EXTRA_HTTP_HEADERS", - "INSTANA_ENDPOINT_URL", - "INSTANA_ENDPOINT_PROXY", - "INSTANA_AGENT_KEY", - "INSTANA_LOG_LEVEL", - "INSTANA_SERVICE_NAME", - "INSTANA_SECRETS", - "INSTANA_TAGS", - ) - - for variable_name in variable_names: - if variable_name in os.environ: - os.environ.pop(variable_name) - - def test_secrets(self): - assert hasattr(self.agent.options, "secrets_matcher") - assert self.agent.options.secrets_matcher == "contains-ignore-case" - assert hasattr(self.agent.options, "secrets_list") - assert self.agent.options.secrets_list == ["key", "pass", "secret"] - - def test_options_have_extra_http_headers(self): - assert hasattr(self.agent, "options") - assert hasattr(self.agent.options, "extra_http_headers") - - def test_has_options(self): - assert hasattr(self.agent, "options") - assert isinstance(self.agent.options, StandardOptions) - - def test_agent_default_log_level(self): - assert self.agent.options.log_level == logging.DEBUG - - def test_agent_instana_debug(self): - os.environ["INSTANA_DEBUG"] = "asdf" - self.agent.options = StandardOptions() - assert self.agent.options.log_level == logging.DEBUG - - def test_agent_instana_service_name(self): - os.environ["INSTANA_SERVICE_NAME"] = "greycake" - self.agent.options = StandardOptions() - assert self.agent.options.service_name == "greycake" - - @patch.object(requests.Session, "put") - def test_announce_is_successful(self, mock_requests_session_put): - test_pid = 4242 - test_process_name = "test_process" - test_process_args = ["-v", "-d"] - test_agent_uuid = "83bf1e09-ab16-4203-abf5-34ee0977023a" - - mock_response = MagicMock() - mock_response.status_code = 200 - mock_response.content = ( - "{" f' "pid": {test_pid}, ' f' "agentUuid": "{test_agent_uuid}"' "}" - ) - - # This mocks the call to self.agent.client.put - mock_requests_session_put.return_value = mock_response - d = Discovery(pid=test_pid, name=test_process_name, args=test_process_args) - payload = self.agent.announce(d) - - assert "pid" in payload - assert test_pid == payload["pid"] - - assert "agentUuid" in payload - assert test_agent_uuid == payload["agentUuid"] - - @patch.object(requests.Session, "put") - def test_announce_fails_with_non_200(self, mock_requests_session_put, caplog): - caplog.set_level(logging.DEBUG, logger="instana") - test_pid = 4242 - test_process_name = "test_process" - test_process_args = ["-v", "-d"] - test_agent_uuid = "83bf1e09-ab16-4203-abf5-34ee0977023a" - - mock_response = MagicMock() - mock_response.status_code = 404 - mock_response.content = "" - mock_requests_session_put.return_value = mock_response - - d = Discovery(pid=test_pid, name=test_process_name, args=test_process_args) - - payload = self.agent.announce(d) - assert not payload - assert len(caplog.messages) == 1 - assert len(caplog.records) == 1 - assert "response status code" in caplog.messages[0] - assert "is NOT 200" in caplog.messages[0] - - @patch.object(requests.Session, "put") - def test_announce_fails_with_non_json(self, mock_requests_session_put, caplog): - caplog.set_level(logging.DEBUG, logger="instana") - test_pid = 4242 - test_process_name = "test_process" - test_process_args = ["-v", "-d"] - test_agent_uuid = "83bf1e09-ab16-4203-abf5-34ee0977023a" - - mock_response = MagicMock() - mock_response.status_code = 200 - mock_response.content = "" - mock_requests_session_put.return_value = mock_response - - d = Discovery(pid=test_pid, name=test_process_name, args=test_process_args) - payload = self.agent.announce(d) - assert not payload - assert len(caplog.messages) == 1 - assert len(caplog.records) == 1 - assert "response is not JSON" in caplog.messages[0] - - @patch.object(requests.Session, "put") - def test_announce_fails_with_empty_list_json( - self, mock_requests_session_put, caplog - ): - caplog.set_level(logging.DEBUG, logger="instana") - test_pid = 4242 - test_process_name = "test_process" - test_process_args = ["-v", "-d"] - test_agent_uuid = "83bf1e09-ab16-4203-abf5-34ee0977023a" - - mock_response = MagicMock() - mock_response.status_code = 200 - mock_response.content = "[]" - mock_requests_session_put.return_value = mock_response - - d = Discovery(pid=test_pid, name=test_process_name, args=test_process_args) - payload = self.agent.announce(d) - assert not payload - assert len(caplog.messages) == 1 - assert len(caplog.records) == 1 - assert "payload has no fields" in caplog.messages[0] - - @patch.object(requests.Session, "put") - def test_announce_fails_with_missing_pid(self, mock_requests_session_put, caplog): - caplog.set_level(logging.DEBUG, logger="instana") - test_pid = 4242 - test_process_name = "test_process" - test_process_args = ["-v", "-d"] - test_agent_uuid = "83bf1e09-ab16-4203-abf5-34ee0977023a" - - mock_response = MagicMock() - mock_response.status_code = 200 - mock_response.content = "{" f' "agentUuid": "{test_agent_uuid}"' "}" - mock_requests_session_put.return_value = mock_response - - d = Discovery(pid=test_pid, name=test_process_name, args=test_process_args) - payload = self.agent.announce(d) - assert not payload - assert len(caplog.messages) == 1 - assert len(caplog.records) == 1 - assert "response payload has no pid" in caplog.messages[0] - - @patch.object(requests.Session, "put") - def test_announce_fails_with_missing_uuid(self, mock_requests_session_put, caplog): - caplog.set_level(logging.DEBUG, logger="instana") - test_pid = 4242 - test_process_name = "test_process" - test_process_args = ["-v", "-d"] - test_agent_uuid = "83bf1e09-ab16-4203-abf5-34ee0977023a" - - mock_response = MagicMock() - mock_response.status_code = 200 - mock_response.content = "{" f' "pid": {test_pid} ' "}" - mock_requests_session_put.return_value = mock_response - - d = Discovery(pid=test_pid, name=test_process_name, args=test_process_args) - payload = self.agent.announce(d) - assert not payload - assert len(caplog.messages) == 1 - assert len(caplog.records) == 1 - assert "response payload has no agentUuid" in caplog.messages[0] - - @pytest.mark.original - @patch.object(requests.Session, "get") - def test_agent_connection_attempt(self, mock_requests_session_get, caplog): - caplog.set_level(logging.DEBUG, logger="instana") - mock_response = MagicMock() - mock_response.status_code = 200 - mock_requests_session_get.return_value = mock_response - - host = self.agent.options.agent_host - port = self.agent.options.agent_port - msg = f"Instana host agent found on {host}:{port}" - - result = self.agent.is_agent_listening(host, port) - - assert result - assert msg in caplog.messages[0] - - @pytest.mark.original - @patch.object(requests.Session, "get") - def test_agent_connection_attempt_fails_with_404( - self, mock_requests_session_get, caplog - ): - caplog.set_level(logging.DEBUG, logger="instana") - mock_response = MagicMock() - mock_response.status_code = 404 - mock_requests_session_get.return_value = mock_response - - host = self.agent.options.agent_host - port = self.agent.options.agent_port - msg = ( - "The attempt to connect to the Instana host agent on " - f"{host}:{port} has failed with an unexpected status code. " - f"Expected HTTP 200 but received: {mock_response.status_code}" - ) - - result = self.agent.is_agent_listening(host, port) - - assert not result - assert msg in caplog.messages[0]