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 c44303c52..7e6f31eba 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 @@ -1,8 +1,6 @@ """Tests related to sync ansible plugin collection content type.""" from tempfile import NamedTemporaryFile -from pulp_smash import api, config from pulp_smash.pulp3.bindings import PulpTestCase, delete_orphans, monitor_task -from pulp_smash.pulp3.utils import gen_repo, sync from pulp_smash.utils import http_get from pulpcore.client.pulp_ansible import ContentCollectionVersionsApi @@ -10,66 +8,54 @@ from pulp_ansible.tests.functional.constants import ( - ANSIBLE_COLLECTION_REMOTE_PATH, - ANSIBLE_COLLECTION_VERSION_CONTENT_PATH, ANSIBLE_DEMO_COLLECTION_REQUIREMENTS as DEMO_REQUIREMENTS, - ANSIBLE_REPO_PATH, GALAXY_ANSIBLE_BASE_URL, ) -from pulp_ansible.tests.functional.utils import gen_ansible_client, gen_ansible_remote, skip_if +from pulp_ansible.tests.functional.utils import gen_ansible_client, skip_if from pulp_ansible.tests.functional.utils import set_up_module as setUpModule # noqa:F401 -class ListContentVersionsCase(PulpTestCase): - """Test listing CollectionVersions.""" +def test_tags_filter( + ansible_collection_version_api_client, + ansible_collection_remote_factory, + ansible_sync_factory, +): + """Filter CollectionVersions by using the tags filter. - @classmethod - def setUpClass(cls): - """Create class-wide variables.""" - cls.cfg = config.get_config() - cls.client = api.Client(cls.cfg) - - def test_tags_filter(self): - """Filter CollectionVersions by using the tags filter. - - This test targets the following issue: - - * `Pulp #5571 `_ - - Do the following: + This test targets the following issue: - 1. Create a repository, and a remote. - 2. Sync the remote. - 3. Attempt to filter the CollectionVersions by different tags + * `Pulp #5571 `_ - Note that the testing.k8s_demo_collection collection has tags 'k8s' and 'kubernetes'. - """ - repo = self.client.post(ANSIBLE_REPO_PATH, gen_repo()) - self.addCleanup(self.client.delete, repo["pulp_href"]) - - body = gen_ansible_remote(url=GALAXY_ANSIBLE_BASE_URL, requirements_file=DEMO_REQUIREMENTS) - remote = self.client.post(ANSIBLE_COLLECTION_REMOTE_PATH, body) - self.addCleanup(self.client.delete, remote["pulp_href"]) - - # Sync the repository. - sync(self.cfg, remote, repo) + Do the following: - # filter collection versions by tags - params = {"tags": "nada"} - collections = self.client.get(ANSIBLE_COLLECTION_VERSION_CONTENT_PATH, params=params) - self.assertEqual(len(collections), 0, collections) + 1. Create a repository, and a remote. + 2. Sync the remote. + 3. Attempt to filter the CollectionVersions by different tags - params = {"tags": "k8s"} - collections = self.client.get(ANSIBLE_COLLECTION_VERSION_CONTENT_PATH, params=params) - self.assertEqual(len(collections), 1, collections) - - params = {"tags": "k8s,kubernetes"} - collections = self.client.get(ANSIBLE_COLLECTION_VERSION_CONTENT_PATH, params=params) - self.assertEqual(len(collections), 1, collections) - - params = {"tags": "nada,k8s"} - collections = self.client.get(ANSIBLE_COLLECTION_VERSION_CONTENT_PATH, params=params) - self.assertEqual(len(collections), 0, collections) + Note that the testing.k8s_demo_collection collection has tags 'k8s' and 'kubernetes'. + """ + ansible_sync_factory( + remote=ansible_collection_remote_factory( + url=GALAXY_ANSIBLE_BASE_URL, requirements_file=DEMO_REQUIREMENTS + ).pulp_href + ) + + # filter collection versions by tags + params = {"tags": "nada"} + collections = ansible_collection_version_api_client.list(**params).results + assert len(collections) == 0, collections + + params = {"tags": "k8s"} + collections = ansible_collection_version_api_client.list(**params).results + assert len(collections) == 1, collections + + params = {"tags": "k8s,kubernetes"} + collections = ansible_collection_version_api_client.list(**params).results + assert len(collections) == 1, collections + + params = {"tags": "nada,k8s"} + collections = ansible_collection_version_api_client.list(**params).results + assert len(collections) == 0, collections class ContentUnitTestCase(PulpTestCase): diff --git a/pulp_ansible/tests/functional/api/collection/test_signatures.py b/pulp_ansible/tests/functional/api/collection/test_signatures.py index 045c76f4f..bb279ccda 100644 --- a/pulp_ansible/tests/functional/api/collection/test_signatures.py +++ b/pulp_ansible/tests/functional/api/collection/test_signatures.py @@ -3,7 +3,7 @@ import pytest -from pulp_smash.pulp3.bindings import monitor_task, PulpTaskError +from pulpcore.tests.functional.utils import PulpTaskError from pulp_smash.pulp3.utils import get_content_summary from pulp_ansible.tests.functional.utils import ( @@ -16,13 +16,13 @@ def test_upload_then_sign_then_try_to_upload_duplicate_signature( build_and_upload_collection, - ansible_collections_api_client, ansible_repo_api_client, ansible_repo, ascii_armored_detached_signing_service, tmp_path, sign_with_ascii_armored_detached_signing_service, ansible_collection_signatures_client, + monitor_task, ): """Test server signing of collections, and that a duplicate signature can't be uploaded.""" collection, collection_url = build_and_upload_collection() @@ -70,11 +70,11 @@ def test_sign_locally_then_upload_signature( build_and_upload_collection, sign_with_ascii_armored_detached_signing_service, tmp_path, - ansible_collections_api_client, ansible_collection_signatures_client, ansible_repo_factory, pulp_trusted_public_key_fingerprint, pulp_trusted_public_key, + monitor_task, ): """Test uploading a locally produced Collection Signature.""" repository = ansible_repo_factory(gpgkey=pulp_trusted_public_key) @@ -144,6 +144,7 @@ def distro_serving_one_signed_one_unsigned_collection( ansible_repo, ansible_repo_api_client, ansible_distro_api_client, + monitor_task, ): """Create a distro serving two collections, one signed, one unsigned.""" collections = [] @@ -171,6 +172,7 @@ def test_sync_signatures( ansible_remote_collection_api_client, gen_object_with_cleanup, ansible_repo_api_client, + monitor_task, ): """Test that signatures are also synced.""" distro = distro_serving_one_signed_one_unsigned_collection @@ -193,18 +195,19 @@ def test_sync_signatures( def test_sync_signatures_only( ansible_repo_factory, + ansible_collection_remote_factory, distro_serving_one_signed_one_unsigned_collection, - ansible_remote_collection_api_client, ansible_repo_api_client, - gen_object_with_cleanup, + monitor_task, ): """Test that only collections with a signatures are synced when specified.""" distro = distro_serving_one_signed_one_unsigned_collection new_repo = ansible_repo_factory() # Create Remote - body = gen_ansible_remote(distro.client_url, signed_only=True, include_pulp_auth=True) - remote = gen_object_with_cleanup(ansible_remote_collection_api_client, body) + remote = ansible_collection_remote_factory( + url=distro.client_url, signed_only=True, include_pulp_auth=True + ) # Sync repository_sync_data = AnsibleRepositorySyncURL(remote=remote.pulp_href) diff --git a/pulp_ansible/tests/functional/api/collection/v3/test_deprecation.py b/pulp_ansible/tests/functional/api/collection/v3/test_deprecation.py index 04178edb9..e225b15f0 100644 --- a/pulp_ansible/tests/functional/api/collection/v3/test_deprecation.py +++ b/pulp_ansible/tests/functional/api/collection/v3/test_deprecation.py @@ -5,7 +5,6 @@ @pytest.mark.parallel @pytest.mark.parametrize("repo_kwargs", [{}, {"retain_repo_versions": 1}]) def test_deprecation( - bindings_cfg, ansible_remote_collection_api_client, galaxy_v3_collections_api_client, ansible_collection_remote_factory, @@ -49,8 +48,7 @@ def test_deprecation( url=first_distribution.client_url, requirements_file=requirements, sync_dependencies=False, - username=bindings_cfg.username, - password=bindings_cfg.password, + include_pulp_auth=True, ) second_repo = ansible_sync_factory( diff --git a/pulp_ansible/tests/functional/api/git/test_sync.py b/pulp_ansible/tests/functional/api/git/test_sync.py index da10b8749..56fdd4744 100644 --- a/pulp_ansible/tests/functional/api/git/test_sync.py +++ b/pulp_ansible/tests/functional/api/git/test_sync.py @@ -2,8 +2,6 @@ import pytest import subprocess -from os import path - @pytest.fixture def sync_and_count( @@ -30,20 +28,17 @@ def _sync_and_count(remote_body, repo=None, **sync_kwargs): @pytest.mark.parallel def test_sync_collection_from_git( - bindings_cfg, sync_and_count, ansible_repo_factory, ansible_distribution_factory, gen_user, gen_object_with_cleanup, ansible_dir_factory, - pulp_admin_user, ): """Sync collections from Git repositories and then install one of them.""" body = { "url": "https://github.com/pulp/pulp_installer.git", - "username": bindings_cfg.username, - "password": bindings_cfg.password, + "include_pulp_auth": True, } repo = ansible_repo_factory() @@ -62,20 +57,29 @@ def test_sync_collection_from_git( collection_name = "pulp.squeezer" - temp_dir = ansible_dir_factory(distribution.client_url, pulp_admin_user) - with temp_dir: - # The install command needs --pre so a pre-release collection versions install - cmd = "ansible-galaxy collection install --pre {} -c -p {}".format( - collection_name, temp_dir - ) - - directory = "{}/ansible_collections/{}".format(temp_dir, collection_name.replace(".", "/")) - - assert not path.exists(directory), "Directory {} already exists".format(directory) - - subprocess.run(cmd.split(), cwd=temp_dir) + ansible_dir = ansible_dir_factory(distribution.client_url) + + directory = ansible_dir / "ansible_collections" / collection_name.replace(".", "/") + + assert not directory.exists(), f"Directory {directory} already exists" + + # The install command needs --pre so a pre-release collection versions install + subprocess.run( + ( + "ansible-galaxy", + "collection", + "install", + "--pre", + collection_name, + "-c", + "-p", + ansible_dir, + ), + cwd=ansible_dir, + check=True, + ) - assert path.exists(directory), "Could not find directory {}".format(directory) + assert directory.exists(), f"Could not find directory {directory}" @pytest.mark.parallel @@ -137,7 +141,6 @@ def test_sync_collection_from_git_commit_sha( @pytest.mark.parallel def test_sync_metadata_only_collection_from_pulp( - bindings_cfg, ansible_sync_factory, ansible_repo_factory, ansible_distribution_factory, @@ -161,8 +164,7 @@ def test_sync_metadata_only_collection_from_pulp( url="https://github.com/ansible-collections/amazon.aws/", metadata_only=True, git_ref="2.1.0", - username=bindings_cfg.username, - password=bindings_cfg.password, + include_pulp_auth=True, ) first_repo = ansible_repo_factory() @@ -181,8 +183,7 @@ def test_sync_metadata_only_collection_from_pulp( url=distribution.client_url, requirements_file=requirements_file, sync_dependencies=False, - username=bindings_cfg.username, - password=bindings_cfg.password, + include_pulp_auth=True, ) second_repo = ansible_repo_factory(remote=second_remote.pulp_href) diff --git a/pulp_ansible/tests/functional/cli/test_collection_download_count.py b/pulp_ansible/tests/functional/cli/test_collection_download_count.py index a5aa8b38c..314bcac7c 100644 --- a/pulp_ansible/tests/functional/cli/test_collection_download_count.py +++ b/pulp_ansible/tests/functional/cli/test_collection_download_count.py @@ -49,64 +49,63 @@ def test_collection_download_count( ansible_dir_factory, galaxy_v3_collections_api_client, install_scenario_distribution, - pulp_admin_user, pulp_settings, ): """Test that the collection download counting functions.""" if not pulp_settings.ANSIBLE_COLLECT_DOWNLOAD_COUNT: pytest.skip("ANSIBLE_COLLECT_DOWNLOAD_COUNT not enabled") - with pulp_admin_user: - collection_namespace = ANSIBLE_DEMO_COLLECTION.split(".")[0] - collection_name = ANSIBLE_DEMO_COLLECTION.split(".")[1] - download_count = get_current_download_count( - api_client=galaxy_v3_collections_api_client, - path=install_scenario_distribution.base_path, - namespace=collection_namespace, - name=collection_name, - ) - - temp_dir = ansible_dir_factory(install_scenario_distribution.client_url, pulp_admin_user) - - cmd = [ - "ansible-galaxy", - "collection", - "install", - f"{ANSIBLE_DEMO_COLLECTION}:{ANSIBLE_DEMO_COLLECTION_VERSION}", - "-c", - "-p", - temp_dir, - ] - - directory = temp_dir / "ansible_collections" / ANSIBLE_DEMO_COLLECTION.replace(".", "/") - - assert not directory.exists(), "Directory {} already exists".format(directory) - subprocess.run(cmd, cwd=temp_dir) - assert directory.exists(), "Could not find directory {}".format(directory) - - assert (download_count + 1) == get_current_download_count( - api_client=galaxy_v3_collections_api_client, - path=install_scenario_distribution.base_path, - namespace=collection_namespace, - name=collection_name, - ) - - # TODO update to use a second collection version when available - cmd = [ - "ansible-galaxy", - "collection", - "install", - f"{ANSIBLE_DEMO_COLLECTION}:{ANSIBLE_DEMO_COLLECTION_VERSION}", - "-c", - "-f", - "-p", - temp_dir, - ] - - subprocess.run(cmd, cwd=temp_dir) - - assert (download_count + 2) == get_current_download_count( - api_client=galaxy_v3_collections_api_client, - path=install_scenario_distribution.base_path, - namespace=collection_namespace, - name=collection_name, - ) + + collection_namespace = ANSIBLE_DEMO_COLLECTION.split(".")[0] + collection_name = ANSIBLE_DEMO_COLLECTION.split(".")[1] + download_count = get_current_download_count( + api_client=galaxy_v3_collections_api_client, + path=install_scenario_distribution.base_path, + namespace=collection_namespace, + name=collection_name, + ) + + temp_dir = ansible_dir_factory(install_scenario_distribution.client_url) + + cmd = [ + "ansible-galaxy", + "collection", + "install", + f"{ANSIBLE_DEMO_COLLECTION}:{ANSIBLE_DEMO_COLLECTION_VERSION}", + "-c", + "-p", + temp_dir, + ] + + directory = temp_dir / "ansible_collections" / ANSIBLE_DEMO_COLLECTION.replace(".", "/") + + assert not directory.exists(), "Directory {} already exists".format(directory) + subprocess.run(cmd, cwd=temp_dir) + assert directory.exists(), "Could not find directory {}".format(directory) + + assert (download_count + 1) == get_current_download_count( + api_client=galaxy_v3_collections_api_client, + path=install_scenario_distribution.base_path, + namespace=collection_namespace, + name=collection_name, + ) + + # TODO update to use a second collection version when available + cmd = [ + "ansible-galaxy", + "collection", + "install", + f"{ANSIBLE_DEMO_COLLECTION}:{ANSIBLE_DEMO_COLLECTION_VERSION}", + "-c", + "-f", + "-p", + temp_dir, + ] + + subprocess.run(cmd, cwd=temp_dir) + + assert (download_count + 2) == get_current_download_count( + api_client=galaxy_v3_collections_api_client, + path=install_scenario_distribution.base_path, + namespace=collection_namespace, + name=collection_name, + ) diff --git a/pulp_ansible/tests/functional/cli/test_collection_install.py b/pulp_ansible/tests/functional/cli/test_collection_install.py index 7b9123715..52b2321e3 100644 --- a/pulp_ansible/tests/functional/cli/test_collection_install.py +++ b/pulp_ansible/tests/functional/cli/test_collection_install.py @@ -4,8 +4,6 @@ import subprocess import pytest -from pulpcore.client.pulp_ansible import AnsibleRepositorySyncURL - from pulp_ansible.tests.functional.constants import ( ANSIBLE_DEMO_COLLECTION, ANSIBLE_DEMO_COLLECTION_VERSION, @@ -16,43 +14,34 @@ @pytest.fixture def install_scenario_distribution( - delete_orphans_pre, - ansible_repo_api_client, - ansible_repo_factory, + ansible_sync_factory, ansible_collection_remote_factory, ansible_distribution_factory, - monitor_task, ): """Prepare a distribution to install from.""" remote = ansible_collection_remote_factory( url=GALAXY_ANSIBLE_BASE_URL, requirements_file=DEMO_REQUIREMENTS ) - repo = ansible_repo_factory(remote=remote.pulp_href) - - # Sync - repository_sync_data = AnsibleRepositorySyncURL() - sync_response = ansible_repo_api_client.sync(repo.pulp_href, repository_sync_data) - monitor_task(sync_response.task) - - # Distribute + repo = ansible_sync_factory(remote=remote.pulp_href) return ansible_distribution_factory(repo) -def test_install_collection( - install_scenario_distribution, pulp_admin_user, ansible_dir_factory, pulp_settings -): +# Do not mark this test parallel, because we need to assert that the last dl log is ours. +def test_install_collection(install_scenario_distribution, ansible_dir_factory, pulp_settings): """Test that the collection can be installed from Pulp.""" if not pulp_settings.ANSIBLE_COLLECT_DOWNLOAD_LOG: pytest.skip("ANSIBLE_COLLECT_DOWNLOAD_LOG not enabled") - with pulp_admin_user: - collection_name = ANSIBLE_DEMO_COLLECTION - collection_version = ANSIBLE_DEMO_COLLECTION_VERSION - temp_dir = str( - ansible_dir_factory(install_scenario_distribution.client_url, pulp_admin_user) - ) + collection_name = ANSIBLE_DEMO_COLLECTION + collection_version = ANSIBLE_DEMO_COLLECTION_VERSION + + temp_dir = str(ansible_dir_factory(install_scenario_distribution.client_url)) - cmd = [ + directory = "{}/ansible_collections/{}".format(temp_dir, collection_name.replace(".", "/")) + + assert not path.exists(directory), "Directory {} already exists".format(directory) + subprocess.run( + ( "ansible-galaxy", "collection", "install", @@ -60,13 +49,12 @@ def test_install_collection( "-c", "-p", temp_dir, - ] - - directory = "{}/ansible_collections/{}".format(temp_dir, collection_name.replace(".", "/")) + ), + cwd=temp_dir, + check=True, + ) - assert not path.exists(directory), "Directory {} already exists".format(directory) - subprocess.run(cmd, cwd=temp_dir) - assert path.exists(directory), "Could not find directory {}".format(directory) + assert path.exists(directory), "Could not find directory {}".format(directory) dl_log_dump = subprocess.check_output(["pulpcore-manager", "download-log"]) dl_log = json.loads(dl_log_dump) assert ( @@ -75,6 +63,7 @@ def test_install_collection( assert dl_log[-1]["user"] == "admin" +@pytest.mark.parallel def test_install_signed_collection( ansible_repo_api_client, install_scenario_distribution, @@ -94,22 +83,24 @@ def test_install_signed_collection( signing_body = {"signing_service": signing_service.pulp_href, "content_units": ["*"]} monitor_task(ansible_repo_api_client.sign(repository_href, signing_body).task) - temp_dir = str(ansible_dir_factory(install_scenario_distribution.client_url, user)) + ansible_dir = ansible_dir_factory(install_scenario_distribution.client_url) - cmd = [ - "ansible-galaxy", - "collection", - "install", - collection_name, - "-c", - "-p", - temp_dir, - "--keyring", - f"{signing_gpg_homedir_path}/pubring.kbx", - ] - - directory = "{}/ansible_collections/{}".format(temp_dir, collection_name.replace(".", "/")) + directory = ansible_dir / "ansible_collections" / collection_name.replace(".", "/") assert not path.exists(directory), "Directory {} already exists".format(directory) - subprocess.run(cmd, cwd=temp_dir) + subprocess.run( + ( + "ansible-galaxy", + "collection", + "install", + collection_name, + "-c", + "-p", + ansible_dir, + "--keyring", + f"{signing_gpg_homedir_path}/pubring.kbx", + ), + cwd=ansible_dir, + check=True, + ) assert path.exists(directory), "Could not find directory {}".format(directory) diff --git a/pulp_ansible/tests/functional/cli/test_collection_upload.py b/pulp_ansible/tests/functional/cli/test_collection_upload.py index e58850dd9..57773ecf7 100644 --- a/pulp_ansible/tests/functional/cli/test_collection_upload.py +++ b/pulp_ansible/tests/functional/cli/test_collection_upload.py @@ -1,153 +1,80 @@ """Tests that Collections can be uploaded to Pulp with the ansible-galaxy CLI.""" -import requests -import os +import pytest import random import string import subprocess -import tempfile -from pulpcore.client.pulp_ansible import ( - DistributionsAnsibleApi, - PulpAnsibleApiV3CollectionsApi, - PulpAnsibleApiV3CollectionsVersionsApi, - RemotesCollectionApi, - RepositoriesAnsibleApi, - RepositoriesAnsibleVersionsApi, -) -from pulp_smash.pulp3.bindings import delete_orphans, monitor_task, PulpTestCase -from pulp_smash.pulp3.utils import gen_distribution, gen_repo -from pulp_ansible.tests.functional.utils import gen_ansible_client, wait_tasks -from pulp_ansible.tests.functional.utils import set_up_module as setUpModule # noqa:F401 - - -class InstallCollectionTestCase(PulpTestCase): +@pytest.mark.parallel +def test_upload_collection( + ansible_repo_api_client, + galaxy_v3_collections_api_client, + galaxy_v3_collection_versions_api_client, + ansible_repo_factory, + ansible_distribution_factory, + ansible_dir_factory, + wait_tasks, +): """Test whether ansible-galaxy can upload a Collection to Pulp.""" - - @classmethod - def setUpClass(cls): - """Create class-wide variables.""" - delete_orphans() - cls.client = gen_ansible_client() - cls.repo_api = RepositoriesAnsibleApi(cls.client) - cls.repo_versions_api = RepositoriesAnsibleVersionsApi(cls.client) - cls.remote_collection_api = RemotesCollectionApi(cls.client) - cls.distributions_api = DistributionsAnsibleApi(cls.client) - cls.collections_v3api = PulpAnsibleApiV3CollectionsApi(cls.client) - cls.collections_versions_v3api = PulpAnsibleApiV3CollectionsVersionsApi(cls.client) - - def test_upload_collection(self): - """Test whether ansible-galaxy can upload a Collection to Pulp.""" - repo = self.repo_api.create(gen_repo()) - self.addCleanup(self.repo_api.delete, repo.pulp_href) - - # Create a distribution. - body = gen_distribution() - body["repository"] = repo.pulp_href - distribution_create = self.distributions_api.create(body) - created_resources = monitor_task(distribution_create.task).created_resources - distribution = self.distributions_api.read(created_resources[0]) - - self.addCleanup(self.distributions_api.delete, distribution.pulp_href) - - with tempfile.TemporaryDirectory() as temp_dir: - collection_name = "".join([random.choice(string.ascii_lowercase) for i in range(26)]) - subprocess.run( - ( - "ansible-galaxy", - "collection", - "init", - "--init-path", - temp_dir, - f"pulp.{collection_name}", - ) - ) - - collection_meta = os.path.join(temp_dir, f"pulp/{collection_name}/meta") - os.mkdir(collection_meta) - with open(os.path.join(collection_meta, "runtime.yml"), "w") as runtime: - runtime.write('requires_ansible: ">=2.9"') - - subprocess.run( - ( - "ansible-galaxy", - "collection", - "build", - "--output-path", - temp_dir, - f"{temp_dir}/pulp/{collection_name}/", - ) - ) - - repo_version = self.repo_versions_api.read(repo.latest_version_href) - self.assertEqual(repo_version.number, 0) # We uploaded 1 collection - - subprocess.run( - ( - "ansible-galaxy", - "collection", - "publish", - "-c", - "-s", - distribution.client_url, - f"{temp_dir}/pulp-{collection_name}-1.0.0.tar.gz", - ) - ) - wait_tasks() - - repo = self.repo_api.read(repo.pulp_href) - repo_version = self.repo_versions_api.read(repo.latest_version_href) - self.assertEqual(repo_version.number, 1) # We uploaded 1 collection - - def test_upload_collection_with_requires_ansible(self): - """Test whether ansible-galaxy can upload a Collection to Pulp.""" - delete_orphans() - repo = self.repo_api.create(gen_repo()) - self.addCleanup(self.repo_api.delete, repo.pulp_href) - - # Create a distribution. - body = gen_distribution() - body["repository"] = repo.pulp_href - distribution_create = self.distributions_api.create(body) - created_resources = monitor_task(distribution_create.task).created_resources - distribution = self.distributions_api.read(created_resources[0]) - - self.addCleanup(self.distributions_api.delete, distribution.pulp_href) - - collections = self.collections_v3api.list(distribution.base_path) - self.assertEqual(collections.meta.count, 0) - - with tempfile.TemporaryDirectory() as temp_dir: - dl_collection = requests.get( - "https://galaxy.ansible.com/download/pulp-squeezer-0.0.9.tar.gz" - ) - collection_path = f"{temp_dir}/pulp-squeezer-0.0.9.tar.gz" - with open(collection_path, "wb") as f: - f.write(dl_collection.content) - - subprocess.run( - ( - "ansible-galaxy", - "collection", - "publish", - "-c", - "-s", - distribution.client_url, - collection_path, - ) - ) - wait_tasks() - - collections = self.collections_v3api.list(distribution.base_path) - self.assertEqual(collections.meta.count, 1) - - repo = self.repo_api.read(repo.pulp_href) - repo_version = self.repo_versions_api.read(repo.latest_version_href) - self.assertEqual(repo_version.number, 1) # We uploaded 1 collection - - version = self.collections_versions_v3api.read( - "squeezer", "pulp", distribution.base_path, "0.0.9" - ) - - self.assertEqual(version.requires_ansible, ">=2.8") + repository = ansible_repo_factory() + distribution = ansible_distribution_factory(repository=repository) + ansible_dir = ansible_dir_factory(distribution.client_url) + + collection_name = "".join(random.choices(string.ascii_lowercase, k=26)) + subprocess.run( + ( + "ansible-galaxy", + "collection", + "init", + "--init-path", + ansible_dir, + f"pulp.{collection_name}", + ), + cwd=ansible_dir, + check=True, + ) + + collection_meta = ansible_dir / "pulp" / collection_name / "meta" + collection_meta.mkdir() + runtime_yml = collection_meta / "runtime.yml" + runtime_yml.write_text('requires_ansible: ">=2.9"\n') + + subprocess.run( + ( + "ansible-galaxy", + "collection", + "build", + "--output-path", + ansible_dir, + ansible_dir / "pulp" / collection_name, + ), + cwd=ansible_dir, + check=True, + ) + + assert repository.latest_version_href.endswith("/0/") + collections = galaxy_v3_collections_api_client.list(distribution.base_path) + assert collections.meta.count == 0 + + subprocess.run( + ( + "ansible-galaxy", + "collection", + "publish", + "-c", + ansible_dir / f"pulp-{collection_name}-1.0.0.tar.gz", + ), + cwd=ansible_dir, + check=True, + ) + wait_tasks(repository.pulp_href) + + repository = ansible_repo_api_client.read(repository.pulp_href) + assert repository.latest_version_href.endswith("/1/") + collections = galaxy_v3_collections_api_client.list(distribution.base_path) + assert collections.meta.count == 1 # We uploaded 1 collection + version = galaxy_v3_collection_versions_api_client.read( + collection_name, "pulp", distribution.base_path, "1.0.0" + ) + assert version.requires_ansible == ">=2.9" diff --git a/pulp_ansible/tests/functional/cli/test_role_install.py b/pulp_ansible/tests/functional/cli/test_role_install.py index 39d4dd6ad..135e44cd3 100644 --- a/pulp_ansible/tests/functional/cli/test_role_install.py +++ b/pulp_ansible/tests/functional/cli/test_role_install.py @@ -2,73 +2,42 @@ from os import path import subprocess -import tempfile - -from pulpcore.client.pulp_ansible import ( - DistributionsAnsibleApi, - RepositoriesAnsibleApi, - AnsibleRepositorySyncURL, - RemotesRoleApi, -) -from pulp_smash.pulp3.bindings import monitor_task, PulpTestCase -from pulp_smash.pulp3.utils import gen_distribution, gen_repo from pulp_ansible.tests.functional.constants import ( ANSIBLE_ELASTIC_FIXTURE_URL, ANSIBLE_ELASTIC_ROLE_NAMESPACE_NAME, ANSIBLE_ELASTIC_ROLE, ) -from pulp_ansible.tests.functional.utils import gen_ansible_client, gen_ansible_remote -from pulp_ansible.tests.functional.utils import set_up_module as setUpModule # noqa:F401 -class InstallRoleTestCase(PulpTestCase): +def test_install_role( + ansible_distribution_factory, + ansible_role_remote_factory, + ansible_sync_factory, + ansible_dir_factory, +): """Test whether ansible-galaxy can install a Role hosted by Pulp.""" - - @classmethod - def setUpClass(cls): - """Create class-wide variables.""" - cls.client = gen_ansible_client() - cls.repo_api = RepositoriesAnsibleApi(cls.client) - cls.remote_role_api = RemotesRoleApi(cls.client) - cls.distributions_api = DistributionsAnsibleApi(cls.client) - - def test_install_role(self): - """Test whether ansible-galaxy can install a Role hosted by Pulp.""" - repo = self.repo_api.create(gen_repo()) - self.addCleanup(self.repo_api.delete, repo.pulp_href) - - body = gen_ansible_remote(url=ANSIBLE_ELASTIC_FIXTURE_URL) - remote = self.remote_role_api.create(body) - self.addCleanup(self.remote_role_api.delete, remote.pulp_href) - - # Sync the repository. - self.assertEqual(repo.latest_version_href, f"{repo.pulp_href}versions/0/") - repository_sync_data = AnsibleRepositorySyncURL(remote=remote.pulp_href) - sync_response = self.repo_api.sync(repo.pulp_href, repository_sync_data) - monitor_task(sync_response.task) - repo = self.repo_api.read(repo.pulp_href) - - # Create a distribution. - body = gen_distribution() - body["repository"] = repo.pulp_href - distribution_create = self.distributions_api.create(body) - created_resources = monitor_task(distribution_create.task).created_resources - distribution = self.distributions_api.read(created_resources[0]) - - self.addCleanup(self.distributions_api.delete, distribution.pulp_href) - - with tempfile.TemporaryDirectory() as temp_dir: - cmd = "ansible-galaxy role install {} -c -s {} -p {}".format( - ANSIBLE_ELASTIC_ROLE, distribution.client_url, temp_dir - ) - - directory = "{}/{}".format(temp_dir, ANSIBLE_ELASTIC_ROLE_NAMESPACE_NAME) - - self.assertTrue( - not path.exists(directory), "Directory {} already exists".format(directory) - ) - - subprocess.run(cmd.split()) - - self.assertTrue(path.exists(directory), "Could not find directory {}".format(directory)) + distribution = ansible_distribution_factory( + repository=ansible_sync_factory( + remote=ansible_role_remote_factory(url=ANSIBLE_ELASTIC_FIXTURE_URL).pulp_href + ) + ) + + ansible_dir = ansible_dir_factory(distribution.client_url) + directory = ansible_dir / ANSIBLE_ELASTIC_ROLE_NAMESPACE_NAME + + assert not path.exists(directory), "Directory {} already exists".format(directory) + subprocess.run( + ( + "ansible-galaxy", + "role", + "install", + ANSIBLE_ELASTIC_ROLE, + "-c", + "-p", + ansible_dir, + ), + check=True, + ) + + assert path.exists(directory), "Could not find directory {}".format(directory) diff --git a/pulp_ansible/tests/functional/conftest.py b/pulp_ansible/tests/functional/conftest.py index af8ab1cc8..d707218bf 100644 --- a/pulp_ansible/tests/functional/conftest.py +++ b/pulp_ansible/tests/functional/conftest.py @@ -1,9 +1,8 @@ import uuid - import pytest - import numpy as np from PIL import Image +import time from orionutils.generator import build_collection, randstr @@ -230,12 +229,17 @@ def _ansible_distribution_factory(repository=None, **kwargs): @pytest.fixture(scope="class") -def ansible_role_remote_factory(ansible_remote_role_api_client, gen_object_with_cleanup): +def ansible_role_remote_factory( + bindings_cfg, ansible_remote_role_api_client, gen_object_with_cleanup +): """A factory to generate an Ansible Collection Remote with auto-cleanup.""" def _ansible_role_remote_factory(**kwargs): kwargs.setdefault("name", str(uuid.uuid4())) kwargs.setdefault("url", ANSIBLE_GALAXY_URL) + if kwargs.pop("include_pulp_auth", False): + kwargs["username"] = bindings_cfg.username + kwargs["password"] = bindings_cfg.password return gen_object_with_cleanup(ansible_remote_role_api_client, kwargs) return _ansible_role_remote_factory @@ -243,23 +247,31 @@ def _ansible_role_remote_factory(**kwargs): @pytest.fixture(scope="class") def ansible_collection_remote_factory( - ansible_remote_collection_api_client, gen_object_with_cleanup + bindings_cfg, ansible_remote_collection_api_client, gen_object_with_cleanup ): """A factory to generate an Ansible Collection Remote with auto-cleanup.""" def _ansible_collection_remote_factory(**kwargs): kwargs.setdefault("name", str(uuid.uuid4())) + if kwargs.pop("include_pulp_auth", False): + kwargs["username"] = bindings_cfg.username + kwargs["password"] = bindings_cfg.password return gen_object_with_cleanup(ansible_remote_collection_api_client, kwargs) return _ansible_collection_remote_factory @pytest.fixture(scope="class") -def ansible_git_remote_factory(ansible_remote_git_api_client, gen_object_with_cleanup): +def ansible_git_remote_factory( + bindings_cfg, ansible_remote_git_api_client, gen_object_with_cleanup +): """A factory to generate an Ansible Git Remote with auto-cleanup.""" def _ansible_git_remote_factory(**kwargs): kwargs.setdefault("name", str(uuid.uuid4())) + if kwargs.pop("include_pulp_auth", False): + kwargs["username"] = bindings_cfg.username + kwargs["password"] = bindings_cfg.password return gen_object_with_cleanup(ansible_remote_git_api_client, kwargs) return _ansible_git_remote_factory @@ -285,21 +297,24 @@ def _build_and_upload_collection(ansible_repo=None, **kwargs): @pytest.fixture -def ansible_dir_factory(tmp_path): +def ansible_dir_factory(tmp_path, bindings_cfg): """A factory to create a local ansible.cfg file with authentication""" - def _ansible_dir_factory(server, user): + def _ansible_dir_factory(server): + username = bindings_cfg.username + password = bindings_cfg.password + ansible_cfg = ( "[galaxy]\nserver_list = pulp_ansible\n\n" "[galaxy_server.pulp_ansible]\n" - "url = {}\n" - "username = {}\n" - "password = {}" - ).format(server, user.username, user.password) - - cfg_file = "{}/ansible.cfg".format(tmp_path) - with open(cfg_file, "w", encoding="utf8") as f: - f.write(ansible_cfg) + f"url = {server}\n" + ) + + if username is not None and password is not None: + ansible_cfg += f"username = {username}\npassword = {password}\n" + + cfg_file = tmp_path / "ansible.cfg" + cfg_file.write_text(ansible_cfg, encoding="UTF_8") return tmp_path return _ansible_dir_factory @@ -317,3 +332,23 @@ def _random_image(): return path return _random_image + + +# Utility fixtures + + +@pytest.fixture(scope="session") +def wait_tasks(tasks_api_client): + """Polls the Task API until all tasks for a resource are in a completed state.""" + + def _wait_tasks(resource): + pending_tasks = tasks_api_client.list( + state__in=["running", "waiting"], reserved_resources=resource + ) + while pending_tasks.count: + time.sleep(1) + pending_tasks = tasks_api_client.list( + state__in=["running", "waiting"], reserved_resources=resource + ) + + return _wait_tasks diff --git a/pulp_ansible/tests/functional/utils.py b/pulp_ansible/tests/functional/utils.py index c8a462908..0980fcc0b 100644 --- a/pulp_ansible/tests/functional/utils.py +++ b/pulp_ansible/tests/functional/utils.py @@ -1,8 +1,6 @@ """Utilities for tests for the ansible plugin.""" from dynaconf import Dynaconf from functools import partial -from time import sleep -import os import unittest from urllib.parse import urlparse, parse_qs @@ -30,7 +28,6 @@ from pulpcore.client.pulpcore import ( ApiClient as CoreApiClient, TasksApi, - SigningServicesApi, StatusApi, ) from pulpcore.client.pulp_ansible import ( @@ -44,12 +41,9 @@ RemotesGitApi, RemotesRoleApi, AnsibleRepositorySyncURL, - PulpAnsibleArtifactsCollectionsV3Api, RepositoriesAnsibleVersionsApi, ) -from orionutils.generator import build_collection, randstr - cfg = config.get_config() configuration = cfg.get_bindings_config() @@ -156,61 +150,6 @@ def populate_pulp(cfg, url=ANSIBLE_FIXTURE_URL): core_client = CoreApiClient(configuration) tasks = TasksApi(core_client) -signing = SigningServicesApi(core_client) - - -def wait_tasks(): - """Polls the Task API until all tasks are in a completed state.""" - running_tasks = tasks.list(state="running") - while running_tasks.count: - sleep(1) - running_tasks = tasks.list(state="running") - - -def gen_collection_in_distribution( - base_path, name=None, namespace=None, versions=None, **cfg_kwargs -): - """ - Generate a randomized collection and upload it to the specified repository. - - Params: - - base_path: distribtion base path for the repository to upload the - collection to. - - name: Name of the collection. If none is provided a random name will - be generated. - - namespace: Namespace of the collection. If none is provided a random - name will be generated. - - versions: A list of versions to create for the collection. If none - are specified the collection will be generated with version 1.0.0 - - Additional galaxy.yaml configuration options can be added as kwargs. - For example dependencies={"foo.bar": "1.0.0"} will set foo.bar as a - dependency for the newly generated collection - - Returns: a dictionary with the name and namespace names of the newly generated - collection. - """ - if versions is None: - versions = ["1.0.0"] - namespace = namespace or randstr() - name = name or randstr() - - ansible_client = gen_ansible_client() - upload = PulpAnsibleArtifactsCollectionsV3Api(ansible_client) - - for version in versions: - artifact = build_collection( - "skeleton", - config={"namespace": namespace, "name": name, "version": version, **cfg_kwargs}, - ) - - upload.create(path=base_path, file=artifact.filename) - - wait_tasks() - - os.remove(artifact.filename) - - return {"name": name, "namespace": namespace} class TestCaseUsingBindings(PulpTestCase):