Light, fluffy, and always free - The AWS Local Emulator alternative
fix(codeartifact): retry Verdaccio configuration uploads (#5982)
Aniket Shukla241806a4f32a
diff --git a/docs/configuration/docker.md b/docs/configuration/docker.mdindex 72e1d2195d..e2cad1a143 100644--- a/docs/configuration/docker.md+++ b/docs/configuration/docker.md@@ -324,10 +324,10 @@ A request is only replayed when doing so cannot change semantics: requests carryArchive uploads that use the shared tar-copy helper retry above the transport layer, rebuildingthe upload stream for each attempt with the same six-attempt backoff. This includes user-data-scripts, the container CA bundle, and EKS audit policies, IRSA signing keys, the containerd-certificate-directory link and the Pod Identity manifest. Only the upload is retried, not-script execution. Daemon rejections still stop immediately, and each caller keeps its existing-failure handling after retries are exhausted.+scripts, the container CA bundle, Verdaccio's configuration, and EKS audit policies, IRSA+signing keys, the containerd certificate-directory link and the Pod Identity manifest. Only the+upload is retried, not script execution. Daemon rejections still stop immediately, and each+caller keeps its existing failure handling after retries are exhausted.## Full Referencediff --git a/src/main/java/io/github/hectorvent/floci/services/codeartifact/VerdaccioSidecarManager.java b/src/main/java/io/github/hectorvent/floci/services/codeartifact/VerdaccioSidecarManager.javaindex 5307248a09..4471ce3604 100644--- a/src/main/java/io/github/hectorvent/floci/services/codeartifact/VerdaccioSidecarManager.java+++ b/src/main/java/io/github/hectorvent/floci/services/codeartifact/VerdaccioSidecarManager.java@@ -9,15 +9,11 @@ import io.github.hectorvent.floci.core.common.docker.ContainerLifecycleManager.Eimport io.github.hectorvent.floci.core.common.docker.ContainerSpec;import io.github.hectorvent.floci.core.common.docker.ContainerStorageHelper;import io.github.hectorvent.floci.core.common.docker.PerKeyContainerPool;+import io.github.hectorvent.floci.core.common.docker.RetryingTarCopier;import jakarta.enterprise.context.ApplicationScoped;import jakarta.inject.Inject;-import org.apache.commons.compress.archivers.tar.TarArchiveEntry;-import org.apache.commons.compress.archivers.tar.TarArchiveOutputStream;import org.jboss.logging.Logger;-import java.io.ByteArrayInputStream;-import java.io.ByteArrayOutputStream;-import java.io.IOException;import java.nio.charset.StandardCharsets;/**@@ -106,7 +102,7 @@ public class VerdaccioSidecarManager implements ContainerTeardown {.build();String containerId = lifecycleManager.create(spec);try {- copyConfig(containerId);+ copyConfig(containerId, RetryingTarCopier.DEFAULT_BACKOFF_MS);ContainerInfo info = lifecycleManager.startCreated(containerId, spec);EndpointInfo endpoint = info.getEndpoint(VERDACCIO_PORT);String url = "http://" + endpoint;@@ -124,25 +120,10 @@ public class VerdaccioSidecarManager implements ContainerTeardown {* when Floci itself runs in Docker, its own persistent path is not a host path the daemon can* mount into a sibling container.*/- private void copyConfig(String containerId) {- byte[] content = CONFIG_YAML.getBytes(StandardCharsets.UTF_8);- try {- ByteArrayOutputStream archive = new ByteArrayOutputStream(content.length + 512);- try (TarArchiveOutputStream tar = new TarArchiveOutputStream(archive)) {- TarArchiveEntry entry = new TarArchiveEntry(CONFIG_FILE_NAME);- entry.setSize(content.length);- entry.setMode(0644);- tar.putArchiveEntry(entry);- tar.write(content);- tar.closeArchiveEntry();- }- lifecycleManager.getDockerClient().copyArchiveToContainerCmd(containerId)- .withRemotePath(CONFIG_REMOTE_DIR)- .withTarInputStream(new ByteArrayInputStream(archive.toByteArray()))- .exec();- } catch (IOException e) {- throw new IllegalStateException("Could not write the Verdaccio config into container " + containerId, e);- }+ void copyConfig(String containerId, long backoffMillis) {+ RetryingTarCopier.copyBytes(lifecycleManager.getDockerClient(), containerId, CONFIG_REMOTE_DIR,+ CONFIG_FILE_NAME, CONFIG_YAML.getBytes(StandardCharsets.UTF_8), 0644,+ RetryingTarCopier.DEFAULT_MAX_ATTEMPTS, backoffMillis);}@Overridediff --git a/src/test/java/io/github/hectorvent/floci/services/codeartifact/VerdaccioConfigRetryTest.java b/src/test/java/io/github/hectorvent/floci/services/codeartifact/VerdaccioConfigRetryTest.javanew file mode 100644index 0000000000..ce35a8e490--- /dev/null+++ b/src/test/java/io/github/hectorvent/floci/services/codeartifact/VerdaccioConfigRetryTest.java@@ -0,0 +1,146 @@+package io.github.hectorvent.floci.services.codeartifact;++import com.github.dockerjava.api.DockerClient;+import com.github.dockerjava.api.command.CopyArchiveToContainerCmd;+import com.github.dockerjava.api.exception.NotFoundException;+import io.github.hectorvent.floci.config.EmulatorConfig;+import io.github.hectorvent.floci.core.common.dns.EmbeddedDnsServer;+import io.github.hectorvent.floci.core.common.docker.ContainerBuilder;+import io.github.hectorvent.floci.core.common.docker.ContainerLifecycleManager;+import io.github.hectorvent.floci.core.common.docker.ContainerSpec;+import io.github.hectorvent.floci.core.common.docker.DockerHostResolver;+import org.apache.commons.compress.archivers.tar.TarArchiveEntry;+import org.apache.commons.compress.archivers.tar.TarArchiveInputStream;+import org.junit.jupiter.api.BeforeEach;+import org.junit.jupiter.api.Test;+import org.junit.jupiter.params.ParameterizedTest;+import org.junit.jupiter.params.provider.ValueSource;++import java.io.ByteArrayInputStream;+import java.io.InputStream;+import java.net.SocketException;+import java.nio.charset.StandardCharsets;+import java.util.ArrayList;+import java.util.List;+import java.util.Optional;+import java.util.concurrent.atomic.AtomicReference;++import static org.junit.jupiter.api.Assertions.assertEquals;+import static org.junit.jupiter.api.Assertions.assertNotSame;+import static org.junit.jupiter.api.Assertions.assertNull;+import static org.junit.jupiter.api.Assertions.assertSame;+import static org.junit.jupiter.api.Assertions.assertThrows;+import static org.junit.jupiter.api.Assertions.assertTrue;+import static org.mockito.ArgumentMatchers.any;+import static org.mockito.Mockito.RETURNS_DEEP_STUBS;+import static org.mockito.Mockito.RETURNS_SELF;+import static org.mockito.Mockito.doAnswer;+import static org.mockito.Mockito.mock;+import static org.mockito.Mockito.never;+import static org.mockito.Mockito.verify;+import static org.mockito.Mockito.when;++class VerdaccioConfigRetryTest {++ private final DockerClient docker = mock(DockerClient.class);+ private final ContainerLifecycleManager lifecycle = mock(ContainerLifecycleManager.class);+ private final List<InputStream> streams = new ArrayList<>();+ private final List<CopyArchiveToContainerCmd> commands = new ArrayList<>();+ private final List<byte[]> completed = new ArrayList<>();+ private VerdaccioSidecarManager manager;+ private int failures;+ private int consumedBytes;+ private RuntimeException failure;++ @BeforeEach+ void setUp() {+ manager = new VerdaccioSidecarManager(mock(ContainerBuilder.class), lifecycle, mock(EmulatorConfig.class));+ when(lifecycle.getDockerClient()).thenReturn(docker);+ when(docker.copyArchiveToContainerCmd("container")).thenAnswer(ignored -> {+ CopyArchiveToContainerCmd command = mock(CopyArchiveToContainerCmd.class, RETURNS_SELF);+ commands.add(command);+ AtomicReference<InputStream> input = new AtomicReference<>();+ when(command.withTarInputStream(any(InputStream.class))).thenAnswer(invocation -> {+ InputStream stream = invocation.getArgument(0);+ streams.add(stream);+ input.set(stream);+ return command;+ });+ doAnswer(invocation -> {+ if (commands.size() <= failures) {+ input.get().readNBytes(consumedBytes);+ throw failure;+ }+ completed.add(input.get().readAllBytes());+ return null;+ }).when(command).exec();+ return command;+ });+ }++ @ParameterizedTest+ @ValueSource(ints = {0, 73, 8192})+ void transientFailureRebuildsTheCompleteConfigArchive(int consumed) throws Exception {+ failures = 1;+ consumedBytes = consumed;+ failure = new RuntimeException(new SocketException("Connection reset"));++ manager.copyConfig("container", 0);++ assertEquals(2, commands.size());+ assertNotSame(streams.get(0), streams.get(1));+ for (CopyArchiveToContainerCmd command : commands) {+ verify(command).withRemotePath("/verdaccio/conf");+ }+ try (TarArchiveInputStream tar = new TarArchiveInputStream(new ByteArrayInputStream(completed.getFirst()))) {+ TarArchiveEntry entry = tar.getNextEntry();+ assertEquals("config.yaml", entry.getName());+ assertEquals(0644, entry.getMode());+ String yaml = new String(tar.readAllBytes(), StandardCharsets.UTF_8);+ assertTrue(yaml.contains("uplinks: {}"));+ assertTrue(yaml.contains("publish: $all"));+ assertTrue(yaml.contains("listen: 0.0.0.0:4873"));+ assertNull(tar.getNextEntry());+ }+ }++ @Test+ void exhaustedTransportFailurePropagatesAfterTheRetryBudget() {+ failures = 10;+ failure = new RuntimeException(new SocketException("Connection reset"));+ RuntimeException thrown = assertThrows(RuntimeException.class, () -> manager.copyConfig("container", 0));+ assertEquals(6, commands.size());+ assertSame(failure, thrown.getCause());+ }++ @Test+ void daemonRejectionDoesNotRetry() {+ failures = 10;+ failure = new NotFoundException("container missing");+ RuntimeException thrown = assertThrows(RuntimeException.class, () -> manager.copyConfig("container", 0));+ assertEquals(1, commands.size());+ assertTrue(thrown.getMessage().contains("container missing"));+ }++ @Test+ void failedConfigurationUploadRemovesTheUnstartedContainer() {+ EmulatorConfig config = mock(EmulatorConfig.class, RETURNS_DEEP_STUBS);+ when(config.services().codeartifact().npmImage()).thenReturn("verdaccio/verdaccio:test");+ when(config.services().dockerNetwork()).thenReturn(Optional.empty());+ when(config.docker().resourceNamespace()).thenReturn(Optional.empty());+ when(config.docker().logMaxSize()).thenReturn("10m");+ when(config.docker().logMaxFile()).thenReturn("3");+ ContainerBuilder builder = new ContainerBuilder(config,+ mock(DockerHostResolver.class), mock(EmbeddedDnsServer.class));+ manager = new VerdaccioSidecarManager(builder, lifecycle, config);+ when(lifecycle.create(any(ContainerSpec.class))).thenReturn("container");+ failures = 1;+ failure = new NotFoundException("container missing");++ assertThrows(RuntimeException.class, () -> manager.ensureReady("npm-repo", "http://localhost:4566"));++ verify(lifecycle).stopAndRemove("container", null);+ verify(lifecycle, never()).startCreated(any(), any());+ assertEquals(1, commands.size());+ }+}