From 619828a3b4132902c19fc7dc20429a1560cdd9bf Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Kristian=20Zar=C4=99bski?= Date: Fri, 24 May 2024 12:08:20 +0100 Subject: [PATCH 1/2] Use test name for tag via fixture and add CI tag if relevant --- tests/refactor/conftest.py | 27 ++++++++++++++++----------- tests/refactor/test_client.py | 2 -- tests/refactor/test_run_class.py | 7 ------- 3 files changed, 16 insertions(+), 20 deletions(-) diff --git a/tests/refactor/conftest.py b/tests/refactor/conftest.py index ae440cae..af2305aa 100644 --- a/tests/refactor/conftest.py +++ b/tests/refactor/conftest.py @@ -42,35 +42,35 @@ def log_messages(caplog): @pytest.fixture -def create_test_run() -> typing.Generator[typing.Tuple[sv_run.Run, dict], None, None]: +def create_test_run(request) -> typing.Generator[typing.Tuple[sv_run.Run, dict], None, None]: with sv_run.Run() as run: - yield run, setup_test_run(run, True) + yield run, setup_test_run(run, True, request) @pytest.fixture -def create_test_run_offline(mocker: pytest_mock.MockerFixture) -> typing.Generator[typing.Tuple[sv_run.Run, dict], None, None]: +def create_test_run_offline(mocker: pytest_mock.MockerFixture, request) -> typing.Generator[typing.Tuple[sv_run.Run, dict], None, None]: with tempfile.TemporaryDirectory() as temp_d: mocker.patch.object(simvue.utilities, "get_offline_directory", lambda *_: temp_d) with sv_run.Run("offline") as run: - yield run, setup_test_run(run, True) + yield run, setup_test_run(run, True, request) @pytest.fixture -def create_plain_run() -> typing.Generator[typing.Tuple[sv_run.Run, dict], None, None]: +def create_plain_run(request) -> typing.Generator[typing.Tuple[sv_run.Run, dict], None, None]: with sv_run.Run() as run: - yield run, setup_test_run(run, False) + yield run, setup_test_run(run, False, request) @pytest.fixture -def create_plain_run_offline(mocker: pytest_mock.MockerFixture) -> typing.Generator[typing.Tuple[sv_run.Run, dict], None, None]: +def create_plain_run_offline(mocker: pytest_mock.MockerFixture, request) -> typing.Generator[typing.Tuple[sv_run.Run, dict], None, None]: with tempfile.TemporaryDirectory() as temp_d: mocker.patch.object(simvue.utilities, "get_offline_directory", lambda *_: temp_d) with sv_run.Run("offline") as run: - yield run, setup_test_run(run, False) + yield run, setup_test_run(run, False, request) -def setup_test_run(run: sv_run.Run, create_objects: bool): +def setup_test_run(run: sv_run.Run, create_objects: bool, request: pytest.FixtureRequest): fix_use_id: str = str(uuid.uuid4()).split('-', 1)[0] TEST_DATA = { "event_contains": "sent event", @@ -78,12 +78,17 @@ def setup_test_run(run: sv_run.Run, create_objects: bool): "test_engine": "pytest", "test_identifier": fix_use_id }, - "folder": f"/simvue_unit_testing/{fix_use_id}" + "folder": f"/simvue_unit_testing/{fix_use_id}", + "tags": ["simvue_client_unit_tests", request.node.name] } + + if os.environ.get("CI"): + TEST_DATA["tags"].append("ci") + run.config(suppress_errors=False) run.init( name=f"test_run_{TEST_DATA['metadata']['test_identifier']}", - tags=["simvue_client_unit_tests"], + tags=TEST_DATA["tags"], folder=TEST_DATA["folder"], visibility="tenant", retention_period="1 hour" diff --git a/tests/refactor/test_client.py b/tests/refactor/test_client.py index 9d68d2e5..3622a0a8 100644 --- a/tests/refactor/test_client.py +++ b/tests/refactor/test_client.py @@ -175,7 +175,6 @@ def test_run_deletion(create_test_run: tuple[sv_run.Run, dict]) -> None: @pytest.mark.client(depends=PRE_DELETION_TESTS) def test_runs_deletion(create_test_run: tuple[sv_run.Run, dict]) -> None: run, run_data = create_test_run - run.update_tags(["simvue_client_unit_tests", "test_runs_deletion"]) run.close() client = svc.Client() assert len(client.delete_runs(run_data["folder"])) > 0 @@ -185,7 +184,6 @@ def test_runs_deletion(create_test_run: tuple[sv_run.Run, dict]) -> None: @pytest.mark.client(depends=PRE_DELETION_TESTS + ["test_runs_deletion"]) def test_folder_deletion(create_test_run: tuple[sv_run.Run, dict]) -> None: run, run_data = create_test_run - run.update_tags(["simvue_client_unit_tests", "test_folder_deletion"]) run.close() client = svc.Client() # This test is called last, one run created so expect length 1 diff --git a/tests/refactor/test_run_class.py b/tests/refactor/test_run_class.py index 9a3a4c3e..79da9574 100644 --- a/tests/refactor/test_run_class.py +++ b/tests/refactor/test_run_class.py @@ -64,8 +64,6 @@ def test_log_metrics( retention_period="1 hour", ) - run.update_tags(["simvue_client_unit_tests", "test_log_metrics"]) - # Speed up the read rate for this test run._dispatcher._max_buffer_size = 10 run._dispatcher._max_read_rate *= 10 @@ -110,7 +108,6 @@ def test_log_metrics( def test_log_metrics_offline(create_test_run_offline: tuple[sv_run.Run, dict]) -> None: METRICS = {"a": 10, "b": 1.2, "c": 2} run, _ = create_test_run_offline - run.update_tags(["simvue_client_unit_tests", "test_log_metrics"]) run.log_metrics(METRICS) @@ -118,7 +115,6 @@ def test_log_metrics_offline(create_test_run_offline: tuple[sv_run.Run, dict]) - def test_log_events(create_test_run: tuple[sv_run.Run, dict]) -> None: EVENT_MSG = "Hello world!" run, _ = create_test_run - run.update_tags(["simvue_client_unit_tests", "test_log_events"]) run.log_event(EVENT_MSG) @@ -126,7 +122,6 @@ def test_log_events(create_test_run: tuple[sv_run.Run, dict]) -> None: def test_log_events_offline(create_test_run_offline: tuple[sv_run.Run, dict]) -> None: EVENT_MSG = "Hello world!" run, _ = create_test_run_offline - run.update_tags(["simvue_client_unit_tests", "test_log_events"]) run.log_event(EVENT_MSG) @@ -134,7 +129,6 @@ def test_log_events_offline(create_test_run_offline: tuple[sv_run.Run, dict]) -> def test_update_metadata(create_test_run: tuple[sv_run.Run, dict]) -> None: METADATA = {"a": 10, "b": 1.2, "c": "word"} run, _ = create_test_run - run.update_tags(["simvue_client_unit_tests", "test_update_metadata"]) run.update_metadata(METADATA) @@ -144,7 +138,6 @@ def test_update_metadata_offline( ) -> None: METADATA = {"a": 10, "b": 1.2, "c": "word"} run, _ = create_test_run_offline - run.update_tags(["simvue_client_unit_tests", "test_update_metadata"]) run.update_metadata(METADATA) From 4d55975b8ab355a2ffa3a25d74a960f33332d94a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Kristian=20Zar=C4=99bski?= Date: Fri, 24 May 2024 12:08:20 +0100 Subject: [PATCH 2/2] Use test name for tag via fixture and add CI tag if relevant --- tests/functional/test_run_offline_folder.py | 2 +- tests/refactor/conftest.py | 27 ++++++++++------- tests/refactor/test_client.py | 2 -- tests/refactor/test_executor.py | 5 ++-- tests/refactor/test_run_class.py | 32 +++++++++------------ 5 files changed, 33 insertions(+), 35 deletions(-) diff --git a/tests/functional/test_run_offline_folder.py b/tests/functional/test_run_offline_folder.py index cd1c4389..3fe84bee 100644 --- a/tests/functional/test_run_offline_folder.py +++ b/tests/functional/test_run_offline_folder.py @@ -24,7 +24,7 @@ def test_basic_run_folder(self): pass name = "test-%s" % str(uuid.uuid4()) - folder = "/test-%s" % str(uuid.uuid4()) + folder = "/simvue_unit_testing" metadata = {str(uuid.uuid4()): 100 * random.random()} run = Run("offline") run.init(name, folder=folder) diff --git a/tests/refactor/conftest.py b/tests/refactor/conftest.py index ae440cae..af2305aa 100644 --- a/tests/refactor/conftest.py +++ b/tests/refactor/conftest.py @@ -42,35 +42,35 @@ def log_messages(caplog): @pytest.fixture -def create_test_run() -> typing.Generator[typing.Tuple[sv_run.Run, dict], None, None]: +def create_test_run(request) -> typing.Generator[typing.Tuple[sv_run.Run, dict], None, None]: with sv_run.Run() as run: - yield run, setup_test_run(run, True) + yield run, setup_test_run(run, True, request) @pytest.fixture -def create_test_run_offline(mocker: pytest_mock.MockerFixture) -> typing.Generator[typing.Tuple[sv_run.Run, dict], None, None]: +def create_test_run_offline(mocker: pytest_mock.MockerFixture, request) -> typing.Generator[typing.Tuple[sv_run.Run, dict], None, None]: with tempfile.TemporaryDirectory() as temp_d: mocker.patch.object(simvue.utilities, "get_offline_directory", lambda *_: temp_d) with sv_run.Run("offline") as run: - yield run, setup_test_run(run, True) + yield run, setup_test_run(run, True, request) @pytest.fixture -def create_plain_run() -> typing.Generator[typing.Tuple[sv_run.Run, dict], None, None]: +def create_plain_run(request) -> typing.Generator[typing.Tuple[sv_run.Run, dict], None, None]: with sv_run.Run() as run: - yield run, setup_test_run(run, False) + yield run, setup_test_run(run, False, request) @pytest.fixture -def create_plain_run_offline(mocker: pytest_mock.MockerFixture) -> typing.Generator[typing.Tuple[sv_run.Run, dict], None, None]: +def create_plain_run_offline(mocker: pytest_mock.MockerFixture, request) -> typing.Generator[typing.Tuple[sv_run.Run, dict], None, None]: with tempfile.TemporaryDirectory() as temp_d: mocker.patch.object(simvue.utilities, "get_offline_directory", lambda *_: temp_d) with sv_run.Run("offline") as run: - yield run, setup_test_run(run, False) + yield run, setup_test_run(run, False, request) -def setup_test_run(run: sv_run.Run, create_objects: bool): +def setup_test_run(run: sv_run.Run, create_objects: bool, request: pytest.FixtureRequest): fix_use_id: str = str(uuid.uuid4()).split('-', 1)[0] TEST_DATA = { "event_contains": "sent event", @@ -78,12 +78,17 @@ def setup_test_run(run: sv_run.Run, create_objects: bool): "test_engine": "pytest", "test_identifier": fix_use_id }, - "folder": f"/simvue_unit_testing/{fix_use_id}" + "folder": f"/simvue_unit_testing/{fix_use_id}", + "tags": ["simvue_client_unit_tests", request.node.name] } + + if os.environ.get("CI"): + TEST_DATA["tags"].append("ci") + run.config(suppress_errors=False) run.init( name=f"test_run_{TEST_DATA['metadata']['test_identifier']}", - tags=["simvue_client_unit_tests"], + tags=TEST_DATA["tags"], folder=TEST_DATA["folder"], visibility="tenant", retention_period="1 hour" diff --git a/tests/refactor/test_client.py b/tests/refactor/test_client.py index 9d68d2e5..3622a0a8 100644 --- a/tests/refactor/test_client.py +++ b/tests/refactor/test_client.py @@ -175,7 +175,6 @@ def test_run_deletion(create_test_run: tuple[sv_run.Run, dict]) -> None: @pytest.mark.client(depends=PRE_DELETION_TESTS) def test_runs_deletion(create_test_run: tuple[sv_run.Run, dict]) -> None: run, run_data = create_test_run - run.update_tags(["simvue_client_unit_tests", "test_runs_deletion"]) run.close() client = svc.Client() assert len(client.delete_runs(run_data["folder"])) > 0 @@ -185,7 +184,6 @@ def test_runs_deletion(create_test_run: tuple[sv_run.Run, dict]) -> None: @pytest.mark.client(depends=PRE_DELETION_TESTS + ["test_runs_deletion"]) def test_folder_deletion(create_test_run: tuple[sv_run.Run, dict]) -> None: run, run_data = create_test_run - run.update_tags(["simvue_client_unit_tests", "test_folder_deletion"]) run.close() client = svc.Client() # This test is called last, one run created so expect length 1 diff --git a/tests/refactor/test_executor.py b/tests/refactor/test_executor.py index a191d0e3..513e4eec 100644 --- a/tests/refactor/test_executor.py +++ b/tests/refactor/test_executor.py @@ -8,13 +8,14 @@ @pytest.mark.executor @pytest.mark.parametrize("successful", (True, False), ids=("successful", "failing")) def test_executor_add_process( - successful: bool + successful: bool, + request: pytest.FixtureRequest ) -> None: run = simvue.Run() completion_trigger = multiprocessing.Event() run.init( f"test_executor_{'success' if successful else 'fail'}", - tags=["simvue_client_unit_tests"], + tags=["simvue_client_unit_tests", request.node.name], folder="/simvue_unit_test_folder" ) diff --git a/tests/refactor/test_run_class.py b/tests/refactor/test_run_class.py index 9a3a4c3e..76ae29dc 100644 --- a/tests/refactor/test_run_class.py +++ b/tests/refactor/test_run_class.py @@ -32,6 +32,7 @@ def test_log_metrics( overload_buffer: bool, setup_logging: "CountingLogHandler", mocker, + request: pytest.FixtureRequest, visibility: typing.Union[typing.Literal["public", "tenant"], list[str], None] ) -> None: METRICS = {"a": 10, "b": 1.2} @@ -47,7 +48,7 @@ def test_log_metrics( with pytest.raises(RuntimeError): run.init( name=f"test_run_{str(uuid.uuid4()).split('-', 1)[0]}", - tags=["simvue_client_unit_tests"], + tags=["simvue_client_unit_tests", request.node.name], folder="/simvue_unit_testing", retention_period="1 hour", visibility=visibility, @@ -57,15 +58,13 @@ def test_log_metrics( run.init( name=f"test_run_{str(uuid.uuid4()).split('-', 1)[0]}", - tags=["simvue_client_unit_tests"], + tags=["simvue_client_unit_tests", request.node.name], folder="/simvue_unit_testing", visibility=visibility, resources_metrics_interval=1, retention_period="1 hour", ) - run.update_tags(["simvue_client_unit_tests", "test_log_metrics"]) - # Speed up the read rate for this test run._dispatcher._max_buffer_size = 10 run._dispatcher._max_read_rate *= 10 @@ -110,7 +109,6 @@ def test_log_metrics( def test_log_metrics_offline(create_test_run_offline: tuple[sv_run.Run, dict]) -> None: METRICS = {"a": 10, "b": 1.2, "c": 2} run, _ = create_test_run_offline - run.update_tags(["simvue_client_unit_tests", "test_log_metrics"]) run.log_metrics(METRICS) @@ -118,7 +116,6 @@ def test_log_metrics_offline(create_test_run_offline: tuple[sv_run.Run, dict]) - def test_log_events(create_test_run: tuple[sv_run.Run, dict]) -> None: EVENT_MSG = "Hello world!" run, _ = create_test_run - run.update_tags(["simvue_client_unit_tests", "test_log_events"]) run.log_event(EVENT_MSG) @@ -126,7 +123,6 @@ def test_log_events(create_test_run: tuple[sv_run.Run, dict]) -> None: def test_log_events_offline(create_test_run_offline: tuple[sv_run.Run, dict]) -> None: EVENT_MSG = "Hello world!" run, _ = create_test_run_offline - run.update_tags(["simvue_client_unit_tests", "test_log_events"]) run.log_event(EVENT_MSG) @@ -134,7 +130,6 @@ def test_log_events_offline(create_test_run_offline: tuple[sv_run.Run, dict]) -> def test_update_metadata(create_test_run: tuple[sv_run.Run, dict]) -> None: METADATA = {"a": 10, "b": 1.2, "c": "word"} run, _ = create_test_run - run.update_tags(["simvue_client_unit_tests", "test_update_metadata"]) run.update_metadata(METADATA) @@ -144,13 +139,12 @@ def test_update_metadata_offline( ) -> None: METADATA = {"a": 10, "b": 1.2, "c": "word"} run, _ = create_test_run_offline - run.update_tags(["simvue_client_unit_tests", "test_update_metadata"]) run.update_metadata(METADATA) @pytest.mark.run @pytest.mark.parametrize("multi_threaded", (True, False), ids=("multi", "single")) -def test_runs_multiple_parallel(multi_threaded: bool) -> None: +def test_runs_multiple_parallel(multi_threaded: bool, request: pytest.FixtureRequest) -> None: N_RUNS: int = 2 if multi_threaded: @@ -159,7 +153,7 @@ def thread_func(index: int) -> tuple[int, list[dict[str, typing.Any]], str]: run.config(suppress_errors=False) run.init( name=f"test_runs_multiple_{index + 1}", - tags=["simvue_client_unit_tests", "test_multi_run_threaded"], + tags=["simvue_client_unit_tests", request.node.name], folder="/simvue_unit_testing", retention_period="1 hour", ) @@ -196,7 +190,7 @@ def thread_func(index: int) -> tuple[int, list[dict[str, typing.Any]], str]: run_1.config(suppress_errors=False) run_1.init( name="test_runs_multiple_unthreaded_1", - tags=["simvue_client_unit_tests", "test_multi_run_unthreaded"], + tags=["simvue_client_unit_tests", request.node.name], folder="/simvue_unit_testing", retention_period="1 hour", ) @@ -238,7 +232,7 @@ def thread_func(index: int) -> tuple[int, list[dict[str, typing.Any]], str]: @pytest.mark.run -def test_runs_multiple_series() -> None: +def test_runs_multiple_series(request: pytest.FixtureRequest) -> None: N_RUNS: int = 2 metrics = [] @@ -250,7 +244,7 @@ def test_runs_multiple_series() -> None: run.config(suppress_errors=False) run.init( name=f"test_runs_multiple_series_{index}", - tags=["simvue_client_unit_tests", "test_multi_run_series"], + tags=["simvue_client_unit_tests", request.node.name], folder="/simvue_unit_testing", retention_period="1 hour", ) @@ -284,7 +278,7 @@ def test_runs_multiple_series() -> None: @pytest.mark.run @pytest.mark.parametrize("post_init", (True, False), ids=("pre-init", "post-init")) def test_suppressed_errors( - setup_logging: "CountingLogHandler", post_init: bool + setup_logging: "CountingLogHandler", post_init: bool, request: pytest.FixtureRequest ) -> None: setup_logging.captures = ["Skipping call to"] @@ -300,7 +294,7 @@ def test_suppressed_errors( run.init( name="test_suppressed_errors", folder="/simvue_unit_testing", - tags=["simvue_client_unit_tests"], + tags=["simvue_client_unit_tests", request.node.name], retention_period="1 hour" ) @@ -323,11 +317,11 @@ def test_bad_run_arguments() -> None: run.init("sdas", [34]) -def test_set_folder_details() -> None: +def test_set_folder_details(request: pytest.FixtureRequest) -> None: with sv_run.Run() as run: - folder_name: str ="/simvue_unit_test_folder" + folder_name: str ="/simvue_unit_tests" description: str = "test description" - tags: list[str] = ["simvue_client_unit_tests", "test_set_folder_details"] + tags: list[str] = ["simvue_client_unit_tests", request.node.name] run.init(folder=folder_name) run.set_folder_details(path=folder_name, tags=tags, description=description)