fix(ecr): clean up registry storage (#2982)

Remove every manifest for a forced repository deletion and reclaim the shared registry volume only when storage pruning is configured.
This commit is contained in:
Simone Primarosa authored and GitHub committed 2026-09-03 08:14:56 -04:00
1 parent 2b531dad3a
commit 732e009207
5 files changed
+257 -13

No files matched your search

@@ -3,6 +3,7 @@ package io.github.hectorvent.floci.services.ecr;
import io.github.hectorvent.floci.config.EmulatorConfig;
import io.github.hectorvent.floci.core.common.AwsException;
import io.github.hectorvent.floci.core.common.RegionResolver;
import io.github.hectorvent.floci.core.storage.AccountAwareStorageBackend;
import io.github.hectorvent.floci.core.storage.InMemoryStorage;
import io.github.hectorvent.floci.services.ecr.model.ImageDetail;
import io.github.hectorvent.floci.services.ecr.model.ImageIdentifier;
@@ -28,6 +29,8 @@ import java.util.Map;
import static org.junit.jupiter.api.Assertions.*;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.ArgumentMatchers.anyString;
import static org.mockito.Mockito.never;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;
/**
@@ -161,6 +164,57 @@ class EcrServiceTest {
assertEquals("RepositoryNotFoundException", ex.getErrorCode());
}
@Test
void deleteRepository_forceRemovesRepositoryStorageAndPrunesFinalRepositoryStorage() {
service.createRepository(REPO, null, null, null, null, null, null, REGION);
service.deleteRepository(REPO, null, true, REGION);
verify(registryManager).deleteRepositoryStorage(ACCOUNT, REGION, REPO);
verify(registryManager).pruneStorage();
}
@Test
void deleteRepository_keepsSharedRegistryStorageForRemainingRepositories() {
service.createRepository(REPO, null, null, null, null, null, null, REGION);
service.createRepository("other", null, null, null, null, null, null, REGION);
service.deleteRepository(REPO, null, true, REGION);
verify(registryManager, never()).pruneStorage();
}
@Test
void deleteRepository_forceRetainsMetadataWhenRegistryCleanupFails() {
Mockito.doThrow(new IllegalStateException("registry unavailable"))
.when(registryManager).deleteRepositoryStorage(ACCOUNT, REGION, REPO);
service.createRepository(REPO, null, null, null, null, null, null, REGION);
AwsException ex = assertThrows(AwsException.class,
() -> service.deleteRepository(REPO, null, true, REGION));
assertEquals("ServerException", ex.getErrorCode());
assertEquals(REPO, service.describeRepositories(List.of(REPO), null, REGION).getFirst().getRepositoryName());
verify(registryManager, never()).pruneStorage();
}
@Test
void deleteRepository_keepsSharedStorageForRepositoriesOwnedByAnotherAccount() {
AccountAwareStorageBackend<Repository> repositories = AccountAwareStorageBackend.inMemory(ACCOUNT);
EcrService accountAwareService = new EcrService(
repositories,
new InMemoryStorage<>(),
registryManager,
Mockito.mock(EmulatorConfig.class),
new RegionResolver(REGION, ACCOUNT));
accountAwareService.createRepository(REPO, null, null, null, null, null, null, REGION);
repositories.putForAccount("111111111111", REGION + "::111111111111::other", new Repository());
accountAwareService.deleteRepository(REPO, null, true, REGION);
verify(registryManager, never()).pruneStorage();
}
// ------------------------------------------------------------
// GetAuthorizationToken
// ------------------------------------------------------------
@@ -9,7 +9,15 @@ import io.github.hectorvent.floci.core.common.docker.ContainerLogStreamer;
import io.github.hectorvent.floci.core.common.docker.CurrentContainerNetworkResolver;
import io.github.hectorvent.floci.core.common.docker.ContainerSpec;
import io.github.hectorvent.floci.core.common.docker.PortAllocator;
import com.github.dockerjava.api.DockerClient;
import com.github.dockerjava.api.async.ResultCallback;
import com.github.dockerjava.api.command.ExecCreateCmd;
import com.github.dockerjava.api.command.ExecCreateCmdResponse;
import com.github.dockerjava.api.command.ExecStartCmd;
import com.github.dockerjava.api.command.InspectExecCmd;
import com.github.dockerjava.api.command.InspectExecResponse;
import com.github.dockerjava.api.model.Container;
import com.github.dockerjava.api.model.Frame;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.mockito.Mockito;
@@ -45,6 +53,7 @@ class EcrRegistryManagerTest {
private CurrentContainerNetworkResolver currentContainerNetworkResolver;
private EmulatorConfig.DockerConfig docker;
private EmulatorConfig.EcrServiceConfig ecr;
private EmulatorConfig.StorageConfig storage;
private ContainerBuilder.Builder builder;
private EcrRegistryManager manager;
@@ -68,7 +77,7 @@ class EcrRegistryManagerTest {
EmulatorConfig config = Mockito.mock(EmulatorConfig.class);
ecr = Mockito.mock(EmulatorConfig.EcrServiceConfig.class);
docker = Mockito.mock(EmulatorConfig.DockerConfig.class);
EmulatorConfig.StorageConfig storage = Mockito.mock(EmulatorConfig.StorageConfig.class);
storage = Mockito.mock(EmulatorConfig.StorageConfig.class);
when(config.services()).thenReturn(Mockito.mock(EmulatorConfig.ServicesConfig.class));
when(config.services().ecr()).thenReturn(ecr);
when(config.docker()).thenReturn(docker);
@@ -76,6 +85,7 @@ class EcrRegistryManagerTest {
when(docker.resourceNamespace()).thenReturn(Optional.empty());
// Empty host-persistent-path selects named-volume mode (no host bind-mount logic).
when(storage.hostPersistentPath()).thenReturn("");
when(storage.mode()).thenReturn("persistent");
when(ecr.registryContainerName()).thenReturn(REGISTRY_NAME);
when(ecr.registryImage()).thenReturn("registry:2");
when(ecr.registryBasePort()).thenReturn(BASE_PORT);
@@ -200,4 +210,75 @@ class EcrRegistryManagerTest {
void rewriteImageUri_nullImage_returnsNull() {
assertNull(manager.rewriteImageUri(null));
}
@Test
void pruneStorageRemovesTheRegistryAndVolumeWhenConfigured() {
when(storage.pruneVolumesOnDelete()).thenReturn(true);
when(lifecycleManager.createAndStart(any())).thenReturn(
new ContainerLifecycleManager.ContainerInfo("container-id", Map.of()));
manager.ensureStarted();
manager.pruneStorage();
verify(lifecycleManager).stopAndRemove(Mockito.eq("container-id"), Mockito.isNull());
verify(lifecycleManager).removeVolume("floci-ecr-registry-data");
}
@Test
void pruneStorageRetainsTheRegistryWhenVolumePruningIsDisabled() {
when(lifecycleManager.createAndStart(any())).thenReturn(
new ContainerLifecycleManager.ContainerInfo("container-id", Map.of()));
manager.ensureStarted();
manager.pruneStorage();
verify(lifecycleManager, Mockito.never()).stopAndRemove(anyString(), Mockito.isNull());
verify(lifecycleManager, Mockito.never()).removeVolume(anyString());
}
@Test
void shutdownRemovesTheRegistryVolumeWhenItIsNotRetained() {
when(storage.pruneVolumesOnDelete()).thenReturn(true);
when(lifecycleManager.createAndStart(any())).thenReturn(
new ContainerLifecycleManager.ContainerInfo("container-id", Map.of()));
manager.ensureStarted();
manager.shutdown();
verify(lifecycleManager).stopAndRemove(Mockito.eq("container-id"), Mockito.isNull());
verify(lifecycleManager).removeVolume("floci-ecr-registry-data");
}
@Test
void deleteRepositoryStorageBoundsCleanupAndPreservesNestedRepositories() {
when(lifecycleManager.createAndStart(any())).thenReturn(
new ContainerLifecycleManager.ContainerInfo("container-id", Map.of()));
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.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.deleteRepositoryStorage("000000000000", "us-east-1", "team/app");
verify(execCreate).withCmd("timeout", "-k", "1", "10", "rm", "-rf",
"/var/lib/registry/docker/registry/v2/repositories/000000000000/us-east-1/team/app/_layers",
"/var/lib/registry/docker/registry/v2/repositories/000000000000/us-east-1/team/app/_manifests",
"/var/lib/registry/docker/registry/v2/repositories/000000000000/us-east-1/team/app/_uploads");
}
}