Skip to content

Commit

Permalink
Merge pull request #1 from Moustafa-Moustafa/momousta/UploadSourcePac…
Browse files Browse the repository at this point in the history
…kageTest

Adding uploading source package tests
  • Loading branch information
Moustafa-Moustafa authored Jun 1, 2023
2 parents f9cc644 + bf7a78a commit f26ccdb
Show file tree
Hide file tree
Showing 10 changed files with 149 additions and 32 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ class Migration(migrations.Migration):

dependencies = [
('core', '0102_add_domain_relations'),
('deb', '0020_remove_fk_relations_in_packageindex_and_installerfileindex'),
('deb', '0022_alter_aptdistribution_distribution_ptr_and_more'),
]

operations = [
Expand Down
18 changes: 8 additions & 10 deletions pulp_deb/app/serializers/content_serializers.py
Original file line number Diff line number Diff line change
Expand Up @@ -1037,10 +1037,17 @@ class SourcePackageSerializer(MultipleArtifactContentSerializer):
def __init__(self, *args, **kwargs):
"""Initializer for DscFileSerializer."""
super().__init__(*args, **kwargs)
self.fields["artifacts"].read_only = True
if "artifact" in self.fields:
self.fields["artifacts"].read_only = True

def create(self, validated_data):
"""Create DscFileSerializer"""
source_package = SourcePackage.objects.filter(
source=validated_data["source"],
version=validated_data["version"],
)
if source_package.exists():
return source_package.first()
skip_keys = ["artifact", "files", "checksums_sha1", "checksums_sha256", "checksums_sha512"]
return super().create({k: validated_data[k] for k in validated_data if k not in skip_keys})

Expand Down Expand Up @@ -1078,15 +1085,6 @@ def deferred_validate(self, data):
)
)

content = SourcePackage.objects.filter(source=data["source"], version=data["version"])
if content.exists():
raise ValidationError(
_(
"There is already a DSC file with version '{version}' and source name "
"'{source}'."
).format(version=data["version"], source=data["source"])
)

artifacts = {data["relative_path"]: data["artifact"]}
for source in data["checksums_sha256"]:
content = Artifact.objects.filter(sha256=source["sha256"], size=source["size"])
Expand Down
1 change: 1 addition & 0 deletions pulp_deb/app/viewsets/content.py
Original file line number Diff line number Diff line change
Expand Up @@ -370,6 +370,7 @@ class Meta:
"maintainer",
"uploaders",
"homepage",
"relative_path",
"vcs_browser",
"vcs_arch",
"vcs_bzr",
Expand Down
80 changes: 62 additions & 18 deletions pulp_deb/tests/functional/api/test_crud_packages.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,28 +4,68 @@

from pulp_smash import utils

from pulp_deb.tests.functional.constants import DEB_PACKAGE_RELPATH
from pulp_deb.tests.functional.constants import (
DEB_PACKAGE_RELPATH,
DEB_SOURCE_PACKAGE_RELPATH,
DEB_SOURCE_PACKAGE_ARTIFACTS,
)
from pulp_deb.tests.functional.utils import get_local_package_absolute_path


@pytest.mark.parallel
def test_create_package(apt_package_api, deb_package_factory):
"""Verify all allowed CRUD actions are working and the ones that don't exist fail."""
# Create a package and verify its attributes
def create_source_package(deb_artifact_factory, deb_package_factory):
for artifact_name in DEB_SOURCE_PACKAGE_ARTIFACTS:
attrs = {
"file": get_local_package_absolute_path(artifact_name),
}
artifact = deb_artifact_factory(**attrs)
if artifact_name == DEB_SOURCE_PACKAGE_RELPATH:
dsc_artifact = artifact

attrs = {
"deb_source_package": {
"relative_path": DEB_SOURCE_PACKAGE_RELPATH,
"artifact": dsc_artifact.pulp_href,
},
}
package = deb_package_factory(**attrs)

return package


def create_binary_package(deb_artifact_factory, deb_package_factory):
attrs = {
"relative_path": DEB_PACKAGE_RELPATH,
"file": get_local_package_absolute_path(DEB_PACKAGE_RELPATH),
}
package = deb_package_factory(**attrs)
assert package.relative_path == DEB_PACKAGE_RELPATH

return package


@pytest.mark.parallel
@pytest.mark.parametrize("create_package, apt_package_api, package_relpath", [
(create_source_package, "source", DEB_SOURCE_PACKAGE_RELPATH),
(create_binary_package, "binary", DEB_PACKAGE_RELPATH),
], indirect=["apt_package_api"])
def test_create_package(
create_package,
apt_package_api,
package_relpath,
deb_artifact_factory,
deb_package_factory,
):
"""Verify all allowed CRUD actions are working and the ones that don't exist fail."""
# Create a package and verify its attributes
package = create_package(deb_artifact_factory, deb_package_factory)
assert package.relative_path == package_relpath

