Skip to content

armgit / floci

Public Imported from GitHub
0

Light, fluffy, and always free - The AWS Local Emulator alternative

fix(codeartifact): pypi can have more than one asset per version in ListPackageVersionAssets (#6259)

Priamo Germosen49ed87c81535
diff --git a/src/main/java/io/github/hectorvent/floci/services/codeartifact/CodeArtifactService.java b/src/main/java/io/github/hectorvent/floci/services/codeartifact/CodeArtifactService.java
index e3eaee5016..ef51ca51fc 100644
--- a/src/main/java/io/github/hectorvent/floci/services/codeartifact/CodeArtifactService.java
+++ b/src/main/java/io/github/hectorvent/floci/services/codeartifact/CodeArtifactService.java
@@ -829,10 +829,10 @@ public class CodeArtifactService implements Resettable {
* size and hash, the same way {@link #getContainerBackedPackageVersionAsset} already computes
* a single asset's hash on the fly rather than trusting a stored value that doesn't exist.
*
- * <p>{@code npm}/{@code pypi}/{@code nuget} genuinely have exactly one asset per version
- * (confirmed by {@link RepositorySidecarManager#listPackageVersionAssetNames}'s own
- * implementations for those three); only {@code maven} and {@code generic} can return more
- * than one.
+ * <p>{@code npm}/{@code nuget} genuinely have exactly one asset per version. {@code pypi} does
+ * not: a single release can hold both a source distribution and a wheel, and {@link
+ * PypiserverSidecarClient#listPackageVersionAssetNames} already lists every filename
+ * pypiserver's own per-project JSON reports for that version, not just one.
*
* <p>For a container-backed format, only the names are enumerated up front; {@code
* maxResults}/{@code nextToken} are applied to that name list before any byte is fetched, so
diff --git a/src/test/java/io/github/hectorvent/floci/services/codeartifact/CodeArtifactPypiDockerIntegrationTest.java b/src/test/java/io/github/hectorvent/floci/services/codeartifact/CodeArtifactPypiDockerIntegrationTest.java
index 9a9856553a..f8f6ec83a7 100644
--- a/src/test/java/io/github/hectorvent/floci/services/codeartifact/CodeArtifactPypiDockerIntegrationTest.java
+++ b/src/test/java/io/github/hectorvent/floci/services/codeartifact/CodeArtifactPypiDockerIntegrationTest.java
@@ -620,11 +620,13 @@ class CodeArtifactPypiDockerIntegrationTest {
/**
* The real-world gap this closes: {@code ListPackageVersionAssets} used to not exist at all.
- * Confirms the bridge enumerates pypi's one-asset-per-version simple index entry against the
- * real pypiserver sidecar (not a mock) and reports the exact byte size and hash of a freshly
- * uploaded file. A throwaway package, not {@link #PACKAGE_NAME}: {@link
+ * Confirms the bridge enumerates a single-asset pypi version against the real pypiserver
+ * sidecar (not a mock) and reports the exact byte size and hash of a freshly uploaded file. A
+ * throwaway package, not {@link #PACKAGE_NAME}: {@link
* #deletePackageRemovesTheFileFromTheRealPypiserverSidecar} deletes that fixture for good with
- * no republish afterward, unlike this format's sibling test classes.
+ * no republish afterward, unlike this format's sibling test classes. {@link
+ * #listPackageVersionAssetsListsBothTheSdistAndTheWheelOfOneVersion} covers the multi-asset
+ * case this one doesn't.
*/
@Test
@Order(18)
@@ -672,6 +674,60 @@ class CodeArtifactPypiDockerIntegrationTest {
.body("versions[0].origin.originType", equalTo("INTERNAL"));
}
+ /**
+ * The bug this fixes: {@code CodeArtifactService#listPackageVersionAssets}'s own javadoc used
+ * to claim {@code pypi} genuinely has exactly one asset per version, the same wrong assumption
+ * {@link #listPackageVersionAssetsBridgesToTheRealPypiserverSidecar}'s javadoc made. A real
+ * pypi release commonly carries both a source distribution and a wheel for the same version
+ * (the exact shape {@link #copyPackageVersionsOverwriteKeepsEveryAssetWhenAPypiReleaseHasBothAnSdistAndAWheel}
+ * already uploads for its own purpose), so this confirms the real pypiserver sidecar lists
+ * both, each with its own correct size and hash, not just one of them.
+ */
+ @Test
+ @Order(20)
+ @SuppressWarnings("unchecked")
+ void listPackageVersionAssetsListsBothTheSdistAndTheWheelOfOneVersion() {
+ String packageName = "list-assets-multi-pypi-pkg";
+ String sdistFilename = "list_assets_multi_pypi_pkg-1.0.0.tar.gz";
+ String wheelFilename = "list_assets_multi_pypi_pkg-1.0.0-py3-none-any.whl";
+ byte[] sdistContent = "list-assets-multi-sdist-bytes".getBytes(StandardCharsets.UTF_8);
+ byte[] wheelContent = "list-assets-multi-wheel-bytes".getBytes(StandardCharsets.UTF_8);
+ given().header("Authorization", "Bearer " + bearerToken)
+ .contentType("multipart/form-data; boundary=boundary123")
+ .body(twineUploadBody("boundary123", packageName, sdistFilename, sdistContent))
+ .post("/codeartifact/pypi/" + DOMAIN + "/" + REPO + "/")
+ .then().statusCode(200);
+ given().header("Authorization", "Bearer " + bearerToken)
+ .contentType("multipart/form-data; boundary=boundary123")
+ .body(twineUploadBody("boundary123", packageName, wheelFilename, wheelContent))
+ .post("/codeartifact/pypi/" + DOMAIN + "/" + REPO + "/")
+ .then().statusCode(200);
+
+ JsonPath body = given().header("Authorization", AUTH)
+ .post("/v1/package/version/assets?domain=" + DOMAIN + "&repository=" + REPO + "&format=pypi"
+ + "&package=" + packageName + "&version=1.0.0")
+ .then().statusCode(200)
+ .body("package", equalTo(packageName))
+ .body("assets.size()", equalTo(2))
+ .extract().jsonPath();
+ List<Map<String, Object>> assets = (List<Map<String, Object>>) (List<?>) body.getList("assets");
+ Map<String, Object> sdistAsset = findAssetByName(assets, sdistFilename);
+ assertEquals(sdistContent.length, sdistAsset.get("size"));
+ assertEquals(Digests.sha256Hex(sdistContent), ((Map<?, ?>) sdistAsset.get("hashes")).get("SHA-256"));
+ Map<String, Object> wheelAsset = findAssetByName(assets, wheelFilename);
+ assertEquals(wheelContent.length, wheelAsset.get("size"));
+ assertEquals(Digests.sha256Hex(wheelContent), ((Map<?, ?>) wheelAsset.get("hashes")).get("SHA-256"));
+ }
+
+ private static Map<String, Object> findAssetByName(List<Map<String, Object>> assets, String name) {
+ for (Map<String, Object> asset : assets) {
+ if (name.equals(asset.get("name"))) {
+ return asset;
+ }
+ }
+ throw new AssertionError("expected an asset named " + name + " among " + assets);
+ }
+
/**
* Proves the actual mechanism, not just that {@code PypiserverSidecarManager} has a method
* named right: {@code ContainerTeardowns.stopAll} runs every {@code ContainerTeardown} on