fix(rds): fail fast when the Docker daemon refuses the container create (#8581)

* fix(rds): fail fast when the Docker daemon refuses the container create
* fix(rds): keep a Docker timeout or throttling on create retryable
This commit is contained in:
Vinícius Ribeiro authored and GitHub committed 2026-11-16 10:41:59 -04:00
1 parent 293514ca86
commit a76c67f0c2
7 files changed
+207 -3

No files matched your search

@@ -0,0 +1,119 @@
package io.floci.aws.core.common.docker;
import com.github.dockerjava.api.DockerClient;
import com.github.dockerjava.api.command.CreateContainerCmd;
import com.github.dockerjava.api.exception.BadRequestException;
import com.github.dockerjava.api.exception.DockerException;
import com.github.dockerjava.api.exception.InternalServerErrorException;
import io.floci.aws.config.EmulatorConfig;
import io.floci.aws.services.lambda.launcher.ImageCacheService;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.ValueSource;
import org.mockito.Mock;
import org.mockito.junit.jupiter.MockitoExtension;
import java.util.List;
import java.util.Map;
import java.util.Optional;
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.Mockito.RETURNS_SELF;
import static org.mockito.Mockito.lenient;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;
/**
* A daemon that refuses a create with a client error, such as an authorization plugin denying
* {@code HostConfig.Mounts}, refuses every retry of it too. The create reports that as a
* {@link ContainerRejectedException} so services can answer with a non-retryable error.
*/
@ExtendWith(MockitoExtension.class)
class ContainerLifecycleManagerCreateRejectionTest {
@Mock
DockerClient dockerClient;
@Mock
ImageCacheService imageCacheService;
@Mock
ContainerDetector containerDetector;
@Mock
EmulatorConfig config;
@Mock
EmulatorConfig.DockerConfig dockerConfig;
@Mock
EmulatorConfig.TlsConfig tlsConfig;
@BeforeEach
void setUp() {
lenient().when(config.docker()).thenReturn(dockerConfig);
lenient().when(config.tls()).thenReturn(tlsConfig);
lenient().when(imageCacheService.ensureImageExists("busybox:stable")).thenReturn("busybox:stable");
lenient().when(dockerConfig.resourceNamespace()).thenReturn(Optional.empty());
}
@Test
void createReportsAnAuthorizationPluginDenialAsARejection() {
DockerException denied = new DockerException(
"{\"message\":\"authorization denied by plugin: --mounts is not allowed\"}", 403);
failCreateWith(denied);
ContainerRejectedException rejected = assertThrows(ContainerRejectedException.class,
() -> manager().create(spec()));
assertSame(denied, rejected.getCause());
assertTrue(rejected.getMessage().contains("--mounts is not allowed"), rejected.getMessage());
}
@Test
void createReportsABadRequestAsARejection() {
failCreateWith(new BadRequestException("invalid mount config"));
assertThrows(ContainerRejectedException.class, () -> manager().create(spec()));
}
@ParameterizedTest
@ValueSource(ints = {408, 429})
void createKeepsATimeoutOrThrottlingRetryable(int status) {
DockerException temporary = new DockerException("try again later", status);
failCreateWith(temporary);
assertSame(temporary, assertThrows(DockerException.class, () -> manager().create(spec())));
}
@Test
void createKeepsADaemonErrorRetryable() {
InternalServerErrorException daemonError = new InternalServerErrorException("daemon failure");
failCreateWith(daemonError);
assertSame(daemonError, assertThrows(InternalServerErrorException.class,
() -> manager().create(spec())));
}
private void failCreateWith(DockerException failure) {
CreateContainerCmd createCmd = mock(CreateContainerCmd.class, RETURNS_SELF);
when(dockerClient.createContainerCmd("busybox:stable")).thenReturn(createCmd);
when(createCmd.exec()).thenThrow(failure);
}
private static ContainerSpec spec() {
return new ContainerSpec(
"busybox:stable", "emulator-fixed-name", List.of(), null, null, null, Map.of(),
List.of(), null, List.of(), List.of(), List.of(), Map.of(), null, false, null,
List.of(), null, null, List.of());
}
private ContainerLifecycleManager manager() {
return new ContainerLifecycleManager(
dockerClient, imageCacheService, containerDetector, config);
}
}
@@ -1,7 +1,9 @@
package io.floci.aws.services.rds;
import com.github.dockerjava.api.exception.DockerException;
import io.floci.aws.config.EmulatorConfig;
import io.floci.aws.core.common.AwsException;
import io.floci.aws.core.common.docker.ContainerRejectedException;
import io.floci.aws.services.docdb.DocDbQueryHandler;
import io.floci.aws.services.ec2.Ec2Service;
import io.floci.aws.services.neptune.NeptuneQueryHandler;
@@ -933,6 +935,26 @@ class RdsQueryHandlerTest {
assertFalse(body.contains("<MasterUserSecret>"));
}
@Test
void createDbClusterAnswersADockerRejectionWithANonRetryableCapacityError() {
when(service.createDbCluster(any(), any(), any(), any(), any(), any(), anyBoolean(), any(),
any(), any(), anyBoolean(), any(), any(), any(), any(), anyBoolean(), any(),
any(), anyBoolean()))
.thenThrow(new ContainerRejectedException(
new DockerException("authorization denied by plugin: --mounts is not allowed", 403)));
MultivaluedMap<String, String> p = params();
p.add("DBClusterIdentifier", "mycluster");
p.add("Engine", "aurora-postgresql");
p.add("MasterUsername", "admin");
Response response = handler.handle("CreateDBCluster", p);
assertEquals(400, response.getStatus());
String body = (String) response.getEntity();
assertTrue(body.contains("<Code>InsufficientDBInstanceCapacity</Code>"));
assertTrue(body.contains("--mounts is not allowed"));
}
@Test
void createDbClusterDeletesTheClusterWhenItsRolesAreRefused() {
DbCluster cluster = makeCluster("mycluster");
@@ -1,6 +1,7 @@
package io.floci.aws.services.redshift;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.github.dockerjava.api.exception.DockerException;
import io.floci.aws.config.EmulatorConfig;
import io.floci.aws.core.common.AwsArnUtils;
import io.floci.aws.core.common.AwsException;
@@ -8,6 +9,7 @@ import io.floci.aws.core.common.AwsRegions;
import io.floci.aws.core.common.PaginatedResult;
import io.floci.aws.core.common.RegionResolver;
import io.floci.aws.core.common.ServiceCallAuthorizer;
import io.floci.aws.core.common.docker.ContainerRejectedException;
import io.floci.aws.core.common.docker.DockerHostResolver;
import io.floci.aws.core.storage.AccountAwareStorageBackend;
import io.floci.aws.core.storage.StorageFactory;
@@ -955,6 +957,19 @@ class RedshiftServiceTest {
verify(clusterBackend, never()).put(any(), any());
}
@Test
void createClusterAnswersADockerRejectionWithANonRetryableCapacityError() {
when(cm.start("111111111111", "c1", "admin", "pw")).thenThrow(new ContainerRejectedException(
new DockerException("authorization denied by plugin: --mounts is not allowed", 403)));
AwsException ex = assertThrows(AwsException.class, () -> service.createCluster("c1", "dc2.large",
"admin", "pw", null, List.of(), List.of(), null));
assertEquals("InsufficientClusterCapacity", ex.getErrorCode());
assertEquals(400, ex.getHttpStatus());
assertTrue(ex.getMessage().contains("--mounts is not allowed"));
}
@Test
void createClusterStoresDefaultRoleAndConfiguresProxy() {
String role = "arn:aws:iam::111111111111:role/copy";