# Verify that only one package with this relative path exists
package_list = apt_package_api.list(relative_path=package.relative_path)
assert package_list.count == 1

# Verify that reading the package works and has the same attributes
package = apt_package_api.read(package.pulp_href)
assert package.relative_path == DEB_PACKAGE_RELPATH
assert package.relative_path == package_relpath

# Verify that partial update does not work for packages
with pytest.raises(AttributeError) as exc:
Expand All @@ -42,25 +82,29 @@ def test_create_package(apt_package_api, deb_package_factory):
apt_package_api.delete(package.pulp_href)
assert "object has no attribute 'delete'" in exc.value.args[0]


def test_same_sha256_same_relative_path_no_repo(apt_package_api, deb_package_factory):
@pytest.mark.parametrize("create_package, apt_package_api, package_relpath", [
(create_source_package, "source", DEB_SOURCE_PACKAGE_RELPATH),
(create_binary_package, "binary", DEB_PACKAGE_RELPATH),
], indirect=["apt_package_api"])
def test_same_sha256_same_relative_path_no_repo(
create_package,
apt_package_api,
package_relpath,
deb_artifact_factory,
deb_package_factory,
):
"""Test whether uploading the same package works and that it stays unique."""
attrs = {
"file": get_local_package_absolute_path(DEB_PACKAGE_RELPATH),
"relative_path": DEB_PACKAGE_RELPATH,
}

# Create the first package and verify its attributes
package_1 = deb_package_factory(**attrs)
assert package_1.relative_path == attrs["relative_path"]
package_1 = create_package(deb_artifact_factory, deb_package_factory)
assert package_1.relative_path == package_relpath

# Create the second package and verify it has the same href as the first one
package_2 = deb_package_factory(**attrs)
package_2 = create_package(deb_artifact_factory, deb_package_factory)
assert package_2.pulp_href == package_1.pulp_href
assert apt_package_api.read(package_1.pulp_href).pulp_href == package_2.pulp_href

# Verify the package is one
package_list = apt_package_api.list(relative_path=DEB_PACKAGE_RELPATH)
package_list = apt_package_api.list(relative_path=package_relpath)
assert package_list.count == 1


Expand Down
21 changes: 18 additions & 3 deletions pulp_deb/tests/functional/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
import os
import stat

from pulp_deb.tests.functional.utils import gen_deb_remote, gen_local_deb_remote
from pulp_deb.tests.functional.utils import gen_deb_remote, gen_local_deb_remote, sha256
from pulp_smash.utils import execute_pulpcore_python, uuid4
from pulp_deb.tests.functional.constants import DEB_FIXTURE_STANDARD_REPOSITORY_NAME

Expand All @@ -14,6 +14,7 @@
AptRepositorySyncURL,
ContentGenericContentsApi,
ContentPackagesApi,
ContentSourcePackagesApi,
DebAptPublication,
DebVerbatimPublication,
DistributionsAptApi,
Expand All @@ -23,7 +24,6 @@
RepositoriesAptApi,
)


@pytest.fixture
def apt_client(cid, bindings_cfg):
"""Fixture for APT client."""
Expand Down Expand Up @@ -63,8 +63,10 @@ def apt_distribution_api(apt_client):


@pytest.fixture
def apt_package_api(apt_client):
def apt_package_api(apt_client, request):
"""Fixture for APT package API."""
if hasattr(request, "param") and request.param == "source":
return ContentSourcePackagesApi(apt_client)
return ContentPackagesApi(apt_client)


Expand Down Expand Up @@ -95,6 +97,19 @@ def _deb_generic_content_factory(**kwargs):

return _deb_generic_content_factory

@pytest.fixture
def deb_artifact_factory(artifacts_api_client, gen_object_with_cleanup):
"""Fixture that generates artifacts with cleanup."""

def _deb_artifact_factory(**kwargs):
file_sha256 = sha256(kwargs["file"])
resp = artifacts_api_client.list(sha256=file_sha256)
if resp.count > 0:
return resp.results[0]
return gen_object_with_cleanup(artifacts_api_client, **kwargs)

return _deb_artifact_factory


@pytest.fixture
def deb_package_factory(apt_package_api, gen_object_with_cleanup):
Expand Down
6 changes: 6 additions & 0 deletions pulp_deb/tests/functional/constants.py
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,12 @@ def _clean_dict(d):
DEB_REPORT_CODE_SKIP_PACKAGE = "sync.package_index.was_skipped"

