Skip to content

Commit

Permalink
Fix requirements.yml parser for pinned collection version
Browse files Browse the repository at this point in the history
https://pulp.plan.io/issues/8627
closes #8627

(cherry picked from commit 84e1c22)
  • Loading branch information
fao89 committed Apr 29, 2021
1 parent db5c634 commit 1c6afd4
Show file tree
Hide file tree
Showing 4 changed files with 50 additions and 4 deletions.
1 change: 1 addition & 0 deletions CHANGES/8627.bugfix
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Fix requirements.yml parser for pinned collection version
12 changes: 11 additions & 1 deletion pulp_ansible/app/tasks/collections.py
Original file line number Diff line number Diff line change
Expand Up @@ -535,7 +535,17 @@ async def _fetch_collection_metadata(self, requirements_entry):
if requirements_entry.version == "*":
requirement_version = Requirement.parse("collection")
else:
requirement_version = Requirement.parse(f"collection{requirements_entry.version}")
# We need specifiers to enforce Requirement object criteria
# https://setuptools.readthedocs.io/en/latest/pkg_resources.html#requirements-parsing
# https://setuptools.readthedocs.io/en/latest/pkg_resources.html#requirement-methods-and-attributes
# If requirements_entry.version is a valid version, adds == specifier to the requirement
try:
Version(requirements_entry.version)
req_to_parse = f"collection=={requirements_entry.version}"
except ValueError:
req_to_parse = f"collection{requirements_entry.version}"

requirement_version = Requirement.parse(req_to_parse)

namespace, name = requirements_entry.name.split(".")

Expand Down
38 changes: 36 additions & 2 deletions pulp_ansible/tests/functional/api/collection/v2/test_sync.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,40 @@ def test_sync_with_slash(self):
content = self.cv_api.list(repository_version=f"{repo.pulp_href}versions/1/")
self.assertEqual(len(content.results), 1)

def test_sync_with_specific_version(self):
"""Sync with simple requirements file, expected to download one CollectionVersion."""
body = gen_ansible_remote(
url="https://galaxy.ansible.com",
requirements_file="collections:\n - name: arista.avd\n version: 2.0.0",
sync_dependencies=False,
)
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)
distribution = self._create_distribution_from_repo(repo)

versions = self.collections_versions_v3api.list("avd", "arista", distribution.base_path)

self.assertEqual(versions.meta.count, 1)

def test_sync_all_versions(self):
"""Sync with simple requirements file, expected to download CollectionVersion."""
body = gen_ansible_remote(
url="https://galaxy.ansible.com",
requirements_file="collections:\n - name: arista.avd",
sync_dependencies=False,
)
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)
distribution = self._create_distribution_from_repo(repo)

versions = self.collections_versions_v3api.list("avd", "arista", distribution.base_path)

self.assertGreater(versions.meta.count, 1)

def test_sync_with_attached_remote(self):
"""Sync with a CollectionRemote attached to the repository."""
body = gen_ansible_remote(
Expand Down Expand Up @@ -141,9 +175,9 @@ def test_sync_with_two_equality_entries_on_different_lines(self):
"---\n"
"collections:\n"
"- name: robertdebock.ansible_development_environment\n"
' version: "==1.0.1"\n'
' version: "1.0.1"\n'
"- name: robertdebock.ansible_development_environment\n"
' version: "==1.0.0"\n'
' version: "1.0.0"\n'
)
body = gen_ansible_remote(
url="https://galaxy.ansible.com",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,8 @@ def test_v3_collection_version_from_synced_data(self):
"""Test Collection Versions V3 endpoint fields."""
body = gen_ansible_remote(
url="https://galaxy.ansible.com",
requirements_file="collections:\n - name: cisco.nxos\n version: ==1.4.0",
requirements_file="collections:\n - name: cisco.nxos\n version: 1.4.0",
sync_dependencies=False,
)

remote = self.remote_collection_api.create(body)
Expand Down

0 comments on commit 1c6afd4

Please sign in to comment.