-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #35 from Rohde-Schwarz/5-add-timestamps-to-fluentd…
…-messages 5 add timestamps to fluentd messages
- Loading branch information
Showing
5 changed files
with
204 additions
and
67 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -79,4 +79,4 @@ log_level = "INFO" | |
log_cli = false | ||
|
||
[tool.mypy] | ||
ignore_missing_imports = true | ||
ignore_missing_imports = true |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,53 +1,134 @@ | ||
import typing | ||
import uuid | ||
|
||
import pytest | ||
from fluent.sender import EventTime | ||
|
||
tag = "unittest" | ||
label = "pytest" | ||
FLUENTD_TAG = "unittest" | ||
FLUENTD_LABEL = "pytest" | ||
|
||
|
||
def test_fluentd_logged_parameters(run_mocked_pytest, session_uuid, logging_content): | ||
runpytest, sender = run_mocked_pytest | ||
FAKE_TEST_UUID = "6d653fee-0c6a-4923-9216-dfc949bd05a0" | ||
|
||
|
||
def get_data_from_call_args(call_args, fields: typing.List[str]) -> typing.Dict: | ||
return {field: call_args.args[2].get(field) for field in fields} | ||
|
||
|
||
@pytest.fixture(name="monkeypatched_uuid4") | ||
def monkeypatched_uuid4_fixture(monkeypatch): | ||
def myuuid4(): | ||
return uuid.UUID(FAKE_TEST_UUID) | ||
|
||
monkeypatch.setattr(uuid, "uuid4", myuuid4) | ||
|
||
|
||
def test_fluentd_logged_parameters( | ||
monkeypatched_uuid4, runpytest, fluentd_sender, session_uuid, logging_content | ||
): | ||
result = runpytest( | ||
f"--session-uuid={session_uuid}", | ||
f"--fluentd-tag={tag}", | ||
f"--fluentd-label={label}", | ||
f"--fluentd-tag={FLUENTD_TAG}", | ||
f"--fluentd-label={FLUENTD_LABEL}", | ||
"--extend-logging", | ||
) | ||
fluent_sender = sender.return_value | ||
call_args = fluent_sender.emit_with_time.call_args_list | ||
call_args = fluentd_sender.emit_with_time.call_args_list | ||
result.assert_outcomes(passed=1) | ||
assert len(call_args) > 0 | ||
for idx, call_arg in enumerate(call_args): | ||
args = call_arg.args | ||
if idx not in [2, 3]: | ||
assert args[0] == label | ||
assert isinstance(args[1], int) | ||
else: | ||
assert args[0] is None | ||
assert isinstance(args[1], EventTime) | ||
data = args[2] | ||
assert isinstance(data, dict) | ||
if idx in [0, 1]: | ||
assert data.get("status") == "start" | ||
if idx in [5, 6]: | ||
assert data.get("status") == "finish" | ||
assert data.get("sessionId") == str(session_uuid) | ||
if idx in [2, 3]: | ||
assert data.get("type") == "logging" | ||
assert "host" in data | ||
assert "where" in data | ||
assert "level" in data | ||
assert "stack_trace" in data | ||
assert data.get("message") == logging_content | ||
if idx in [0, 6]: | ||
assert data.get("stage") == "session" | ||
if idx == 6: | ||
assert data.get("duration") > 0 | ||
if idx in [1, 2, 3, 4, 5]: | ||
assert data.get("stage") == "testcase" | ||
assert "testId" in data | ||
try: | ||
uuid.UUID(data["testId"]) | ||
except ValueError: | ||
assert False, "Not a UUID string" | ||
assert len(call_args) == 7 | ||
|
||
# Message 0 | ||
assert call_args[0].args[0] == FLUENTD_LABEL | ||
assert isinstance(call_args[0].args[1], int) | ||
message_0 = get_data_from_call_args(call_args[0], ["status", "stage", "sessionId"]) | ||
assert message_0 == { | ||
"status": "start", | ||
"stage": "session", | ||
"sessionId": f"{session_uuid}", | ||
} | ||
|
||
# Message 1 | ||
assert call_args[1].args[0] == FLUENTD_LABEL | ||
assert isinstance(call_args[1].args[1], int) | ||
message_1 = get_data_from_call_args(call_args[1], ["status", "stage", "testId"]) | ||
assert message_1 == { | ||
"status": "start", | ||
"stage": "testcase", | ||
"testId": FAKE_TEST_UUID, | ||
} | ||
|
||
# Message 2 | ||
assert call_args[2].args[0] is None | ||
assert isinstance(call_args[2].args[1], EventTime) | ||
message_2 = get_data_from_call_args(call_args[2], ["stage", "message"]) | ||
assert message_2 == {"stage": "testcase", "message": logging_content} | ||
assert {"host", "where", "level", "stack_trace"}.issubset( | ||
call_args[2].args[2].keys() | ||
) | ||
|
||
# Message 3 | ||
assert call_args[3].args[0] is None | ||
assert isinstance(call_args[3].args[1], EventTime) | ||
message_3 = get_data_from_call_args(call_args[2], ["type", "stage", "message"]) | ||
assert message_3 == { | ||
"type": "logging", | ||
"stage": "testcase", | ||
"message": logging_content, | ||
} | ||
assert {"host", "where", "level", "stack_trace"}.issubset( | ||
call_args[2].args[2].keys() | ||
) | ||
|
||
# Message 4 | ||
assert call_args[4].args[0] == FLUENTD_LABEL | ||
assert isinstance(call_args[4].args[1], int) | ||
assert call_args[4].args[2].get("stage") == "testcase" | ||
|
||
# Message 5 | ||
assert call_args[5].args[0] == FLUENTD_LABEL | ||
assert isinstance(call_args[5].args[1], int) | ||
message_5 = get_data_from_call_args(call_args[5], ["status", "stage"]) | ||
assert message_5 == {"status": "finish", "stage": "testcase"} | ||
|
||
# Message 6 | ||
assert call_args[6].args[0] == FLUENTD_LABEL | ||
assert isinstance(call_args[6].args[1], int) | ||
message_6 = get_data_from_call_args(call_args[6], ["status", "stage"]) | ||
assert message_6 == {"status": "finish", "stage": "session"} | ||
assert call_args[6].args[2].get("duration") > 0 | ||
|
||
|
||
def is_pytest_message(args): | ||
return args[0] is not None and args[0] == "pytest" | ||
|
||
|
||
def test_fluentd_with_options_and_timestamp_enabled_shows_timestamp_field_in_output( | ||
runpytest, fluentd_sender, session_uuid | ||
): | ||
result = runpytest( | ||
f"--session-uuid={session_uuid}", | ||
f"--fluentd-tag={FLUENTD_TAG}", | ||
f"--fluentd-label={FLUENTD_LABEL}", | ||
f"--fluentd-timestamp=@timestamp", | ||
"--extend-logging", | ||
) | ||
result.assert_outcomes(passed=1) | ||
call_args = fluentd_sender.emit_with_time.call_args_list | ||
assert len(call_args) == 7 | ||
|
||
for call_arg in filter(is_pytest_message, call_args): | ||
assert "@timestamp" in call_arg.args[2] | ||
|
||
|
||
def test_fluentd_with_timestamp_enabled_shows_timestamp_field_in_output( | ||
runpytest, fluentd_sender, session_uuid | ||
): | ||
result = runpytest( | ||
f"--session-uuid={session_uuid}", | ||
f"--fluentd-timestamp=@timestamp", | ||
) | ||
result.assert_outcomes(passed=1) | ||
call_args = fluentd_sender.emit_with_time.call_args_list | ||
assert len(call_args) == 5 | ||
|
||
for call_arg in filter(is_pytest_message, call_args): | ||
assert "@timestamp" in call_arg.args[2] |