From 9e3cf4ba56522f174a5880d8ec3c8727e8a1282b Mon Sep 17 00:00:00 2001 From: Matthias Dellweg Date: Fri, 8 Dec 2023 15:12:10 +0100 Subject: [PATCH] WIP: Remove api v2 Removed the routing from urls. fixes #691 --- CHANGES/691.removal | 1 + pulp_ansible/app/galaxy/serializers.py | 145 ------- pulp_ansible/app/galaxy/views.py | 144 +------ pulp_ansible/app/urls.py | 20 - .../functional/api/collection/v2/__init__.py | 0 .../functional/api/collection/v2/test_sync.py | 376 ------------------ .../api/collection/v2/test_upload.py | 56 --- 7 files changed, 6 insertions(+), 736 deletions(-) create mode 100644 CHANGES/691.removal delete mode 100644 pulp_ansible/tests/functional/api/collection/v2/__init__.py delete mode 100644 pulp_ansible/tests/functional/api/collection/v2/test_sync.py delete mode 100644 pulp_ansible/tests/functional/api/collection/v2/test_upload.py diff --git a/CHANGES/691.removal b/CHANGES/691.removal new file mode 100644 index 000000000..f0c95705f --- /dev/null +++ b/CHANGES/691.removal @@ -0,0 +1 @@ +Removed the galaxy v2 apis. The v3 apis should be used instead. diff --git a/pulp_ansible/app/galaxy/serializers.py b/pulp_ansible/app/galaxy/serializers.py index 71818ee91..c0179d120 100644 --- a/pulp_ansible/app/galaxy/serializers.py +++ b/pulp_ansible/app/galaxy/serializers.py @@ -52,148 +52,3 @@ def get_source(self, obj) -> str: class Meta: fields = ("name", "source") model = Role - - -class GalaxyCollectionSerializer(serializers.Serializer): - """ - A serializer for a Collection. - """ - - id = serializers.CharField(source="pulp_id") - name = serializers.CharField() - namespace = serializers.SerializerMethodField() - href = serializers.SerializerMethodField(read_only=True) - versions_url = serializers.SerializerMethodField(read_only=True) - created = serializers.DateTimeField(source="pulp_created") - modified = serializers.DateTimeField(source="pulp_last_updated") - latest_version = serializers.SerializerMethodField() - - @extend_schema_field(OpenApiTypes.OBJECT) - def get_namespace(self, obj): - """Create a namespace dict.""" - return {"name": obj.namespace} - - def get_versions_url(self, obj) -> str: - """ - Get versions_url. - """ - return ( - "{hostname}/pulp_ansible/galaxy/{path}/api/v2/collections/{namespace}/{name}/" - "versions/".format( - path=self.context["path"], - hostname=settings.ANSIBLE_API_HOSTNAME, - namespace=obj.namespace, - name=obj.name, - ) - ) - - def get_href(self, obj) -> str: - """ - Get href. - """ - return ( - "{hostname}/pulp_ansible/galaxy/{path}/api/v2/collections/{namespace}/" - "{name}/".format( - path=self.context["path"], - hostname=settings.ANSIBLE_API_HOSTNAME, - namespace=obj.namespace, - name=obj.name, - ) - ) - - @extend_schema_field(OpenApiTypes.OBJECT) - def get_latest_version(self, obj): - """ - Get latest version. - """ - rv = obj.versions.filter(is_highest=True).first() - href = reverse( - "v2-collection-versions-detail", - kwargs={ - "path": self.context["path"], - "namespace": obj.namespace, - "name": obj.name, - "version": rv.version, - }, - ) - return {"href": href, "version": rv.version} - - class Meta: - fields = ( - "id", - "href", - "name", - "namespace", - "versions_url", - "latest_version", - "created", - "modified", - ) - model = Collection - - -class GalaxyCollectionVersionSerializer(serializers.Serializer): - """ - A serializer for a CollectionVersion. - """ - - version = serializers.CharField() - href = serializers.SerializerMethodField(read_only=True) - namespace = serializers.SerializerMethodField(read_only=True) - collection = serializers.SerializerMethodField(read_only=True) - artifact = serializers.SerializerMethodField(read_only=True) - metadata = CollectionMetadataSerializer(source="*") - - def get_href(self, obj) -> str: - """ - Get href. - """ - return ( - "{hostname}/pulp_ansible/galaxy/{path}/api/v2/collections/{namespace}/{name}/" - "versions/{version}/".format( - path=self.context["path"], - hostname=settings.ANSIBLE_API_HOSTNAME, - namespace=obj.collection.namespace, - name=obj.collection.name, - version=obj.version, - ) - ) - - @extend_schema_field(OpenApiTypes.OBJECT) - def get_namespace(self, obj): - """Create a namespace dict.""" - return {"name": obj.collection.namespace} - - @extend_schema_field(OpenApiTypes.OBJECT) - def get_collection(self, obj): - """Create a collection dict.""" - return {"name": obj.collection.name} - - @extend_schema_field(OpenApiTypes.OBJECT) - def get_artifact(self, obj): - """Create an artifact dict.""" - artifact = obj.contentartifact_set.get().artifact - return {"sha256": artifact.sha256, "size": artifact.size} - - class Meta: - fields = ("version", "href", "metadata") - model = CollectionVersion - - -class GalaxyCollectionUploadSerializer(serializers.Serializer): - """ - A serializer for Collection Uploads. - """ - - file = serializers.FileField( - help_text=_("The file containing the Artifact binary data."), required=True - ) - - def validate(self, data): - """Ensure duplicate artifact isn't uploaded.""" - data = super().validate(data) - sha256 = data["file"].hashers["sha256"].hexdigest() - artifact = Artifact.objects.filter(sha256=sha256).first() - if artifact: - raise serializers.ValidationError(_("Artifact already exists")) - return data diff --git a/pulp_ansible/app/galaxy/views.py b/pulp_ansible/app/galaxy/views.py index d48130bb8..40d8b793f 100644 --- a/pulp_ansible/app/galaxy/views.py +++ b/pulp_ansible/app/galaxy/views.py @@ -1,25 +1,12 @@ import re -from django.conf import settings -from django.shortcuts import get_object_or_404, HttpResponse +from django.shortcuts import get_object_or_404 from drf_spectacular.utils import extend_schema, extend_schema_view -from rest_framework import generics, pagination, response, views -from rest_framework.reverse import reverse +from rest_framework import generics, response, views -from pulpcore.plugin.models import PulpTemporaryFile -from pulpcore.plugin.viewsets import OperationPostponedResponse -from pulpcore.plugin.models import ContentArtifact +from pulp_ansible.app.models import AnsibleDistribution, Role -from pulp_ansible.app.galaxy.mixins import UploadGalaxyCollectionMixin -from pulp_ansible.app.models import AnsibleDistribution, Collection, CollectionVersion, Role - -from .serializers import ( - GalaxyCollectionSerializer, - GalaxyCollectionUploadSerializer, - GalaxyCollectionVersionSerializer, - GalaxyRoleSerializer, - GalaxyRoleVersionSerializer, -) +from .serializers import GalaxyRoleSerializer, GalaxyRoleVersionSerializer class DistributionMixin: @@ -76,7 +63,7 @@ def get(self, request, **kwargs): """ Return a response to the "GET" action. """ - available_versions = {"v1": "v1/", "v2": "v2/", "v3": "v3/"} + available_versions = {"v1": "v1/", "v3": "v3/"} if self.v3_only: available_versions = {"v3": "v3/"} @@ -132,124 +119,3 @@ def get_queryset(self): namespace, name = re.split(r"\.", self.kwargs["role_pk"]) versions = Role.objects.filter(pk__in=self._distro_content, name=name, namespace=namespace) return versions - - -class GalaxyCollectionDetailView(DistributionMixin, generics.RetrieveAPIView): - """ - View for a Collection Detail. - """ - - model = Collection - serializer_class = GalaxyCollectionSerializer - authentication_classes = [] - permission_classes = [] - - def get(self, request, path=None, namespace=None, name=None): - """ - Get the detail view of a Collection. - """ - # This seems wrong, no repository scoping occurring - collection = get_object_or_404(Collection, namespace=namespace, name=name) - context = self.get_serializer_context() - return response.Response(GalaxyCollectionSerializer(collection, context=context).data) - - -class GalaxyCollectionView(DistributionMixin, UploadGalaxyCollectionMixin, generics.ListAPIView): - """ - View for Collection models. - """ - - model = Collection - serializer_class = GalaxyCollectionSerializer - authentication_classes = [] - permission_classes = [] - pagination_class = pagination.PageNumberPagination - - def get_queryset(self): - """ - Get the list of Collections for this view. - """ - return Collection.objects.filter(versions__pk__in=self._distro_content).distinct() - - @extend_schema(deprecated=True) - def post(self, request, path): - """ - Queues a task that creates a new Collection from an uploaded artifact. - """ - distro = get_object_or_404(AnsibleDistribution, base_path=path) - if not distro.repository and not distro.repository_version: - return HttpResponse(status=400, reason="Distribution has no repository.") - - serializer = GalaxyCollectionUploadSerializer( - data=request.data, context={"request": request} - ) - serializer.is_valid(raise_exception=True) - - temp_file = PulpTemporaryFile.init_and_validate(serializer.validated_data["file"]) - temp_file.save() - - async_result = self._dispatch_import_collection_task(temp_file.pk, distro.repository) - return OperationPostponedResponse(async_result, request) - - -@extend_schema_view(get=extend_schema(operation_id="api_v2_collection_versions_list")) -class GalaxyCollectionVersionList(DistributionMixin, generics.ListAPIView): - """ - APIView for Collections by namespace/name. - """ - - model = CollectionVersion - serializer_class = GalaxyCollectionVersionSerializer - pagination_class = pagination.PageNumberPagination - authentication_classes = [] - permission_classes = [] - - def get_queryset(self): - """ - Get the list of items for this view. - """ - collection = get_object_or_404( - Collection, namespace=self.kwargs["namespace"], name=self.kwargs["name"] - ) - versions = collection.versions.filter(pk__in=self._distro_content) - - return versions - - -class GalaxyCollectionVersionDetail(DistributionMixin, generics.GenericAPIView): - """ - APIView for Galaxy Collections Detail view. - """ - - authentication_classes = [] - permission_classes = [] - - def get(self, request, path, namespace, name, version): - """ - Return a response to the "GET" action. - """ - version = CollectionVersion.objects.get( - collection__namespace=namespace, collection__name=name, version=version - ) - - get_object_or_404( - ContentArtifact, content__in=self._distro_content, relative_path=version.relative_path - ) - - # Normally would just pass request to reverse and DRF would automatically build the - # absolute URI for us. However there's a weird bug where, because there's a kwarg - # in the URL for this view called "version" (the collection version), DRF gets - # confused and thinks we're using a versioning scheme - # (https://www.django-rest-framework.org/api-guide/versioning/) and attempts to insert - # the version into kwargs, which causes the reverse lookup to fail. - path = reverse( - settings.ANSIBLE_URL_NAMESPACE + "collection-artifact-download", - kwargs={"distro_base_path": self.kwargs["path"], "filename": version.relative_path}, - ) - context = self.get_serializer_context() - request = context["request"] - download_url = request.build_absolute_uri(path) - - data = GalaxyCollectionVersionSerializer(version, context=context).data - data["download_url"] = download_url - return response.Response(data) diff --git a/pulp_ansible/app/urls.py b/pulp_ansible/app/urls.py index 7ce8bd036..02013ebad 100644 --- a/pulp_ansible/app/urls.py +++ b/pulp_ansible/app/urls.py @@ -2,10 +2,6 @@ from django.urls import include, path, re_path from pulp_ansible.app.galaxy.views import ( - GalaxyCollectionVersionDetail, - GalaxyCollectionVersionList, - GalaxyCollectionDetailView, - GalaxyCollectionView, GalaxyVersionView, RoleList, RoleVersionList, @@ -25,21 +21,6 @@ path("roles//versions/", RoleVersionList.as_view()), ] -v2_urls = [ - path("collections/", GalaxyCollectionView.as_view()), - path("collections///", GalaxyCollectionDetailView.as_view()), - path("collections///versions/", GalaxyCollectionVersionList.as_view()), - path( - "collections///versions//", - GalaxyCollectionVersionDetail.as_view(), - name="v2-collection-versions-detail", - ), - path( - "collection-imports//", - views_v3.CollectionImportViewSet.as_view({"get": "retrieve"}), - ), -] - # Legacy urls that need to be redirected to plugin/ansible/collections// legacy_v3_collection_urls = [ path( @@ -266,7 +247,6 @@ GalaxyVersionView.as_view(v3_only=True), ), path(GALAXY_API_ROOT + "v1/", include(v1_urls)), - path(GALAXY_API_ROOT + "v2/", include(v2_urls)), path(GALAXY_API_ROOT + "v3/", include(v3_urls)), path(GALAXY_API_ROOT, GalaxyVersionView.as_view()), re_path(r"^pulp/api/v3/ansible/copy/$", CopyViewSet.as_view({"post": "create"})), diff --git a/pulp_ansible/tests/functional/api/collection/v2/__init__.py b/pulp_ansible/tests/functional/api/collection/v2/__init__.py deleted file mode 100644 index e69de29bb..000000000 diff --git a/pulp_ansible/tests/functional/api/collection/v2/test_sync.py b/pulp_ansible/tests/functional/api/collection/v2/test_sync.py deleted file mode 100644 index c3dcce336..000000000 --- a/pulp_ansible/tests/functional/api/collection/v2/test_sync.py +++ /dev/null @@ -1,376 +0,0 @@ -"""Tests collection sync using the Galaxy V2 API.""" -import pytest -from pulpcore.client.pulp_ansible import ApiException -from pulpcore.client.pulp_ansible import ( - PulpAnsibleDefaultApiV3PluginAnsibleSearchCollectionVersionsApi, -) - -from pulp_ansible.tests.functional.utils import ( - gen_ansible_remote, - SyncHelpersMixin, - TestCaseUsingBindings, -) -from pulp_smash.pulp3.bindings import monitor_task, delete_orphans - - -# TODO: Rewrite or delete? -class SyncTestCase(TestCaseUsingBindings, SyncHelpersMixin): - """Galaxy V2 Collection sync tests.""" - - def test_sync_simple_collections_file(self): - """Sync with simple requirements file, expected to download one CollectionVersion.""" - body = gen_ansible_remote( - url="https://old-galaxy.ansible.com/api/", - requirements_file="collections:\n - testing.k8s_demo_collection", - 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) - - content = self.cv_api.list(repository_version=f"{repo.pulp_href}versions/1/") - self.assertEqual(len(content.results), 1) - - @pytest.mark.skip("TODO: Move to V3 tests") - def test_sync_with_slash(self): - """Sync with a slash used in remote.url, expected to download one CollectionVersion.""" - body = gen_ansible_remote( - url="https://old-galaxy.ansible.com/api/", - requirements_file="collections:\n - testing.k8s_demo_collection", - 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) - - 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://old-galaxy.ansible.com/api/", - 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://old-galaxy.ansible.com/api/", - 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( - url="https://old-galaxy.ansible.com/api/", - requirements_file="collections:\n - testing.k8s_demo_collection", - sync_dependencies=False, - ) - remote = self.remote_collection_api.create(body) - self.addCleanup(self.remote_collection_api.delete, remote.pulp_href) - - repo = self._create_repo_with_attached_remote_and_sync(remote) - - content = self.cv_api.list(repository_version=f"{repo.pulp_href}versions/1/") - self.assertEqual(len(content.results), 1) - - def test_successive_syncs_repo_version(self): - """Test whether successive syncs do not produce more repository versions.""" - body = gen_ansible_remote( - url="https://old-galaxy.ansible.com/api/", - requirements_file="collections:\n - testing.k8s_demo_collection", - sync_dependencies=False, - ) - remote = self.remote_collection_api.create(body) - self.addCleanup(self.remote_collection_api.delete, remote.pulp_href) - - repo = self._create_repo_with_attached_remote_and_sync(remote) - self.assertEqual(repo.latest_version_href, f"{repo.versions_href}1/") - repo = self._sync_repo(repo) - self.assertEqual(repo.latest_version_href, f"{repo.versions_href}1/") - - def test_sync_with_multiple_versions_pages(self): - """Sync with requirements.yml that requires parsing multiple "versions" pages.""" - requirements_file_string = "\n" "---\n" "collections:\n" "- name: amazon.aws\n" - body = gen_ansible_remote( - url="https://old-galaxy.ansible.com/api/", - requirements_file=requirements_file_string, - 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) - - content = self.cv_api.list(repository_version=f"{repo.pulp_href}versions/1/") - self.assertGreaterEqual(len(content.results), 1) - - def test_sync_with_invalid_requirements(self): - """Sync with invalid requirement.""" - body = gen_ansible_remote( - url="https://old-galaxy.ansible.com/api/", - requirements_file="INAVLID", - sync_dependencies=False, - ) - self.assertRaises(ApiException, self.remote_collection_api.create, body) - - def test_sync_collection_missing_requires_ansible(self): - """Sync a collection with the expected `requires_ansible` data missing.""" - body = gen_ansible_remote( - url="https://old-galaxy.ansible.com/api/", - requirements_file="collections:\n - name: inexio.thola\n version: 1.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) - - content = self.cv_api.list(repository_version=f"{repo.pulp_href}versions/1/") - self.assertGreaterEqual(len(content.results), 1) - - @pytest.mark.nightly - def test_mirror_galaxy(self): - """Mirror Galaxy.""" - body = gen_ansible_remote(url="https://galaxy.ansible.com") - 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) - - content = self.cv_api.list(repository_version=f"{repo.pulp_href}versions/1/") - self.assertGreaterEqual(content.count, 300) - - -class RequirementsFileVersionsTestCase(TestCaseUsingBindings, SyncHelpersMixin): - """Galaxy V2 Collection sync tests for version restriction.""" - - def test_sync_with_version_equality(self): - """Sync using a requirements.yml with an equality specifier.""" - requirements_file_string = ( - "\n" - "---\n" - "collections:\n" - "- name: robertdebock.ansible_development_environment\n" - ' version: "==1.0.1"\n' - ) - body = gen_ansible_remote( - url="https://old-galaxy.ansible.com/api/", - requirements_file=requirements_file_string, - 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) - - content = self.cv_api.list(repository_version=f"{repo.pulp_href}versions/1/") - self.assertGreaterEqual(len(content.results), 1) - - def test_sync_with_two_equality_entries_on_different_lines(self): - """Sync using a requirements.yml with the same collection used on two lines.""" - requirements_file_string = ( - "\n" - "---\n" - "collections:\n" - "- name: robertdebock.ansible_development_environment\n" - ' version: "1.0.1"\n' - "- name: robertdebock.ansible_development_environment\n" - ' version: "1.0.0"\n' - ) - body = gen_ansible_remote( - url="https://old-galaxy.ansible.com/api/", - requirements_file=requirements_file_string, - 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) - - content = self.cv_api.list(repository_version=f"{repo.pulp_href}versions/1/") - self.assertGreaterEqual(len(content.results), 2) - - def test_sync_with_one_version_inequality(self): - """Sync using a requirements.yml with one version inequality.""" - requirements_file_string = ( - "\n" - "---\n" - "collections:\n" - "- name: robertdebock.ansible_development_environment\n" - ' version: ">=1.0.1"\n' - ) - body = gen_ansible_remote( - url="https://old-galaxy.ansible.com/api/", - requirements_file=requirements_file_string, - 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) - - content = self.cv_api.list(repository_version=f"{repo.pulp_href}versions/1/") - self.assertGreaterEqual(len(content.results), 2) - - def test_sync_with_two_version_inequalities(self): - """Sync using a requirements.yml with two version inequalities.""" - requirements_file_string = ( - "\n" - "---\n" - "collections:\n" - "- name: robertdebock.ansible_development_environment\n" - ' version: ">1.0.0,<1.0.2"\n' - ) - body = gen_ansible_remote( - url="https://old-galaxy.ansible.com/api/", - requirements_file=requirements_file_string, - 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) - - content = self.cv_api.list(repository_version=f"{repo.pulp_href}versions/1/") - self.assertGreaterEqual(len(content.results), 1) - - def test_sync_and_rebuild(self): - """Sync with simple requirements file, expected to download one CollectionVersion.""" - # /pulp/api/v3/repositories/ansible/ansible//rebuild_metadata/ - # pulp_ansible.app.viewsets.AnsibleRepositoryViewSet - # repositories-ansible/ansible-rebuild-metadata - # /pulp/api/v3/repositories/ansible/ansible//rebuild_metadata\./ - # pulp_ansible.app.viewsets.AnsibleRepositoryViewSet - # repositories-ansible/ansible-rebuild-metadata - - body = gen_ansible_remote( - url="https://old-galaxy.ansible.com/api/", - requirements_file="collections:\n - name: community.docker\n version: 3.0.0", - sync_dependencies=False, - ) - remote = self.remote_collection_api.create(body) - self.addCleanup(self.remote_collection_api.delete, remote.pulp_href) - - # this will make the repo and sync the collection from upstream into it - # when the builtin repo cleanup is called, the collection will be - # orphaned and testcaseusebindings will delete it via orphan_cleanup - # in the teardown method - repo = self._create_repo_and_sync_with_remote(remote) - - # get the before data - before = self.cv_api.list(repository_version=repo.latest_version_href) - before = before.results[0].to_dict() - assert before["docs_blob"] == {} - - # pass in the namespace/name/version just to verify kwargs are allowed - rebuild_task = self.repo_api.rebuild_metadata( - repo.pulp_href, {"namespace": "community", "name": "docker", "version": "3.0.0"} - ) - res = monitor_task(rebuild_task.task) - assert res.state == "completed" - - # get the after data - after = self.cv_api.list(repository_version=repo.latest_version_href) - after = after.results[0].to_dict() - - assert after["docs_blob"]["collection_readme"]["name"] == "README.md" - assert ( - "

