Skip to content

Commit

Permalink
Modernize some tests
Browse files Browse the repository at this point in the history
[noissue]
  • Loading branch information
mdellweg committed Dec 8, 2023
1 parent 076a9bb commit 0fbd319
Show file tree
Hide file tree
Showing 15 changed files with 85 additions and 322 deletions.
8 changes: 4 additions & 4 deletions pulp_ansible/app/tasks/collections.py
Original file line number Diff line number Diff line change
Expand Up @@ -99,17 +99,17 @@ async def declarative_content_from_git_repo(remote, url, git_ref=None, metadata_
gitrepo = Repo.clone_from(url, uuid4(), depth=1, multi_options=["--recurse-submodules"])
commit_sha = gitrepo.head.commit.hexsha
metadata, artifact_path = sync_collection(gitrepo.working_dir, ".")
if not metadata_only:
if metadata_only:
metadata["artifact"] = None
metadata["artifact_url"] = None
else:
artifact = Artifact.init_and_validate(artifact_path)
try:
await sync_to_async(artifact.save)()
except IntegrityError:
artifact = Artifact.objects.get(sha256=artifact.sha256)
metadata["artifact_url"] = reverse("artifacts-detail", args=[artifact.pk])
metadata["artifact"] = artifact
else:
metadata["artifact"] = None
metadata["artifact_url"] = None
metadata["remote_artifact_url"] = "{}/commit/{}".format(url.rstrip("/"), commit_sha)

artifact = metadata["artifact"]
Expand Down
Original file line number Diff line number Diff line change
@@ -1,19 +1,15 @@
"""Tests related to sync ansible plugin collection content type."""
from tempfile import NamedTemporaryFile
from pulp_smash.pulp3.bindings import PulpTestCase, delete_orphans, monitor_task
from pulp_smash.utils import http_get

from pulpcore.client.pulp_ansible import ContentCollectionVersionsApi
import pytest

from orionutils.generator import randstr

from pulp_ansible.tests.functional.constants import (
ANSIBLE_DEMO_COLLECTION_REQUIREMENTS as DEMO_REQUIREMENTS,
GALAXY_ANSIBLE_BASE_URL,
)
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


@pytest.mark.parallel
def test_tags_filter(
ansible_collection_version_api_client,
ansible_collection_remote_factory,
Expand Down Expand Up @@ -57,110 +53,50 @@ def test_tags_filter(
assert len(collections) == 0, collections


class ContentUnitTestCase(PulpTestCase):
"""CRUD content unit.
This test targets the following issues:
* `Pulp #2872 <https://pulp.plan.io/issues/2872>`_
* `Pulp #3445 <https://pulp.plan.io/issues/3445>`_
* `Pulp Smash #870 <https://github.com/pulp/pulp-smash/issues/870>`_
"""

@classmethod
def setUpClass(cls):
"""Create class-wide variable."""
delete_orphans()
cls.content_unit = {}
cls.cv_content_api = ContentCollectionVersionsApi(gen_ansible_client())

@classmethod
def tearDownClass(cls):
"""Clean class-wide variable."""
delete_orphans()

def upload_collection(self, namespace="pulp", name="squeezer", version="0.0.9"):
"""Upload collection."""
url = f"https://galaxy.ansible.com/download/{namespace}-{name}-{version}.tar.gz"
collection_content = http_get(url)
with NamedTemporaryFile() as temp_file:
temp_file.write(collection_content)
return self.cv_content_api.create(
file=temp_file.name,
expected_namespace=namespace,
expected_name=name,
expected_version=version,
)

def test_01_create_content_unit(self):
"""Create content unit."""
attrs = dict(namespace="pulp", name="squeezer", version="0.0.9")
response = self.upload_collection(**attrs)
created_resources = monitor_task(response.task).created_resources
content_unit = self.cv_content_api.read(created_resources[0])
self.content_unit.update(content_unit.to_dict())
for key, val in attrs.items():
with self.subTest(key=key):
self.assertEqual(self.content_unit[key], val)

@skip_if(bool, "content_unit", False)
def test_02_read_content_unit(self):
"""Read a content unit by its href."""
content_unit = self.cv_content_api.read(self.content_unit["pulp_href"]).to_dict()
for key, val in self.content_unit.items():
with self.subTest(key=key):
self.assertEqual(content_unit[key], val)

@skip_if(bool, "content_unit", False)
def test_02_read_content_units(self):
"""Read a content unit by its pkg_id."""
page = self.cv_content_api.list(
namespace=self.content_unit["namespace"], name=self.content_unit["name"]
)
self.assertEqual(len(page.results), 1)
for key, val in self.content_unit.items():
with self.subTest(key=key):
self.assertEqual(page.results[0].to_dict()[key], val)

@skip_if(bool, "content_unit", False)
def test_03_partially_update(self):
"""Attempt to update a content unit using HTTP PATCH.
This HTTP method is not supported and a HTTP exception is expected.
"""
attrs = {"name": "testing"}
with self.assertRaises(AttributeError) as exc:
self.cv_content_api.partial_update(self.content_unit["pulp_href"], attrs)
msg = "object has no attribute 'partial_update'"
self.assertIn(msg, exc.exception.args[0])

@skip_if(bool, "content_unit", False)
def test_03_fully_update(self):
"""Attempt to update a content unit using HTTP PUT.
This HTTP method is not supported and a HTTP exception is expected.
"""
attrs = {"name": "testing"}
with self.assertRaises(AttributeError) as exc:
self.cv_content_api.update(self.content_unit["pulp_href"], attrs)
msg = "object has no attribute 'update'"
self.assertIn(msg, exc.exception.args[0])

@skip_if(bool, "content_unit", False)
def test_04_delete(self):
"""Attempt to delete a content unit using HTTP DELETE.
This HTTP method is not supported and a HTTP exception is expected.
"""
with self.assertRaises(AttributeError) as exc:
self.cv_content_api.delete(self.content_unit["pulp_href"])
msg = "object has no attribute 'delete'"
self.assertIn(msg, exc.exception.args[0])

@skip_if(bool, "content_unit", False)
def test_05_duplicate_no_error(self):
"""Attempt to create duplicate collection."""
attrs = dict(namespace="pulp", name="squeezer", version="0.0.9")
response = self.upload_collection(**attrs)
created_resources = monitor_task(response.task).created_resources
self.assertEqual(created_resources[0], self.content_unit["pulp_href"])
# Looks like orionutils are not meant to be used in parallel
# @pytest.mark.parallel
def test_content_unit_lifecycle(
ansible_collection_version_api_client, build_and_upload_collection, monitor_task
):
"""Create content unit."""
attrs = {"namespace": randstr(), "name": "squeezer", "version": "0.0.9"}
collection_artifact, content_unit_href = build_and_upload_collection(config=attrs)

# Read a content unit by its href.
content_unit = ansible_collection_version_api_client.read(content_unit_href)
for key, val in attrs.items():
assert content_unit.to_dict()[key] == val

# Read a content unit by its pkg_id.
page = ansible_collection_version_api_client.list(
namespace=content_unit.namespace, name=content_unit.name
)
assert page.count == 1
assert page.results[0] == content_unit

# Attempt to update a content unit using HTTP PATCH.
# This HTTP method is not supported and a HTTP exception is expected.
with pytest.raises(AttributeError) as exc_info:
ansible_collection_version_api_client.partial_update(content_unit_href, name="testing")
msg = "object has no attribute 'partial_update'"
assert msg in exc_info.value.args[0]

# Attempt to update a content unit using HTTP PUT.
# This HTTP method is not supported and a HTTP exception is expected.
with pytest.raises(AttributeError) as exc_info:
ansible_collection_version_api_client.update(content_unit_href, {"name": "testing"})
msg = "object has no attribute 'update'"
assert msg in exc_info.value.args[0]

# Attempt to delete a content unit using HTTP DELETE.
# This HTTP method is not supported and a HTTP exception is expected.
with pytest.raises(AttributeError) as exc_info:
ansible_collection_version_api_client.delete(content_unit_href)
msg = "object has no attribute 'delete'"
assert msg in exc_info.value.args[0]

# Attempt to create duplicate collection.
create_task = monitor_task(
ansible_collection_version_api_client.create(file=collection_artifact.filename).task
)
assert content_unit_href in create_task.created_resources
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 @@ -9,7 +9,6 @@
from pulp_ansible.tests.functional.utils import (
gen_ansible_remote,
gen_distribution,
get_content,
)
from pulpcore.client.pulp_ansible import AnsibleRepositorySyncURL

Expand Down Expand Up @@ -172,6 +171,7 @@ def test_sync_signatures(
ansible_remote_collection_api_client,
gen_object_with_cleanup,
ansible_repo_api_client,
ansible_repo_version_api_client,
monitor_task,
):
"""Test that signatures are also synced."""
Expand All @@ -188,16 +188,18 @@ def test_sync_signatures(
monitor_task(sync_response.task)
repo = ansible_repo_api_client.read(new_repo.pulp_href)

content_response = get_content(repo.to_dict())
assert len(content_response["ansible.collection_version"]) == 2
assert len(content_response["ansible.collection_signature"]) == 1
content_response = ansible_repo_version_api_client.read(repo.latest_version_href)
print(content_response)
assert content_response.content_summary.present["ansible.collection_version"]["count"] == 2
assert content_response.content_summary.present["ansible.collection_signature"]["count"] == 1


def test_sync_signatures_only(
ansible_repo_factory,
ansible_collection_remote_factory,
distro_serving_one_signed_one_unsigned_collection,
ansible_repo_api_client,
ansible_repo_version_api_client,
monitor_task,
):
"""Test that only collections with a signatures are synced when specified."""
Expand All @@ -215,6 +217,7 @@ def test_sync_signatures_only(
monitor_task(sync_response.task)
repo = ansible_repo_api_client.read(new_repo.pulp_href)

content_response = get_content(repo.to_dict())
assert len(content_response["ansible.collection_version"]) == 1
assert len(content_response["ansible.collection_signature"]) == 1
content_response = ansible_repo_version_api_client.read(repo.latest_version_href)
print(content_response)
assert content_response.content_summary.present["ansible.collection_version"]["count"] == 1
assert content_response.content_summary.present["ansible.collection_signature"]["count"] == 1
5 changes: 1 addition & 4 deletions pulp_ansible/tests/functional/api/collection/test_sync.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,7 @@
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 ( # noqa
monitor_task,
set_up_module as setUpModule,
)
from pulp_ansible.tests.functional.utils import monitor_task


class MirrorTestCase(TestCaseUsingBindings, SyncHelpersMixin):
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@
SyncHelpersMixin,
TestCaseUsingBindings,
)
from pulp_ansible.tests.functional.utils import set_up_module as setUpModule # noqa:F401
from pulp_smash.pulp3.bindings import monitor_task, delete_orphans


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@
)

from pulp_ansible.tests.functional.utils import gen_ansible_client
from pulp_ansible.tests.functional.utils import set_up_module as setUpModule # noqa:F401


# TODO: Check if this url will stay around
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
import pytest


@pytest.mark.parallel
def test_client_config_distro(
ansible_repo, ansible_distribution_factory, ansible_client_configuration_api_client
):
Expand All @@ -6,8 +10,7 @@ def test_client_config_distro(
assert res.default_distribution_path == distro.base_path


def test_client_config_default_distro(
ansible_repo, ansible_distribution_factory, ansible_client_default_configuration_api_client
):
@pytest.mark.parallel
def test_client_config_default_distro(ansible_client_default_configuration_api_client):
res = ansible_client_default_configuration_api_client.read()
assert res.default_distribution_path is None
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@
is_galaxy_ng_installed,
)
from pulp_ansible.tests.functional.utils import SyncHelpersMixin, TestCaseUsingBindings
from pulp_ansible.tests.functional.utils import set_up_module as setUpModule # noqa:F401

from pulpcore.client.pulpcore import ContentguardsContentRedirectApi, ApiClient as CoreApiClient

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@

from pulp_ansible.tests.functional.utils import SyncHelpersMixin, TestCaseUsingBindings
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 CollectionsV3TestCase(TestCaseUsingBindings, SyncHelpersMixin):
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@
tasks,
)
from pulp_ansible.tests.functional.utils import SyncHelpersMixin, TestCaseUsingBindings
from pulp_ansible.tests.functional.utils import set_up_module as setUpModule # noqa:F401


class SyncCollectionsFromPulpServerTestCase(TestCaseUsingBindings, SyncHelpersMixin):
Expand Down
2 changes: 1 addition & 1 deletion pulp_ansible/tests/functional/api/git/test_sync.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ def _sync_and_count(remote_body, repo=None, **sync_kwargs):
c = ansible_collection_version_api_client.list(repository_version=repo.latest_version_href)
return c.count

yield _sync_and_count
return _sync_and_count


@pytest.mark.parallel
Expand Down
2 changes: 2 additions & 0 deletions pulp_ansible/tests/functional/cli/test_role_install.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
"""Tests that Roles hosted by Pulp can be installed by ansible-galaxy."""

import pytest
from os import path
import subprocess

Expand All @@ -10,6 +11,7 @@
)


@pytest.mark.parallel
def test_install_role(
ansible_distribution_factory,
ansible_role_remote_factory,
Expand Down
Loading

0 comments on commit 0fbd319

Please sign in to comment.