diff --git a/CHANGES/1101.bugfix b/CHANGES/1101.bugfix new file mode 100644 index 00000000..0bfcf6d5 --- /dev/null +++ b/CHANGES/1101.bugfix @@ -0,0 +1 @@ +Fixed edge case where metadata file did not match wheel metadata. diff --git a/pulp_python/app/utils.py b/pulp_python/app/utils.py index b66edb97..14385092 100644 --- a/pulp_python/app/utils.py +++ b/pulp_python/app/utils.py @@ -220,9 +220,12 @@ def extract_wheel_metadata(filename: str) -> bytes | None: return None try: with zipfile.ZipFile(filename, "r") as f: - for file_path in f.namelist(): - if file_path.endswith(".dist-info/METADATA"): - return f.read(file_path) + metadata_paths = [p for p in f.namelist() if p.endswith("METADATA")] + sorted_paths = sorted(metadata_paths, key=lambda s: s.count("/")) + for metadata_path in sorted_paths: + file = f.read(metadata_path) + if b"Metadata-Version" in file: + return file except (zipfile.BadZipFile, KeyError, OSError) as e: log.warning(f"Failed to extract metadata file from {filename}: {e}") return None diff --git a/pulp_python/tests/functional/api/test_crud_content_unit.py b/pulp_python/tests/functional/api/test_crud_content_unit.py index 2aac0a98..3abf8fa7 100644 --- a/pulp_python/tests/functional/api/test_crud_content_unit.py +++ b/pulp_python/tests/functional/api/test_crud_content_unit.py @@ -200,4 +200,6 @@ def test_package_creation_with_metadata( distro = python_distribution_factory(repository=python_repo) # Test that metadata is accessible - ensure_metadata(pulp_content_url, distro.base_path, PYTHON_WHEEL_FILENAME) + ensure_metadata( + pulp_content_url, distro.base_path, PYTHON_WHEEL_FILENAME, "shelf-reader", "0.1" + ) diff --git a/pulp_python/tests/functional/api/test_pypi_apis.py b/pulp_python/tests/functional/api/test_pypi_apis.py index ce1eced7..35d269ad 100644 --- a/pulp_python/tests/functional/api/test_pypi_apis.py +++ b/pulp_python/tests/functional/api/test_pypi_apis.py @@ -165,7 +165,9 @@ def test_package_upload_with_metadata( assert summary.added["python.python"]["count"] == 1 # Test that metadata is accessible - ensure_metadata(pulp_content_url, distro.base_path, PYTHON_WHEEL_FILENAME) + ensure_metadata( + pulp_content_url, distro.base_path, PYTHON_WHEEL_FILENAME, "shelf-reader", "0.1" + ) @pytest.mark.parallel diff --git a/pulp_python/tests/functional/api/test_sync.py b/pulp_python/tests/functional/api/test_sync.py index 5b19ae6b..a11806a5 100644 --- a/pulp_python/tests/functional/api/test_sync.py +++ b/pulp_python/tests/functional/api/test_sync.py @@ -354,4 +354,6 @@ def test_package_sync_with_metadata( distro = python_distribution_factory(repository=repo) # Test that metadata is accessible - ensure_metadata(pulp_content_url, distro.base_path, "pytz-2023.2-py2.py3-none-any.whl") + ensure_metadata( + pulp_content_url, distro.base_path, "pytz-2023.2-py2.py3-none-any.whl", "pytz", "2023.2" + ) diff --git a/pulp_python/tests/functional/api/test_upload.py b/pulp_python/tests/functional/api/test_upload.py index 5d2e207e..43c65cdf 100644 --- a/pulp_python/tests/functional/api/test_upload.py +++ b/pulp_python/tests/functional/api/test_upload.py @@ -3,6 +3,7 @@ from pulp_python.tests.functional.constants import ( PYTHON_EGG_FILENAME, PYTHON_EGG_URL, + PYTHON_FIXTURES_URL, PYTHON_WHEEL_FILENAME, PYTHON_WHEEL_URL, PYTHON_EGG_SHA256, @@ -61,7 +62,9 @@ def test_synchronous_package_upload_with_metadata( """ Test that the synchronous upload of a Python wheel package creates a metadata artifact. """ - python_file = download_python_file(PYTHON_WHEEL_FILENAME, PYTHON_WHEEL_URL) + wheel_filename = "setuptools-80.9.0-py3-none-any.whl" + wheel_url = urljoin(urljoin(PYTHON_FIXTURES_URL, "packages/"), wheel_filename) + python_file = download_python_file(wheel_filename, wheel_url) content_body = {"file": python_file} content = python_bindings.ContentPackagesApi.upload(**content_body) @@ -70,7 +73,7 @@ def test_synchronous_package_upload_with_metadata( distro = python_distribution_factory(repository=python_repo) # Test that metadata is accessible - ensure_metadata(pulp_content_url, distro.base_path, PYTHON_WHEEL_FILENAME) + ensure_metadata(pulp_content_url, distro.base_path, wheel_filename, "setuptools", "80.9.0") @pytest.mark.parallel diff --git a/pulp_python/tests/functional/utils.py b/pulp_python/tests/functional/utils.py index b47215aa..9d672b80 100644 --- a/pulp_python/tests/functional/utils.py +++ b/pulp_python/tests/functional/utils.py @@ -123,7 +123,7 @@ def explore_links(page_url, page_name, links_found, msgs): return len(msgs) == 0, msgs -def ensure_metadata(pulp_content_url, distro_base_path, filename): +def ensure_metadata(pulp_content_url, distro_base_path, filename, name, version): """ Tests that metadata is accessible for a given wheel package filename. """ @@ -132,4 +132,5 @@ def ensure_metadata(pulp_content_url, distro_base_path, filename): metadata_response = requests.get(metadata_url) assert metadata_response.status_code == 200 assert len(metadata_response.content) > 0 - assert "Name: " in metadata_response.text + assert f"Name: {name}" in metadata_response.text + assert f"Version: {version}" in metadata_response.text