Docker Community Collection

" - in after["docs_blob"]["collection_readme"]["html"] - ) - - def test_sync_and_rebuild_version(self): - """Sync with simple requirements file, expected to download one CollectionVersion.""" - # pulp/api/v3/repositories/ansible/ansible//versions//rebuild_metadata/ - # pulp_ansible.app.viewsets.AnsibleRepositoryVersionViewSet - # versions-rebuild-metadata - - # clean up collection versions from previous test - delete_orphans() - - body = gen_ansible_remote( - url="https://old-galaxy.ansible.com/api/", - requirements_file="collections:\n - name: community.docker\n version: 3.0.0", - sync_dependencies=False, - ) - remote = self.remote_collection_api.create(body) - self.addCleanup(self.remote_collection_api.delete, remote.pulp_href) - - # this will make the repo and sync the collection from upstream into it - # when the builtin repo cleanup is called, the collection will be - # orphaned and testcaseusebindings will delete it via orphan_cleanup - # in the teardown method - repo = self._create_repo_and_sync_with_remote(remote) - - # get the before data - before = self.cv_api.list(repository_version=repo.latest_version_href) - - before = before.results[0].to_dict() - assert before["docs_blob"] == {} - - rebuild_version_task = self.repo_version_api.rebuild_metadata( - repo.latest_version_href, - {"namespace": "community", "name": "docker", "version": "3.0.0"}, - ) - - res = monitor_task(rebuild_version_task.task) - assert res.state == "completed" - - # get the after data - after = self.cv_api.list(repository_version=repo.latest_version_href) - after = after.results[0].to_dict() - - assert after["docs_blob"]["collection_readme"]["name"] == "README.md" - assert ( - "

Docker Community Collection

" - in after["docs_blob"]["collection_readme"]["html"] - ) - - def test_sync_updates_cv_index(self): - # clean up collection versions from previous test - delete_orphans() - - body = gen_ansible_remote( - url="https://old-galaxy.ansible.com/api/", - requirements_file="collections:\n - name: community.molecule\n version: 0.1.0", - sync_dependencies=False, - ) - remote = self.remote_collection_api.create(body) - self.addCleanup(self.remote_collection_api.delete, remote.pulp_href) - - # this will make the repo and sync the collection from upstream into it - # when the builtin repo cleanup is called, the collection will be - # orphaned and testcaseusebindings will delete it via orphan_cleanup - # in the teardown method - repo = self._create_repo_and_sync_with_remote(remote, distribution=True) - - sc = PulpAnsibleDefaultApiV3PluginAnsibleSearchCollectionVersionsApi(self.client) - cv_list = sc.list(repository_name=[repo.name]) - assert cv_list.meta.count == 1 - assert cv_list.data[0].collection_version.namespace == "community" - assert cv_list.data[0].collection_version.name == "molecule" - assert cv_list.data[0].collection_version.version == "0.1.0" diff --git a/pulp_ansible/tests/functional/api/collection/v2/test_upload.py b/pulp_ansible/tests/functional/api/collection/v2/test_upload.py deleted file mode 100644 index 9e68adbc4..000000000 --- a/pulp_ansible/tests/functional/api/collection/v2/test_upload.py +++ /dev/null @@ -1,56 +0,0 @@ -"""Tests related to upload of collections.""" -import hashlib -from tempfile import NamedTemporaryFile - -from pulp_smash.pulp3.bindings import delete_orphans, monitor_task, PulpTestCase -from pulp_smash.utils import http_get - -from pulpcore.client.pulp_ansible import ( - AnsibleCollectionsApi, - ContentCollectionVersionsApi, -) - -from pulp_ansible.tests.functional.utils import gen_ansible_client - - -# TODO: Check if this url will stay around -COLLECTION_URL = "https://galaxy.ansible.com/download/pulp-squeezer-0.0.9.tar.gz" - - -class UploadCollectionTestCase(PulpTestCase): - """Upload a collection.""" - - @classmethod - def setUpClass(cls): - """Create class-wide variables.""" - delete_orphans() - cls.client = gen_ansible_client() - cls.collections_api = AnsibleCollectionsApi(cls.client) - cls.content_api = ContentCollectionVersionsApi(cls.client) - - @classmethod - def tearDownClass(cls): - """Clean up after tests.""" - delete_orphans() - - def upload_collection(self, url=COLLECTION_URL): - """Uploada collection.""" - collection_content = http_get(url) - self.collection_sha256 = hashlib.sha256(collection_content).hexdigest() - with NamedTemporaryFile() as temp_file: - temp_file.write(collection_content) - response = self.collections_api.upload_collection(file=temp_file.name) - collection_version_href = monitor_task(response.task).created_resources[0] - return self.content_api.read(collection_version_href) - - def test_collection_upload(self): - """Upload a collection. - - This test targets the following issue: - - * `Pulp #5262 `_ - """ - response = self.upload_collection() - self.assertEqual(response.sha256, self.collection_sha256, response) - - delete_orphans()