Skip to content

Commit

Permalink
Use pulp_ansible collection upload refactor
Browse files Browse the repository at this point in the history
[noissue]
Required PR: pulp/pulp_ansible#1176
  • Loading branch information
gerrod3 committed Nov 17, 2022
1 parent d4f8456 commit 4aef651
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 63 deletions.
6 changes: 4 additions & 2 deletions galaxy_ng/app/api/v3/viewsets/collection.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,13 +46,15 @@ class CollectionUploadViewSet(api_base.LocalSettingsMixin,
parser_classes = [AnsibleGalaxy29MultiPartParser]
serializer_class = CollectionUploadSerializer

def _dispatch_import_collection_task(self, temp_file_pk, repository=None, **kwargs):
def _dispatch_upload_collection_task(self, args=None, kwargs=None, repository=None):
"""Dispatch a pulp task started on upload of collection version."""
locks = []
context = super().get_serializer_context()
request = context.get("request", None)

kwargs["temp_file_pk"] = temp_file_pk
kwargs = kwargs or {}
kwargs["general_args"] = args

kwargs["username"] = request.user.username

if repository:
Expand Down
50 changes: 19 additions & 31 deletions galaxy_ng/app/tasks/publishing.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
from django.contrib.contenttypes.models import ContentType
from django.utils.translation import gettext_lazy as _
from pulp_ansible.app.models import AnsibleDistribution, AnsibleRepository, CollectionVersion
from pulp_ansible.app.tasks.collections import import_collection
from pulpcore.plugin.tasking import general_create
from pulpcore.plugin.models import Task
from pulpcore.plugin.models import SigningService

Expand Down Expand Up @@ -34,29 +34,23 @@ def get_created_collection_versions():
return created_collection_versions


def import_and_move_to_staging(temp_file_pk, **kwargs):
def import_and_move_to_staging(username, repository_pk=None, **kwargs):
"""Import collection version and move to staging repository.
Custom task to call pulp_ansible's import_collection() task then
Custom task to call pulpcore's general_create() task then
enqueue two tasks to add to staging repo and remove from inbound repo.
This task will not wait for the enqueued tasks to finish.
"""
inbound_repository_pk = kwargs.get('repository_pk')
import_collection(
temp_file_pk=temp_file_pk,
repository_pk=inbound_repository_pk,
expected_namespace=kwargs['expected_namespace'],
expected_name=kwargs['expected_name'],
expected_version=kwargs['expected_version'],
)
general_args = kwargs.pop("general_args")
general_create(*general_args, **kwargs)

try:
staging_repo = AnsibleDistribution.objects.get(name=STAGING_NAME).repository
except AnsibleRepository.DoesNotExist:
raise RuntimeError(_('Could not find staging repository: "%s"') % STAGING_NAME)

inbound_repo = AnsibleRepository.objects.get(pk=inbound_repository_pk)
inbound_repo = AnsibleRepository.objects.get(pk=repository_pk)

created_collection_versions = get_created_collection_versions()

Expand All @@ -65,35 +59,29 @@ def import_and_move_to_staging(temp_file_pk, **kwargs):

if settings.GALAXY_ENABLE_API_ACCESS_LOG:
_log_collection_upload(
kwargs["username"],
kwargs["expected_namespace"],
kwargs["expected_name"],
kwargs["expected_version"]
username,
collection_version.namespace,
collection_version.name,
collection_version.version,
)


def import_and_auto_approve(temp_file_pk, **kwargs):
def import_and_auto_approve(username, repository_pk=None, **kwargs):
"""Import collection version and automatically approve.
Custom task to call pulp_ansible's import_collection() task
Custom task to call pulpcore's general_create() task
then automatically approve collection version so no
manual approval action needs to occur.
"""
inbound_repository_pk = kwargs.get('repository_pk')
import_collection(
temp_file_pk=temp_file_pk,
repository_pk=inbound_repository_pk,
expected_namespace=kwargs['expected_namespace'],
expected_name=kwargs['expected_name'],
expected_version=kwargs['expected_version'],
)
general_args = kwargs.pop("general_args")
general_create(*general_args, **kwargs)

try:
golden_repo = AnsibleDistribution.objects.get(name=GOLDEN_NAME).repository
except AnsibleRepository.DoesNotExist:
raise RuntimeError(_('Could not find staging repository: "%s"') % GOLDEN_NAME)

inbound_repo = AnsibleRepository.objects.get(pk=inbound_repository_pk)
inbound_repo = AnsibleRepository.objects.get(pk=repository_pk)

created_collection_versions = get_created_collection_versions()

Expand Down Expand Up @@ -121,10 +109,10 @@ def import_and_auto_approve(temp_file_pk, **kwargs):

if settings.GALAXY_ENABLE_API_ACCESS_LOG:
_log_collection_upload(
kwargs["username"],
kwargs["expected_namespace"],
kwargs["expected_name"],
kwargs["expected_version"]
username,
collection_version.namespace,
collection_version.name,
collection_version.version,
)


Expand Down
48 changes: 18 additions & 30 deletions galaxy_ng/tests/unit/app/test_tasks.py
Original file line number Diff line number Diff line change
Expand Up @@ -80,25 +80,22 @@ def test_task_move_content(self):
self.assertEqual(repo2_version_number + 1, repo2.latest_version().number)

@mock.patch('galaxy_ng.app.tasks.publishing.get_created_collection_versions')
@mock.patch('galaxy_ng.app.tasks.publishing.import_collection')
@mock.patch('galaxy_ng.app.tasks.publishing.general_create')
@mock.patch('galaxy_ng.app.tasks.promotion.dispatch')
def test_import_and_auto_approve(self, mocked_dispatch, mocked_import, mocked_get_created):
def test_import_and_auto_approve(self, mocked_dispatch, mocked_create, mocked_get_created):
inbound_repo = AnsibleRepository.objects.get(name=staging_name)

golden_repo = AnsibleRepository.objects.get(name=golden_name)

mocked_get_created.return_value = [self.collection_version]

import_and_auto_approve(
self.pulp_temp_file.pk,
repository_pk=inbound_repo.pk,
expected_namespace='',
expected_name='',
expected_version='',
username='',
'', # username
inbound_repo.pk,
**{"general_args": ()}
)

self.assertTrue(mocked_import.call_count == 1)
self.assertTrue(mocked_create.call_count == 1)
self.assertTrue(mocked_dispatch.call_count == 1)

# test cannot find golden repo
Expand All @@ -107,18 +104,15 @@ def test_import_and_auto_approve(self, mocked_dispatch, mocked_import, mocked_ge
mocked_get_created.side_effect = AnsibleDistribution.DoesNotExist
with self.assertRaises(AnsibleDistribution.DoesNotExist):
import_and_auto_approve(
self.artifact.pk,
repository_pk=inbound_repo.pk,
expected_namespace='',
expected_name='',
expected_version='',
username='',
'', # username
inbound_repo.pk,
**{"general_args": ()}
)

@mock.patch('galaxy_ng.app.tasks.publishing.get_created_collection_versions')
@mock.patch('galaxy_ng.app.tasks.publishing.import_collection')
@mock.patch('galaxy_ng.app.tasks.publishing.general_create')
@mock.patch('galaxy_ng.app.tasks.promotion.dispatch')
def test_import_and_move_to_staging(self, mocked_dispatch, mocked_import, mocked_get_created):
def test_import_and_move_to_staging(self, mocked_dispatch, mocked_create, mocked_get_created):
staging_repo = AnsibleRepository.objects.get(name=staging_name)

inbound_name = 'the_incoming_repo'
Expand All @@ -130,15 +124,12 @@ def test_import_and_move_to_staging(self, mocked_dispatch, mocked_import, mocked
mocked_get_created.return_value = [self.collection_version]

import_and_move_to_staging(
self.pulp_temp_file.pk,
repository_pk=inbound_repo.pk,
expected_namespace='',
expected_name='',
expected_version='',
username='',
'', # username
inbound_repo.pk,
**{"general_args": ()}
)

self.assertTrue(mocked_import.call_count == 1)
self.assertTrue(mocked_create.call_count == 1)
self.assertTrue(mocked_dispatch.call_count == 1)

# test cannot find staging repo
Expand All @@ -147,12 +138,9 @@ def test_import_and_move_to_staging(self, mocked_dispatch, mocked_import, mocked
mocked_get_created.side_effect = AnsibleDistribution.DoesNotExist
with self.assertRaises(AnsibleDistribution.DoesNotExist):
import_and_move_to_staging(
self.pulp_temp_file.pk,
repository_pk=inbound_repo.pk,
expected_namespace='',
expected_name='',
expected_version='',
username='',
'', # username
inbound_repo.pk,
**{"general_args": ()}
)

def test_log_collection_upload(self):
Expand Down

0 comments on commit 4aef651

Please sign in to comment.