From 239032ea7500eb60e4b7fc05754de29e16532715 Mon Sep 17 00:00:00 2001 From: Matthias Dellweg Date: Fri, 25 Oct 2024 11:15:51 +0200 Subject: [PATCH] Remove test dependency orion-util This package has a flaw and does not seem to be well maintained. Also we don't use much of its features anyway. --- functest_requirements.txt | 5 +- pulp_ansible/pytest_plugin.py | 42 +++- .../test_crud_collection_versions.py | 3 +- .../functional/api/collection/test_sync.py | 195 ++++++++---------- .../api/collection/v3/test_collection.py | 46 ++--- .../v3/test_collection_naming_edgecases.py | 15 +- .../v3/test_collection_version_search.py | 16 +- .../api/collection/v3/test_content_guard.py | 18 +- .../api/collection/v3/test_deletion.py | 3 +- .../functional/api/test_export_import.py | 5 +- pulp_ansible/tests/functional/constants.py | 15 -- pulp_ansible/tests/functional/utils.py | 21 +- pulp_ansible/tests/unit/test_cv_index.py | 4 +- pulp_ansible/tests/unit/utils.py | 25 ++- unittest_requirements.txt | 1 - 15 files changed, 193 insertions(+), 221 deletions(-) diff --git a/functest_requirements.txt b/functest_requirements.txt index d02dc528d..9a263af99 100644 --- a/functest_requirements.txt +++ b/functest_requirements.txt @@ -1,5 +1,4 @@ ansible-core!=2.13.9,!=2.14.5 -orionutils pytest<8 pytest-custom_exit_code pytest-xdist @@ -7,5 +6,7 @@ numpy requests proxy.py~=2.4.9 trustme~=1.2.0 -pulp-smash @ git+https://github.com/pulp/pulp-smash.git pytest-timeout + +# Legacy: +pulp-smash @ git+https://github.com/pulp/pulp-smash.git diff --git a/pulp_ansible/pytest_plugin.py b/pulp_ansible/pytest_plugin.py index 1267e3ccf..9eaca9193 100644 --- a/pulp_ansible/pytest_plugin.py +++ b/pulp_ansible/pytest_plugin.py @@ -1,13 +1,14 @@ import uuid import pytest import numpy as np -import shutil +import subprocess import time - -from orionutils.generator import build_collection, randstr +import yaml +from types import SimpleNamespace from pulpcore.tests.functional.utils import BindingsNamespace from pulp_ansible.tests.functional.constants import ANSIBLE_FIXTURE_URL +from pulp_ansible.tests.functional.utils import randstr # Bindings API Fixtures @@ -129,16 +130,41 @@ def _ansible_git_remote_factory(include_pulp_auth=False, **kwargs): @pytest.fixture(scope="session") def ansible_collection_factory(tmp_path_factory): - def _collection_factory(**kwargs): + def _collection_factory(config=None): + config = {} if config is None else config.copy() tmpdir = tmp_path_factory.mktemp("collection") - collection = build_collection("skeleton", **kwargs) - collection.filename = shutil.copy(collection.filename, tmpdir) - return collection + namespace = config.setdefault("namespace", randstr()) + name = config.setdefault("name", randstr()) + config.setdefault("version", "1.0.0") + + src_path = tmpdir / namespace / name + + # Template the collection + subprocess.run(f"ansible-galaxy collection init {namespace}.{name}", shell=True, cwd=tmpdir) + + # Adjust the template + (src_path / "meta" / "runtime.yml").write_text('requires_ansible: ">=2.13"\n') + (src_path / "README.md").write_text("# title\ncollection docs\n") + galaxy_yaml = yaml.safe_load((src_path / "galaxy.yml").read_text()) + galaxy_yaml.update(config) + (src_path / "galaxy.yml").write_text(yaml.safe_dump(galaxy_yaml)) + + # Build the collection artifact + build_proc = subprocess.run( + "ansible-galaxy collection build .", + shell=True, + cwd=src_path, + stdout=subprocess.PIPE, + check=True, + ) + filename = build_proc.stdout.decode("utf-8").strip().split()[-1] + + return SimpleNamespace(filename=filename, **config) return _collection_factory -@pytest.fixture +@pytest.fixture(scope="class") def build_and_upload_collection(ansible_bindings, monitor_task, ansible_collection_factory): """A factory to locally create, build, and upload a collection.""" diff --git a/pulp_ansible/tests/functional/api/collection/test_crud_collection_versions.py b/pulp_ansible/tests/functional/api/collection/test_crud_collection_versions.py index 898a21524..3e23b4676 100644 --- a/pulp_ansible/tests/functional/api/collection/test_crud_collection_versions.py +++ b/pulp_ansible/tests/functional/api/collection/test_crud_collection_versions.py @@ -2,8 +2,7 @@ import pytest -from orionutils.generator import randstr - +from pulp_ansible.tests.functional.utils import randstr from pulp_ansible.tests.functional.constants import ( ANSIBLE_DEMO_COLLECTION_REQUIREMENTS as DEMO_REQUIREMENTS, GALAXY_ANSIBLE_BASE_URL, diff --git a/pulp_ansible/tests/functional/api/collection/test_sync.py b/pulp_ansible/tests/functional/api/collection/test_sync.py index 7ea45f861..8319db8e7 100644 --- a/pulp_ansible/tests/functional/api/collection/test_sync.py +++ b/pulp_ansible/tests/functional/api/collection/test_sync.py @@ -4,15 +4,7 @@ import pytest -from pulp_ansible.tests.functional.utils import ( - gen_ansible_remote, - SyncHelpersMixin, - TestCaseUsingBindings, -) -from pulp_ansible.tests.functional.constants import TEST_COLLECTION_CONFIGS -from orionutils.generator import build_collection -from pulpcore.client.pulp_ansible import PulpAnsibleArtifactsCollectionsV3Api -from pulp_ansible.tests.functional.utils import monitor_task +from pulp_ansible.tests.functional.utils import randstr @pytest.mark.parallel @@ -83,6 +75,7 @@ def test_sync_supports_mirror_option_false( assert content_version_two.count == 4 +@pytest.mark.parallel def test_sync_mirror_defaults_to_false( ansible_bindings, ansible_collection_remote_factory, ansible_sync_factory ): @@ -161,109 +154,94 @@ def test_sync_collection_with_specialities( assert content.count >= min_count -class FullDependenciesSync(TestCaseUsingBindings, SyncHelpersMixin): - """ - Collection sync tests for syncing collections and their dependencies. - - Dependency Trees: - A E - | / \ - B F G - | | | - C H(1-3) D - | - D - - H has 5 versions, no dependencies - """ - - @classmethod - def setUpClass(cls): - """Set up class variables.""" - super().setUpClass() - cls._build_and_publish_collections() - - @classmethod - def tearDownClass(cls): - """Tear down class variables.""" - cls.repo_api.delete(cls.repo.pulp_href) - cls.distributions_api.delete(cls.distro.pulp_href) - - @classmethod - def _build_and_publish_collections(cls): - """Builds and publishes the collections to be used in this test.""" - from django.conf import settings - - cls.collections = [] - cls.repo, cls.distro = cls._create_empty_repo_and_distribution(cls(), cleanup=False) - - upload_api = PulpAnsibleArtifactsCollectionsV3Api(cls.client) - for cfg in TEST_COLLECTION_CONFIGS: - collection = build_collection("skeleton", config=cfg) - upload_response = upload_api.create(cls.distro.base_path, collection.filename) - api_root = settings.API_ROOT - monitor_task("{}api/v3/tasks/{}/".format(api_root, upload_response.task[-37:-1])) - cls.collections.append(collection) - cls.distro.client_url += "api/" - - def test_simple_one_level_dependency(self): - """Sync test.c which requires test.d.""" - body = gen_ansible_remote( - url=self.distro.client_url, - requirements_file="collections:\n - test.c", - include_pulp_auth=True, - ) - remote = self.remote_collection_api.create(body) - self.addCleanup(self.remote_collection_api.delete, remote.pulp_href) - repo = self._create_repo_and_sync_with_remote(remote) - - content = self.cv_api.list(repository_version=f"{repo.pulp_href}versions/1/") - assert content.count == 2 - - def test_simple_multi_level_dependency(self): - """Sync test.a which should get the dependency chain: test.b -> test.c -> test.d.""" - body = gen_ansible_remote( - url=self.distro.client_url, - requirements_file="collections:\n - test.a", - include_pulp_auth=True, +class TestFullDependenciesSync: + @pytest.fixture(scope="class") + def distribution_with_dependencies( + self, + ansible_repo_factory, + ansible_distribution_factory, + build_and_upload_collection, + ): + """ + Collections with dependencies in a repository. + """ + + # Dependency Graph: + # E A I + # |\ | |\ + # F G B J K + # | \ | \| + # H(1-3) \ C L + # \| + # D + + namespace = randstr() + TEST_COLLECTION_CONFIGS = [ + {"namespace": namespace, "name": "a", "dependencies": {f"{namespace}.b": "*"}}, + {"namespace": namespace, "name": "b", "dependencies": {f"{namespace}.c": "*"}}, + {"namespace": namespace, "name": "c", "dependencies": {f"{namespace}.d": "*"}}, + {"namespace": namespace, "name": "d"}, + { + "namespace": namespace, + "name": "e", + "dependencies": {f"{namespace}.f": "*", f"{namespace}.g": "*"}, + }, + {"namespace": namespace, "name": "f", "dependencies": {f"{namespace}.h": "<=3.0.0"}}, + {"namespace": namespace, "name": "g", "dependencies": {f"{namespace}.d": "*"}}, + {"namespace": namespace, "name": "h", "version": "1.0.0"}, + {"namespace": namespace, "name": "h", "version": "2.0.0"}, + {"namespace": namespace, "name": "h", "version": "3.0.0"}, + { + "namespace": namespace, + "name": "i", + "dependencies": {f"{namespace}.j": "*", f"{namespace}.k": "*"}, + }, + {"namespace": namespace, "name": "j", "dependencies": {f"{namespace}.l": "*"}}, + {"namespace": namespace, "name": "k", "dependencies": {f"{namespace}.l": "*"}}, + {"namespace": namespace, "name": "l"}, + ] + repository = ansible_repo_factory() + distribution = ansible_distribution_factory( + repository=repository, pulp_labels={"namespace": namespace} ) - remote = self.remote_collection_api.create(body) - self.addCleanup(self.remote_collection_api.delete, remote.pulp_href) - - repo = self._create_repo_and_sync_with_remote(remote) - - content = self.cv_api.list(repository_version=f"{repo.pulp_href}versions/1/") - assert content.count == 4 - - def test_complex_one_level_dependency(self): - """Sync test.f which should get 3 versions of test.h.""" - body = gen_ansible_remote( - url=self.distro.client_url, - requirements_file="collections:\n - test.f", + for config in TEST_COLLECTION_CONFIGS: + build_and_upload_collection(repository, config={"namespace": namespace, **config}) + return distribution + + @pytest.mark.parametrize( + "collection_name,expected_count", + [ + pytest.param("d", 1, id="no_dependency"), + pytest.param("c", 2, id="simple_one_level"), + pytest.param("a", 4, id="simple_multi_level"), + pytest.param("f", 4, id="complex_one_level"), + pytest.param("e", 7, id="complex_multi_level"), + pytest.param("i", 4, id="diamond_shaped"), + ], + ) + # Running this test in parallel leads to weird failures with upload. + # @pytest.mark.parallel + def test_dependency_sync( + self, + collection_name, + expected_count, + ansible_bindings, + distribution_with_dependencies, + ansible_collection_remote_factory, + ansible_sync_factory, + ): + namespace = distribution_with_dependencies.pulp_labels["namespace"] + remote = ansible_collection_remote_factory( + url=distribution_with_dependencies.client_url, + requirements_file=f"collections:\n - {namespace}.{collection_name}", include_pulp_auth=True, ) - remote = self.remote_collection_api.create(body) - self.addCleanup(self.remote_collection_api.delete, remote.pulp_href) - - repo = self._create_repo_and_sync_with_remote(remote) + repository = ansible_sync_factory(remote=remote.pulp_href) - content = self.cv_api.list(repository_version=f"{repo.pulp_href}versions/1/") - assert content.count == 4 - - def test_complex_multi_level_dependency(self): - """Sync test.e which should get test.f, test.d, test.g and 3 versions of test.h.""" - body = gen_ansible_remote( - url=self.distro.client_url, - requirements_file="collections:\n - test.e", - include_pulp_auth=True, + content = ansible_bindings.ContentCollectionVersionsApi.list( + repository_version=f"{repository.pulp_href}versions/1/" ) - remote = self.remote_collection_api.create(body) - self.addCleanup(self.remote_collection_api.delete, remote.pulp_href) - - repo = self._create_repo_and_sync_with_remote(remote) - - content = self.cv_api.list(repository_version=f"{repo.pulp_href}versions/1/") - assert content.count == 7 + assert content.count == expected_count @pytest.mark.skip("Skipped until fixture metadata has a published date") @@ -272,6 +250,7 @@ def test_optimized_sync( ansible_bindings, ansible_repo_factory, ansible_collection_remote_factory, + monitor_task, ): # TODO this test is incomplete and may not work remote1 = ansible_collection_remote_factory( @@ -309,6 +288,7 @@ def test_semver_sync( ansible_bindings, ansible_repo_factory, ansible_collection_remote_factory, + monitor_task, ): remote = ansible_collection_remote_factory( url="https://galaxy.ansible.com", @@ -334,6 +314,7 @@ def test_last_synced_metadata_time( ansible_bindings, ansible_repo_factory, ansible_collection_remote_factory, + monitor_task, ): remote1 = ansible_collection_remote_factory( url="https://galaxy.ansible.com", diff --git a/pulp_ansible/tests/functional/api/collection/v3/test_collection.py b/pulp_ansible/tests/functional/api/collection/v3/test_collection.py index 8cbc3e953..83e46108f 100644 --- a/pulp_ansible/tests/functional/api/collection/v3/test_collection.py +++ b/pulp_ansible/tests/functional/api/collection/v3/test_collection.py @@ -2,9 +2,8 @@ import hashlib import logging -import os +import pathlib import re -import shutil from datetime import datetime from urllib.parse import urljoin @@ -20,7 +19,6 @@ ANSIBLE_DISTRIBUTION_PATH, ANSIBLE_REPO_PATH, ) -from orionutils.generator import build_collection logger = logging.getLogger(__name__) @@ -54,25 +52,20 @@ def get_galaxy_url(base, path): return urljoin(GALAXY_API_ROOT, path) -@pytest.fixture(scope="session") -def collection_artifact(): +@pytest.fixture(scope="module") +def collection_artifact(ansible_collection_factory): """Generate a randomized collection for testing.""" - # build_collection will only store one collection, so copy to new location and delete later - artifact = build_collection("skeleton") - artifact.filename = shutil.copy(artifact.filename, "/tmp") - yield artifact - os.remove(artifact.filename) + return ansible_collection_factory() -@pytest.fixture(scope="session") -def collection_artifact2(): +@pytest.fixture(scope="module") +def collection_artifact2(ansible_collection_factory): """ Generate a second randomized collection for testing. This collection will have the same namespace and version, but different name. """ - artifact2 = build_collection("skeleton") - return artifact2 + return ansible_collection_factory() def get_metadata_published(pulp_client, pulp_dist): @@ -91,7 +84,7 @@ def upload_collection(client, filename, base_path): return client.using_handler(upload_handler).post(UPLOAD_PATH, files=collection) -@pytest.fixture(scope="session") +@pytest.fixture(scope="module") def collection_upload(pulp_client, collection_artifact, pulp_dist): """Publish a new collection and return the processed response data.""" published_before_upload = get_metadata_published(pulp_client, pulp_dist) @@ -101,7 +94,7 @@ def collection_upload(pulp_client, collection_artifact, pulp_dist): return response -@pytest.fixture(scope="session") +@pytest.fixture(scope="module") def collection_upload2(pulp_client, collection_artifact2, pulp_dist): """Publish the second new collection and return the processed response data.""" published_before_upload = get_metadata_published(pulp_client, pulp_dist) @@ -111,7 +104,7 @@ def collection_upload2(pulp_client, collection_artifact2, pulp_dist): return response -@pytest.fixture(scope="session") +@pytest.fixture(scope="module") def collection_detail(collection_upload, pulp_client, pulp_dist, collection_artifact): """Fetch and parse a collection details response from an uploaded collection.""" url = get_galaxy_url( @@ -122,7 +115,7 @@ def collection_detail(collection_upload, pulp_client, pulp_dist, collection_arti return response -@pytest.fixture(scope="session") +@pytest.fixture(scope="module") def pulp_client(): """Create and configure a Pulp API client, including custom authentication headers.""" cfg = config.get_config() @@ -133,7 +126,7 @@ def pulp_client(): return client -@pytest.fixture(scope="session") +@pytest.fixture(scope="module") def pulp_repo(pulp_client): """Find or create a Repository to attach to the Ansible Distribution we create.""" repos = pulp_client.get(ANSIBLE_REPO_PATH) @@ -146,7 +139,7 @@ def pulp_repo(pulp_client): pulp_client.delete(repo["pulp_href"]) -@pytest.fixture(scope="session") +@pytest.fixture(scope="module") def pulp_dist(pulp_client, pulp_repo): """Create an Ansible Distribution to simulate the automation hub environment for testing.""" dists = pulp_client.get(ANSIBLE_DISTRIBUTION_PATH + "?base_path=automation-hub") @@ -167,7 +160,7 @@ def pulp_dist(pulp_client, pulp_repo): pulp_client.delete(dist["pulp_href"]) -@pytest.fixture(scope="session") +@pytest.fixture(scope="module") def known_collection(): """Fetch and prepare a known collection from Galaxy to use in an upload test.""" collection_content = http_get(ANSIBLE_COLLECTION_UPLOAD_FIXTURE_URL) @@ -255,7 +248,12 @@ def test_collection_version_list( def test_collection_version_filter_by_q( - ansible_bindings, pulp_client, pulp_dist, monitor_task, delete_orphans_pre + ansible_bindings, + ansible_collection_factory, + pulp_client, + pulp_dist, + monitor_task, + delete_orphans_pre, ): """Verify successive imports do not aggregate tags into search vectors.""" @@ -281,7 +279,7 @@ def publish(new_artifact): "version": "1.0.0", "tags": [spec[0]], } - this_artifact = build_collection("skeleton", config=cfg) + this_artifact = ansible_collection_factory(config=cfg) publish(this_artifact) for spec in specs: @@ -311,7 +309,7 @@ def test_collection_version(collection_artifact, pulp_client, collection_detail) assert version["artifact"]["sha256"] == hashlib.sha256(tarball).hexdigest() assert version["artifact"]["size"] == len(tarball) - assert version["artifact"]["filename"] == collection_artifact.filename.strip("/tmp/") + assert version["artifact"]["filename"] == pathlib.Path(collection_artifact.filename).name assert "updated_at" in version assert "created_at" in version diff --git a/pulp_ansible/tests/functional/api/collection/v3/test_collection_naming_edgecases.py b/pulp_ansible/tests/functional/api/collection/v3/test_collection_naming_edgecases.py index 7dca93625..9a6c1969f 100644 --- a/pulp_ansible/tests/functional/api/collection/v3/test_collection_naming_edgecases.py +++ b/pulp_ansible/tests/functional/api/collection/v3/test_collection_naming_edgecases.py @@ -1,15 +1,12 @@ import hashlib import json -import shutil import subprocess import os import pytest -from orionutils.generator import build_collection -from orionutils.generator import randstr - from pulpcore.client.pulp_ansible.exceptions import ApiException +from pulp_ansible.tests.functional.utils import randstr def busted_collection_build(basedir=None, namespace=None, name=None, version=None): @@ -102,6 +99,7 @@ def test_collection_named_collections( ansible_bindings, tmp_path, ansible_repo, + ansible_collection_factory, ansible_distribution_factory, monitor_task, ): @@ -115,14 +113,9 @@ def test_collection_named_collections( "name": "collections", "version": "1.0.0", } - artifact = build_collection(base="skeleton", config=spec) - - # orionutils creates the tarball inside the site-packages path, - # so we'd like to get it out of there before doing anything further. - artifact_fn = os.path.join(tmp_path, os.path.basename(artifact.filename)) - shutil.move(artifact.filename, artifact_fn) + artifact = ansible_collection_factory(config=spec).filename - body = {"file": artifact_fn} + body = {"file": artifact} body["repository"] = ansible_repo.pulp_href response = ansible_bindings.ContentCollectionVersionsApi.create(**body) monitor_task(response.task) diff --git a/pulp_ansible/tests/functional/api/collection/v3/test_collection_version_search.py b/pulp_ansible/tests/functional/api/collection/v3/test_collection_version_search.py index 18685872b..9fbd6dcd0 100644 --- a/pulp_ansible/tests/functional/api/collection/v3/test_collection_version_search.py +++ b/pulp_ansible/tests/functional/api/collection/v3/test_collection_version_search.py @@ -1,7 +1,6 @@ import copy import pickle import logging -import shutil import os import random import uuid @@ -9,14 +8,12 @@ import pytest -from orionutils.generator import build_collection -from orionutils.generator import randstr - from pulp_smash.pulp3.bindings import delete_orphans from pulp_ansible.tests.functional.utils import ( gen_distribution, gen_remote, gen_repo, + randstr, ) @@ -491,6 +488,7 @@ def create_repos_and_dists( @pytest.fixture() def search_specs( ansible_bindings, + ansible_collection_factory, tmp_path, signing_gpg_homedir_path, ascii_armored_detached_signing_service, @@ -548,19 +546,17 @@ def search_specs( # upload to the repo ... build_spec = copy.deepcopy(spec) build_spec["repository"] = "https://github.com/foo/bar" - artifact = build_collection(base="skeleton", config=build_spec) - new_fn = os.path.join(artifact_cache, os.path.basename(artifact.filename)) - shutil.move(artifact.filename, new_fn) - specs[ids]["artifact"] = new_fn + cv_fn = ansible_collection_factory(config=build_spec).filename + specs[ids]["artifact"] = cv_fn body = { - "file": new_fn, + "file": cv_fn, "repository": created_repos[spec["repository_name"]]["pulp_href"], } res = monitor_task( ansible_bindings.ContentCollectionVersionsApi.create(**body).task ) assert res.state == "completed" - uploaded_artifacts[ckey] = new_fn + uploaded_artifacts[ckey] = cv_fn else: # copy to the repo ... diff --git a/pulp_ansible/tests/functional/api/collection/v3/test_content_guard.py b/pulp_ansible/tests/functional/api/collection/v3/test_content_guard.py index f4564d4dd..dd1a378cb 100644 --- a/pulp_ansible/tests/functional/api/collection/v3/test_content_guard.py +++ b/pulp_ansible/tests/functional/api/collection/v3/test_content_guard.py @@ -1,18 +1,14 @@ import requests -from pulpcore.client.pulp_ansible import ( - PulpAnsibleDefaultApiV3PluginAnsibleContentCollectionsIndexApi, - PulpAnsibleDefaultApiV3PluginAnsibleContentCollectionsIndexVersionsApi, -) - - # Using requests for these tests because the client follows redirects and I need to inspect the URL # that gets redirected to -def _get_download_url(client, distribution, namespace, name): - collection_api = PulpAnsibleDefaultApiV3PluginAnsibleContentCollectionsIndexApi(client) - versions_api = PulpAnsibleDefaultApiV3PluginAnsibleContentCollectionsIndexVersionsApi(client) +def _get_download_url(ansible_bindings, distribution, namespace, name): + collection_api = ansible_bindings.PulpAnsibleDefaultApiV3PluginAnsibleContentCollectionsIndexApi + versions_api = ( + ansible_bindings.PulpAnsibleDefaultApiV3PluginAnsibleContentCollectionsIndexVersionsApi + ) collection = collection_api.read( distribution.base_path, @@ -43,7 +39,7 @@ def test_download( # Fetch content download_url = _get_download_url( - ansible_bindings.client, distribution, collection.namespace, collection.name + ansible_bindings, distribution, collection.namespace, collection.name ) pulp_auth = (bindings_cfg.username, bindings_cfg.password) response = requests.get(download_url, auth=pulp_auth, allow_redirects=False) @@ -81,7 +77,7 @@ def test_download_with_content_guard( # Fetch content download_url = _get_download_url( - ansible_bindings.client, distribution, collection.namespace, collection.name + ansible_bindings, distribution, collection.namespace, collection.name ) pulp_auth = (bindings_cfg.username, bindings_cfg.password) response = requests.get(download_url, auth=pulp_auth, allow_redirects=False) diff --git a/pulp_ansible/tests/functional/api/collection/v3/test_deletion.py b/pulp_ansible/tests/functional/api/collection/v3/test_deletion.py index fc6654df6..c51d31eef 100644 --- a/pulp_ansible/tests/functional/api/collection/v3/test_deletion.py +++ b/pulp_ansible/tests/functional/api/collection/v3/test_deletion.py @@ -1,9 +1,8 @@ import pytest from pulpcore.client.pulp_ansible.exceptions import ApiException -from orionutils.generator import randstr -from pulp_ansible.tests.functional.utils import content_counts +from pulp_ansible.tests.functional.utils import content_counts, randstr def test_collection_deletion( diff --git a/pulp_ansible/tests/functional/api/test_export_import.py b/pulp_ansible/tests/functional/api/test_export_import.py index 29b3610bb..286bc6c3a 100644 --- a/pulp_ansible/tests/functional/api/test_export_import.py +++ b/pulp_ansible/tests/functional/api/test_export_import.py @@ -12,8 +12,11 @@ from pulp_ansible.tests.functional.constants import ANSIBLE_FIXTURE_URL +# As long as we cannot run this test in domains, it must not run as parallel. +# Orphan cleanup is unsafe in any scenario here. + + @pytest.mark.parametrize("cleanup", [True, False]) -@pytest.mark.parallel def test_export_then_import( pulpcore_bindings, ansible_bindings, diff --git a/pulp_ansible/tests/functional/constants.py b/pulp_ansible/tests/functional/constants.py index 2322fee9f..70a9d2be5 100644 --- a/pulp_ansible/tests/functional/constants.py +++ b/pulp_ansible/tests/functional/constants.py @@ -50,18 +50,3 @@ ANSIBLE_COLLECTION_UPLOAD_FIXTURE_URL = urljoin( GALAXY_ANSIBLE_BASE_URL, f"download/{ANSIBLE_COLLECTION_FILE_NAME}" ) - -TEST_COLLECTION_CONFIGS = [ - {"name": "a", "namespace": "test", "dependencies": {"test.b": "*"}}, - {"name": "b", "namespace": "test", "dependencies": {"test.c": "*"}}, - {"name": "c", "namespace": "test", "dependencies": {"test.d": "*"}}, - {"name": "d", "namespace": "test"}, - {"name": "e", "namespace": "test", "dependencies": {"test.f": "*", "test.g": "*"}}, - {"name": "f", "namespace": "test", "dependencies": {"test.h": "<=3.0.0"}}, - {"name": "g", "namespace": "test", "dependencies": {"test.d": "*"}}, - {"name": "h", "namespace": "test", "version": "1.0.0"}, - {"name": "h", "namespace": "test", "version": "2.0.0"}, - {"name": "h", "namespace": "test", "version": "3.0.0"}, - {"name": "h", "namespace": "test", "version": "4.0.0"}, - {"name": "h", "namespace": "test", "version": "5.0.0"}, -] diff --git a/pulp_ansible/tests/functional/utils.py b/pulp_ansible/tests/functional/utils.py index cae028371..63003617c 100644 --- a/pulp_ansible/tests/functional/utils.py +++ b/pulp_ansible/tests/functional/utils.py @@ -1,5 +1,7 @@ """Utilities for tests for the ansible plugin.""" +import random +import string from urllib.parse import urlparse, parse_qs from pulp_smash import config @@ -38,6 +40,10 @@ configuration = cfg.get_bindings_config() +def randstr(): + return "".join(random.choices(string.ascii_lowercase, k=8)) + + def is_galaxy_ng_installed(): """Returns whether or not the galaxy_ng plugin is installed.""" configuration = cfg.get_bindings_config() @@ -176,21 +182,6 @@ def _create_distribution_from_repo(self, repo, cleanup=True): self.addCleanup(self.distributions_api.delete, distribution.pulp_href) return distribution - def _create_empty_repo_and_distribution(self, cleanup=True): - """ - Creates an empty `AnsibleRepository` and an `AnsibleDistribution` serving that repository. - - Args: - cleanup: Whether the repository and distribution should be cleaned up - - Returns: - Tuple of the created `AnsibleRepository`, `AnsibleDistribution` - """ - repo = self.repo_api.create(gen_repo()) - if cleanup: - self.addCleanup(self.repo_api.delete, repo.pulp_href) - return repo, self._create_distribution_from_repo(repo, cleanup=cleanup) - def iterate_all(list_func, **kwargs): """ diff --git a/pulp_ansible/tests/unit/test_cv_index.py b/pulp_ansible/tests/unit/test_cv_index.py index 24ffdb489..b25ead910 100644 --- a/pulp_ansible/tests/unit/test_cv_index.py +++ b/pulp_ansible/tests/unit/test_cv_index.py @@ -1,13 +1,11 @@ from django.test import TestCase -from orionutils.generator import randstr - from pulp_ansible.app.models import AnsibleDistribution from pulp_ansible.app.models import AnsibleRepository from pulp_ansible.app.models import CollectionVersion from pulp_ansible.app.models import CrossRepositoryCollectionVersionIndex as CVIndex -from .utils import build_cvs_from_specs +from .utils import build_cvs_from_specs, randstr class TestCollectionVersionIndex(TestCase): diff --git a/pulp_ansible/tests/unit/utils.py b/pulp_ansible/tests/unit/utils.py index e7cce1577..ffbdb2be2 100644 --- a/pulp_ansible/tests/unit/utils.py +++ b/pulp_ansible/tests/unit/utils.py @@ -1,7 +1,9 @@ import contextlib import hashlib import os +import random import shutil +import string import subprocess import tempfile import yaml @@ -14,31 +16,36 @@ from pulpcore.plugin.models import ContentArtifact +def randstr(): + return "".join(random.choices(string.ascii_lowercase, k=8)) + + @contextlib.contextmanager def make_cv_tarball(namespace, name, version): """Create a collection version from scratch.""" - tdir = tempfile.mkdtemp() - subprocess.run(f"ansible-galaxy collection init {namespace}.{name}", shell=True, cwd=tdir) - os.makedirs(os.path.join(tdir, namespace, name, "meta"), exist_ok=True) - with open(os.path.join(tdir, namespace, name, "meta", "runtime.yml"), "w") as f: + tmpdir = tempfile.mkdtemp() + subprocess.run(f"ansible-galaxy collection init {namespace}.{name}", shell=True, cwd=tmpdir) + os.makedirs(os.path.join(tmpdir, namespace, name, "meta"), exist_ok=True) + with open(os.path.join(tmpdir, namespace, name, "meta", "runtime.yml"), "w") as f: f.write('requires_ansible: ">=2.13"\n') - with open(os.path.join(tdir, namespace, name, "README.md"), "w") as f: + with open(os.path.join(tmpdir, namespace, name, "README.md"), "w") as f: f.write("# title\ncollection docs\n") if version is not None: - with open(os.path.join(tdir, namespace, name, "galaxy.yml"), "r") as f: + with open(os.path.join(tmpdir, namespace, name, "galaxy.yml"), "r") as f: gdata = yaml.safe_load(f.read()) gdata["version"] = version - with open(os.path.join(tdir, namespace, name, "galaxy.yml"), "w") as f: + with open(os.path.join(tmpdir, namespace, name, "galaxy.yml"), "w") as f: f.write(yaml.safe_dump(gdata)) build_pid = subprocess.run( "ansible-galaxy collection build .", shell=True, - cwd=os.path.join(tdir, namespace, name), + cwd=os.path.join(tmpdir, namespace, name), stdout=subprocess.PIPE, + check=True, ) tarfn = build_pid.stdout.decode("utf-8").strip().split()[-1] yield tarfn - shutil.rmtree(tdir) + shutil.rmtree(tmpdir) def build_cvs_from_specs(specs, build_artifacts=True): diff --git a/unittest_requirements.txt b/unittest_requirements.txt index 09e821c0c..ba8e83803 100644 --- a/unittest_requirements.txt +++ b/unittest_requirements.txt @@ -1,5 +1,4 @@ mock pulp-smash @ git+https://github.com/pulp/pulp-smash.git pytest-django -orionutils pytest<8