From b516e90837b55e0c9a604942818079d6d5ae2fc8 Mon Sep 17 00:00:00 2001 From: Brian Bouterse Date: Tue, 7 Dec 2021 15:45:15 -0500 Subject: [PATCH] Makes pulp_smash a pytest plugin This provides a common place for any pytest fixtures to be added. --- pulp_smash/pulp3/conftest.py | 0 pulp_smash/pulp3/fixture_utils.py | 7 +++ pulp_smash/pulp3/pytest_plugin/__init__.py | 54 ++++++++++++++++++++++ setup.py | 6 ++- 4 files changed, 66 insertions(+), 1 deletion(-) create mode 100644 pulp_smash/pulp3/conftest.py create mode 100644 pulp_smash/pulp3/fixture_utils.py create mode 100644 pulp_smash/pulp3/pytest_plugin/__init__.py diff --git a/pulp_smash/pulp3/conftest.py b/pulp_smash/pulp3/conftest.py new file mode 100644 index 000000000..e69de29bb diff --git a/pulp_smash/pulp3/fixture_utils.py b/pulp_smash/pulp3/fixture_utils.py new file mode 100644 index 000000000..7feaf8a24 --- /dev/null +++ b/pulp_smash/pulp3/fixture_utils.py @@ -0,0 +1,7 @@ +from aiohttp import web + + +def add_file_system_routes(app, fixtures_root, fixture_name): + fixture_path = fixtures_root / fixture_name + new_routes = [web.static("/", fixture_path.absolute(), show_index=True)] + app.add_routes(new_routes) diff --git a/pulp_smash/pulp3/pytest_plugin/__init__.py b/pulp_smash/pulp3/pytest_plugin/__init__.py new file mode 100644 index 000000000..11c944931 --- /dev/null +++ b/pulp_smash/pulp3/pytest_plugin/__init__.py @@ -0,0 +1,54 @@ +import asyncio + +import pytest + +from pulp_smash.api import _get_sleep_time +from pulp_smash.config import get_config +from pulp_smash.pulp3.bindings import delete_orphans + +from pulpcore.client.pulpcore import ApiClient, TasksApi + + +cfg = get_config() +SLEEP_TIME = _get_sleep_time(cfg) + + +@pytest.fixture(scope="session") +def pulpcore_client(): + configuration = cfg.get_bindings_config() + return ApiClient(configuration) + + +@pytest.fixture(scope="session") +def tasks_api_client(pulpcore_client): + return TasksApi(pulpcore_client) + + +@pytest.fixture(scope="session") +def async_monitor_task(tasks_api_client): + async def _async_monitor_task(task_href): + completed = ["completed", "failed", "canceled"] + task = tasks_api_client.read(task_href) + while task.state not in completed: + await asyncio.sleep(SLEEP_TIME) + task = tasks_api_client.read(task_href) + return task + + return _async_monitor_task + + +@pytest.fixture +def delete_orphans_pre(): + delete_orphans() + yield + + +@pytest.fixture +def delete_orphans_post(): + yield + delete_orphans() + + +@pytest.fixture +def delete_orphans_pre_post(delete_orphans_pre, delete_orphans_post): + yield diff --git a/setup.py b/setup.py index 3a6ea13fb..9f0ea153c 100755 --- a/setup.py +++ b/setup.py @@ -35,6 +35,7 @@ "Programming Language :: Python :: 3.5", "Programming Language :: Python :: 3.6", "Programming Language :: Python :: 3.7", + "Framework :: Pytest", ], packages=find_packages(include=["pulp_smash", "pulp_smash.*"]), install_requires=[ @@ -47,6 +48,9 @@ "requests", "pulpcore-client", ], - entry_points={"console_scripts": ["pulp-smash=pulp_smash.pulp_smash_cli:pulp_smash"]}, + entry_points={ + "console_scripts": ["pulp-smash=pulp_smash.pulp_smash_cli:pulp_smash"], + "pytest11": ["pulp_smash = pulp_smash.pulp3.pytest_plugin"], + }, test_suite="tests", )