Skip to content

Commit

Permalink
fix(pubsub): flaky tests
Browse files Browse the repository at this point in the history
Signed-off-by: Cagri Yonca <cagri@ibm.com>
  • Loading branch information
CagriYonca committed Dec 16, 2024
1 parent 1b1c653 commit b2684e2
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 1 deletion.
8 changes: 7 additions & 1 deletion tests/agent/test_host.py
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -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,
Expand Down Expand Up @@ -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,
Expand All @@ -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,
Expand All @@ -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,
Expand All @@ -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,
Expand All @@ -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,
Expand Down
11 changes: 11 additions & 0 deletions tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)

0 comments on commit b2684e2

Please sign in to comment.