Files
floci/src/test/java/io/floci/aws/lifecycle/EmulatorInfoControllerTest.java
T
Jackson Price 171f42ce6e fix(rds): acknowledge instance creation before database startup (#9599)
CreateDBInstance waited for the Docker backend before answering, so a
client with the SDK's default timeouts could retry a create that had
already succeeded and get DBInstanceAlreadyExists. It now stores the
instance and answers creating, as AWS does. DescribeDBInstances sees the
record while a background worker starts the container and proxy outside
the instance lock, and the worker publishes the backend only if the same
instance is still creating.

ModifyDBInstance and RebootDBInstance refuse a creating instance with
InvalidDBInstanceState and AWS's messages. A delete accepted during
startup keeps the record and the caller's backup choice until the worker
has torn down what it started, and after a restart Floci reopens the
PostgreSQL volume to save a retained backup before removing it. A state
reset waits up to 15 seconds for pending starts and otherwise refuses
with 503 from checkReset, before any container stops.

Signed-off-by: Jackson Price <jxnprice@amazon.com>
2026-11-27 22:48:48 -04:00

225 lines
10 KiB
Java

package io.floci.aws.lifecycle;
import io.floci.aws.config.EmulatorConfig;
import io.floci.aws.config.FlociCertificateAuthority;
import io.floci.aws.core.common.ContainerTeardown;
import io.floci.aws.core.common.Resettable;
import io.floci.aws.core.common.ServiceRegistry;
import io.floci.aws.core.storage.StorageFactory;
import io.floci.aws.services.dynamodb.backend.DynamoDbBackendSelector;
import jakarta.enterprise.inject.Instance;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.mockito.InOrder;
import org.mockito.Mock;
import org.mockito.MockMakers;
import org.mockito.junit.jupiter.MockitoExtension;
import java.util.List;
import java.util.Map;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertSame;
import static org.junit.jupiter.api.Assertions.assertThrows;
import static org.mockito.Mockito.RETURNS_DEEP_STUBS;
import static org.mockito.Mockito.doThrow;
import static org.mockito.Mockito.inOrder;
import static org.mockito.Mockito.lenient;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.never;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.verifyNoInteractions;
import static org.mockito.Mockito.verifyNoMoreInteractions;
import static org.mockito.Mockito.when;
@ExtendWith(MockitoExtension.class)
class EmulatorInfoControllerTest {
@Mock private ServiceRegistry serviceRegistry;
@Mock private InitLifecycleState initLifecycleState;
@Mock private StorageFactory storageFactory;
@Mock private Instance<Resettable> resettables;
@Mock private Instance<ContainerTeardown> containerTeardowns;
@Mock(mockMaker = MockMakers.INLINE) private FlociCertificateAuthority certificateAuthority;
@Mock private EmulatorConfig config;
@Mock private DynamoDbBackendSelector dynamoDbBackendSelector;
@Mock private ContainerTeardown sageMakerTeardown;
@Mock private ContainerTeardown batchTeardown;
@Mock private Resettable resettable;
private EmulatorInfoController controller;
@BeforeEach
void setUp() {
controller = new EmulatorInfoController(serviceRegistry, initLifecycleState, storageFactory,
resettables, containerTeardowns, certificateAuthority, config, dynamoDbBackendSelector);
}
@Test
@DisplayName("Should report the selected DynamoDB backend in info")
void info_reportsTheSelectedDynamoDbBackend() {
when(dynamoDbBackendSelector.selected()).thenReturn("local");
Map<?, ?> info = (Map<?, ?>) manifestController().info().getEntity();
assertEquals("local", info.get("dynamodb_backend"));
}
@Test
void info_isTheConsoleContractManifest() {
EmulatorConfig manifestConfig = mock(EmulatorConfig.class, RETURNS_DEEP_STUBS);
when(manifestConfig.effectiveBaseUrl()).thenReturn("http://floci:4566");
when(manifestConfig.defaultRegion()).thenReturn("us-east-1");
when(manifestConfig.defaultAccountId()).thenReturn("000000000000");
when(manifestConfig.tls().enabled()).thenReturn(true);
when(manifestConfig.services().ui().enabled()).thenReturn(true);
when(manifestConfig.services().ui().port()).thenReturn(4500);
when(serviceRegistry.getServices()).thenReturn(Map.of("s3", "running", "ec2", "available"));
Map<?, ?> info = (Map<?, ?>) new EmulatorInfoController(serviceRegistry, initLifecycleState, storageFactory,
resettables, containerTeardowns, certificateAuthority, manifestConfig, dynamoDbBackendSelector)
.info().getEntity();
assertEquals(1, info.get("manifest_version"));
assertEquals(2, info.get("console_contract"));
assertEquals("aws", info.get("cloud"));
assertEquals("floci", info.get("emulator"));
assertEquals("http://floci:4566", info.get("endpoint"));
assertEquals("/_floci/health", info.get("health_url"));
assertEquals(Map.of("enabled", true), info.get("tls"));
assertEquals(Map.of("ec2", "disabled", "s3", "enabled"), info.get("services"));
assertEquals(Map.of("region", "us-east-1", "account_id", "000000000000"), info.get("defaults"));
assertEquals(Map.of("enabled", true, "port", 4500), info.get("ui"));
assertEquals("community", info.get("edition"), "the original keys stay");
}
private EmulatorInfoController manifestController() {
return new EmulatorInfoController(serviceRegistry, initLifecycleState, storageFactory,
resettables, containerTeardowns, certificateAuthority, mock(EmulatorConfig.class, RETURNS_DEEP_STUBS),
dynamoDbBackendSelector);
}
@Test
@DisplayName("Should stop every managed container before wiping storage on reset")
void reset_stopsManagedContainersBeforeClearingStorage() {
when(containerTeardowns.iterator()).thenReturn(List.of(sageMakerTeardown, batchTeardown).iterator());
when(resettables.iterator()).thenReturn(List.of(resettable).iterator());
controller.reset();
InOrder order = inOrder(sageMakerTeardown, batchTeardown, storageFactory, resettable);
order.verify(sageMakerTeardown).stopManagedContainers();
order.verify(batchTeardown).stopManagedContainers();
order.verify(storageFactory).clearAll();
order.verify(resettable).clear();
}
@Test
@DisplayName("Should continue tearing down other containers and still wipe storage when one teardown fails")
void reset_continuesAfterATeardownFailure() {
doThrow(new IllegalStateException("docker daemon unreachable"))
.when(sageMakerTeardown).stopManagedContainers();
when(containerTeardowns.iterator()).thenReturn(List.of(sageMakerTeardown, batchTeardown).iterator());
when(resettables.iterator()).thenReturn(List.<Resettable>of().iterator());
controller.reset();
verify(batchTeardown).stopManagedContainers();
verify(storageFactory).clearAll();
}
@Test
@DisplayName("nuke should stop managed containers just like reset")
void nuke_alsoStopsManagedContainers() {
when(containerTeardowns.iterator()).thenReturn(List.of(sageMakerTeardown).iterator());
when(resettables.iterator()).thenReturn(List.<Resettable>of().iterator());
controller.nuke();
verify(sageMakerTeardown).stopManagedContainers();
verify(storageFactory).clearAll();
}
@Test
@DisplayName("Should call beforeReset on every service before the wipe and afterReset after every clear")
void reset_bracketsTheWipeWithBeforeAndAfterHooks() {
Resettable second = mock(Resettable.class);
when(containerTeardowns.iterator()).thenReturn(List.<ContainerTeardown>of().iterator());
when(resettables.iterator()).thenReturn(List.of(resettable, second).iterator());
controller.reset();
InOrder order = inOrder(storageFactory, resettable, second);
order.verify(resettable).beforeReset();
order.verify(second).beforeReset();
order.verify(storageFactory).clearAll();
order.verify(resettable).clear();
order.verify(second).clear();
order.verify(second).afterReset();
order.verify(resettable).afterReset();
}
@Test
@DisplayName("Should still call afterReset when the storage wipe fails, then rethrow")
void reset_runsAfterResetWhenTheWipeFails() {
doThrow(new IllegalStateException("wipe failed")).when(storageFactory).clearAll();
when(containerTeardowns.iterator()).thenReturn(List.<ContainerTeardown>of().iterator());
when(resettables.iterator()).thenReturn(List.of(resettable).iterator());
assertThrows(IllegalStateException.class, controller::reset);
verify(resettable).beforeReset();
verify(resettable).afterReset();
verify(resettable, never()).clear();
}
@Test
@DisplayName("Should call afterReset on every service, even those after a beforeReset that failed, then rethrow")
void reset_runsAfterResetOnEveryServiceWhenABeforeResetFails() {
Resettable failing = mock(Resettable.class);
Resettable later = mock(Resettable.class);
doThrow(new IllegalStateException("drain timed out")).when(failing).beforeReset();
when(containerTeardowns.iterator()).thenReturn(List.of(sageMakerTeardown).iterator());
when(resettables.iterator()).thenReturn(List.of(resettable, failing, later).iterator());
IllegalStateException thrown = assertThrows(IllegalStateException.class, controller::reset);
assertEquals("drain timed out", thrown.getMessage());
// The teardown already shut down the pools that afterReset() restores, so the service
// whose beforeReset() never ran needs afterReset() just as much as the ones that ran.
InOrder order = inOrder(sageMakerTeardown, resettable, failing, later);
order.verify(sageMakerTeardown).stopManagedContainers();
order.verify(resettable).beforeReset();
order.verify(failing).beforeReset();
order.verify(later).afterReset();
order.verify(failing).afterReset();
order.verify(resettable).afterReset();
verify(later, never()).beforeReset();
verify(storageFactory, never()).clearAll();
verify(resettable, never()).clear();
verify(later, never()).clear();
}
@Test
void resetRefusedByPreflightChangesNothing() {
Resettable refusing = mock(Resettable.class);
IllegalStateException refusal = new IllegalStateException("backend refuses reset");
doThrow(refusal).when(refusing).checkReset();
lenient().when(containerTeardowns.iterator()).thenReturn(List.of(sageMakerTeardown).iterator());
when(resettables.iterator()).thenReturn(List.of(resettable, refusing).iterator());
assertSame(refusal, assertThrows(IllegalStateException.class, controller::reset));
verify(resettable).checkReset();
verify(refusing).checkReset();
InOrder rollback = inOrder(refusing, resettable);
rollback.verify(refusing).cancelResetCheck();
rollback.verify(resettable).cancelResetCheck();
verifyNoMoreInteractions(resettable, refusing);
verify(sageMakerTeardown, never()).stopManagedContainers();
verifyNoInteractions(storageFactory);
}
}