Light, fluffy, and always free - The AWS Local Emulator alternative
fix(opensearch,mwaa): remove containers on explicit delete when shutdown retention is on (#4225)
Niels Kootstra2c3332c7cf71
diff --git a/src/main/java/io/github/hectorvent/floci/services/mwaa/MwaaEnvironmentManager.java b/src/main/java/io/github/hectorvent/floci/services/mwaa/MwaaEnvironmentManager.javaindex ce6b8a5cce..51dec997d9 100644--- a/src/main/java/io/github/hectorvent/floci/services/mwaa/MwaaEnvironmentManager.java+++ b/src/main/java/io/github/hectorvent/floci/services/mwaa/MwaaEnvironmentManager.java@@ -319,10 +319,6 @@ public class MwaaEnvironmentManager {/** Stops and removes both containers and all three named volumes for the given environment. */public void stopEnvironment(Environment environment) {- if (config.services().mwaa().keepRunningOnShutdown()) {- LOG.infov("Leaving MWAA containers for environment {0} running", environment.getName());- return;- }String name = environment.getName();if (environment.getAirflowContainerId() != null) {lifecycleManager.stopAndRemove(environment.getAirflowContainerId(), null);diff --git a/src/main/java/io/github/hectorvent/floci/services/opensearch/OpenSearchDomainManager.java b/src/main/java/io/github/hectorvent/floci/services/opensearch/OpenSearchDomainManager.javaindex 65fb45a905..ef2d125184 100644--- a/src/main/java/io/github/hectorvent/floci/services/opensearch/OpenSearchDomainManager.java+++ b/src/main/java/io/github/hectorvent/floci/services/opensearch/OpenSearchDomainManager.java@@ -207,15 +207,9 @@ public class OpenSearchDomainManager {if (domain.getContainerId() == null) {return;}- if (config.services().opensearch().keepRunningOnShutdown()) {- LOG.infov("Leaving OpenSearch container for domain {0} running", domain.getDomainName());- return;- }lifecycleManager.stopAndRemove(domain.getContainerId(), null);// The container no longer holds the binding, so the reservation must go with// it. Repeated create/delete would otherwise exhaust the configured range.- // The keep-running early return above deliberately keeps the reservation:- // the surviving container still owns the binding.if (domain.getHostPort() != null) {portAllocator.release(domain.getHostPort());domain.setHostPort(null);diff --git a/src/main/java/io/github/hectorvent/floci/services/opensearch/OpenSearchService.java b/src/main/java/io/github/hectorvent/floci/services/opensearch/OpenSearchService.javaindex 32b74998f1..e39fcae6d1 100644--- a/src/main/java/io/github/hectorvent/floci/services/opensearch/OpenSearchService.java+++ b/src/main/java/io/github/hectorvent/floci/services/opensearch/OpenSearchService.java@@ -76,7 +76,7 @@ public class OpenSearchService implements ResourceProvider {@PreDestroypublic void shutdown() {poller.shutdownNow();- if (!config.services().opensearch().mock()) {+ if (!config.services().opensearch().mock() && !config.services().opensearch().keepRunningOnShutdown()) {for (Domain domain : allDomains()) {domainManager.stopDomain(domain);}diff --git a/src/test/java/io/github/hectorvent/floci/services/mwaa/MwaaEnvironmentManagerTest.java b/src/test/java/io/github/hectorvent/floci/services/mwaa/MwaaEnvironmentManagerTest.javaindex 8f5cbed096..70a8168187 100644--- a/src/test/java/io/github/hectorvent/floci/services/mwaa/MwaaEnvironmentManagerTest.java+++ b/src/test/java/io/github/hectorvent/floci/services/mwaa/MwaaEnvironmentManagerTest.java@@ -43,6 +43,7 @@ class MwaaEnvironmentManagerTest {private ContainerLifecycleManager lifecycleManager;private ContainerDetector containerDetector;private MwaaEnvironmentManager manager;+ private EmulatorConfig.MwaaServiceConfig mwaaConfig;@BeforeEachvoid setUp() {@@ -51,7 +52,7 @@ class MwaaEnvironmentManagerTest {when(dockerConfig.logMaxSize()).thenReturn("10m");when(dockerConfig.logMaxFile()).thenReturn("3");- EmulatorConfig.MwaaServiceConfig mwaaConfig = Mockito.mock(EmulatorConfig.MwaaServiceConfig.class);+ mwaaConfig = Mockito.mock(EmulatorConfig.MwaaServiceConfig.class);when(mwaaConfig.dockerNetwork()).thenReturn(Optional.empty());when(mwaaConfig.defaultPostgresImage()).thenReturn("postgres:16-alpine");@@ -504,4 +505,23 @@ class MwaaEnvironmentManagerTest {assertFalse(manager.hasAnyContainerExited(environmentWithContainers("db-container-id", "airflow-container-id")));}}++ /**+ * Explicit DeleteEnvironment tears down through {@code stopEnvironment}. The shutdown+ * retention option is decided by {@link MwaaService#shutdown()} and must not turn a Delete+ * into metadata removal that leaves the containers running.+ */+ @Test+ void stopEnvironmentRemovesContainersEvenWhenShutdownRetentionIsEnabled() {+ when(mwaaConfig.keepRunningOnShutdown()).thenReturn(true);+ Environment environment = new Environment();+ environment.setName("deleted-env");+ environment.setAirflowContainerId("airflow-container-id");+ environment.setDbContainerId("db-container-id");++ manager.stopEnvironment(environment);++ Mockito.verify(lifecycleManager).stopAndRemove("airflow-container-id", null);+ Mockito.verify(lifecycleManager).stopAndRemove("db-container-id", null);+ }}diff --git a/src/test/java/io/github/hectorvent/floci/services/mwaa/MwaaServiceTest.java b/src/test/java/io/github/hectorvent/floci/services/mwaa/MwaaServiceTest.javaindex 8b31e6a93a..a4caf05cd1 100644--- a/src/test/java/io/github/hectorvent/floci/services/mwaa/MwaaServiceTest.java+++ b/src/test/java/io/github/hectorvent/floci/services/mwaa/MwaaServiceTest.java@@ -551,6 +551,7 @@ class MwaaServiceTest {private MwaaEnvironmentManager environmentManager;private MwaaProxyManager proxyManager;private MwaaService realModeService;+ private boolean keepRunningOnShutdown;@BeforeEachvoid setUpRealMode() {@@ -571,6 +572,7 @@ class MwaaServiceTest {case "proxyBasePort" -> 8700;case "proxyMaxPort" -> 8799;case "dagSyncIntervalSeconds" -> 30;+ case "keepRunningOnShutdown" -> keepRunningOnShutdown;default -> defaultValue(method);});EmulatorConfig.ServicesConfig servicesConfig = proxy(EmulatorConfig.ServicesConfig.class,@@ -611,6 +613,16 @@ class MwaaServiceTest {verify(portAllocator).release(8701);}+ @Test+ void shutdownLeavesEnvironmentContainersRunningWhenRetentionIsEnabled() {+ keepRunningOnShutdown = true;+ realModeService.createEnvironment("kept-env", createRequest("arn:aws:s3:::my-bucket", "dags"));++ realModeService.shutdown();++ verify(environmentManager, Mockito.never()).stopEnvironment(any());+ }+@Testvoid proxyStartupFailureRollsBackTheAllocatedPortAndAnyStartedContainers() {// Containers start fine; only the proxy bind fails (e.g. the port got taken out fromdiff --git a/src/test/java/io/github/hectorvent/floci/services/opensearch/OpenSearchDomainManagerTest.java b/src/test/java/io/github/hectorvent/floci/services/opensearch/OpenSearchDomainManagerTest.javaindex e7f9a6065e..56581be374 100644--- a/src/test/java/io/github/hectorvent/floci/services/opensearch/OpenSearchDomainManagerTest.java+++ b/src/test/java/io/github/hectorvent/floci/services/opensearch/OpenSearchDomainManagerTest.java@@ -177,11 +177,12 @@ class OpenSearchDomainManagerTest {}/**- * Keep-running shutdown leaves the container holding its Docker binding, so- * the reservation has to survive with it.+ * Explicit DeleteDomain tears down through {@code stopDomain}. The shutdown retention+ * option is decided by {@link OpenSearchService#shutdown()}, so it must not leave a deleted+ * domain's container running or its port reserved.*/@Test- void stopDomainKeepsTheReservationWhenTheContainerKeepsRunning() {+ void stopDomainRemovesTheContainerEvenWhenShutdownRetentionIsEnabled() {EmulatorConfig config = mock(EmulatorConfig.class);EmulatorConfig.ServicesConfig services = mock(EmulatorConfig.ServicesConfig.class);EmulatorConfig.OpenSearchServiceConfig opensearch = mock(EmulatorConfig.OpenSearchServiceConfig.class);@@ -197,14 +198,13 @@ class OpenSearchDomainManagerTest {mock(RegionResolver.class));Domain domain = new Domain();- domain.setDomainName("keep-running");+ domain.setDomainName("deleted-domain");domain.setContainerId("container-id");domain.setHostPort(9401);manager.stopDomain(domain);- verify(lifecycleManager, never()).stopAndRemove(anyString(), any());- verify(portAllocator, never()).release(9401);- assertEquals(Integer.valueOf(9401), domain.getHostPort());+ verify(lifecycleManager).stopAndRemove("container-id", null);+ verify(portAllocator).release(9401);}}diff --git a/src/test/java/io/github/hectorvent/floci/services/opensearch/OpenSearchServiceTest.java b/src/test/java/io/github/hectorvent/floci/services/opensearch/OpenSearchServiceTest.javaindex 3e36b866f1..4c5bdd6238 100644--- a/src/test/java/io/github/hectorvent/floci/services/opensearch/OpenSearchServiceTest.java+++ b/src/test/java/io/github/hectorvent/floci/services/opensearch/OpenSearchServiceTest.java@@ -98,4 +98,15 @@ class OpenSearchServiceTest {assertFalse(service.describeDomain("ready-recovery").isProcessing());verify(domainManager, atLeast(2)).isReady(any());}++ @Test+ void shutdownLeavesDomainContainersRunningWhenRetentionIsEnabled() {+ when(domainManager.tryStartDomain(any())).thenReturn(true);+ when(osConfig.keepRunningOnShutdown()).thenReturn(true);+ service.createDomain("kept-domain", "OpenSearch_2.11", null, null, null, "us-east-1");++ service.shutdown();++ verify(domainManager, Mockito.never()).stopDomain(any());+ }}