fix(lifecycle): stop managed containers on state reset, not just shutdown (#3718)

* fix(lifecycle): stop managed containers on state reset, not just shutdown

/state/reset and /state/nuke clear StorageBackend and every Resettable
bean, but ContainerTeardown was only invoked from EmulatorLifecycle.onStop.
A reset left Batch, CodeBuild, and now SageMaker training/endpoint
containers running with no record of them left in the store to reconcile
against. EmulatorInfoController now runs the same teardown loop before
wiping storage, tolerating individual failures the same way shutdown does.

* fix(lifecycle): use explicit InOrder type instead of var in test

AGENTS.md disallows var for local declarations; the concrete Mockito
type at a call site is the thing worth seeing in a review.
This commit is contained in:
Priamo Germosen authored and GitHub committed 2026-09-16 18:33:40 -04:00
1 parent d4a96913df
commit ef12a83418
3 files changed
+112 -1

No files matched your search

@@ -0,0 +1,87 @@
package io.github.hectorvent.floci.lifecycle;
import io.github.hectorvent.floci.config.EmulatorConfig;
import io.github.hectorvent.floci.config.FlociCertificateAuthority;
import io.github.hectorvent.floci.core.common.ContainerTeardown;
import io.github.hectorvent.floci.core.common.Resettable;
import io.github.hectorvent.floci.core.common.ServiceRegistry;
import io.github.hectorvent.floci.core.storage.StorageFactory;
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.junit.jupiter.MockitoExtension;
import java.util.List;
import static org.mockito.Mockito.doThrow;
import static org.mockito.Mockito.inOrder;
import static org.mockito.Mockito.verify;
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 private FlociCertificateAuthority certificateAuthority;
@Mock private EmulatorConfig config;
@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);
}
@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();
}
}