Skip to content

Commit 196a756

Browse files
committed
refactor(tests): new report
Let's use an off-the-shelf report like JUnit Signed-off-by: Pablo Barbáchano <[email protected]>
1 parent d1bc449 commit 196a756

File tree

5 files changed

+59
-412
lines changed

5 files changed

+59
-412
lines changed

tests/conftest.py

+32-3
Original file line numberDiff line numberDiff line change
@@ -81,13 +81,15 @@ def test_with_any_microvm(test_microvm_any):
8181
by the MicrovmImageFetcher, but not by the fixture template.
8282
"""
8383

84+
import inspect
85+
import json
8486
import os
8587
import platform
88+
import re
8689
import shutil
8790
import sys
8891
import tempfile
8992
import uuid
90-
import json
9193
from pathlib import Path
9294

9395
import pytest
@@ -101,6 +103,7 @@ def test_with_any_microvm(test_microvm_any):
101103
from framework.s3fetcher import MicrovmImageS3Fetcher
102104
from framework.utils import get_firecracker_version_from_toml
103105
from framework.with_filelock import with_filelock
106+
from framework.properties import GLOBAL_PROPS
104107

105108
# Tests root directory.
106109
SCRIPT_FOLDER = os.path.dirname(os.path.realpath(__file__))
@@ -115,8 +118,6 @@ def test_with_any_microvm(test_microvm_any):
115118
raise PermissionError("Test session needs to be run as root.")
116119

117120

118-
119-
120121
def _test_images_s3_bucket():
121122
"""Auxiliary function for getting this session's bucket name."""
122123
return os.environ.get(
@@ -234,6 +235,34 @@ def pytest_collection_modifyitems(config, items):
234235
item.add_marker(skip_marker)
235236

236237

238+
def pytest_runtest_makereport(item, call):
239+
"""Decorate test results with additional properties."""
240+
if call.when != "setup":
241+
return
242+
243+
for prop_name, prop_val in GLOBAL_PROPS.items():
244+
# if record_testsuite_property worked with xdist we could use that
245+
# https://docs.pytest.org/en/7.1.x/reference/reference.html#record-testsuite-property
246+
# to record the properties once per report. But here we record each
247+
# prop per test. It just results in larger report files.
248+
item.user_properties.append((prop_name, prop_val))
249+
250+
function_docstring = inspect.getdoc(item.function)
251+
description = []
252+
attributes = {}
253+
for line in function_docstring.split("\n"):
254+
# extract tags like @type, @issue, etc
255+
match = re.match(r"\s*@(?P<attr>\w+):\s*(?P<value>\w+)", line)
256+
if match:
257+
attr, value = match["attr"], match["value"]
258+
attributes[attr] = value
259+
else:
260+
description.append(line)
261+
for attr_name, attr_value in attributes.items():
262+
item.user_properties.append((attr_name, attr_value))
263+
item.user_properties.append(("description", "".join(description)))
264+
265+
237266
def test_session_root_path():
238267
"""Create and return the testrun session root directory.
239268

tests/framework/mpsing.py

-114
This file was deleted.

tests/framework/properties.py

+26
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
# Copyright 2022 Amazon.com, Inc. or its affiliates. All Rights Reserved.
2+
# SPDX-License-Identifier: Apache-2.0
3+
4+
"""
5+
Metadata we want to attach to tests for further analysis and troubleshooting
6+
"""
7+
8+
import platform
9+
import subprocess
10+
11+
from framework.utils_cpuid import get_cpu_model_name
12+
13+
14+
def run_cmd(cmd):
15+
"""Return the stdout of a command"""
16+
stdout = subprocess.check_output(cmd, shell=True).decode().strip()
17+
return stdout
18+
19+
20+
GLOBAL_PROPS = {
21+
"architecture": platform.machine(),
22+
"host_linux_kernel": platform.release(),
23+
"libc_ver": "-".join(platform.libc_ver()),
24+
"cpu_model": get_cpu_model_name(),
25+
"commit_id": run_cmd("git rev-parse HEAD"),
26+
}

0 commit comments

Comments
 (0)