Skip to content

Commit

Permalink
Modernize functional tests II (#1554)
Browse files Browse the repository at this point in the history
[noissue]
  • Loading branch information
mdellweg authored Aug 16, 2023
1 parent d303c8f commit baa20b1
Show file tree
Hide file tree
Showing 10 changed files with 316 additions and 468 deletions.
Original file line number Diff line number Diff line change
@@ -1,75 +1,61 @@
"""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
from pulpcore.client.pulp_ansible.exceptions import ApiException


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 <https://pulp.plan.io/issues/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 <https://pulp.plan.io/issues/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):
Expand Down
17 changes: 10 additions & 7 deletions pulp_ansible/tests/functional/api/collection/test_signatures.py
Original file line number Diff line number Diff line change
Expand Up @@ -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 (
Expand All @@ -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()
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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 = []
Expand Down Expand Up @@ -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
Expand All @@ -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)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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(
Expand Down
49 changes: 25 additions & 24 deletions pulp_ansible/tests/functional/api/git/test_sync.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,6 @@
import pytest
import subprocess

from os import path


@pytest.fixture
def sync_and_count(
Expand All @@ -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()
Expand All @@ -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
Expand Down Expand Up @@ -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,
Expand All @@ -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()
Expand All @@ -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)
Expand Down
Loading

0 comments on commit baa20b1

Please sign in to comment.