DEB_PACKAGE_RELPATH = "frigg_1.0_ppc64.deb"
DEB_SOURCE_PACKAGE_RELPATH = "hello_2.10-2ubuntu2.dsc"
DEB_SOURCE_PACKAGE_ARTIFACTS = [
DEB_SOURCE_PACKAGE_RELPATH,
"hello_2.10-2ubuntu2.debian.tar.xz",
"hello_2.10.orig.tar.gz",
]
DEB_GENERIC_CONTENT_RELPATH = "dists/ragnarok/asgard/binary-armeb/Release"
DEB_GENERIC_CONTENT_URL = urljoin(DEB_FIXTURE_URL, DEB_GENERIC_CONTENT_RELPATH)

Expand Down
Binary file not shown.
43 changes: 43 additions & 0 deletions pulp_deb/tests/functional/data/packages/hello_2.10-2ubuntu2.dsc
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA512

Format: 3.0 (quilt)
Source: hello
Binary: hello
Architecture: any
Version: 2.10-2ubuntu2
Maintainer: Ubuntu Developers <ubuntu-devel-discuss@lists.ubuntu.com>
Homepage: http://www.gnu.org/software/hello/
Standards-Version: 4.3.0
Testsuite: autopkgtest
Build-Depends: debhelper-compat (= 9)
Package-List:
hello deb devel optional arch=any
Checksums-Sha1:
f7bebf6f9c62a2295e889f66e05ce9bfaed9ace3 725946 hello_2.10.orig.tar.gz
560b84044b18d69dda0ad03e8e3792d6c571b763 6560 hello_2.10-2ubuntu2.debian.tar.xz
Checksums-Sha256:
31e066137a962676e89f69d1b65382de95a7ef7d914b8cb956f41ea72e0f516b 725946 hello_2.10.orig.tar.gz
2ae08bfe430c90986e74afbffdc7f98d1920527fd292963a567cb8c29d645b07 6560 hello_2.10-2ubuntu2.debian.tar.xz
Files:
6cd0ffea3884a4e79330338dcc2987d6 725946 hello_2.10.orig.tar.gz
446c355fd690fbebb49bdd51eeec6f6f 6560 hello_2.10-2ubuntu2.debian.tar.xz
Original-Maintainer: Santiago Vila <sanvila@debian.org>

-----BEGIN PGP SIGNATURE-----

iQJOBAEBCgA4FiEErEg/aN5yj0PyIC/KVo0w8yGyEz0FAlzmJ0caHHN0ZXZlLmxh
bmdhc2VrQHVidW50dS5jb20ACgkQVo0w8yGyEz2rkxAAg+yJexJtgzKhXiAqYTHW
9Us3VMVicCzxDJbeJN11J45Bx1d1L3m+pCHgDpyFnYGJDCuaABRE3HUURIOWbEVX
Zd5Jns6Aj28wIXPj35ju9swb5K7rghYnHJ++QViwaRDcB+KrEJyRprevDTBA37b7
KwYfY/hE7ZD8sMYeWHq5Ao814umOpNh7ruw2Jpc4GOcHEpcpt23cFLPKGumWVtWQ
PNvYg94yId8J8fcF2NTrp681i0QXmtCIT53d7HLpyBlXcA8IlrpSRBTpcwXnIUAv
MGTwYFQSEWYUw57H4XGzhJU1jGti5YgtIk78tce3r9m14oshVKXVJ0WsT1YZBAiY
ngFg7yb1jREMf+uNgCZZXFbXLMndyGoZls6UywvyoEQOiCMDCb7eeaXN2qrcRWpu
tF3p+alC2HAxWsgvv9A2pvhB86YMqDiOGs6+V4KuUleQpWhIlGQEIsasNqtVPKi0
64r1DwEbPYGJ28KSQd4YQEBYy1tcaiqwymfsdkDKD/a2xXCpMAFmJp35guLuezcs
RhiKEBkcW4i7nLLfW35N6zsMoL0kLC3GDmglQW4klFnMM+bqs909kXG3J41E9N9J
d9k+0doufnui6P/SxYlAmMpf7tkaAZAk0nXaC6/Hem9fDSqa6dP+mcZjzUZ+D9Jg
JMZ93pTsFDrozldams1rDKM=
=xqJQ
-----END PGP SIGNATURE-----
Binary file not shown.
10 changes: 10 additions & 0 deletions pulp_deb/tests/functional/utils.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
# coding=utf-8
"""Utilities for tests for the deb plugin."""
import hashlib
import os
from functools import partial
from unittest import SkipTest
Expand Down Expand Up @@ -320,3 +321,12 @@ def get_local_package_absolute_path(package_name):
"""
p = Path(__file__).parent.absolute()
return p.joinpath(f"data/packages/{package_name}")


def sha256(file: str) -> str:
file_hash = hashlib.sha256()
buffer_size = 4 * 1048
with open(file, "rb") as f:
while chunk := f.read(buffer_size):
file_hash.update(chunk)
return file_hash.hexdigest()

0 comments on commit f26ccdb

Please sign in to comment.