fetched = Optional.empty();
+ try {
+ fetched = fetch.fetch(candidate, candidateRepoId)
+ .filter(version -> downloadable(service, owner, region, domain, candidate, format,
+ namespace, packageName, version, candidateRepoId))
+ .map(version -> new Fetched(candidate, candidateRepoId, version));
+ } finally {
+ if (fetched.isEmpty()) {
+ sidecar.endUse(candidateRepoId);
+ }
+ }
+ if (fetched.isPresent()) {
+ return fetched;
+ }
+ }
+ return Optional.empty();
+ }
+
+ private static boolean downloadable(CodeArtifactService service, String owner, String region, String domain,
+ String repository, String format, String namespace, String packageName, String version, String repoId) {
+ try {
+ service.requireContainerBackedVersionDownloadable(owner, region, domain, repository, format, namespace,
+ packageName, version, repoId);
+ return true;
+ } catch (AwsException e) {
+ return false;
+ }
+ }
+}
diff --git a/src/main/java/io/floci/aws/services/codeartifact/UpstreamRepositoryResolver.java b/src/main/java/io/floci/aws/services/codeartifact/UpstreamRepositoryResolver.java
index 00328b8950..dbfca71700 100644
--- a/src/main/java/io/floci/aws/services/codeartifact/UpstreamRepositoryResolver.java
+++ b/src/main/java/io/floci/aws/services/codeartifact/UpstreamRepositoryResolver.java
@@ -4,33 +4,25 @@ import java.util.ArrayDeque;
import java.util.ArrayList;
import java.util.Deque;
import java.util.HashSet;
+import java.util.Iterator;
import java.util.List;
import java.util.Set;
import java.util.function.Function;
/**
- * Flattens a repository's {@code upstreams} graph into the real order AWS searches it in when
- * resolving a package-manager request: confirmed against the live "Upstream repository priority
- * order" documentation, a repository's own direct upstreams are searched in the order they were
- * configured, and that search is transitive (a direct upstream's own upstreams are searched too,
- * region-wide quota page confirms as many as 25 repositories total in one resolution), not limited
- * to the one repository's own direct list.
+ * Flattens a repository's {@code upstreams} graph into the order AWS searches it in when resolving a
+ * package-manager request. The User Guide's "Upstream repository priority order": a repository's
+ * upstreams are searched in the order they were listed, and "if an upstream repository has its own
+ * upstream repositories, the same logic is used to find a package version before moving to the next
+ * upstream repository". That is a depth-first walk in list order: its own example, {@code my_repo}
+ * with upstreams {@code A} and {@code B}, {@code A} with {@code C} and {@code D}, and {@code D} with
+ * {@code E}, is searched {@code my_repo, A, C, D, E, B}.
*
- * Breadth-first, not depth-first: AWS's own ordering guarantee is that a repository's direct
- * upstreams are searched, in their configured order, before any of their own transitive upstreams.
- * A depth-first walk would violate this the moment a closer sibling exists alongside a deeper
- * match: given {@code A -> [B, C]} and {@code B -> [D]}, a depth-first walk would recurse into
- * {@code B}'s own subtree before ever reaching {@code C} (a direct upstream of {@code A}),
- * producing the search order {@code [B, D, C]} rather than the correct {@code [B, C, D]}: C, one
- * hop away, searched after D, two hops away. Breadth-first expands one full level (a "frontier")
- * at a time, so every direct upstream is always searched before any of their own upstreams,
- * matching AWS's documented guarantee exactly.
- *
- *
Cycle-safe by construction: a repository is enqueued at most once (the {@code visited} set),
- * so a cycle anywhere in the graph simply stops expanding rather than looping. {@code
- * validateUpstreams} independently rejects a cycle at the point it would be introduced (see its
- * own javadoc), but this walk does not rely on that validation having run, since an upstream list
- * can only be validated against the graph as it existed at that moment, not against every later
+ *
Cycle-safe by construction: a repository is searched at most once (the {@code visited} set),
+ * where it is first reached, so a cycle anywhere in the graph simply stops expanding rather than
+ * looping. {@code validateUpstreams} independently rejects a cycle at the point it would be introduced
+ * (see its own javadoc), but this walk does not rely on that validation having run, since an upstream
+ * list can only be validated against the graph as it existed at that moment, not against every later
* edit to a different repository's own upstreams.
*/
final class UpstreamRepositoryResolver {
@@ -57,20 +49,22 @@ final class UpstreamRepositoryResolver {
List order = new ArrayList<>();
Set visited = new HashSet<>();
visited.add(startingRepository);
- Deque frontier = new ArrayDeque<>();
- frontier.add(startingRepository);
- while (!frontier.isEmpty() && order.size() < MAX_RESOLUTION_REPOSITORIES) {
- String current = frontier.removeFirst();
- for (String upstream : upstreamsOf.apply(current)) {
- if (!visited.add(upstream)) {
- continue;
- }
- order.add(upstream);
- if (order.size() >= MAX_RESOLUTION_REPOSITORIES) {
- break;
- }
- frontier.addLast(upstream);
+ // A stack of each repository's remaining upstreams: the top is the list being walked, so a
+ // repository's own upstreams are searched before the next of its siblings.
+ Deque> pending = new ArrayDeque<>();
+ pending.push(upstreamsOf.apply(startingRepository).iterator());
+ while (!pending.isEmpty() && order.size() < MAX_RESOLUTION_REPOSITORIES) {
+ Iterator siblings = pending.peek();
+ if (!siblings.hasNext()) {
+ pending.pop();
+ continue;
}
+ String upstream = siblings.next();
+ if (!visited.add(upstream)) {
+ continue;
+ }
+ order.add(upstream);
+ pending.push(upstreamsOf.apply(upstream).iterator());
}
return order;
}
diff --git a/src/test/java/io/floci/aws/services/codeartifact/CodeArtifactMavenExternalConnectionImporterTest.java b/src/test/java/io/floci/aws/services/codeartifact/CodeArtifactMavenExternalConnectionImporterTest.java
index 9eb5b9a933..8414c49c08 100644
--- a/src/test/java/io/floci/aws/services/codeartifact/CodeArtifactMavenExternalConnectionImporterTest.java
+++ b/src/test/java/io/floci/aws/services/codeartifact/CodeArtifactMavenExternalConnectionImporterTest.java
@@ -523,4 +523,14 @@ class CodeArtifactMavenExternalConnectionImporterTest {
"com/example/widget/1.0-SNAPSHOT/widget-1.0-20231001.123456-1.jar"));
verify(externalConnectionClient, never()).exists(anyString(), anyString());
}
+
+ /** An upstream deleted since a search order named it has no connection to fetch through. */
+ @Test
+ void aDeletedRepositoryHasNoExternalConnection() {
+ when(service.describeRepository(REGION, DOMAIN, OWNER, "deleted"))
+ .thenThrow(new AwsException("ResourceNotFoundException", "Repository 'deleted' not found.", 404));
+
+ assertFalse(importer.hasExternalConnection(REGION, DOMAIN, OWNER, "deleted"));
+ assertTrue(importer.hasExternalConnection(REGION, DOMAIN, OWNER, REPOSITORY));
+ }
}
diff --git a/src/test/java/io/floci/aws/services/codeartifact/CodeArtifactMavenUpstreamRepositoryImporterTest.java b/src/test/java/io/floci/aws/services/codeartifact/CodeArtifactMavenUpstreamRepositoryImporterTest.java
index 53f1226496..ea8ca558c6 100644
--- a/src/test/java/io/floci/aws/services/codeartifact/CodeArtifactMavenUpstreamRepositoryImporterTest.java
+++ b/src/test/java/io/floci/aws/services/codeartifact/CodeArtifactMavenUpstreamRepositoryImporterTest.java
@@ -4,6 +4,7 @@ import io.floci.aws.core.common.AwsException;
import io.floci.aws.services.codeartifact.model.CodeArtifactRepository;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
+import org.mockito.InOrder;
import java.io.ByteArrayInputStream;
import java.io.InputStream;
@@ -22,8 +23,10 @@ import static org.mockito.ArgumentMatchers.anyLong;
import static org.mockito.ArgumentMatchers.anyString;
import static org.mockito.ArgumentMatchers.eq;
import static org.mockito.Mockito.doThrow;
+import static org.mockito.Mockito.inOrder;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.never;
+import static org.mockito.Mockito.times;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;
@@ -52,13 +55,15 @@ class CodeArtifactMavenUpstreamRepositoryImporterTest {
private CodeArtifactService service;
private ReposiliteSidecarClient reposilite;
+ private CodeArtifactMavenExternalConnectionImporter externalConnectionImporter;
private CodeArtifactMavenUpstreamRepositoryImporter importer;
@BeforeEach
void setUp() {
service = mock(CodeArtifactService.class);
reposilite = mock(ReposiliteSidecarClient.class);
- importer = new CodeArtifactMavenUpstreamRepositoryImporter(service, reposilite);
+ externalConnectionImporter = mock(CodeArtifactMavenExternalConnectionImporter.class);
+ importer = new CodeArtifactMavenUpstreamRepositoryImporter(service, reposilite, externalConnectionImporter);
CodeArtifactRepository repository = new CodeArtifactRepository();
repository.setUpstreams(List.of(UPSTREAM));
@@ -298,6 +303,98 @@ class CodeArtifactMavenUpstreamRepositoryImporterTest {
PACKAGE_NAME, VERSION, EPISODE_ID, false);
}
+ /**
+ * An artifact no upstream holds is fetched through an upstream's own external connection, retained
+ * there first, then retained here from that upstream.
+ */
+ @Test
+ void anArtifactNoUpstreamHoldsIsFetchedThroughAnUpstreamsExternalConnection() {
+ when(reposilite.artifactExists(UPSTREAM_REPO_ID, GAV)).thenReturn(false);
+ when(externalConnectionImporter.hasExternalConnection(REGION, DOMAIN, OWNER, UPSTREAM)).thenReturn(true);
+ when(externalConnectionImporter.tryImport(REGION, DOMAIN, OWNER, UPSTREAM, UPSTREAM_REPO_ID, GAV))
+ .thenReturn(true);
+ stubFetchArtifact(UPSTREAM_REPO_ID, GAV, "jar-bytes".getBytes());
+
+ assertTrue(importer.tryImport(REGION, DOMAIN, OWNER, REPOSITORY, REPO_ID, GAV));
+ // The fences here are read before the fetch through the upstream's connection, so a deletion
+ // here during that fetch moves them away from the snapshot.
+ InOrder order = inOrder(service, externalConnectionImporter);
+ order.verify(service).currentContainerBackedPackageFence(REGION, DOMAIN, OWNER, REPOSITORY, "maven",
+ NAMESPACE, PACKAGE_NAME);
+ order.verify(service).currentContainerBackedVersionFence(REGION, DOMAIN, OWNER, REPOSITORY, "maven",
+ NAMESPACE, PACKAGE_NAME, VERSION);
+ order.verify(externalConnectionImporter).tryImport(REGION, DOMAIN, OWNER, UPSTREAM, UPSTREAM_REPO_ID, GAV);
+ order.verify(service).claimMavenRetainedVersionOrigin(REGION, DOMAIN, OWNER, REPOSITORY, NAMESPACE,
+ PACKAGE_NAME, VERSION, UPSTREAM, RetentionOriginKind.UPSTREAM_REPOSITORY, 42L);
+ // Leased once by the walk and once for the fetch through its connection, each released.
+ verify(service, times(2)).resolveContainerIdAndBeginUse("maven", REGION, DOMAIN, OWNER, UPSTREAM, reposilite);
+ verify(reposilite, times(2)).endUse(UPSTREAM_REPO_ID);
+ }
+
+ /**
+ * The upstream's connection answers for a version the upstream already holds, Archived: the walk
+ * over what the upstreams hold skipped it, and the fetch through the connection skips it too.
+ */
+ @Test
+ void aVersionArchivedAtTheUpstreamIsNotCopiedThroughItsExternalConnection() {
+ doThrow(new AwsException("ResourceNotFoundException", "archived", 404)).when(service)
+ .requireContainerBackedVersionDownloadable(OWNER, REGION, DOMAIN, UPSTREAM, "maven", NAMESPACE,
+ PACKAGE_NAME, VERSION, UPSTREAM_REPO_ID);
+ when(externalConnectionImporter.hasExternalConnection(REGION, DOMAIN, OWNER, UPSTREAM)).thenReturn(true);
+ when(externalConnectionImporter.tryImport(REGION, DOMAIN, OWNER, UPSTREAM, UPSTREAM_REPO_ID, GAV))
+ .thenReturn(true);
+
+ assertFalse(importer.tryImport(REGION, DOMAIN, OWNER, REPOSITORY, REPO_ID, GAV));
+ verify(service, never()).claimMavenRetainedVersionOrigin(anyString(), anyString(), anyString(), anyString(),
+ anyString(), anyString(), anyString(), anyString(), any(), anyLong());
+ verify(reposilite, times(2)).endUse(UPSTREAM_REPO_ID);
+ }
+
+ /** A merge through the connection that fails leaves the upstream's own metadata to read. */
+ @Test
+ void aFailedMergeThroughAnUpstreamsExternalConnectionStillReadsItsOwnMetadata() {
+ String metadataGav = "com/example/widget/maven-metadata.xml";
+ when(externalConnectionImporter.hasExternalConnection(REGION, DOMAIN, OWNER, UPSTREAM)).thenReturn(true);
+ when(externalConnectionImporter.tryImport(REGION, DOMAIN, OWNER, UPSTREAM, UPSTREAM_REPO_ID, metadataGav))
+ .thenThrow(new IllegalStateException("sidecar write failed"));
+ byte[] metadataBytes = METADATA_WITH_ONE_VERSION;
+ when(reposilite.fetchArtifactStreamed(UPSTREAM_REPO_ID, metadataGav)).thenAnswer(invocation ->
+ Optional.of(new ReposiliteSidecarClient.StreamedArtifact(
+ new ByteArrayInputStream(metadataBytes), metadataBytes.length, "text/xml")));
+ when(service.mergeMavenMetadataUnderPackageFence(eq(REGION), eq(DOMAIN), eq(OWNER), eq(REPOSITORY),
+ eq(NAMESPACE), eq(PACKAGE_NAME), eq(42L), eq(reposilite), eq(REPO_ID), eq(metadataBytes), any(),
+ eq(true))).thenReturn(true);
+
+ assertTrue(importer.tryImport(REGION, DOMAIN, OWNER, REPOSITORY, REPO_ID, metadataGav));
+ verify(reposilite).endUse(UPSTREAM_REPO_ID);
+ }
+
+ @Test
+ void anUpstreamWithoutAnExternalConnectionIsNotAskedToFetch() {
+ when(reposilite.artifactExists(UPSTREAM_REPO_ID, GAV)).thenReturn(false);
+
+ assertFalse(importer.tryImport(REGION, DOMAIN, OWNER, REPOSITORY, REPO_ID, GAV));
+ verify(externalConnectionImporter, never()).tryImport(anyString(), anyString(), anyString(), anyString(),
+ anyString(), anyString());
+ }
+
+ @Test
+ void anUpstreamsExternalConnectionMergesItsReleasesIntoTheMetadataReadThroughIt() {
+ String metadataGav = "com/example/widget/maven-metadata.xml";
+ when(externalConnectionImporter.hasExternalConnection(REGION, DOMAIN, OWNER, UPSTREAM)).thenReturn(true);
+ byte[] metadataBytes = METADATA_WITH_ONE_VERSION;
+ when(reposilite.fetchArtifactStreamed(UPSTREAM_REPO_ID, metadataGav)).thenAnswer(invocation ->
+ Optional.of(new ReposiliteSidecarClient.StreamedArtifact(
+ new ByteArrayInputStream(metadataBytes), metadataBytes.length, "text/xml")));
+ when(service.mergeMavenMetadataUnderPackageFence(eq(REGION), eq(DOMAIN), eq(OWNER), eq(REPOSITORY),
+ eq(NAMESPACE), eq(PACKAGE_NAME), eq(42L), eq(reposilite), eq(REPO_ID), eq(metadataBytes), any(),
+ eq(true))).thenReturn(true);
+
+ assertTrue(importer.tryImport(REGION, DOMAIN, OWNER, REPOSITORY, REPO_ID, metadataGav));
+ verify(externalConnectionImporter).tryImport(REGION, DOMAIN, OWNER, UPSTREAM, UPSTREAM_REPO_ID,
+ metadataGav);
+ }
+
/** Names at least one real version, so the eligibility pre-check lets this candidate through. */
private static final byte[] METADATA_WITH_ONE_VERSION = (""
+ "1.0").getBytes();
diff --git a/src/test/java/io/floci/aws/services/codeartifact/CodeArtifactNpmExternalConnectionImporterTest.java b/src/test/java/io/floci/aws/services/codeartifact/CodeArtifactNpmExternalConnectionImporterTest.java
index c220cd2b94..1ea3ef7797 100644
--- a/src/test/java/io/floci/aws/services/codeartifact/CodeArtifactNpmExternalConnectionImporterTest.java
+++ b/src/test/java/io/floci/aws/services/codeartifact/CodeArtifactNpmExternalConnectionImporterTest.java
@@ -651,4 +651,14 @@ class CodeArtifactNpmExternalConnectionImporterTest {
return Optional.of(new NpmExternalConnectionClient.FetchedFile(content.length));
});
}
+
+ /** An upstream deleted since a search order named it has no connection to fetch through. */
+ @Test
+ void aDeletedRepositoryHasNoExternalConnection() {
+ when(service.describeRepository(REGION, DOMAIN, OWNER, "deleted"))
+ .thenThrow(new AwsException("ResourceNotFoundException", "Repository 'deleted' not found.", 404));
+
+ assertFalse(importer.hasExternalConnection(REGION, DOMAIN, OWNER, "deleted"));
+ assertTrue(importer.hasExternalConnection(REGION, DOMAIN, OWNER, REPOSITORY));
+ }
}
diff --git a/src/test/java/io/floci/aws/services/codeartifact/CodeArtifactNpmUpstreamRepositoryImporterTest.java b/src/test/java/io/floci/aws/services/codeartifact/CodeArtifactNpmUpstreamRepositoryImporterTest.java
index 84130a143c..fd19e5e0e4 100644
--- a/src/test/java/io/floci/aws/services/codeartifact/CodeArtifactNpmUpstreamRepositoryImporterTest.java
+++ b/src/test/java/io/floci/aws/services/codeartifact/CodeArtifactNpmUpstreamRepositoryImporterTest.java
@@ -7,6 +7,7 @@ import io.floci.aws.core.common.AwsException;
import io.floci.aws.services.codeartifact.model.CodeArtifactRepository;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
+import org.mockito.InOrder;
import java.io.ByteArrayInputStream;
import java.nio.file.Files;
@@ -23,8 +24,10 @@ import static org.mockito.ArgumentMatchers.anyLong;
import static org.mockito.ArgumentMatchers.anyString;
import static org.mockito.ArgumentMatchers.eq;
import static org.mockito.Mockito.doThrow;
+import static org.mockito.Mockito.inOrder;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.never;
+import static org.mockito.Mockito.times;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;
@@ -53,13 +56,16 @@ class CodeArtifactNpmUpstreamRepositoryImporterTest {
private CodeArtifactService service;
private VerdaccioSidecarClient verdaccio;
+ private CodeArtifactNpmExternalConnectionImporter externalConnectionImporter;
private CodeArtifactNpmUpstreamRepositoryImporter importer;
@BeforeEach
void setUp() {
service = mock(CodeArtifactService.class);
verdaccio = mock(VerdaccioSidecarClient.class);
- importer = new CodeArtifactNpmUpstreamRepositoryImporter(service, verdaccio, new ObjectMapper());
+ externalConnectionImporter = mock(CodeArtifactNpmExternalConnectionImporter.class);
+ importer = new CodeArtifactNpmUpstreamRepositoryImporter(service, verdaccio, new ObjectMapper(),
+ externalConnectionImporter);
CodeArtifactRepository repository = new CodeArtifactRepository();
repository.setUpstreams(List.of(UPSTREAM));
@@ -306,7 +312,7 @@ class CodeArtifactNpmUpstreamRepositoryImporterTest {
when(verdaccio.fetchPackageDocument(BASE_URL, null, PACKAGE_NAME)).thenReturn(Optional.empty());
Optional merged = importer.mergedPackument(REGION, DOMAIN, OWNER, REPOSITORY, REPO_ID, null,
- PACKAGE_NAME);
+ PACKAGE_NAME, false);
assertTrue(merged.isPresent());
JsonNode versionDoc = merged.get().path("versions").path(VERSION);
@@ -316,6 +322,93 @@ class CodeArtifactNpmUpstreamRepositoryImporterTest {
verify(verdaccio).endUse(UPSTREAM_REPO_ID);
}
+ /**
+ * A version no upstream holds is fetched through an upstream's own external connection, retained
+ * there first, then retained here from that upstream, as the User Guide's chain example has it.
+ */
+ @Test
+ void aVersionNoUpstreamHoldsIsFetchedThroughAnUpstreamsExternalConnection() {
+ when(verdaccio.packageVersionExists(UPSTREAM_REPO_ID, DOMAIN, UPSTREAM, null, PACKAGE_NAME, VERSION))
+ .thenReturn(false);
+ when(externalConnectionImporter.hasExternalConnection(REGION, DOMAIN, OWNER, UPSTREAM)).thenReturn(true);
+ when(externalConnectionImporter.tryImportAsset(REGION, DOMAIN, OWNER, UPSTREAM, UPSTREAM_REPO_ID, null,
+ PACKAGE_NAME, VERSION, ASSET_NAME)).thenReturn(true);
+ when(verdaccio.fetchPackageDocument(UPSTREAM_BASE_URL, null, PACKAGE_NAME))
+ .thenReturn(Optional.of(upstreamDocument("")));
+ byte[] tarballBytes = "tarball-content".getBytes();
+ when(verdaccio.fetchPackageVersionAssetStreamed(UPSTREAM_REPO_ID, DOMAIN, UPSTREAM, null, PACKAGE_NAME,
+ VERSION, ASSET_NAME)).thenAnswer(invocation -> Optional.of(new RepositorySidecarManager.StreamedAsset(
+ new ByteArrayInputStream(tarballBytes), tarballBytes.length)));
+
+ boolean imported = importer.tryImportAsset(REGION, DOMAIN, OWNER, REPOSITORY, REPO_ID, null, PACKAGE_NAME,
+ VERSION, ASSET_NAME);
+
+ assertTrue(imported);
+ // The fences here are read before the fetch through the upstream's connection, so a deletion
+ // here during that fetch moves them away from the snapshot.
+ InOrder order = inOrder(service, externalConnectionImporter);
+ order.verify(service).currentContainerBackedPackageFence(REGION, DOMAIN, OWNER, REPOSITORY, "npm", null,
+ PACKAGE_NAME);
+ order.verify(service).currentContainerBackedVersionFence(REGION, DOMAIN, OWNER, REPOSITORY, "npm", null,
+ PACKAGE_NAME, VERSION);
+ order.verify(externalConnectionImporter).tryImportAsset(REGION, DOMAIN, OWNER, UPSTREAM, UPSTREAM_REPO_ID,
+ null, PACKAGE_NAME, VERSION, ASSET_NAME);
+ order.verify(service).claimRetainedVersionOrigin(REGION, DOMAIN, OWNER, REPOSITORY, "npm", null,
+ PACKAGE_NAME, VERSION, UPSTREAM, RetentionOriginKind.UPSTREAM_REPOSITORY, 42L);
+ // Leased once by the walk and once for the fetch through its connection, each released.
+ verify(service, times(2)).resolveContainerIdAndBeginUse("npm", REGION, DOMAIN, OWNER, UPSTREAM, verdaccio);
+ verify(verdaccio, times(2)).endUse(UPSTREAM_REPO_ID);
+ }
+
+ /**
+ * The upstream's connection answers for a version the upstream already holds, Archived: the walk
+ * over what the upstreams hold skipped it, and the fetch through the connection skips it too.
+ */
+ @Test
+ void aVersionArchivedAtTheUpstreamIsNotCopiedThroughItsExternalConnection() {
+ doThrow(new AwsException("ResourceNotFoundException", "archived", 404)).when(service)
+ .requireContainerBackedVersionDownloadable(OWNER, REGION, DOMAIN, UPSTREAM, "npm", null, PACKAGE_NAME,
+ VERSION, UPSTREAM_REPO_ID);
+ when(externalConnectionImporter.hasExternalConnection(REGION, DOMAIN, OWNER, UPSTREAM)).thenReturn(true);
+ when(externalConnectionImporter.tryImportAsset(REGION, DOMAIN, OWNER, UPSTREAM, UPSTREAM_REPO_ID, null,
+ PACKAGE_NAME, VERSION, ASSET_NAME)).thenReturn(true);
+
+ assertFalse(importer.tryImportAsset(REGION, DOMAIN, OWNER, REPOSITORY, REPO_ID, null, PACKAGE_NAME,
+ VERSION, ASSET_NAME));
+ verify(service, never()).claimRetainedVersionOrigin(anyString(), anyString(), anyString(), anyString(),
+ anyString(), any(), anyString(), anyString(), anyString(), any(), anyLong());
+ verify(verdaccio, times(2)).endUse(UPSTREAM_REPO_ID);
+ }
+
+ @Test
+ void anUpstreamWithoutAnExternalConnectionIsNotAskedToFetch() {
+ when(verdaccio.packageVersionExists(UPSTREAM_REPO_ID, DOMAIN, UPSTREAM, null, PACKAGE_NAME, VERSION))
+ .thenReturn(false);
+
+ assertFalse(importer.tryImportAsset(REGION, DOMAIN, OWNER, REPOSITORY, REPO_ID, null, PACKAGE_NAME,
+ VERSION, ASSET_NAME));
+ verify(externalConnectionImporter, never()).tryImportAsset(anyString(), anyString(), anyString(),
+ anyString(), anyString(), any(), anyString(), anyString(), anyString());
+ }
+
+ /** The client's ask for the full document goes on to the upstream's connection, readme and all. */
+ @Test
+ void mergedPackumentOffersWhatAnUpstreamsExternalConnectionHas() {
+ when(externalConnectionImporter.hasExternalConnection(REGION, DOMAIN, OWNER, UPSTREAM)).thenReturn(true);
+ when(externalConnectionImporter.mergedPackument(REGION, DOMAIN, OWNER, UPSTREAM, UPSTREAM_REPO_ID, null,
+ PACKAGE_NAME, true)).thenReturn(Optional.of((ObjectNode) upstreamDocument("the readme")));
+ when(verdaccio.fetchPackageDocument(BASE_URL, null, PACKAGE_NAME)).thenReturn(Optional.empty());
+
+ Optional merged = importer.mergedPackument(REGION, DOMAIN, OWNER, REPOSITORY, REPO_ID, null,
+ PACKAGE_NAME, true);
+
+ assertTrue(merged.isPresent());
+ assertEquals(BASE_URL + PACKAGE_NAME + "/-/" + ASSET_NAME,
+ merged.get().path("versions").path(VERSION).path("dist").path("tarball").asText());
+ assertEquals("the readme", merged.get().path("readme").asText());
+ verify(verdaccio, never()).fetchPackageDocument(UPSTREAM_BASE_URL, null, PACKAGE_NAME);
+ }
+
@Test
void mergedPackumentReturnsEmptyWhenNoUpstreamsAreConfigured() {
CodeArtifactRepository repository = new CodeArtifactRepository();
@@ -323,7 +416,7 @@ class CodeArtifactNpmUpstreamRepositoryImporterTest {
when(service.describeRepository(REGION, DOMAIN, OWNER, REPOSITORY)).thenReturn(repository);
Optional merged = importer.mergedPackument(REGION, DOMAIN, OWNER, REPOSITORY, REPO_ID, null,
- PACKAGE_NAME);
+ PACKAGE_NAME, false);
assertFalse(merged.isPresent());
}
diff --git a/src/test/java/io/floci/aws/services/codeartifact/CodeArtifactNuGetExternalConnectionImporterTest.java b/src/test/java/io/floci/aws/services/codeartifact/CodeArtifactNuGetExternalConnectionImporterTest.java
index bd8888e08a..8795aa4be3 100644
--- a/src/test/java/io/floci/aws/services/codeartifact/CodeArtifactNuGetExternalConnectionImporterTest.java
+++ b/src/test/java/io/floci/aws/services/codeartifact/CodeArtifactNuGetExternalConnectionImporterTest.java
@@ -1,5 +1,6 @@
package io.floci.aws.services.codeartifact;
+import io.floci.aws.core.common.AwsException;
import io.floci.aws.services.codeartifact.model.CodeArtifactRepository;
import io.floci.aws.services.codeartifact.model.ExternalConnection;
import org.junit.jupiter.api.BeforeEach;
@@ -241,4 +242,14 @@ class CodeArtifactNuGetExternalConnectionImporterTest {
return Optional.of(new NuGetExternalConnectionClient.FetchedFile(content.length));
});
}
+
+ /** An upstream deleted since a search order named it has no connection to fetch through. */
+ @Test
+ void aDeletedRepositoryHasNoExternalConnection() {
+ when(service.describeRepository(REGION, DOMAIN, OWNER, "deleted"))
+ .thenThrow(new AwsException("ResourceNotFoundException", "Repository 'deleted' not found.", 404));
+
+ assertFalse(importer.hasExternalConnection(REGION, DOMAIN, OWNER, "deleted"));
+ assertTrue(importer.hasExternalConnection(REGION, DOMAIN, OWNER, REPOSITORY));
+ }
}
diff --git a/src/test/java/io/floci/aws/services/codeartifact/CodeArtifactNuGetUpstreamRepositoryImporterTest.java b/src/test/java/io/floci/aws/services/codeartifact/CodeArtifactNuGetUpstreamRepositoryImporterTest.java
index 67096f4dfd..8f1b701cea 100644
--- a/src/test/java/io/floci/aws/services/codeartifact/CodeArtifactNuGetUpstreamRepositoryImporterTest.java
+++ b/src/test/java/io/floci/aws/services/codeartifact/CodeArtifactNuGetUpstreamRepositoryImporterTest.java
@@ -4,6 +4,7 @@ import io.floci.aws.core.common.AwsException;
import io.floci.aws.services.codeartifact.model.CodeArtifactRepository;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
+import org.mockito.InOrder;
import java.io.ByteArrayInputStream;
import java.nio.file.Files;
@@ -20,8 +21,10 @@ import static org.mockito.ArgumentMatchers.anyString;
import static org.mockito.ArgumentMatchers.eq;
import static org.mockito.ArgumentMatchers.isNull;
import static org.mockito.Mockito.doThrow;
+import static org.mockito.Mockito.inOrder;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.never;
+import static org.mockito.Mockito.times;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;
@@ -50,13 +53,15 @@ class CodeArtifactNuGetUpstreamRepositoryImporterTest {
private CodeArtifactService service;
private BagetterSidecarClient bagetter;
+ private CodeArtifactNuGetExternalConnectionImporter externalConnectionImporter;
private CodeArtifactNuGetUpstreamRepositoryImporter importer;
@BeforeEach
void setUp() {
service = mock(CodeArtifactService.class);
bagetter = mock(BagetterSidecarClient.class);
- importer = new CodeArtifactNuGetUpstreamRepositoryImporter(service, bagetter);
+ externalConnectionImporter = mock(CodeArtifactNuGetExternalConnectionImporter.class);
+ importer = new CodeArtifactNuGetUpstreamRepositoryImporter(service, bagetter, externalConnectionImporter);
CodeArtifactRepository repository = new CodeArtifactRepository();
repository.setUpstreams(List.of(UPSTREAM));
@@ -94,6 +99,64 @@ class CodeArtifactNuGetUpstreamRepositoryImporterTest {
any())).thenReturn(new CodeArtifactService.AssetRetentionAttemptResult(true, true));
}
+ /**
+ * A version no upstream holds is fetched through an upstream's own external connection, retained
+ * there first, then retained here from that upstream.
+ */
+ @Test
+ void aVersionNoUpstreamHoldsIsFetchedThroughAnUpstreamsExternalConnection() {
+ when(bagetter.packageVersionExists(UPSTREAM_REPO_ID, DOMAIN, UPSTREAM, null, PACKAGE_ID, VERSION))
+ .thenReturn(false);
+ when(externalConnectionImporter.hasExternalConnection(REGION, DOMAIN, OWNER, UPSTREAM)).thenReturn(true);
+ when(externalConnectionImporter.tryImport(REGION, DOMAIN, OWNER, UPSTREAM, UPSTREAM_REPO_ID, PACKAGE_ID,
+ VERSION)).thenReturn(true);
+
+ assertTrue(importer.tryImport(REGION, DOMAIN, OWNER, REPOSITORY, REPO_ID, PACKAGE_ID, VERSION));
+ // The fences here are read before the fetch through the upstream's connection, so a deletion
+ // here during that fetch moves them away from the snapshot.
+ InOrder order = inOrder(service, externalConnectionImporter);
+ order.verify(service).currentContainerBackedPackageFence(REGION, DOMAIN, OWNER, REPOSITORY, "nuget", null,
+ PACKAGE_ID);
+ order.verify(service).currentContainerBackedVersionFence(REGION, DOMAIN, OWNER, REPOSITORY, "nuget", null,
+ PACKAGE_ID, VERSION);
+ order.verify(externalConnectionImporter).tryImport(REGION, DOMAIN, OWNER, UPSTREAM, UPSTREAM_REPO_ID,
+ PACKAGE_ID, VERSION);
+ order.verify(service).claimRetainedVersionOrigin(REGION, DOMAIN, OWNER, REPOSITORY, "nuget", null,
+ PACKAGE_ID, VERSION, UPSTREAM, RetentionOriginKind.UPSTREAM_REPOSITORY, 42L);
+ // Leased once by the walk and once for the fetch through its connection, each released.
+ verify(service, times(2)).resolveContainerIdAndBeginUse("nuget", REGION, DOMAIN, OWNER, UPSTREAM, bagetter);
+ verify(bagetter, times(2)).endUse(UPSTREAM_REPO_ID);
+ }
+
+ /**
+ * The upstream's connection answers for a version the upstream already holds, Archived: the walk
+ * over what the upstreams hold skipped it, and the fetch through the connection skips it too.
+ */
+ @Test
+ void aVersionArchivedAtTheUpstreamIsNotCopiedThroughItsExternalConnection() {
+ doThrow(new AwsException("ResourceNotFoundException", "archived", 404)).when(service)
+ .requireContainerBackedVersionDownloadable(OWNER, REGION, DOMAIN, UPSTREAM, "nuget", null, PACKAGE_ID,
+ VERSION, UPSTREAM_REPO_ID);
+ when(externalConnectionImporter.hasExternalConnection(REGION, DOMAIN, OWNER, UPSTREAM)).thenReturn(true);
+ when(externalConnectionImporter.tryImport(REGION, DOMAIN, OWNER, UPSTREAM, UPSTREAM_REPO_ID, PACKAGE_ID,
+ VERSION)).thenReturn(true);
+
+ assertFalse(importer.tryImport(REGION, DOMAIN, OWNER, REPOSITORY, REPO_ID, PACKAGE_ID, VERSION));
+ verify(service, never()).claimRetainedVersionOrigin(anyString(), anyString(), anyString(), anyString(),
+ anyString(), any(), anyString(), anyString(), anyString(), any(), anyLong());
+ verify(bagetter, times(2)).endUse(UPSTREAM_REPO_ID);
+ }
+
+ @Test
+ void anUpstreamWithoutAnExternalConnectionIsNotAskedToFetch() {
+ when(bagetter.packageVersionExists(UPSTREAM_REPO_ID, DOMAIN, UPSTREAM, null, PACKAGE_ID, VERSION))
+ .thenReturn(false);
+
+ assertFalse(importer.tryImport(REGION, DOMAIN, OWNER, REPOSITORY, REPO_ID, PACKAGE_ID, VERSION));
+ verify(externalConnectionImporter, never()).tryImport(anyString(), anyString(), anyString(), anyString(),
+ anyString(), anyString(), anyString());
+ }
+
@Test
void importsFromTheDirectUpstreamWhenNotFoundLocally() {
boolean imported = importer.tryImport(REGION, DOMAIN, OWNER, REPOSITORY, REPO_ID, PACKAGE_ID, VERSION);
diff --git a/src/test/java/io/floci/aws/services/codeartifact/CodeArtifactPypiExternalConnectionImporterTest.java b/src/test/java/io/floci/aws/services/codeartifact/CodeArtifactPypiExternalConnectionImporterTest.java
index bb670a97bd..4008fd0b76 100644
--- a/src/test/java/io/floci/aws/services/codeartifact/CodeArtifactPypiExternalConnectionImporterTest.java
+++ b/src/test/java/io/floci/aws/services/codeartifact/CodeArtifactPypiExternalConnectionImporterTest.java
@@ -370,6 +370,16 @@ class CodeArtifactPypiExternalConnectionImporterTest {
assertEquals(List.of(), additional);
}
+ /** A downstream repository listing these files through its upstreams leaves out what it refuses. */
+ @Test
+ void additionalExternalFilenamesLeavesOutAVersionTheCallerRefuses() {
+ List additional =
+ importer.additionalExternalFilenames(REGION, DOMAIN, OWNER, REPOSITORY, REPO_ID, PACKAGE_NAME,
+ Set.of(), version -> !VERSION.equals(version));
+
+ assertEquals(List.of(), additional);
+ }
+
@Test
void additionalExternalFilenamesReturnsEmptyWhenNoConnectionIsConfigured() {
CodeArtifactRepository repository = new CodeArtifactRepository();
@@ -617,4 +627,14 @@ class CodeArtifactPypiExternalConnectionImporterTest {
}
return toBytes(root);
}
+
+ /** An upstream deleted since a search order named it has no connection to fetch through. */
+ @Test
+ void aDeletedRepositoryHasNoExternalConnection() {
+ when(service.describeRepository(REGION, DOMAIN, OWNER, "deleted"))
+ .thenThrow(new AwsException("ResourceNotFoundException", "Repository 'deleted' not found.", 404));
+
+ assertFalse(importer.hasExternalConnection(REGION, DOMAIN, OWNER, "deleted"));
+ assertTrue(importer.hasExternalConnection(REGION, DOMAIN, OWNER, REPOSITORY));
+ }
}
diff --git a/src/test/java/io/floci/aws/services/codeartifact/CodeArtifactPypiUpstreamRepositoryImporterTest.java b/src/test/java/io/floci/aws/services/codeartifact/CodeArtifactPypiUpstreamRepositoryImporterTest.java
index de26bc4444..f2d7b62e71 100644
--- a/src/test/java/io/floci/aws/services/codeartifact/CodeArtifactPypiUpstreamRepositoryImporterTest.java
+++ b/src/test/java/io/floci/aws/services/codeartifact/CodeArtifactPypiUpstreamRepositoryImporterTest.java
@@ -4,6 +4,8 @@ import io.floci.aws.core.common.AwsException;
import io.floci.aws.services.codeartifact.model.CodeArtifactRepository;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
+import org.mockito.ArgumentCaptor;
+import org.mockito.InOrder;
import java.io.ByteArrayInputStream;
import java.nio.file.Files;
@@ -12,7 +14,9 @@ import java.time.Instant;
import java.util.List;
import java.util.Optional;
import java.util.Set;
+import java.util.function.Predicate;
+import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertTrue;
import static org.mockito.ArgumentMatchers.any;
@@ -21,8 +25,10 @@ import static org.mockito.ArgumentMatchers.anyLong;
import static org.mockito.ArgumentMatchers.anyString;
import static org.mockito.ArgumentMatchers.eq;
import static org.mockito.Mockito.doThrow;
+import static org.mockito.Mockito.inOrder;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.never;
+import static org.mockito.Mockito.times;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;
@@ -50,13 +56,15 @@ class CodeArtifactPypiUpstreamRepositoryImporterTest {
private CodeArtifactService service;
private PypiserverSidecarClient pypiserver;
+ private CodeArtifactPypiExternalConnectionImporter externalConnectionImporter;
private CodeArtifactPypiUpstreamRepositoryImporter importer;
@BeforeEach
void setUp() {
service = mock(CodeArtifactService.class);
pypiserver = mock(PypiserverSidecarClient.class);
- importer = new CodeArtifactPypiUpstreamRepositoryImporter(service, pypiserver);
+ externalConnectionImporter = mock(CodeArtifactPypiExternalConnectionImporter.class);
+ importer = new CodeArtifactPypiUpstreamRepositoryImporter(service, pypiserver, externalConnectionImporter);
CodeArtifactRepository repository = new CodeArtifactRepository();
repository.setUpstreams(List.of(UPSTREAM));
@@ -97,6 +105,100 @@ class CodeArtifactPypiUpstreamRepositoryImporterTest {
any())).thenReturn(new CodeArtifactService.AssetRetentionAttemptResult(true, true));
}
+ /**
+ * A file no upstream holds is fetched through an upstream's own external connection, retained
+ * there first, then retained here from that upstream.
+ */
+ @Test
+ void aFileNoUpstreamHoldsIsFetchedThroughAnUpstreamsExternalConnection() {
+ // Nothing listed at the upstream until its external connection has fetched the version.
+ when(pypiserver.listPackageVersions(UPSTREAM_REPO_ID, DOMAIN, UPSTREAM, null, PACKAGE_NAME))
+ .thenReturn(List.of())
+ .thenReturn(List.of(new RepositorySidecarManager.PackageVersionTimestamp(VERSION,
+ Instant.ofEpochSecond(1000))));
+ when(externalConnectionImporter.hasExternalConnection(REGION, DOMAIN, OWNER, UPSTREAM)).thenReturn(true);
+ when(externalConnectionImporter.tryImportAsset(REGION, DOMAIN, OWNER, UPSTREAM, UPSTREAM_REPO_ID, FILENAME))
+ .thenReturn(true);
+
+ assertTrue(importer.tryImportAsset(REGION, DOMAIN, OWNER, REPOSITORY, REPO_ID, FILENAME));
+ // The package fence here is read before the fetch through the upstream's connection, so a
+ // DeletePackage here during that fetch moves it away from the snapshot.
+ InOrder order = inOrder(service, externalConnectionImporter);
+ order.verify(service).currentContainerBackedPackageFence(REGION, DOMAIN, OWNER, REPOSITORY, "pypi", null,
+ PACKAGE_NAME);
+ order.verify(externalConnectionImporter).tryImportAsset(REGION, DOMAIN, OWNER, UPSTREAM, UPSTREAM_REPO_ID,
+ FILENAME);
+ order.verify(service).claimRetainedVersionOrigin(REGION, DOMAIN, OWNER, REPOSITORY, "pypi", null,
+ PACKAGE_NAME, VERSION, UPSTREAM, RetentionOriginKind.UPSTREAM_REPOSITORY, 42L);
+ // Leased once by the walk and once for the fetch through its connection, each released.
+ verify(service, times(2)).resolveContainerIdAndBeginUse("pypi", REGION, DOMAIN, OWNER, UPSTREAM, pypiserver);
+ verify(pypiserver, times(2)).endUse(UPSTREAM_REPO_ID);
+ }
+
+ /**
+ * The upstream's connection answers for a version the upstream already holds, Archived: the walk
+ * over what the upstreams hold skipped it, and the fetch through the connection skips it too.
+ */
+ @Test
+ void aVersionArchivedAtTheUpstreamIsNotCopiedThroughItsExternalConnection() {
+ doThrow(new AwsException("ResourceNotFoundException", "archived", 404)).when(service)
+ .requireContainerBackedVersionDownloadable(OWNER, REGION, DOMAIN, UPSTREAM, "pypi", null, PACKAGE_NAME,
+ VERSION, UPSTREAM_REPO_ID);
+ when(externalConnectionImporter.hasExternalConnection(REGION, DOMAIN, OWNER, UPSTREAM)).thenReturn(true);
+ when(externalConnectionImporter.tryImportAsset(REGION, DOMAIN, OWNER, UPSTREAM, UPSTREAM_REPO_ID, FILENAME))
+ .thenReturn(true);
+
+ assertFalse(importer.tryImportAsset(REGION, DOMAIN, OWNER, REPOSITORY, REPO_ID, FILENAME));
+ verify(service, never()).claimRetainedVersionOrigin(anyString(), anyString(), anyString(), anyString(),
+ anyString(), any(), anyString(), anyString(), anyString(), any(), anyLong());
+ verify(pypiserver, times(2)).endUse(UPSTREAM_REPO_ID);
+ }
+
+ @Test
+ void anUpstreamWithoutAnExternalConnectionIsNotAskedToFetch() {
+ when(pypiserver.listPackageVersions(UPSTREAM_REPO_ID, DOMAIN, UPSTREAM, null, PACKAGE_NAME))
+ .thenReturn(List.of());
+
+ assertFalse(importer.tryImportAsset(REGION, DOMAIN, OWNER, REPOSITORY, REPO_ID, FILENAME));
+ verify(externalConnectionImporter, never()).tryImportAsset(anyString(), anyString(), anyString(),
+ anyString(), anyString(), anyString());
+ }
+
+ @Test
+ void additionalInternalFilenamesOffersWhatAnUpstreamsExternalConnectionHas() {
+ when(externalConnectionImporter.hasExternalConnection(REGION, DOMAIN, OWNER, UPSTREAM)).thenReturn(true);
+ String publicOnly = "widget-2.0.0.tar.gz";
+ when(externalConnectionImporter.additionalExternalFilenames(eq(REGION), eq(DOMAIN), eq(OWNER), eq(UPSTREAM),
+ eq(UPSTREAM_REPO_ID), eq(PACKAGE_NAME), eq(Set.of(FILENAME)), any())).thenReturn(List.of(
+ new CodeArtifactPypiExternalConnectionImporter.ExternalIndexFile(publicOnly, false, null)));
+
+ List offered = importer.additionalInternalFilenames(REGION, DOMAIN, OWNER, REPOSITORY, REPO_ID,
+ PACKAGE_NAME, Set.of()).stream()
+ .map(CodeArtifactPypiExternalConnectionImporter.ExternalIndexFile::filename).toList();
+
+ assertEquals(List.of(FILENAME, publicOnly), offered);
+ }
+
+ /**
+ * A version this repository marked Archived or Disposed is left out of what an upstream's external
+ * connection offers here, as it is left out of what the upstream itself holds.
+ */
+ @Test
+ void additionalInternalFilenamesLeavesOutAVersionArchivedHereFromAnUpstreamsExternalConnection() {
+ when(externalConnectionImporter.hasExternalConnection(REGION, DOMAIN, OWNER, UPSTREAM)).thenReturn(true);
+ doThrow(new AwsException("ResourceNotFoundException", "archived", 404)).when(service)
+ .requireContainerBackedVersionDownloadable(OWNER, REGION, DOMAIN, REPOSITORY, "pypi", null,
+ PACKAGE_NAME, "2.0.0", REPO_ID);
+
+ importer.additionalInternalFilenames(REGION, DOMAIN, OWNER, REPOSITORY, REPO_ID, PACKAGE_NAME, Set.of());
+
+ ArgumentCaptor> listable = ArgumentCaptor.forClass(Predicate.class);
+ verify(externalConnectionImporter).additionalExternalFilenames(eq(REGION), eq(DOMAIN), eq(OWNER),
+ eq(UPSTREAM), eq(UPSTREAM_REPO_ID), eq(PACKAGE_NAME), any(), listable.capture());
+ assertFalse(listable.getValue().test("2.0.0"));
+ assertTrue(listable.getValue().test(VERSION));
+ }
+
@Test
void importsFromTheDirectUpstreamWhenNotFoundLocally() {
boolean imported = importer.tryImportAsset(REGION, DOMAIN, OWNER, REPOSITORY, REPO_ID, FILENAME);
diff --git a/src/test/java/io/floci/aws/services/codeartifact/CodeArtifactServiceTest.java b/src/test/java/io/floci/aws/services/codeartifact/CodeArtifactServiceTest.java
index 3d424497f1..ae0f5ff2d8 100644
--- a/src/test/java/io/floci/aws/services/codeartifact/CodeArtifactServiceTest.java
+++ b/src/test/java/io/floci/aws/services/codeartifact/CodeArtifactServiceTest.java
@@ -2639,6 +2639,117 @@ class CodeArtifactServiceTest {
"repo", "maven", "com.example", "widget", "1.0"));
}
+ /** Retains npm {@code widget@1.0} into {@code repository} from {@code origin}, a connection or a repository. */
+ private void retainNpmWidget(String repository, String origin, RetentionOriginKind kind) {
+ long fence = service.currentContainerBackedPackageFence(REGION, "dom", null, repository, "npm", null,
+ "widget");
+ CodeArtifactService.RetainedVersionOriginClaimOutcome claim = service.claimRetainedVersionOrigin(REGION,
+ "dom", null, repository, "npm", null, "widget", "1.0", origin, kind, fence);
+ service.finishRetainedVersionOriginAttempt(REGION, "dom", null, repository, "npm", null, "widget", "1.0",
+ claim.episodeId(), true);
+ }
+
+ private CodeArtifactService.PackageVersionSummary listedNpmWidget(String repository) {
+ when(verdaccioClient.listPackageVersions(anyString(), eq("dom"), eq(repository), isNull(), eq("widget")))
+ .thenReturn(List.of(new RepositorySidecarManager.PackageVersionTimestamp("1.0",
+ Instant.ofEpochSecond(1000))));
+ return service.listPackageVersions(REGION, "dom", null, repository, "npm", null, "widget", null, null, null,
+ null, null).versions().get(0);
+ }
+
+ /**
+ * A version's origin names where it "originally entered" the domain: one retained through an
+ * upstream that had itself retained it from a repository further up names that repository, not
+ * the upstream it was read through.
+ */
+ @Test
+ void aVersionRetainedThroughAnIntermediateUpstreamNamesTheRepositoryItWasPublishedTo() {
+ service.createDomain(REGION, "dom", null, Map.of());
+ for (String repository : List.of("origin", "mid", "repo")) {
+ service.createRepository(REGION, "dom", null, repository, null, null, Map.of());
+ }
+ retainNpmWidget("mid", "origin", RetentionOriginKind.UPSTREAM_REPOSITORY);
+ retainNpmWidget("repo", "mid", RetentionOriginKind.UPSTREAM_REPOSITORY);
+
+ CodeArtifactService.PackageVersionSummary summary = listedNpmWidget("repo");
+ assertEquals("INTERNAL", summary.originType());
+ assertEquals("origin", summary.upstreamRepositoryName());
+ assertEquals(null, summary.externalConnectionName());
+ assertEquals("origin", listedNpmWidget("mid").upstreamRepositoryName());
+ }
+
+ /** One an upstream fetched through its external connection entered the domain there, so it is EXTERNAL. */
+ @Test
+ void aVersionRetainedThroughAnUpstreamThatFetchedItExternallyIsExternal() {
+ service.createDomain(REGION, "dom", null, Map.of());
+ for (String repository : List.of("up", "repo")) {
+ service.createRepository(REGION, "dom", null, repository, null, null, Map.of());
+ }
+ retainNpmWidget("up", "public:npmjs", RetentionOriginKind.EXTERNAL_CONNECTION);
+ retainNpmWidget("repo", "up", RetentionOriginKind.UPSTREAM_REPOSITORY);
+
+ CodeArtifactService.PackageVersionSummary summary = listedNpmWidget("repo");
+ assertEquals("EXTERNAL", summary.originType());
+ assertEquals("public:npmjs", summary.externalConnectionName());
+ assertEquals(null, summary.upstreamRepositoryName());
+ assertEquals(Optional.of("up"), service.retainedUpstreamRepositoryOrigin(REGION, "dom", null, "repo", "npm",
+ null, "widget", "1.0"));
+ }
+
+ /**
+ * The upstream has written what it fetched through its connection but not yet confirmed it when
+ * the copy here is claimed and confirmed: the copy is still the one that entered the domain
+ * through that connection, and stays so once the upstream confirms.
+ */
+ @Test
+ void aVersionCopiedWhileTheUpstreamsExternalFetchIsUnconfirmedIsExternal() {
+ service.createDomain(REGION, "dom", null, Map.of());
+ for (String repository : List.of("up", "repo")) {
+ service.createRepository(REGION, "dom", null, repository, null, null, Map.of());
+ }
+ long upFence = service.currentContainerBackedPackageFence(REGION, "dom", null, "up", "npm", null, "widget");
+ CodeArtifactService.RetainedVersionOriginClaimOutcome upClaim = service.claimRetainedVersionOrigin(REGION,
+ "dom", null, "up", "npm", null, "widget", "1.0", "public:npmjs",
+ RetentionOriginKind.EXTERNAL_CONNECTION, upFence);
+
+ retainNpmWidget("repo", "up", RetentionOriginKind.UPSTREAM_REPOSITORY);
+ service.finishRetainedVersionOriginAttempt(REGION, "dom", null, "up", "npm", null, "widget", "1.0",
+ upClaim.episodeId(), true);
+
+ CodeArtifactService.PackageVersionSummary summary = listedNpmWidget("repo");
+ assertEquals("EXTERNAL", summary.originType());
+ assertEquals("public:npmjs", summary.externalConnectionName());
+ }
+
+ /**
+ * The upstream's attempt gives its claim up after the copy here was claimed, the upstream's own
+ * copy having been published there: confirmed, the copy here names the upstream it came from.
+ */
+ @Test
+ void aCopyConfirmedAfterTheUpstreamGaveItsClaimUpNamesTheUpstream() {
+ service.createDomain(REGION, "dom", null, Map.of());
+ for (String repository : List.of("up", "repo")) {
+ service.createRepository(REGION, "dom", null, repository, null, null, Map.of());
+ }
+ long upFence = service.currentContainerBackedPackageFence(REGION, "dom", null, "up", "npm", null, "widget");
+ CodeArtifactService.RetainedVersionOriginClaimOutcome upClaim = service.claimRetainedVersionOrigin(REGION,
+ "dom", null, "up", "npm", null, "widget", "1.0", "public:npmjs",
+ RetentionOriginKind.EXTERNAL_CONNECTION, upFence);
+ long fence = service.currentContainerBackedPackageFence(REGION, "dom", null, "repo", "npm", null, "widget");
+ CodeArtifactService.RetainedVersionOriginClaimOutcome claim = service.claimRetainedVersionOrigin(REGION,
+ "dom", null, "repo", "npm", null, "widget", "1.0", "up", RetentionOriginKind.UPSTREAM_REPOSITORY,
+ fence);
+
+ service.finishRetainedVersionOriginAttempt(REGION, "dom", null, "up", "npm", null, "widget", "1.0",
+ upClaim.episodeId(), false);
+ service.finishRetainedVersionOriginAttempt(REGION, "dom", null, "repo", "npm", null, "widget", "1.0",
+ claim.episodeId(), true);
+
+ CodeArtifactService.PackageVersionSummary summary = listedNpmWidget("repo");
+ assertEquals("INTERNAL", summary.originType());
+ assertEquals("up", summary.upstreamRepositoryName());
+ }
+
/**
* {@code originType} stays {@code "INTERNAL"} for a same-domain upstream-repository retention
* (real AWS's own {@code PackageVersionOrigin.originType} has no third value for this), with
diff --git a/src/test/java/io/floci/aws/services/codeartifact/UpstreamRepositoryResolverTest.java b/src/test/java/io/floci/aws/services/codeartifact/UpstreamRepositoryResolverTest.java
index 8d1121c78d..cc8242bcc7 100644
--- a/src/test/java/io/floci/aws/services/codeartifact/UpstreamRepositoryResolverTest.java
+++ b/src/test/java/io/floci/aws/services/codeartifact/UpstreamRepositoryResolverTest.java
@@ -11,24 +11,26 @@ import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertTrue;
/**
- * {@code resolveSearchOrder}'s own javadoc works through the exact A/B/C/D counterexample this
- * test locks in: a depth-first walk would wrongly search a 2-hop match before a 1-hop sibling.
+ * {@code resolveSearchOrder} walks the upstream graph the way the User Guide's "Upstream repository
+ * priority order" example does: depth-first, in each repository's listed order.
*/
class UpstreamRepositoryResolverTest {
@Test
- void directUpstreamsAreSearchedBeforeAnyOfTheirOwnTransitiveUpstreams() {
- // A -> [B, C], B -> [D]. A depth-first walk reaches D (via B) before C, a direct upstream
- // of A; the correct, breadth-first order is B, C (both direct), then D.
+ void theUserGuidesExampleIsSearchedDepthFirstInListOrder() {
+ // "Upstream repository priority order": my_repo -> [A, B], A -> [C, D], D -> [E] is
+ // searched my_repo, A, C, D, E, B.
Map> upstreams = Map.of(
- "A", List.of("B", "C"),
- "B", List.of("D"),
+ "my_repo", List.of("A", "B"),
+ "A", List.of("C", "D"),
+ "B", List.of(),
"C", List.of(),
- "D", List.of());
+ "D", List.of("E"),
+ "E", List.of());
- List order = UpstreamRepositoryResolver.resolveSearchOrder("A", upstreams::get);
+ List order = UpstreamRepositoryResolver.resolveSearchOrder("my_repo", upstreams::get);
- assertEquals(List.of("B", "C", "D"), order);
+ assertEquals(List.of("A", "C", "D", "E", "B"), order);
}
@Test
@@ -57,7 +59,7 @@ class UpstreamRepositoryResolverTest {
@Test
void aRepositoryReachableThroughTwoPathsAppearsExactlyOnce() {
// A -> [B, C], B -> [D], C -> [D]: D is reachable from both B and C, but must only be
- // searched once, at the position its first discovery (through B) places it.
+ // searched once, where it is first reached, through B.
Map> upstreams = Map.of(
"A", List.of("B", "C"),
"B", List.of("D"),
@@ -66,7 +68,7 @@ class UpstreamRepositoryResolverTest {
List order = UpstreamRepositoryResolver.resolveSearchOrder("A", upstreams::get);
- assertEquals(List.of("B", "C", "D"), order);
+ assertEquals(List.of("B", "D", "C"), order);
}
@Test