|
4 | 4 | # pylint: disable=unused-argument |
5 | 5 | # pylint: disable=unused-variable |
6 | 6 |
|
| 7 | +import datetime |
7 | 8 | import logging |
8 | 9 | import os |
9 | 10 | from pathlib import Path |
|
14 | 15 | import pytest |
15 | 16 | from httpx import AsyncClient, BasicAuth |
16 | 17 | from numpy import random |
| 18 | +from osparc._models import ConfigurationModel |
17 | 19 | from pydantic import ByteSize |
18 | 20 |
|
19 | 21 | _KB: ByteSize = ByteSize(1024) # in bytes |
20 | 22 | _MB: ByteSize = ByteSize(_KB * 1024) # in bytes |
21 | 23 | _GB: ByteSize = ByteSize(_MB * 1024) # in bytes |
22 | 24 |
|
23 | 25 |
|
24 | | -@pytest.fixture |
25 | | -def configuration() -> osparc.Configuration: |
26 | | - assert (host := os.environ.get("OSPARC_API_HOST")) |
27 | | - assert (username := os.environ.get("OSPARC_API_KEY")) |
28 | | - assert (password := os.environ.get("OSPARC_API_SECRET")) |
29 | | - return osparc.Configuration( |
30 | | - host=host, |
31 | | - username=username, |
32 | | - password=password, |
33 | | - ) |
| 26 | +# Dictionary to store start times of tests |
| 27 | +_test_start_times = {} |
| 28 | + |
| 29 | + |
| 30 | +def _utc_now(): |
| 31 | + return datetime.datetime.now(tz=datetime.timezone.utc) |
| 32 | + |
| 33 | + |
| 34 | +def _construct_graylog_url(api_host, start_time, end_time): |
| 35 | + """ |
| 36 | + Construct a Graylog URL for the given time interval. |
| 37 | + """ |
| 38 | + base_url = api_host.replace("api.", "monitoring.", 1).rstrip("/") |
| 39 | + url = f"{base_url}/graylog/search" |
| 40 | + start_time_str = start_time.strftime("%Y-%m-%dT%H:%M:%S.%fZ") |
| 41 | + end_time_str = end_time.strftime("%Y-%m-%dT%H:%M:%S.%fZ") |
| 42 | + query = f"from={start_time_str}&to={end_time_str}" |
| 43 | + return f"{url}?{query}" |
| 44 | + |
| 45 | + |
| 46 | +def pytest_runtest_setup(item): |
| 47 | + """ |
| 48 | + Hook to capture the start time of each test. |
| 49 | + """ |
| 50 | + _test_start_times[item.name] = _utc_now() |
| 51 | + |
| 52 | + |
| 53 | +def pytest_runtest_makereport(item, call): |
| 54 | + """ |
| 55 | + Hook to add extra information when a test fails. |
| 56 | + """ |
| 57 | + if call.when == "call": |
| 58 | + # Check if the test failed |
| 59 | + if call.excinfo is not None: |
| 60 | + test_name = item.name |
| 61 | + test_location = item.location |
| 62 | + api_host = os.environ.get("OSPARC_API_HOST", "") |
| 63 | + |
| 64 | + diagnostics = { |
| 65 | + "test_name": test_name, |
| 66 | + "test_location": test_location, |
| 67 | + "api_host": api_host, |
| 68 | + } |
| 69 | + |
| 70 | + # Get the start and end times of the test |
| 71 | + start_time = _test_start_times.get(test_name) |
| 72 | + end_time = _utc_now() |
| 73 | + |
| 74 | + if start_time: |
| 75 | + diagnostics["graylog_url"] = _construct_graylog_url( |
| 76 | + api_host, start_time, end_time |
| 77 | + ) |
| 78 | + |
| 79 | + # Print the diagnostics |
| 80 | + print(f"\nDiagnostics for {test_name}:") |
| 81 | + for key, value in diagnostics.items(): |
| 82 | + print(" ", key, ":", value) |
| 83 | + |
| 84 | + |
| 85 | +@pytest.hookimpl(tryfirst=True) |
| 86 | +def pytest_configure(config): |
| 87 | + config.pluginmanager.register(pytest_runtest_setup, "osparc_test_times_plugin") |
| 88 | + config.pluginmanager.register(pytest_runtest_makereport, "osparc_makereport_plugin") |
34 | 89 |
|
35 | 90 |
|
36 | 91 | @pytest.fixture |
37 | | -def api_client(configuration: osparc.Configuration) -> Iterable[osparc.ApiClient]: |
38 | | - with osparc.ApiClient(configuration=configuration) as _api_client: |
39 | | - yield _api_client |
| 92 | +def api_client() -> Iterable[osparc.ApiClient]: |
| 93 | + with osparc.ApiClient() as api_client: |
| 94 | + yield api_client |
40 | 95 |
|
41 | 96 |
|
42 | 97 | @pytest.fixture |
43 | | -def async_client(configuration: osparc.Configuration) -> AsyncClient: |
44 | | - return AsyncClient( |
45 | | - base_url=configuration.host, |
| 98 | +def async_client() -> Iterable[AsyncClient]: |
| 99 | + configuration = ConfigurationModel() |
| 100 | + yield AsyncClient( |
| 101 | + base_url=f"{configuration.OSPARC_API_HOST}".rstrip("/"), |
46 | 102 | auth=BasicAuth( |
47 | | - username=configuration.username, password=configuration.password |
| 103 | + username=configuration.OSPARC_API_KEY, |
| 104 | + password=configuration.OSPARC_API_SECRET, |
48 | 105 | ), |
49 | 106 | ) # type: ignore |
50 | 107 |
|
|
0 commit comments