Skip to content

Commit

Permalink
Update collection deletion task to clean up related content.
Browse files Browse the repository at this point in the history
fixes: pulp#889
  • Loading branch information
newswangerd committed Apr 5, 2022
1 parent 6c869c1 commit 011b54d
Show file tree
Hide file tree
Showing 4 changed files with 91 additions and 2 deletions.
1 change: 1 addition & 0 deletions CHANGES/889.bugfix
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Fix a bug where when a collection version is removed from a repository, it's associated signatures and deprecated content remains in the repository.
32 changes: 32 additions & 0 deletions pulp_ansible/app/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -315,6 +315,38 @@ class Meta:

permissions = (("modify_ansible_repo_content", "Can modify ansible repository content"),)

def finalize_new_version(self, new_version):
"""Finalize repo version."""
removed_collection_versions = new_version.removed(
base_version=new_version.base_version
).filter(pulp_type=CollectionVersion.get_pulp_type())

# Remove any deprecated and signature content associated with the removed collection
# versions
for version in removed_collection_versions:
version = version.cast()

signatures = new_version.get_content(
content_qs=CollectionVersionSignature.objects.filter(signed_collection=version)
)
new_version.remove_content(signatures)

other_collection_versions = new_version.get_content(
content_qs=CollectionVersion.objects.filter(collection=version.collection)
)

# AnsibleCollectionDeprecated applies to all collection versions in a repository,
# so only remove it if there are no more collection versions for the specified
# collection present.
if not other_collection_versions.exists():
deprecations = new_version.get_content(
content_qs=AnsibleCollectionDeprecated.objects.filter(
namespace=version.namespace, name=version.name
)
)

new_version.remove_content(deprecations)


class AnsibleDistribution(Distribution):
"""
Expand Down
58 changes: 56 additions & 2 deletions pulp_ansible/tests/functional/api/collection/v3/test_deletion.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
from pulp_ansible.tests.functional.utils import gen_collection_in_distribution
from pulp_ansible.tests.functional.utils import SyncHelpersMixin, TestCaseUsingBindings
from pulp_ansible.tests.functional.utils import (
gen_collection_in_distribution,
SyncHelpersMixin,
TestCaseUsingBindings,
create_signing_service,
)

from pulpcore.client.pulp_ansible.exceptions import ApiException
from pulp_smash.pulp3.bindings import monitor_task
Expand Down Expand Up @@ -192,3 +196,53 @@ def test_invalid_deletion(self):
)

resp = monitor_task(resp.task)

def test_delete_deprecated_content(self):
"""Test that deprecated content is removed correctly."""
# Deprecate the collection
result = self.collections_v3api.update(
path=self.distribution.base_path,
name=self.collection_name,
namespace=self.collection_namespace,
body={"deprecated": True},
)
monitor_task(result.task)

resp = self.collections_v3api.delete(
path=self.distribution.base_path,
name=self.collection_name,
namespace=self.collection_namespace,
)
monitor_task(resp.task)

# Verify that all the content is gone
repo = self.repo_api.read(self.repo.pulp_href)
latest_version = self.repo_version_api.read(repo.latest_version_href)

assert len(latest_version.content_summary.present) == 0

def test_delete_signed_content(self):
"""Test that signature content is removed correctly."""
sign_service = create_signing_service()

# Sign the collections
body = {
"content_units": [
"*",
],
"signing_service": sign_service.pulp_href,
}
monitor_task(self.repo_api.sign(self.repo.pulp_href, body).task)

resp = self.collections_v3api.delete(
path=self.distribution.base_path,
name=self.collection_name,
namespace=self.collection_namespace,
)
monitor_task(resp.task)

# Verify that all the content is gone
repo = self.repo_api.read(self.repo.pulp_href)
latest_version = self.repo_version_api.read(repo.latest_version_href)

assert len(latest_version.content_summary.present) == 0
2 changes: 2 additions & 0 deletions pulp_ansible/tests/functional/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@
RemotesRoleApi,
AnsibleRepositorySyncURL,
PulpAnsibleArtifactsCollectionsV3Api,
RepositoriesAnsibleVersionsApi,
)

from orionutils.generator import build_collection, randstr
Expand Down Expand Up @@ -221,6 +222,7 @@ def setUpClass(cls):
"""Create class-wide variables."""
cls.client = gen_ansible_client()
cls.repo_api = RepositoriesAnsibleApi(cls.client)
cls.repo_version_api = RepositoriesAnsibleVersionsApi(cls.client)
cls.remote_collection_api = RemotesCollectionApi(cls.client)
cls.remote_git_api = RemotesGitApi(cls.client)
cls.remote_role_api = RemotesRoleApi(cls.client)
Expand Down

0 comments on commit 011b54d

Please sign in to comment.