From 47aeb58d6dc73da8c0d5d93d01b45c9322a29220 Mon Sep 17 00:00:00 2001 From: Gerrod Ubben Date: Sun, 25 Sep 2022 20:49:01 -0400 Subject: [PATCH] Use pulp_ansible collection upload refactor No-Issue Required PR: https://github.com/pulp/pulp_ansible/pull/1176/ --- galaxy_ng/app/api/v3/viewsets/collection.py | 6 ++- galaxy_ng/app/tasks/publishing.py | 50 +++++++------------ .../tests/integration/cli/test_cli_flow.py | 2 +- galaxy_ng/tests/unit/app/test_tasks.py | 48 +++++++----------- 4 files changed, 42 insertions(+), 64 deletions(-) diff --git a/galaxy_ng/app/api/v3/viewsets/collection.py b/galaxy_ng/app/api/v3/viewsets/collection.py index 5c006869fd..8a1e510e54 100644 --- a/galaxy_ng/app/api/v3/viewsets/collection.py +++ b/galaxy_ng/app/api/v3/viewsets/collection.py @@ -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: diff --git a/galaxy_ng/app/tasks/publishing.py b/galaxy_ng/app/tasks/publishing.py index f674348784..dee54cdb11 100644 --- a/galaxy_ng/app/tasks/publishing.py +++ b/galaxy_ng/app/tasks/publishing.py @@ -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 @@ -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() @@ -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() @@ -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, ) diff --git a/galaxy_ng/tests/integration/cli/test_cli_flow.py b/galaxy_ng/tests/integration/cli/test_cli_flow.py index 9830d1fd49..1865000fff 100644 --- a/galaxy_ng/tests/integration/cli/test_cli_flow.py +++ b/galaxy_ng/tests/integration/cli/test_cli_flow.py @@ -82,7 +82,7 @@ def test_publish_same_collection_version(ansible_config): check_retcode=1, ansible_config=ansible_config("admin", namespace=collection.namespace) ) - assert "duplicate key value violates unique constraint" in str(p.stderr) + assert "Artifact already exists" in str(p.stderr) @pytest.mark.cli diff --git a/galaxy_ng/tests/unit/app/test_tasks.py b/galaxy_ng/tests/unit/app/test_tasks.py index 8abc6d3979..5c40efa062 100644 --- a/galaxy_ng/tests/unit/app/test_tasks.py +++ b/galaxy_ng/tests/unit/app/test_tasks.py @@ -80,9 +80,9 @@ 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) @@ -90,15 +90,12 @@ def test_import_and_auto_approve(self, mocked_dispatch, mocked_import, mocked_ge 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 @@ -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' @@ -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 @@ -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):