|
| 1 | +import datetime |
1 | 2 | import logging |
2 | 3 | import os |
3 | 4 | from pathlib import Path |
|
14 | 15 | _GB: ByteSize = ByteSize(_MB * 1024) # in bytes |
15 | 16 |
|
16 | 17 |
|
| 18 | +# Dictionary to store start times of tests |
| 19 | +_test_start_times = {} |
| 20 | + |
| 21 | + |
| 22 | +def _utc_now(): |
| 23 | + return datetime.datetime.now(tz=datetime.timezone.utc) |
| 24 | + |
| 25 | + |
| 26 | +def _construct_graylog_url(api_host, start_time, end_time): |
| 27 | + """ |
| 28 | + Construct a Graylog URL for the given time interval. |
| 29 | + """ |
| 30 | + base_url = api_host.replace("api.", "monitoring.", 1).rstrip("/") |
| 31 | + url = f"{base_url}/graylog/search" |
| 32 | + start_time_str = start_time.strftime("%Y-%m-%dT%H:%M:%S.%fZ") |
| 33 | + end_time_str = end_time.strftime("%Y-%m-%dT%H:%M:%S.%fZ") |
| 34 | + query = f"from={start_time_str}&to={end_time_str}" |
| 35 | + return f"{url}?{query}" |
| 36 | + |
| 37 | + |
| 38 | +def pytest_runtest_setup(item): |
| 39 | + """ |
| 40 | + Hook to capture the start time of each test. |
| 41 | + """ |
| 42 | + _test_start_times[item.name] = _utc_now() |
| 43 | + |
| 44 | + |
| 45 | +def pytest_runtest_makereport(item, call): |
| 46 | + """ |
| 47 | + Hook to add extra information when a test fails. |
| 48 | + """ |
| 49 | + if call.when == "call": |
| 50 | + |
| 51 | + # Check if the test failed |
| 52 | + if call.excinfo is not None: |
| 53 | + test_name = item.name |
| 54 | + test_location = item.location |
| 55 | + api_host = os.environ.get("OSPARC_API_HOST", "") |
| 56 | + |
| 57 | + diagnostics = { |
| 58 | + "test_name": test_name, |
| 59 | + "test_location": test_location, |
| 60 | + "api_host": api_host, |
| 61 | + } |
| 62 | + |
| 63 | + # Get the start and end times of the test |
| 64 | + start_time = _test_start_times.get(test_name) |
| 65 | + end_time = _utc_now() |
| 66 | + |
| 67 | + if start_time: |
| 68 | + diagnostics["graylog_url"] = _construct_graylog_url(api_host, start_time, end_time) |
| 69 | + |
| 70 | + # Print the diagnostics |
| 71 | + print(f"\nDiagnostics for {test_name}:") |
| 72 | + for key, value in diagnostics.items(): |
| 73 | + print(" ", key, ":", value) |
| 74 | + |
| 75 | + |
| 76 | +@pytest.hookimpl(tryfirst=True) |
| 77 | +def pytest_configure(config): |
| 78 | + config.pluginmanager.register(pytest_runtest_setup, "osparc_test_times_plugin") |
| 79 | + config.pluginmanager.register(pytest_runtest_makereport, "osparc_makereport_plugin") |
| 80 | + |
| 81 | + |
17 | 82 | @pytest.fixture |
18 | 83 | def configuration() -> Iterable[osparc.Configuration]: |
19 | 84 | assert (host := os.environ.get("OSPARC_API_HOST")) |
@@ -60,9 +125,9 @@ def tmp_file(tmp_path: Path, caplog) -> Path: |
60 | 125 |
|
61 | 126 |
|
62 | 127 | @pytest.fixture |
63 | | -def sleeper(api_client: osparc.ApiClient) -> Iterable[osparc.Solver]: |
| 128 | +def sleeper(api_client: osparc.ApiClient) -> osparc.Solver: |
64 | 129 | solvers_api = osparc.SolversApi(api_client=api_client) |
65 | 130 | sleeper: osparc.Solver = solvers_api.get_solver_release( |
66 | 131 | "simcore/services/comp/itis/sleeper", "2.0.2" |
67 | 132 | ) # type: ignore |
68 | | - yield sleeper |
| 133 | + return sleeper |
0 commit comments