fix(ecr): apply repository lifecycle policies in the background (#9318)

A sweep now applies every repository's lifecycle policy, as ECR does
within a day of an image meeting a rule, and expires the images its
rules select: imageCountMoreThan keeps the newest by push time and
sinceImagePushed expires those older than the count, with rules applied
in priority order and none marking an image a higher rule selected. A
platform image of a held manifest list is kept until the list goes, and
an artifact naming a subject expires with that image. Get and
DeleteLifecyclePolicy report lastEvaluatedAt, and the delete echoes the
policy it removed.

Closes #5874.
This commit is contained in:
Priamo Germosen authored and GitHub committed 2026-11-23 22:54:39 -04:00
1 parent 365bad3dde
commit ddad3ca1df
19 files changed
+1652 -53

No files matched your search

@@ -0,0 +1,208 @@
package io.floci.aws.services.ecr;
import io.floci.aws.services.ecr.EcrLifecycleEvaluator.Expiry;
import io.floci.aws.services.ecr.EcrLifecycleEvaluator.Image;
import org.junit.jupiter.api.Test;
import java.time.Duration;
import java.time.Instant;
import java.util.List;
import static org.junit.jupiter.api.Assertions.assertEquals;
/**
* The worked examples of the ECR user guide's lifecycle policy examples page, each with the images it lists and the
* result it states, then what Floci adds: manifest lists, and the rules it applies no action for.
*/
class EcrLifecycleEvaluatorTest {
private static final Instant NOW = Instant.parse("2026-11-23T12:00:00Z");
private static final String EXPIRE = "{\"type\":\"expire\"}";
private static final String TRANSITION = "{\"type\":\"transition\",\"targetStorageClass\":\"archive\"}";
@Test
void multipleRulesLowerPriorityRuleSkipsAnImageAHigherOneSelected() {
List<Image> images = List.of(image("A", 10, "beta-1", "prod-1"), image("B", 9, "beta-2", "prod-2"),
image("C", 8, "beta-3"));
assertEquals(List.of("A"), expired(policy(rule(1, tagged("prod*", 1), EXPIRE),
rule(2, tagged("beta*", 1), EXPIRE)), images));
assertEquals(List.of("B", "A"), expired(policy(rule(1, tagged("beta*", 1), EXPIRE),
rule(2, tagged("prod*", 1), EXPIRE)), images));
}
@Test
void multipleTagsInOneRuleSelectOnlyImagesMatchingEveryPattern() {
List<Image> byAge = List.of(image("A", 12, "alpha-1"), image("B", 11, "beta-1"),
image("C", 10, "alpha-2", "beta-2"), image("D", 4, "alpha-3"), image("E", 3, "beta-3"),
image("F", 2, "alpha-4", "beta-4"));
assertEquals(List.of("C"), expired(policy(rule(1, "{\"tagStatus\":\"tagged\",\"tagPatternList\":"
+ "[\"alpha*\",\"beta*\"],\"countType\":\"sinceImagePushed\",\"countNumber\":5,\"countUnit\":\"days\"}",
EXPIRE)), byAge));
List<Image> byCount = List.of(image("A", 10, "alpha-1", "beta-1", "gamma-1"),
image("B", 9, "alpha-2", "beta-2"), image("C", 8, "alpha-3", "beta-3", "gamma-2"));
assertEquals(List.of("B", "A"), expired(policy(rule(1, "{\"tagStatus\":\"tagged\",\"tagPatternList\":"
+ "[\"alpha*\",\"beta*\"],\"countType\":\"imageCountMoreThan\",\"countNumber\":1}", EXPIRE)), byCount));
}
@Test
void anAnyRuleKeepsOnlyTheNewestImages() {
List<Image> images = List.of(image("A", 4, "alpha-1"), image("B", 3, "beta-1"), image("C", 2),
image("D", 1, "alpha-2"));
assertEquals(List.of("C", "B", "A"), expired(policy(rule(1, any(1), EXPIRE)), images));
}
/** "Images A, B, and D are expired": the last rule can mark only D, the one image no earlier rule selected. */
@Test
void anAnyRuleAfterTaggedAndUntaggedRulesMarksOnlyWhatTheySkipped() {
List<Image> images = List.of(image("A", Duration.ofDays(4), "alpha-1", "beta-1"),
image("B", Duration.ofDays(3)), image("C", Duration.ofDays(2), "alpha-2"),
image("D", Duration.ofDays(1), "git hash"), image("E", Duration.ofDays(1).minusMinutes(1)));
assertEquals(List.of("A", "B", "D"), expired(policy(rule(1, tagged("alpha*", 1), EXPIRE),
rule(2, "{\"tagStatus\":\"untagged\",\"countType\":\"sinceImagePushed\",\"countUnit\":\"days\","
+ "\"countNumber\":1}", EXPIRE),
rule(3, any(1), EXPIRE)), images));
}
@Test
void anUntaggedRuleKeepsOneUntaggedImage() {
List<Image> images = List.of(image("A", 3), image("B", 2), image("C", 1), image("T", 5, "latest"));
assertEquals(List.of("B", "A"), expired(policy(rule(1, "{\"tagStatus\":\"untagged\","
+ "\"countType\":\"imageCountMoreThan\",\"countNumber\":1}", EXPIRE)), images));
}
@Test
void aPrefixListSelectsImagesWithATagStartingWithEachPrefix() {
List<Image> images = List.of(image("A", 5, "prod-1", "v1"), image("B", 4, "prod-2"), image("C", 3, "v2-prod"),
image("D", 2, "prod-3", "v3"));
assertEquals(List.of("A"), expired(policy(rule(1, "{\"tagStatus\":\"tagged\",\"tagPrefixList\":"
+ "[\"prod\",\"v\"],\"countType\":\"imageCountMoreThan\",\"countNumber\":1}", EXPIRE)), images));
}
@Test
void aWildcardMatchesAnyRunOfCharactersIncludingNone() {
List<Image> images = List.of(image("A", 5, "prod"), image("B", 4, "repo-production"), image("C", 3, "dev"),
image("D", 2, "prod-team"));
assertEquals(List.of("B", "A"), expired(policy(rule(1, tagged("*prod*", 1), EXPIRE)), images));
}
/** "Older than" the count: an image pushed exactly that many days ago stays. */
@Test
void sinceImagePushedExpiresOnlyImagesOlderThanTheCount() {
List<Image> images = List.of(image("A", Duration.ofDays(7).plusSeconds(1)), image("B", Duration.ofDays(7)),
image("C", Duration.ofDays(1)));
assertEquals(List.of("A"), expired(policy(rule(1, "{\"tagStatus\":\"any\",\"countType\":\"sinceImagePushed\","
+ "\"countUnit\":\"days\",\"countNumber\":7}", EXPIRE)), images));
}
@Test
void imagesPushedAtOnceAreKeptInDigestOrder() {
List<Image> images = List.of(image("B", 1), image("A", 1), image("C", 1));
assertEquals(List.of("C"), expired(policy(rule(1, any(2), EXPIRE)), images));
}
/**
* "If an image is referenced by a manifest list, it cannot be expired or archived without the manifest list being
* deleted or archived first."
*/
@Test
void anImageAManifestListNamesStillCountsButNeverExpires() {
List<Image> images = List.of(new Image("child-1", List.of(), NOW.minus(Duration.ofDays(1)), true),
new Image("child-2", List.of(), NOW.minus(Duration.ofDays(1)), true), image("orphan-1", 8),
image("orphan-2", 7), image("index", 1, "v1"));
assertEquals(List.of("orphan-2", "orphan-1"), expired(policy(rule(1, "{\"tagStatus\":\"untagged\","
+ "\"countType\":\"imageCountMoreThan\",\"countNumber\":1}", EXPIRE)), images));
}
/** Floci has no archive storage: a transition rule moves nothing, but lower rules still cannot take its images. */
@Test
void aTransitionRuleExpiresNothingAndKeepsLowerRulesOffWhatItSelects() {
List<Image> images = List.of(image("A", 40, "prod-1"), image("B", 35, "dev-1"), image("C", 1, "prod-2"));
assertEquals(List.of("B"), expired(policy(rule(1, "{\"tagStatus\":\"tagged\",\"tagPatternList\":[\"prod*\"],"
+ "\"countType\":\"sinceImagePushed\",\"countUnit\":\"days\",\"countNumber\":30}", TRANSITION),
rule(2, "{\"tagStatus\":\"any\",\"countType\":\"sinceImagePushed\",\"countUnit\":\"days\","
+ "\"countNumber\":30}", EXPIRE)), images));
}
/** Floci records no pulls, so a pull-age rule marks nothing, yet keeps lower rules off what it selects. */
@Test
void aSinceImagePulledRuleExpiresNothingAndKeepsLowerRulesOffWhatItSelects() {
List<Image> images = List.of(image("A", 100, "prod-1"), image("B", 100, "dev-1"));
assertEquals(List.of("B"), expired(policy(rule(1, "{\"tagStatus\":\"tagged\",\"tagPatternList\":[\"prod*\"],"
+ "\"countType\":\"sinceImagePulled\",\"countUnit\":\"days\",\"countNumber\":1}", EXPIRE),
rule(2, "{\"tagStatus\":\"any\",\"countType\":\"sinceImagePushed\",\"countUnit\":\"days\","
+ "\"countNumber\":1}", EXPIRE)), images));
}
/** Every image is in standard storage, so an archive rule selects none and keeps no later rule off them. */
@Test
void anArchiveRuleSelectsNoImage() {
List<Image> images = List.of(image("A", 400), image("B", 500, "v1"));
assertEquals(List.of("A", "B"), expired(policy(rule(1, "{\"tagStatus\":\"any\",\"storageClass\":\"archive\","
+ "\"countType\":\"sinceImageTransitioned\",\"countUnit\":\"days\",\"countNumber\":365}",
EXPIRE),
rule(2, "{\"tagStatus\":\"any\",\"countType\":\"sinceImagePushed\",\"countUnit\":\"days\","
+ "\"countNumber\":30}", EXPIRE)), images));
}
@Test
void aPatternMatchesTheWholeTag() {
List<Image> images = List.of(image("A", 5, "preprod"), image("B", 4, "prod-1"), image("C", 3, "prod-2"));
assertEquals(List.of("B"), expired(policy(rule(1, tagged("prod*", 1), EXPIRE)), images));
}
/** Rules apply in priority order whatever order the document lists them in. */
@Test
void anExpiryNamesTheRuleThatMarkedIt() {
List<Image> images = List.of(image("A", 5, "prod-1"), image("B", 4, "prod-2"), image("C", 3),
image("D", 10, "dev"));
assertEquals(List.of(new Expiry("A", 4), new Expiry("D", 9)), EcrLifecycleEvaluator.expiries(
EcrLifecyclePolicy.parse(policy(rule(9, any(2), EXPIRE), rule(4, tagged("prod*", 1), EXPIRE),
rule(6, "{\"tagStatus\":\"untagged\",\"countType\":\"sinceImagePushed\","
+ "\"countUnit\":\"days\",\"countNumber\":30}", EXPIRE))), images, NOW));
}
private static List<String> expired(String policy, List<Image> images) {
return EcrLifecycleEvaluator.expiries(EcrLifecyclePolicy.parse(policy), images, NOW).stream()
.map(Expiry::digest).toList();
}
private static Image image(String digest, int pushedDaysAgo, String... tags) {
return image(digest, Duration.ofDays(pushedDaysAgo), tags);
}
private static Image image(String digest, Duration pushedAgo, String... tags) {
return new Image(digest, List.of(tags), NOW.minus(pushedAgo), false);
}
private static String policy(String... rules) {
return "{\"rules\":[" + String.join(",", rules) + "]}";
}
private static String rule(int priority, String selection, String action) {
return "{\"rulePriority\":" + priority + ",\"selection\":" + selection + ",\"action\":" + action + "}";
}
private static String tagged(String pattern, int count) {
return "{\"tagStatus\":\"tagged\",\"tagPatternList\":[\"" + pattern
+ "\"],\"countType\":\"imageCountMoreThan\",\"countNumber\":" + count + "}";
}
private static String any(int count) {
return "{\"tagStatus\":\"any\",\"countType\":\"imageCountMoreThan\",\"countNumber\":" + count + "}";
}
}
@@ -13,10 +13,12 @@ import org.junit.jupiter.params.provider.MethodSource;
import software.amazon.awssdk.regions.Region;
import software.amazon.awssdk.services.cloudformation.CloudFormationClient;
import software.amazon.awssdk.services.ecr.EcrClient;
import software.amazon.awssdk.services.ecr.model.DeleteLifecyclePolicyResponse;
import software.amazon.awssdk.services.ecr.model.InvalidParameterException;
import software.amazon.awssdk.services.ecr.model.LifecyclePolicyNotFoundException;
import software.amazon.awssdk.services.ecr.model.RepositoryNotFoundException;
import java.time.Instant;
import java.util.UUID;
import java.util.stream.Stream;
@@ -128,6 +130,27 @@ class EcrLifecyclePolicyValidationIntegrationTest {
}
}
/** ECR answers with a time stamp before the sweep first applies the policy, and deleting it echoes it. */
@Test
void getAndDeleteSayWhenThePolicyWasLastApplied() {
String repository = "lifecycle-validation/" + UUID.randomUUID();
try (EcrClient client = AwsTestClients.create(EcrClient.builder(), "test", Region.US_EAST_1)) {
client.createRepository(r -> r.repositoryName(repository));
try {
client.putLifecyclePolicy(r -> r.repositoryName(repository).lifecyclePolicyText(VALID_POLICY));
assertEquals(Instant.EPOCH, client.getLifecyclePolicy(r -> r.repositoryName(repository))
.lastEvaluatedAt());
DeleteLifecyclePolicyResponse deleted = client.deleteLifecyclePolicy(
r -> r.repositoryName(repository));
assertEquals(VALID_POLICY, deleted.lifecyclePolicyText());
assertEquals(Instant.EPOCH, deleted.lastEvaluatedAt());
} finally {
client.deleteRepository(r -> r.repositoryName(repository));
}
}
}
/** ECR's validator ends its text with a newline, which the message keeps inside the closing quote. */
@Test
void aRefusedDocumentCarriesEcrsMessage() {
@@ -0,0 +1,112 @@
package io.floci.aws.services.ecr;
import io.floci.aws.core.common.Digests;
import io.floci.aws.testing.EcrTestImages;
import io.floci.aws.testing.RestAssuredJsonUtils;
import io.floci.aws.testing.SidecarContainersProfile;
import io.quarkus.test.junit.QuarkusTest;
import io.quarkus.test.junit.TestProfile;
import io.restassured.RestAssured;
import io.restassured.response.ValidatableResponse;
import jakarta.inject.Inject;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.Test;
import java.nio.charset.StandardCharsets;
import java.time.Instant;
import java.util.List;
import java.util.UUID;
import static io.restassured.RestAssured.given;
import static org.hamcrest.Matchers.equalTo;
import static org.hamcrest.Matchers.greaterThan;
import static org.hamcrest.Matchers.hasSize;
/**
* A repository's lifecycle policy applied by the sweep against the real backing registry: images pushed through the
* registry API as {@code docker push} sends them, the sweep run, and the expired image gone from the registry and from
* {@code DescribeImages}.
*/
@QuarkusTest
@TestProfile(SidecarContainersProfile.class)
class EcrLifecycleSweepDockerIntegrationTest {
private static final String DOCKER_MANIFEST = "application/vnd.docker.distribution.manifest.v2+json";
private static final String KEEP_ONE = "{\"rules\":[{\"rulePriority\":1,\"selection\":{\"tagStatus\":\"any\","
+ "\"countType\":\"imageCountMoreThan\",\"countNumber\":1},\"action\":{\"type\":\"expire\"}}]}";
@Inject
EcrLifecycleScheduler scheduler;
private final String repo = "lifecycle/" + UUID.randomUUID().toString().substring(0, 8);
@BeforeAll
static void requireDocker() {
SidecarContainersProfile.requireDockerAndImage("floci.services.ecr.registry-image");
RestAssuredJsonUtils.configureAwsContentTypes();
}
@AfterEach
void deleteRepository() {
call("DeleteRepository", "{\"repositoryName\":\"" + repo + "\",\"force\":true}");
}
@Test
void theSweepExpiresTheImagesTheRepositorysPolicySelects() {
call("CreateRepository", "{\"repositoryName\":\"" + repo + "\"}").statusCode(200);
String host = "000000000000.dkr.ecr.us-east-1.localhost:" + RestAssured.port;
byte[] layer = ("layer " + repo).getBytes(StandardCharsets.UTF_8);
String layerDigest = "sha256:" + Digests.sha256Hex(layer);
EcrTestImages.uploadBlob(host, repo, layerDigest, layer);
for (String tag : List.of("v1", "v2", "v3")) {
push(host, tag, layer.length, layerDigest);
}
call("PutLifecyclePolicy", "{\"repositoryName\":\"" + repo + "\",\"lifecyclePolicyText\":"
+ quoted(KEEP_ONE) + "}").statusCode(200);
call("GetLifecyclePolicy", "{\"repositoryName\":\"" + repo + "\"}").statusCode(200)
.body("lastEvaluatedAt", equalTo(0));
scheduler.tick(Instant.now());
call("DescribeImages", "{\"repositoryName\":\"" + repo + "\"}").statusCode(200)
.body("imageDetails", hasSize(1))
.body("imageDetails[0].imageTags", equalTo(List.of("v3")));
given().header("Host", host).accept(DOCKER_MANIFEST)
.when().get("/v2/" + repo + "/manifests/v1")
.then().statusCode(404);
call("GetLifecyclePolicy", "{\"repositoryName\":\"" + repo + "\"}").statusCode(200)
.body("lastEvaluatedAt", greaterThan(0));
call("DeleteLifecyclePolicy", "{\"repositoryName\":\"" + repo + "\"}").statusCode(200)
.body("lifecyclePolicyText", equalTo(KEEP_ONE))
.body("lastEvaluatedAt", greaterThan(0));
}
/** One image per tag: each has its own config blob over the shared layer, so each has its own digest. */
private void push(String host, String tag, int layerSize, String layerDigest) {
byte[] config = ("{\"architecture\":\"amd64\",\"os\":\"linux\",\"config\":{\"Labels\":{\"tag\":\"" + tag
+ "\"}},\"rootfs\":{\"type\":\"layers\",\"diff_ids\":[]}}").getBytes(StandardCharsets.UTF_8);
String configDigest = "sha256:" + Digests.sha256Hex(config);
EcrTestImages.uploadBlob(host, repo, configDigest, config);
String manifest = """
{"schemaVersion":2,"mediaType":"%s",
"config":{"mediaType":"application/vnd.docker.container.image.v1+json","size":%d,"digest":"%s"},
"layers":[{"mediaType":"application/vnd.docker.image.rootfs.diff.tar.gzip","size":%d,"digest":"%s"}]}
""".formatted(DOCKER_MANIFEST, config.length, configDigest, layerSize, layerDigest);
given().header("Host", host).contentType(DOCKER_MANIFEST).body(manifest)
.when().put("/v2/" + repo + "/manifests/" + tag)
.then().statusCode(201);
}
private static String quoted(String json) {
return "\"" + json.replace("\"", "\\\"") + "\"";
}
private static ValidatableResponse call(String action, String body) {
return given().header("X-Amz-Target", "AmazonEC2ContainerRegistry_V20150921." + action)
.contentType("application/x-amz-json-1.1")
.body(body)
.when().post("/")
.then();
}
}
@@ -0,0 +1,658 @@
package io.floci.aws.services.ecr;
import com.sun.net.httpserver.HttpExchange;
import com.sun.net.httpserver.HttpServer;
import io.floci.aws.config.EmulatorConfig;
import io.floci.aws.core.common.AwsException;
import io.floci.aws.core.common.Digests;
import io.floci.aws.core.common.RegionResolver;
import io.floci.aws.core.storage.InMemoryStorage;
import io.floci.aws.services.ecr.model.ImageMetadata;
import io.floci.aws.services.ecr.model.Repository;
import io.floci.aws.services.ecr.registry.EcrRegistryManager;
import io.floci.aws.services.ecr.registry.RegistryHttpClient;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.mockito.Answers;
import org.mockito.Mockito;
import java.io.IOException;
import java.io.OutputStream;
import java.net.InetSocketAddress;
import java.nio.charset.StandardCharsets;
import java.time.Duration;
import java.time.Instant;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.TreeMap;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.Semaphore;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.concurrent.locks.LockSupport;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertNull;
import static org.junit.jupiter.api.Assertions.assertThrows;
import static org.junit.jupiter.api.Assertions.assertTrue;
import static org.mockito.ArgumentMatchers.anyString;
import static org.mockito.Mockito.when;
/** {@link EcrService#applyLifecyclePolicies} against an in-memory registry speaking the distribution API. */
class EcrLifecycleSweepTest {
private static final String REGION = "us-east-1";
private static final String ACCOUNT = "000000000000";
private static final String REPO = "app";
private static final Instant NOW = Instant.parse("2026-11-23T12:00:00Z");
private static final String KEEP_TWO = policy("{\"rulePriority\":1,\"selection\":{\"tagStatus\":\"any\","
+ "\"countType\":\"imageCountMoreThan\",\"countNumber\":2},\"action\":{\"type\":\"expire\"}}");
private static final String OLDER_THAN_FIVE_DAYS = policy("{\"rulePriority\":1,\"selection\":{"
+ "\"tagStatus\":\"any\",\"countType\":\"sinceImagePushed\",\"countUnit\":\"days\",\"countNumber\":5},"
+ "\"action\":{\"type\":\"expire\"}}");
private FakeRegistry registry;
private EcrRegistryManager registryManager;
private InMemoryStorage<String, Repository> repositories;
private InMemoryStorage<String, ImageMetadata> pushRecords;
private EcrService service;
private EmulatorConfig config;
@BeforeEach
void setUp() throws IOException {
registry = new FakeRegistry();
registryManager = Mockito.mock(EcrRegistryManager.class);
when(registryManager.getRepositoryUri(anyString(), anyString(), anyString())).thenReturn("uri");
when(registryManager.internalRepoName(anyString(), anyString(), anyString()))
.thenAnswer(inv -> inv.getArgument(0) + "/" + inv.getArgument(1) + "/" + inv.getArgument(2));
when(registryManager.isStarted()).thenReturn(true);
when(registryManager.httpClient()).thenReturn(new RegistryHttpClient("http://127.0.0.1:" + registry.port()));
when(registryManager.heldManifestDigests(anyString())).thenAnswer(inv -> registry.digests(inv.getArgument(0)));
config = Mockito.mock(EmulatorConfig.class, Answers.RETURNS_DEEP_STUBS);
when(config.services().ecr().enabled()).thenReturn(true);
repositories = new InMemoryStorage<>();
pushRecords = new InMemoryStorage<>();
service = new EcrService(repositories, pushRecords, new InMemoryStorage<>(), registryManager, config,
new RegionResolver(REGION, ACCOUNT));
}
@AfterEach
void tearDown() {
registry.close();
}
@Test
void theSweepExpiresWhatThePolicySelectsAndRecordsWhenItRan() {
Repository repo = repository(REPO, KEEP_TWO);
String v1 = push(REGION, REPO, "v1", image("v1"), 4);
String v2 = push(REGION, REPO, "v2", image("v2"), 3);
String v3 = push(REGION, REPO, "v3", image("v3"), 2);
String v4 = push(REGION, REPO, "v4", image("v4"), 1);
assertNull(service.lifecyclePolicyEvaluatedAt(repo, REGION));
service.applyLifecyclePolicies(NOW);
assertEquals(Set.of(v3, v4), registry.digests(internal(REGION, REPO)));
assertEquals(Set.of(v3, v4), recordedDigests(REGION, REPO));
assertEquals(NOW, service.lifecyclePolicyEvaluatedAt(repo, REGION));
assertEquals(Set.of(v1, v2), Set.copyOf(registry.deleted));
}
/**
* Untagged images are every manifest the registry holds without a tag: one with no push record is taken as pushed
* when the sweep first sees it, as DescribeImages does, and a push record of a manifest that is gone names nothing.
*/
@Test
void untaggedImagesAreTheManifestsTheRegistryHoldsWithoutATag() {
repository(REPO, policy("{\"rulePriority\":1,\"selection\":{\"tagStatus\":\"untagged\","
+ "\"countType\":\"imageCountMoreThan\",\"countNumber\":1},\"action\":{\"type\":\"expire\"}}"));
String tagged = push(REGION, REPO, "latest", image("tagged"), 9);
String oldest = push(REGION, REPO, null, image("u1"), 8);
String older = push(REGION, REPO, null, image("u2"), 7);
String unrecorded = registry.put(internal(REGION, REPO), null, image("u3"));
service.recordImagePush(ACCOUNT, REGION, REPO, digest(image("gone")), NOW.minus(Duration.ofDays(20)));
service.applyLifecyclePolicies(NOW);
assertEquals(Set.of(tagged, unrecorded), registry.digests(internal(REGION, REPO)));
assertEquals(Set.of(oldest, older), Set.copyOf(registry.deleted));
}
/**
* "If an image is referenced by a manifest list, it cannot be expired or archived without the manifest list being
* deleted or archived first."
*/
@Test
void anImageAManifestListNamesExpiresOnlyOnceTheListIsGone() {
repository(REPO, OLDER_THAN_FIVE_DAYS);
String amd64 = push(REGION, REPO, null, image("amd64"), 10);
String arm64 = push(REGION, REPO, null, image("arm64"), 10);
String index = push(REGION, REPO, "v1", "{\"schemaVersion\":2,\"mediaType\":"
+ "\"application/vnd.oci.image.index.v1+json\",\"manifests\":[{\"digest\":\"" + amd64 + "\"},"
+ "{\"digest\":\"" + arm64 + "\"}]}", 10);
service.applyLifecyclePolicies(NOW);
assertEquals(List.of(index), registry.deleted);
service.applyLifecyclePolicies(NOW);
assertEquals(Set.of(), registry.digests(internal(REGION, REPO)));
}
/**
* "When reference artifacts are present in a repository, Amazon ECR lifecycle policies automatically expire or
* archive those artifacts within 24 hours of the deletion or archival of the subject image."
*/
@Test
void anArtifactExpiresWithItsSubjectAndIsSelectedByNoRule() {
repository(REPO, OLDER_THAN_FIVE_DAYS);
String old = push(REGION, REPO, "old", image("old"), 10);
String current = push(REGION, REPO, "current", image("current"), 1);
String oldSignature = push(REGION, REPO, null, referrer(old, "old-sig"), 1);
String currentSignature = push(REGION, REPO, null, referrer(current, "current-sig"), 10);
service.applyLifecyclePolicies(NOW);
assertEquals(Set.of(current, currentSignature), registry.digests(internal(REGION, REPO)));
assertEquals(Set.of(old, oldSignature), Set.copyOf(registry.deleted));
}
/** The subject's "deletion", in the user guide's words, whatever deleted it: here a BatchDeleteImage before. */
/** An image the registry holds counts as held whether or not Floci recorded its push. */
@Test
void anArtifactOfAnImageWithNoPushRecordIsKept() {
repository(REPO, OLDER_THAN_FIVE_DAYS);
String subject = registry.put(internal(REGION, REPO), null, image("unrecorded"));
String signature = push(REGION, REPO, null, referrer(subject, "sig"), 10);
service.applyLifecyclePolicies(NOW);
assertEquals(Set.of(subject, signature), registry.digests(internal(REGION, REPO)));
}
@Test
void aManifestListWithNoPushRecordStillKeepsItsImages() {
repository(REPO, OLDER_THAN_FIVE_DAYS);
String amd64 = push(REGION, REPO, null, image("amd64"), 10);
String index = registry.put(internal(REGION, REPO), null, "{\"schemaVersion\":2,\"mediaType\":"
+ "\"application/vnd.oci.image.index.v1+json\",\"manifests\":[{\"digest\":\"" + amd64 + "\"}]}");
service.applyLifecyclePolicies(NOW);
assertEquals(Set.of(amd64, index), registry.digests(internal(REGION, REPO)));
}
/** A delete the registry refuses stops the sweep: no artifact goes while the image it names is still there. */
@Test
void aDeleteTheRegistryRefusesStopsTheSweepOfThatRepository() {
Repository repo = repository(REPO, OLDER_THAN_FIVE_DAYS);
String old = push(REGION, REPO, "old", image("old"), 10);
String signature = push(REGION, REPO, null, referrer(old, "sig"), 1);
registry.refusedDeletes.add(old);
assertThrows(IllegalStateException.class, () -> service.applyLifecyclePolicies(NOW));
assertEquals(Set.of(old, signature), registry.digests(internal(REGION, REPO)));
assertNull(service.lifecyclePolicyEvaluatedAt(repo, REGION));
}
@Test
void anArtifactWhoseSubjectIsAlreadyGoneExpires() {
repository(REPO, KEEP_TWO);
String kept = push(REGION, REPO, "v1", image("v1"), 1);
String orphan = push(REGION, REPO, null, referrer(digest(image("deleted")), "sig"), 1);
service.applyLifecyclePolicies(NOW);
assertEquals(Set.of(kept), registry.digests(internal(REGION, REPO)));
assertEquals(List.of(orphan), registry.deleted);
}
@Test
void eachRepositoryIsSweptInItsOwnRegion() {
repository(REPO, KEEP_TWO);
repository("eu-west-1", "other", OLDER_THAN_FIVE_DAYS);
repository("plain", null);
String here = push(REGION, REPO, "v1", image("here"), 30);
String there = push("eu-west-1", "other", "v1", image("there"), 30);
String untouched = push(REGION, "plain", "v1", image("plain"), 30);
service.applyLifecyclePolicies(NOW);
assertEquals(Set.of(here), registry.digests(internal(REGION, REPO)));
assertEquals(Set.of(), registry.digests(internal("eu-west-1", "other")));
assertEquals(Set.of(untouched), registry.digests(internal(REGION, "plain")));
assertEquals(List.of(there), registry.deleted);
}
@Test
void nothingIsSweptWhileTheRegistryIsNotRunning() {
Repository repo = repository(REPO, OLDER_THAN_FIVE_DAYS);
String old = push(REGION, REPO, "v1", image("v1"), 30);
when(registryManager.isStarted()).thenReturn(false);
service.applyLifecyclePolicies(NOW);
assertEquals(Set.of(old), registry.digests(internal(REGION, REPO)));
assertEquals(0, registry.requests.get());
assertNull(service.lifecyclePolicyEvaluatedAt(repo, REGION));
}
/** A policy stored before every rule was checked, which PutLifecyclePolicy now refuses, is left unapplied. */
@Test
void aStoredPolicyThatIsNowRefusedIsNotApplied() {
Repository repo = repository(REPO, null);
repo.setLifecyclePolicyText(policy("{\"rulePriority\":1,\"selection\":{\"tagStatus\":\"tagged\","
+ "\"countType\":\"imageCountMoreThan\",\"countNumber\":1},\"action\":{\"type\":\"expire\"}}"));
repositories.put(REGION + "::" + ACCOUNT + "::" + REPO, repo);
String v1 = push(REGION, REPO, "v1", image("v1"), 30);
String v2 = push(REGION, REPO, "v2", image("v2"), 20);
service.applyLifecyclePolicies(NOW);
assertEquals(Set.of(v1, v2), registry.digests(internal(REGION, REPO)));
assertNull(service.lifecyclePolicyEvaluatedAt(repo, REGION));
}
@Test
void aRepositoryTheRegistryFailsForIsRetriedLaterWhileTheOthersAreSwept() {
repository("broken", OLDER_THAN_FIVE_DAYS);
repository(REPO, OLDER_THAN_FIVE_DAYS);
String old = push(REGION, REPO, "v1", image("v1"), 30);
push(REGION, "broken", "v1", image("broken"), 30);
registry.failing.add(internal(REGION, "broken"));
IllegalStateException failure = assertThrows(IllegalStateException.class,
() -> service.applyLifecyclePolicies(NOW));
assertTrue(failure.getMessage().contains(REGION + "::" + ACCOUNT + "::broken"), failure.getMessage());
assertEquals(List.of(old), registry.deleted);
}
@Test
void aPolicyPutSinceTheSweepReadsAsNeverApplied() {
Repository repo = repository(REPO, KEEP_TWO);
service.applyLifecyclePolicies(NOW);
assertEquals(NOW, service.lifecyclePolicyEvaluatedAt(repo, REGION));
service.putLifecyclePolicy(REPO, null, OLDER_THAN_FIVE_DAYS, REGION);
assertNull(service.lifecyclePolicyEvaluatedAt(service.getLifecyclePolicy(REPO, null, REGION), REGION));
}
@Test
void deletingThePolicyAnswersWithItAndWhenItWasLastApplied() {
repository(REPO, KEEP_TWO);
service.applyLifecyclePolicies(NOW);
EcrService.DeletedLifecyclePolicy deleted = service.deleteLifecyclePolicy(REPO, null, REGION);
assertEquals(new EcrService.DeletedLifecyclePolicy(ACCOUNT, REPO, KEEP_TWO, NOW), deleted);
service.putLifecyclePolicy(REPO, null, KEEP_TWO, REGION);
assertNull(service.lifecyclePolicyEvaluatedAt(service.getLifecyclePolicy(REPO, null, REGION), REGION));
}
/**
* The images are read without the repository's write lock, so a push is never held up by the reads. A write that
* ends between the reads and the deletes puts the expiry off to the next sweep, which reads the images again.
*/
@Test
void aWriteWhileThePolicyIsEvaluatedPutsTheExpiryOffToTheNextSweep() throws InterruptedException {
Repository repo = repository(REPO, OLDER_THAN_FIVE_DAYS);
String old = push(REGION, REPO, "v1", image("v1"), 30);
service.lockRepositoryWrites(internal(REGION, REPO));
Thread sweep = new Thread(() -> service.applyLifecyclePolicies(NOW));
try {
sweep.start();
awaitParkedOnALock(sweep);
assertEquals(1, registry.tagListings.get());
assertEquals(List.of(), registry.deleted);
} finally {
service.unlockRepositoryWrites(internal(REGION, REPO));
}
sweep.join(Duration.ofSeconds(10).toMillis());
assertEquals(List.of(), registry.deleted);
assertNull(service.lifecyclePolicyEvaluatedAt(repo, REGION));
service.applyLifecyclePolicies(NOW);
assertEquals(List.of(old), registry.deleted);
}
@Test
void aSweepThatExpiresNothingNeverTakesTheWriteLock() throws InterruptedException {
Repository repo = repository(REPO, OLDER_THAN_FIVE_DAYS);
push(REGION, REPO, "v1", image("v1"), 1);
service.lockRepositoryWrites(internal(REGION, REPO));
try {
Thread sweep = new Thread(() -> service.applyLifecyclePolicies(NOW));
sweep.start();
sweep.join(Duration.ofSeconds(10).toMillis());
assertEquals(NOW, service.lifecyclePolicyEvaluatedAt(repo, REGION));
} finally {
service.unlockRepositoryWrites(internal(REGION, REPO));
}
}
/**
* The policy is applied as it stood when the sweep came to the repository: a policy put meanwhile waits for the
* sweep, then reads as never applied.
*/
@Test
void aPolicyPutWhileTheSweepAppliesTheOldOneWaitsForIt() throws InterruptedException {
repository(REPO, OLDER_THAN_FIVE_DAYS);
String old = push(REGION, REPO, "v1", image("v1"), 30);
CountDownLatch gate = registry.holdTagListings();
Thread sweep = new Thread(() -> service.applyLifecyclePolicies(NOW));
Thread put = new Thread(() -> service.putLifecyclePolicy(REPO, null, KEEP_TWO, REGION));
try {
sweep.start();
registry.awaitHeldTagListing();
put.start();
awaitParkedOnALock(put);
} finally {
gate.countDown();
}
sweep.join(Duration.ofSeconds(10).toMillis());
put.join(Duration.ofSeconds(10).toMillis());
assertEquals(List.of(old), registry.deleted);
assertEquals(KEEP_TWO, service.getLifecyclePolicy(REPO, null, REGION).getLifecyclePolicyText());
assertNull(service.lifecyclePolicyEvaluatedAt(service.getLifecyclePolicy(REPO, null, REGION), REGION));
}
@Test
void aPolicyDeletedWhileTheSweepAppliesItWaitsForIt() throws InterruptedException {
repository(REPO, OLDER_THAN_FIVE_DAYS);
String old = push(REGION, REPO, "v1", image("v1"), 30);
CountDownLatch gate = registry.holdTagListings();
Thread sweep = new Thread(() -> service.applyLifecyclePolicies(NOW));
Thread delete = new Thread(() -> service.deleteLifecyclePolicy(REPO, null, REGION));
try {
sweep.start();
registry.awaitHeldTagListing();
delete.start();
awaitParkedOnALock(delete);
} finally {
gate.countDown();
}
sweep.join(Duration.ofSeconds(10).toMillis());
delete.join(Duration.ofSeconds(10).toMillis());
assertEquals(List.of(old), registry.deleted);
assertThrows(AwsException.class, () -> service.getLifecyclePolicy(REPO, null, REGION));
}
/** A repository deleted while the sweep applies its policy waits for it, and a new one by its name starts afresh. */
@Test
void aRepositoryDeletedWhileTheSweepAppliesItsPolicyWaitsForIt() throws InterruptedException {
repository(REPO, KEEP_TWO);
CountDownLatch gate = registry.holdTagListings();
Thread sweep = new Thread(() -> service.applyLifecyclePolicies(NOW));
Thread delete = new Thread(() -> service.deleteRepository(REPO, null, true, REGION));
try {
sweep.start();
registry.awaitHeldTagListing();
delete.start();
awaitParkedOnALock(delete);
} finally {
gate.countDown();
}
sweep.join(Duration.ofSeconds(10).toMillis());
delete.join(Duration.ofSeconds(10).toMillis());
assertNull(service.lifecyclePolicyEvaluatedAt(repository(REPO, KEEP_TWO), REGION));
}
@Test
void aResetForgetsWhenEachPolicyWasApplied() {
Repository repo = repository(REPO, KEEP_TWO);
service.applyLifecyclePolicies(NOW);
service.clear();
assertNull(service.lifecyclePolicyEvaluatedAt(repo, REGION));
}
/** A manifest's content never changes, so each is read once; the catalog is read once a sweep. */
@Test
void eachManifestIsReadOnceAndTheCatalogOnceASweep() {
repository(REPO, OLDER_THAN_FIVE_DAYS);
repository("other", OLDER_THAN_FIVE_DAYS);
push(REGION, REPO, "v1", image("v1"), 1);
push(REGION, REPO, null, image("u1"), 1);
push(REGION, "other", "v1", image("other"), 1);
service.applyLifecyclePolicies(NOW);
service.applyLifecyclePolicies(NOW);
assertEquals(3, registry.manifestReads.get());
assertEquals(2, registry.catalogReads.get());
}
private static void awaitParkedOnALock(Thread thread) {
long deadline = System.nanoTime() + Duration.ofSeconds(10).toNanos();
while (!parkedOnALock(thread) && System.nanoTime() < deadline) {
Thread.onSpinWait();
}
assertTrue(parkedOnALock(thread), thread.getName() + " should wait for a repository lock");
}
/** Both repository locks are a semaphore per repository, which a waiting thread parks on; a registry call is not. */
private static boolean parkedOnALock(Thread thread) {
Object blocker = LockSupport.getBlocker(thread);
return blocker != null && blocker.getClass().getEnclosingClass() == Semaphore.class;
}
@Test
void nothingIsSweptWhileEcrIsDisabled() {
repository(REPO, OLDER_THAN_FIVE_DAYS);
String old = push(REGION, REPO, "v1", image("v1"), 30);
int before = registry.requests.get();
when(config.services().ecr().enabled()).thenReturn(false);
service.applyLifecyclePolicies(NOW);
assertEquals(Set.of(old), registry.digests(internal(REGION, REPO)));
assertEquals(before, registry.requests.get());
}
@Test
void aRepositoryCreatedAgainUnderTheSameNameStartsNeverApplied() {
repository(REPO, KEEP_TWO);
service.applyLifecyclePolicies(NOW);
service.deleteRepository(REPO, null, true, REGION);
Repository again = repository(REPO, KEEP_TWO);
assertNull(service.lifecyclePolicyEvaluatedAt(again, REGION));
}
private Repository repository(String name, String policy) {
return repository(REGION, name, policy);
}
private Repository repository(String region, String name, String policy) {
service.createRepository(name, null, null, null, null, null, null, region);
registry.create(internal(region, name));
if (policy != null) {
service.putLifecyclePolicy(name, null, policy, region);
}
return service.describeRepositories(List.of(name), null, region).get(0);
}
private String push(String region, String repo, String tag, String manifest, int pushedDaysAgo) {
String digest = registry.put(internal(region, repo), tag, manifest);
service.recordImagePush(ACCOUNT, region, repo, digest, NOW.minus(Duration.ofDays(pushedDaysAgo)));
return digest;
}
private Set<String> recordedDigests(String region, String repo) {
String prefix = region + "::" + ACCOUNT + "::" + repo + "::";
Set<String> digests = new HashSet<>();
pushRecords.scan(key -> key.startsWith(prefix)).forEach(record -> digests.add(record.getDigest()));
return digests;
}
private static String internal(String region, String repo) {
return ACCOUNT + "/" + region + "/" + repo;
}
private static String policy(String... rules) {
return "{\"rules\":[" + String.join(",", rules) + "]}";
}
private static String image(String name) {
return "{\"schemaVersion\":2,\"mediaType\":\"application/vnd.oci.image.manifest.v1+json\",\"config\":{},"
+ "\"layers\":[],\"annotations\":{\"name\":\"" + name + "\"}}";
}
private static String referrer(String subject, String name) {
return "{\"schemaVersion\":2,\"mediaType\":\"application/vnd.oci.image.manifest.v1+json\","
+ "\"artifactType\":\"application/vnd.example.signature\",\"config\":{},\"layers\":[],"
+ "\"subject\":{\"digest\":\"" + subject + "\"},\"annotations\":{\"name\":\"" + name + "\"}}";
}
private static String digest(String manifest) {
return "sha256:" + Digests.sha256Hex(manifest.getBytes(StandardCharsets.UTF_8));
}
/** The distribution API calls the sweep makes: catalog, tag list, manifest HEAD, GET and DELETE. */
private static final class FakeRegistry implements AutoCloseable {
private final HttpServer server;
private final Map<String, Map<String, String>> manifests = new ConcurrentHashMap<>();
private final Map<String, Map<String, String>> tags = new ConcurrentHashMap<>();
private final Set<String> failing = ConcurrentHashMap.newKeySet();
private final List<String> deleted = new ArrayList<>();
private final AtomicInteger requests = new AtomicInteger();
private final AtomicInteger tagListings = new AtomicInteger();
private final AtomicInteger manifestReads = new AtomicInteger();
private final AtomicInteger catalogReads = new AtomicInteger();
private final Set<String> refusedDeletes = ConcurrentHashMap.newKeySet();
private final CountDownLatch tagListingHeld = new CountDownLatch(1);
private volatile CountDownLatch tagListingGate;
private FakeRegistry() throws IOException {
server = HttpServer.create(new InetSocketAddress("127.0.0.1", 0), 0);
server.createContext("/v2/", this::handle);
server.start();
}
private int port() {
return server.getAddress().getPort();
}
private void create(String repo) {
manifests.put(repo, new TreeMap<>());
tags.put(repo, new TreeMap<>());
}
private String put(String repo, String tag, String manifest) {
String digest = digest(manifest);
manifests.get(repo).put(digest, manifest);
if (tag != null) {
tags.get(repo).put(tag, digest);
}
return digest;
}
private Set<String> digests(String repo) {
return Set.copyOf(manifests.getOrDefault(repo, Map.of()).keySet());
}
/** Holds every tag listing until the returned gate opens. */
private CountDownLatch holdTagListings() {
tagListingGate = new CountDownLatch(1);
return tagListingGate;
}
private void awaitHeldTagListing() throws InterruptedException {
assertTrue(tagListingHeld.await(10, TimeUnit.SECONDS), "no tag listing reached the registry");
}
private synchronized void handle(HttpExchange exchange) throws IOException {
requests.incrementAndGet();
String path = exchange.getRequestURI().getPath().substring("/v2/".length());
String method = exchange.getRequestMethod();
if (path.equals("_catalog")) {
catalogReads.incrementAndGet();
send(exchange, 200, "{\"repositories\":[\"" + String.join("\",\"", manifests.keySet()) + "\"]}",
"application/json");
return;
}
if (path.endsWith("/tags/list")) {
tagListings.incrementAndGet();
CountDownLatch gate = tagListingGate;
if (gate != null) {
tagListingHeld.countDown();
try {
gate.await(10, TimeUnit.SECONDS);
} catch (InterruptedException e) {
Thread.currentThread().interrupt();
}
}
String repo = path.substring(0, path.length() - "/tags/list".length());
if (failing.contains(repo)) {
send(exchange, 500, "{}", "application/json");
return;
}
send(exchange, 200, "{\"name\":\"" + repo + "\",\"tags\":[" + (tags.get(repo).isEmpty() ? ""
: "\"" + String.join("\",\"", tags.get(repo).keySet()) + "\"") + "]}", "application/json");
return;
}
int split = path.lastIndexOf("/manifests/");
String repo = path.substring(0, split);
String reference = path.substring(split + "/manifests/".length());
String digest = reference.startsWith("sha256:") ? reference : tags.get(repo).get(reference);
String manifest = digest == null ? null : manifests.get(repo).get(digest);
if (manifest == null) {
exchange.sendResponseHeaders(404, -1);
exchange.close();
return;
}
switch (method) {
case "DELETE" -> {
if (refusedDeletes.contains(digest)) {
exchange.sendResponseHeaders(500, -1);
exchange.close();
return;
}
manifests.get(repo).remove(digest);
tags.get(repo).values().removeIf(digest::equals);
deleted.add(digest);
exchange.sendResponseHeaders(202, -1);
exchange.close();
}
case "HEAD" -> {
exchange.getResponseHeaders().add("Docker-Content-Digest", digest);
exchange.sendResponseHeaders(200, -1);
exchange.close();
}
default -> {
manifestReads.incrementAndGet();
exchange.getResponseHeaders().add("Docker-Content-Digest", digest);
send(exchange, 200, manifest, "application/vnd.oci.image.manifest.v1+json");
}
}
}
private static void send(HttpExchange exchange, int status, String body, String contentType)
throws IOException {
byte[] bytes = body.getBytes(StandardCharsets.UTF_8);
exchange.getResponseHeaders().add("Content-Type", contentType);
exchange.sendResponseHeaders(status, bytes.length);
try (OutputStream out = exchange.getResponseBody()) {
out.write(bytes);
}
}
@Override
public void close() {
server.stop(0);
}
}
}
@@ -1605,6 +1605,8 @@ class EcrServiceTest {
AwsException ex = assertThrows(AwsException.class,
() -> service.getLifecyclePolicy(REPO, null, REGION));
assertEquals("LifecyclePolicyNotFoundException", ex.getErrorCode());
assertEquals("Lifecycle policy does not exist for the repository with name '" + REPO
+ "' in the registry with id '" + ACCOUNT + "'", ex.getMessage());
}
// ------------------------------------------------------------
@@ -18,6 +18,7 @@ import com.github.dockerjava.api.model.ContainerNetworkSettings;
import com.github.dockerjava.api.model.ContainerPort;
import com.github.dockerjava.api.model.Frame;
import com.github.dockerjava.api.model.Info;
import com.github.dockerjava.api.model.StreamType;
import io.floci.aws.config.EmulatorConfig;
import io.floci.aws.core.common.RegionResolver;
import io.floci.aws.core.common.docker.ContainerBuilder;
@@ -45,6 +46,7 @@ import org.mockito.Mockito;
import java.io.Closeable;
import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.time.Duration;
import java.time.Instant;
import java.util.ArrayList;
@@ -52,6 +54,7 @@ import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.OptionalInt;
import java.util.Set;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.TimeUnit;
@@ -1112,6 +1115,45 @@ class EcrRegistryManagerTest {
"/var/lib/registry/docker/registry/v2/repositories/000000000000/us-east-1/team/app/_uploads");
}
/** The registry's API lists tags only; untagged manifests are read from the revision links of its storage. */
@Test
void heldManifestDigestsAreReadFromTheRevisionLinks() {
when(lifecycleManager.createAndStart(any())).thenReturn(
new ContainerLifecycleManager.ContainerInfo("container-id", Map.of()));
String revisions = "/var/lib/registry/docker/registry/v2/repositories/000000000000/us-east-1/team/app"
+ "/_manifests/revisions/sha256";
String first = "a".repeat(64);
String second = "0123456789abcdef".repeat(4);
DockerClient dockerClient = Mockito.mock(DockerClient.class);
ExecCreateCmd execCreate = Mockito.mock(ExecCreateCmd.class, Mockito.RETURNS_SELF);
ExecCreateCmdResponse exec = Mockito.mock(ExecCreateCmdResponse.class);
when(exec.getId()).thenReturn("exec-id");
when(execCreate.exec()).thenReturn(exec);
when(dockerClient.execCreateCmd("container-id")).thenReturn(execCreate);
ExecStartCmd execStart = Mockito.mock(ExecStartCmd.class);
when(execStart.exec(any())).thenAnswer(invocation -> {
@SuppressWarnings("unchecked")
ResultCallback<Frame> callback = invocation.getArgument(0);
callback.onNext(new Frame(StreamType.STDOUT, (revisions + "/" + first + "/link\n" + revisions + "/"
+ second + "/link\n" + revisions + "/not-a-digest/link\n").getBytes(StandardCharsets.UTF_8)));
callback.onComplete();
return callback;
});
when(dockerClient.execStartCmd("exec-id")).thenReturn(execStart);
InspectExecCmd inspect = Mockito.mock(InspectExecCmd.class);
InspectExecResponse result = Mockito.mock(InspectExecResponse.class);
when(result.getExitCodeLong()).thenReturn(0L);
when(inspect.exec()).thenReturn(result);
when(dockerClient.inspectExecCmd("exec-id")).thenReturn(inspect);
when(lifecycleManager.getDockerClient()).thenReturn(dockerClient);
manager.ensureStarted();
assertEquals(Set.of("sha256:" + first, "sha256:" + second),
manager.heldManifestDigests("000000000000/us-east-1/team/app"));
verify(execCreate).withCmd("timeout", "-k", "1", "10", "sh", "-c", "[ -d '" + revisions + "' ] || exit 0; "
+ "find '" + revisions + "' -mindepth 2 -maxdepth 2 -name link");
}
@Test
@SuppressWarnings("unchecked")
void runGarbageCollectTimeoutWithExecStillRunningKeepsAdmissionClosed() throws Exception {
@@ -163,7 +163,11 @@ public final class EcrTestImages {
return buf.array();
}
private static void uploadBlob(String host, String repo, String digest, byte[] content) {
/**
* Uploads {@code content} to {@code repo} as one monolithic blob upload, sending {@code host}, one of Floci's
* repository hosts, as the {@code Host} header.
*/
public static void uploadBlob(String host, String repo, String digest, byte[] content) {
String location = given().header("Host", host)
.when().post("/v2/" + repo + "/blobs/uploads/")
.then().statusCode(202)