mirror of
https://github.com/floci-io/floci.git
synced 2026-12-01 01:12:48 -05:00
fix(ec2): restore and stop the instances of every account (#5103)
Startup restore runs from @PostConstruct and shutdown from a lifecycle hook, both outside any request, so an account-aware keys() sees only the default account. Every other account's EC2 state was skipped: - after a restart its VPCs and subnets were not re-declared, so paths that do not declare lazily, such as an EKS node joining its VPC, found no network; reconcileOrphans also judged its VPC networks against the default account only; - its instances' private addresses were not re-reserved and their IMDS registrations were not restored, so IMDS stopped answering; - shutdown, and a state reset, which wipes every account, did not stop its running instances. Restore and shutdown now walk every account's VPCs, subnets and instances. Restore runs each instance as its own account, so the security groups and prefix lists it resolves come from that account, in the instance's own region, and both paths write to the owning partition. VPCs are declared default account first, then in account and key order, because overlapping CIDRs go to whichever VPC is declared first. The port-forward persister, which also runs on container executor threads with no request, writes to the account that holds the instance instead of the current one, and writes nothing when no account holds it any more (terminated meanwhile). A pre-multi-account key is migrated once, or dropped when a scoped copy already exists. Co-authored-by: Robin Baeckman <27041454+RobinBaeckman@users.noreply.github.com>
This commit is contained in:
1 parent
9cd9002e73
commit
8eddae4ed6
3 files changed
+488
-33
No files matched your search
+190
@@ -0,0 +1,190 @@
|
||||
package io.github.hectorvent.floci.services.ec2;
|
||||
|
||||
import io.github.hectorvent.floci.core.common.RequestScopes;
|
||||
import io.github.hectorvent.floci.services.ec2.model.Instance;
|
||||
import io.github.hectorvent.floci.services.ec2.model.InstanceState;
|
||||
import io.github.hectorvent.floci.services.ec2.model.Reservation;
|
||||
import io.github.hectorvent.floci.services.ec2.portforward.Ec2PortForwardManager;
|
||||
import io.quarkus.test.InjectMock;
|
||||
import io.quarkus.test.junit.QuarkusTest;
|
||||
import io.quarkus.test.junit.QuarkusTestProfile;
|
||||
import io.quarkus.test.junit.TestProfile;
|
||||
import jakarta.inject.Inject;
|
||||
import org.junit.jupiter.api.BeforeEach;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.concurrent.atomic.AtomicReference;
|
||||
import java.util.function.Consumer;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
import static org.junit.jupiter.api.Assertions.assertFalse;
|
||||
import static org.junit.jupiter.api.Assertions.assertNotNull;
|
||||
import static org.mockito.ArgumentMatchers.any;
|
||||
import static org.mockito.ArgumentMatchers.argThat;
|
||||
import static org.mockito.Mockito.doAnswer;
|
||||
import static org.mockito.Mockito.never;
|
||||
import static org.mockito.Mockito.verify;
|
||||
import static org.mockito.Mockito.when;
|
||||
|
||||
/**
|
||||
* Startup restore runs from {@code @PostConstruct}, on a thread with no request, where account-aware
|
||||
* storage resolves to the default account. These tests run it the same way, on a bare thread, for
|
||||
* an instance owned by another account.
|
||||
*/
|
||||
@QuarkusTest
|
||||
@TestProfile(Ec2RestoreEveryAccountIntegrationTest.EnforcedContainerBackedEc2.class)
|
||||
class Ec2RestoreEveryAccountIntegrationTest {
|
||||
|
||||
/** Container-backed EC2 with security groups enforced, so restore resolves the instance's groups. */
|
||||
public static class EnforcedContainerBackedEc2 implements QuarkusTestProfile {
|
||||
@Override
|
||||
public Map<String, String> getConfigOverrides() {
|
||||
return Map.of("floci.services.ec2.mock", "false",
|
||||
"floci.network.security-group-enforcement.enabled", "true");
|
||||
}
|
||||
}
|
||||
|
||||
private static final String REGION = "us-east-1";
|
||||
private static final String OTHER_ACCOUNT = "111122223333";
|
||||
|
||||
@Inject
|
||||
Ec2Service service;
|
||||
|
||||
@InjectMock
|
||||
Ec2ContainerManager containerManager;
|
||||
|
||||
@InjectMock
|
||||
Ec2PortForwardManager portForwardManager;
|
||||
|
||||
@InjectMock
|
||||
AmiImageResolver amiImageResolver;
|
||||
|
||||
private final AtomicReference<Consumer<Instance>> persister = new AtomicReference<>();
|
||||
|
||||
@BeforeEach
|
||||
void stubCollaborators() {
|
||||
when(amiImageResolver.resolveImage(any())).thenReturn(ResolvedAmiImage.minimal("floci/base:latest"));
|
||||
when(containerManager.restoreMetadataRegistration(any())).thenReturn(true);
|
||||
doAnswer(invocation -> {
|
||||
persister.set(invocation.getArgument(0));
|
||||
return null;
|
||||
}).when(portForwardManager).setPersister(any());
|
||||
}
|
||||
|
||||
@Test
|
||||
void restoreKeepsAnotherAccountsInstanceRunningWhenItsSecurityGroupsAreEnforced() throws Throwable {
|
||||
Instance instance = runningInstanceOf(OTHER_ACCOUNT);
|
||||
|
||||
restoreOutsideAnyRequest();
|
||||
|
||||
verify(containerManager).restoreMetadataRegistration(argThat(restored -> isSame(restored, instance)));
|
||||
verify(containerManager, never()).stopForShutdown(argThat(stopped -> isSame(stopped, instance)));
|
||||
assertEquals("running", RequestScopes.callAs(OTHER_ACCOUNT,
|
||||
() -> describe(instance.getInstanceId()).getFirst().getState().getName()));
|
||||
}
|
||||
|
||||
@Test
|
||||
void portForwardWritesDuringRestoreStayInTheInstancesOwnAccount() throws Throwable {
|
||||
Instance instance = runningInstanceOf(OTHER_ACCOUNT);
|
||||
doAnswer(invocation -> {
|
||||
Instance restored = invocation.getArgument(0);
|
||||
if (isSame(restored, instance)) {
|
||||
restored.getPublishedPorts().put(8080, 30080);
|
||||
persister.get().accept(restored);
|
||||
}
|
||||
return null;
|
||||
}).when(portForwardManager).restore(any());
|
||||
|
||||
restoreOutsideAnyRequest();
|
||||
|
||||
verify(portForwardManager).restore(argThat(restored -> isSame(restored, instance)));
|
||||
assertEquals(List.of(instance.getInstanceId()), RequestScopes.callAs(OTHER_ACCOUNT,
|
||||
() -> describe(instance.getInstanceId()).stream().map(Instance::getInstanceId).toList()));
|
||||
assertFalse(RequestScopes.callAs("000000000000", () -> describe(null).stream()
|
||||
.anyMatch(listed -> listed.getInstanceId().equals(instance.getInstanceId()))));
|
||||
}
|
||||
|
||||
@Test
|
||||
void shutdownWritesAnotherAccountsInstanceOnlyToItsOwnAccount() throws Throwable {
|
||||
Instance instance = runningInstanceOf(OTHER_ACCOUNT);
|
||||
restoreOutsideAnyRequest();
|
||||
// Stopping a container unpublishes its forwards, and the port-forward manager persists that.
|
||||
doAnswer(invocation -> {
|
||||
Instance stopped = invocation.getArgument(0);
|
||||
if (isSame(stopped, instance)) {
|
||||
stopped.getPublishedPorts().clear();
|
||||
persister.get().accept(stopped);
|
||||
}
|
||||
return null;
|
||||
}).when(containerManager).stopForShutdown(any());
|
||||
|
||||
runOutsideAnyRequest(service::stopManagedContainers);
|
||||
|
||||
verify(containerManager).stopForShutdown(argThat(stopped -> isSame(stopped, instance)));
|
||||
assertEquals("stopped", RequestScopes.callAs(OTHER_ACCOUNT,
|
||||
() -> describe(instance.getInstanceId()).getFirst().getState().getName()));
|
||||
assertFalse(RequestScopes.callAs("000000000000", () -> describe(null).stream()
|
||||
.anyMatch(listed -> listed.getInstanceId().equals(instance.getInstanceId()))));
|
||||
}
|
||||
|
||||
@Test
|
||||
void portForwardWritesFromAThreadWithNoRequestStayInTheInstancesOwnAccount() throws Throwable {
|
||||
// Ec2ContainerManager stops containers on its own executor, and unpublishing a forward there
|
||||
// persists the instance with no request, and so no account, in scope.
|
||||
Instance instance = runningInstanceOf(OTHER_ACCOUNT);
|
||||
restoreOutsideAnyRequest();
|
||||
|
||||
assertNotNull(persister.get(), "the service registered no port-forward persister");
|
||||
runOutsideAnyRequest(() -> persister.get().accept(instance));
|
||||
|
||||
assertEquals(List.of(instance.getInstanceId()), RequestScopes.callAs(OTHER_ACCOUNT,
|
||||
() -> describe(instance.getInstanceId()).stream().map(Instance::getInstanceId).toList()));
|
||||
assertFalse(RequestScopes.callAs("000000000000", () -> describe(null).stream()
|
||||
.anyMatch(listed -> listed.getInstanceId().equals(instance.getInstanceId()))));
|
||||
}
|
||||
|
||||
private Instance runningInstanceOf(String accountId) {
|
||||
return RequestScopes.callAs(accountId, () -> {
|
||||
Instance launched = service.runInstances(REGION, "ami-amazonlinux2023", "t3.micro", 1, 1,
|
||||
null, List.of(), null, null, List.of(), null, null).getInstances().getFirst();
|
||||
launched.setDockerContainerId("container-" + launched.getInstanceId());
|
||||
launched.setState(InstanceState.running());
|
||||
return launched;
|
||||
});
|
||||
}
|
||||
|
||||
private void restoreOutsideAnyRequest() throws Throwable {
|
||||
runOutsideAnyRequest(service::restoreMetadataRegistrations);
|
||||
}
|
||||
|
||||
/**
|
||||
* Runs work on a fresh thread, so no request (and no account) is in scope, and rethrows what
|
||||
* it threw: an exception on another thread would otherwise end only that thread.
|
||||
*/
|
||||
private static void runOutsideAnyRequest(Runnable work) throws Throwable {
|
||||
AtomicReference<Throwable> failure = new AtomicReference<>();
|
||||
Thread thread = new Thread(() -> {
|
||||
try {
|
||||
work.run();
|
||||
} catch (Throwable t) {
|
||||
failure.set(t);
|
||||
}
|
||||
});
|
||||
thread.start();
|
||||
thread.join();
|
||||
if (failure.get() != null) {
|
||||
throw failure.get();
|
||||
}
|
||||
}
|
||||
|
||||
private List<Instance> describe(String instanceId) {
|
||||
return service.describeInstances(REGION, instanceId == null ? List.of() : List.of(instanceId), Map.of())
|
||||
.stream().map(Reservation::getInstances).flatMap(List::stream).toList();
|
||||
}
|
||||
|
||||
private static boolean isSame(Instance candidate, Instance instance) {
|
||||
return candidate != null && instance.getInstanceId().equals(candidate.getInstanceId());
|
||||
}
|
||||
}
|
||||
@@ -64,6 +64,8 @@ import org.bouncycastle.openssl.jcajce.JcaPEMKeyConverter;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.junit.jupiter.params.ParameterizedTest;
|
||||
import org.junit.jupiter.params.provider.CsvSource;
|
||||
import org.mockito.ArgumentCaptor;
|
||||
import org.mockito.InOrder;
|
||||
|
||||
import java.io.StringReader;
|
||||
import java.lang.reflect.Field;
|
||||
@@ -84,6 +86,8 @@ import java.util.concurrent.Future;
|
||||
import java.util.concurrent.TimeUnit;
|
||||
import java.util.concurrent.atomic.AtomicReference;
|
||||
import java.util.function.BiConsumer;
|
||||
import java.util.function.BiPredicate;
|
||||
import java.util.function.Consumer;
|
||||
import java.util.function.Predicate;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
@@ -99,6 +103,7 @@ import static org.mockito.ArgumentMatchers.any;
|
||||
import static org.mockito.ArgumentMatchers.anyString;
|
||||
import static org.mockito.ArgumentMatchers.argThat;
|
||||
import static org.mockito.Mockito.doAnswer;
|
||||
import static org.mockito.Mockito.inOrder;
|
||||
import static org.mockito.Mockito.mock;
|
||||
import static org.mockito.Mockito.never;
|
||||
import static org.mockito.Mockito.spy;
|
||||
@@ -3572,6 +3577,180 @@ class Ec2ServiceTest {
|
||||
verify(vpcNetworks, never()).reservePrivateIp("us-east-1", "subnet-a", "10.0.1.11");
|
||||
}
|
||||
|
||||
@Test
|
||||
void restoreRebuildsTheVpcNetworkPlanOfEveryAccount() {
|
||||
// Startup runs with no request account, so an account-scoped keys() sees only the default
|
||||
// account's VPCs: every other account's VPC network was never re-declared after a restart.
|
||||
AccountAwareStorageBackend<Vpc> vpcStore = AccountAwareStorageBackend.inMemory("000000000000");
|
||||
AccountAwareStorageBackend<Subnet> subnetStore = AccountAwareStorageBackend.inMemory("000000000000");
|
||||
AccountAwareStorageBackend<Instance> instanceStore = AccountAwareStorageBackend.inMemory("000000000000");
|
||||
Vpc vpc = new Vpc();
|
||||
vpc.setRegion("us-east-1");
|
||||
vpc.setVpcId("vpc-a");
|
||||
vpc.setCidrBlock("10.20.0.0/16");
|
||||
vpcStore.putForAccount("111122223333", "us-east-1::vpc-a", vpc);
|
||||
Subnet subnet = new Subnet();
|
||||
subnet.setRegion("us-east-1");
|
||||
subnet.setVpcId("vpc-a");
|
||||
subnet.setSubnetId("subnet-a");
|
||||
subnet.setCidrBlock("10.20.1.0/24");
|
||||
subnetStore.putForAccount("111122223333", "us-east-1::subnet-a", subnet);
|
||||
instanceStore.putForAccount("111122223333", "us-east-1::i-other",
|
||||
persistedInstance("i-other", "10.20.1.10", InstanceState.running()));
|
||||
|
||||
VpcNetworkManager vpcNetworks = mock(VpcNetworkManager.class);
|
||||
when(vpcNetworks.enabled()).thenReturn(true);
|
||||
Ec2Service service = new Ec2Service(mockConfig(false), mock(Ec2ContainerManager.class),
|
||||
mock(Ec2PortForwardManager.class), mock(AmiImageResolver.class), mock(Ec2ImageCatalog.class),
|
||||
new Ec2InstanceTypeCatalog(),
|
||||
new InMemoryStorageFactory(Map.of("ec2-vpcs.json", vpcStore, "ec2-subnets.json", subnetStore,
|
||||
"ec2-instances.json", instanceStore)), vpcNetworks);
|
||||
|
||||
service.restoreMetadataRegistrations();
|
||||
|
||||
@SuppressWarnings("unchecked")
|
||||
ArgumentCaptor<BiPredicate<String, String>> stillDeclared = ArgumentCaptor.forClass(BiPredicate.class);
|
||||
verify(vpcNetworks).reconcileOrphans(stillDeclared.capture());
|
||||
assertTrue(stillDeclared.getValue().test("us-east-1", "vpc-a"));
|
||||
assertFalse(stillDeclared.getValue().test("us-east-1", "vpc-gone"));
|
||||
verify(vpcNetworks).declareVpc("us-east-1", "vpc-a", "10.20.0.0/16");
|
||||
verify(vpcNetworks).declareSubnet("us-east-1", "vpc-a", "subnet-a", "10.20.1.0/24");
|
||||
verify(vpcNetworks).reservePrivateIp("us-east-1", "subnet-a", "10.20.1.10");
|
||||
}
|
||||
|
||||
@Test
|
||||
void restoreDeclaresTheDefaultAccountsVpcFirstWhenCidrsOverlap() {
|
||||
// The default account sorts after one of the others by id, so only the default-first rule puts it first.
|
||||
AccountAwareStorageBackend<Vpc> vpcStore = AccountAwareStorageBackend.inMemory("555555555555");
|
||||
for (String[] owned : new String[][] {{"999999999999", "vpc-z"}, {"111122223333", "vpc-y"}, {"555555555555", "vpc-x"}}) {
|
||||
Vpc vpc = new Vpc();
|
||||
vpc.setRegion("us-east-1");
|
||||
vpc.setVpcId(owned[1]);
|
||||
vpc.setCidrBlock("10.0.0.0/16");
|
||||
vpcStore.putForAccount(owned[0], "us-east-1::" + owned[1], vpc);
|
||||
}
|
||||
VpcNetworkManager vpcNetworks = mock(VpcNetworkManager.class);
|
||||
when(vpcNetworks.enabled()).thenReturn(true);
|
||||
EmulatorConfig config = mockConfig(false);
|
||||
when(config.defaultAccountId()).thenReturn("555555555555");
|
||||
Ec2Service service = new Ec2Service(config, mock(Ec2ContainerManager.class),
|
||||
mock(Ec2PortForwardManager.class), mock(AmiImageResolver.class), mock(Ec2ImageCatalog.class),
|
||||
new Ec2InstanceTypeCatalog(),
|
||||
new InMemoryStorageFactory(Map.of("ec2-vpcs.json", vpcStore)), vpcNetworks);
|
||||
|
||||
service.restoreMetadataRegistrations();
|
||||
|
||||
InOrder declared = inOrder(vpcNetworks);
|
||||
declared.verify(vpcNetworks).declareVpc("us-east-1", "vpc-x", "10.0.0.0/16");
|
||||
declared.verify(vpcNetworks).declareVpc("us-east-1", "vpc-y", "10.0.0.0/16");
|
||||
declared.verify(vpcNetworks).declareVpc("us-east-1", "vpc-z", "10.0.0.0/16");
|
||||
}
|
||||
|
||||
@Test
|
||||
void aLatePersistForAnInstanceNoAccountHoldsWritesNothing() {
|
||||
AccountAwareStorageBackend<Instance> instanceStore = AccountAwareStorageBackend.inMemory("000000000000");
|
||||
Ec2PortForwardManager portForwardManager = mock(Ec2PortForwardManager.class);
|
||||
Ec2Service service = new Ec2Service(mockConfig(false), mock(Ec2ContainerManager.class),
|
||||
portForwardManager, mock(AmiImageResolver.class), mock(Ec2ImageCatalog.class),
|
||||
new Ec2InstanceTypeCatalog(),
|
||||
new InMemoryStorageFactory(Map.of("ec2-instances.json", instanceStore)), mock(VpcNetworkManager.class));
|
||||
service.restoreMetadataRegistrations();
|
||||
ArgumentCaptor<Consumer<Instance>> persister = ArgumentCaptor.captor();
|
||||
verify(portForwardManager).setPersister(persister.capture());
|
||||
|
||||
// Terminated and deleted from its account before the port-forward persister ran.
|
||||
persister.getValue().accept(persistedInstance("i-gone", "10.20.1.10", InstanceState.terminated()));
|
||||
|
||||
assertTrue(instanceStore.scanAllAccountEntries(key -> true).isEmpty());
|
||||
}
|
||||
|
||||
@Test
|
||||
void restoreWritesARestoredInstanceBackUnderItsOwnAccount() {
|
||||
AccountAwareStorageBackend<Instance> instanceStore = AccountAwareStorageBackend.inMemory("000000000000");
|
||||
Instance instance = persistedInstance("i-other", "10.20.1.10", InstanceState.running());
|
||||
instance.setDockerContainerId("container-other");
|
||||
instanceStore.putForAccount("111122223333", "us-east-1::i-other", instance);
|
||||
Ec2ContainerManager containerManager = mock(Ec2ContainerManager.class);
|
||||
when(containerManager.restoreMetadataRegistration(any())).thenReturn(true);
|
||||
Ec2Service service = new Ec2Service(mockConfig(false), containerManager,
|
||||
mock(Ec2PortForwardManager.class), mock(AmiImageResolver.class), mock(Ec2ImageCatalog.class),
|
||||
new Ec2InstanceTypeCatalog(),
|
||||
new InMemoryStorageFactory(Map.of("ec2-instances.json", instanceStore)), mock(VpcNetworkManager.class));
|
||||
|
||||
service.restoreMetadataRegistrations();
|
||||
|
||||
verify(containerManager).restoreMetadataRegistration(argThat(restored -> "i-other".equals(restored.getInstanceId())));
|
||||
assertEquals(List.of("111122223333"), instanceStore.scanAllAccountEntries(key -> key.endsWith("i-other")).stream()
|
||||
.map(AccountAwareStorageBackend.AccountEntry::accountId).toList());
|
||||
}
|
||||
|
||||
@Test
|
||||
void restoreMigratesALegacyInstanceKeyInsteadOfCopyingIt() {
|
||||
InMemoryStorage<String, Instance> raw = new InMemoryStorage<>();
|
||||
AccountAwareStorageBackend<Instance> instanceStore = new AccountAwareStorageBackend<>(raw, null, "000000000000");
|
||||
Instance instance = persistedInstance("i-legacy", "10.20.1.10", InstanceState.running());
|
||||
instance.setDockerContainerId("container-legacy");
|
||||
raw.put("us-east-1::i-legacy", instance);
|
||||
Ec2ContainerManager containerManager = mock(Ec2ContainerManager.class);
|
||||
when(containerManager.restoreMetadataRegistration(any())).thenReturn(true);
|
||||
Ec2Service service = new Ec2Service(mockConfig(false), containerManager,
|
||||
mock(Ec2PortForwardManager.class), mock(AmiImageResolver.class), mock(Ec2ImageCatalog.class),
|
||||
new Ec2InstanceTypeCatalog(),
|
||||
new InMemoryStorageFactory(Map.of("ec2-instances.json", instanceStore)), mock(VpcNetworkManager.class));
|
||||
|
||||
service.restoreMetadataRegistrations();
|
||||
|
||||
verify(containerManager, times(1)).restoreMetadataRegistration(any());
|
||||
assertEquals(Set.of("000000000000/us-east-1::i-legacy"), Set.copyOf(raw.keys()));
|
||||
}
|
||||
|
||||
@Test
|
||||
void restoreDropsALegacyKeyWhoseScopedTwinAlreadyExists() {
|
||||
// Left behind, the legacy copy would come back the moment the scoped one is pruned.
|
||||
InMemoryStorage<String, Instance> raw = new InMemoryStorage<>();
|
||||
AccountAwareStorageBackend<Instance> instanceStore = new AccountAwareStorageBackend<>(raw, null, "000000000000");
|
||||
Instance stale = persistedInstance("i-twin", "10.20.1.10", InstanceState.running());
|
||||
stale.setDockerContainerId("container-stale");
|
||||
raw.put("us-east-1::i-twin", stale);
|
||||
Instance current = persistedInstance("i-twin", "10.20.1.10", InstanceState.running());
|
||||
current.setDockerContainerId("container-current");
|
||||
raw.put("000000000000/us-east-1::i-twin", current);
|
||||
Ec2ContainerManager containerManager = mock(Ec2ContainerManager.class);
|
||||
when(containerManager.restoreMetadataRegistration(any())).thenReturn(true);
|
||||
Ec2Service service = new Ec2Service(mockConfig(false), containerManager,
|
||||
mock(Ec2PortForwardManager.class), mock(AmiImageResolver.class), mock(Ec2ImageCatalog.class),
|
||||
new Ec2InstanceTypeCatalog(),
|
||||
new InMemoryStorageFactory(Map.of("ec2-instances.json", instanceStore)), mock(VpcNetworkManager.class));
|
||||
|
||||
service.restoreMetadataRegistrations();
|
||||
|
||||
verify(containerManager, times(1)).restoreMetadataRegistration(any());
|
||||
verify(containerManager).restoreMetadataRegistration(argThat(restored ->
|
||||
"container-current".equals(restored.getDockerContainerId())));
|
||||
assertEquals(Set.of("000000000000/us-east-1::i-twin"), Set.copyOf(raw.keys()));
|
||||
}
|
||||
|
||||
@Test
|
||||
void shutdownStopsAnotherAccountsRunningInstanceInItsOwnPartition() {
|
||||
AccountAwareStorageBackend<Instance> instanceStore = AccountAwareStorageBackend.inMemory("000000000000");
|
||||
Instance instance = persistedInstance("i-other", "10.20.1.10", InstanceState.running());
|
||||
instance.setDockerContainerId("container-other");
|
||||
instanceStore.putForAccount("111122223333", "us-east-1::i-other", instance);
|
||||
Ec2ContainerManager containerManager = mock(Ec2ContainerManager.class);
|
||||
Ec2Service service = new Ec2Service(mockConfig(false), containerManager,
|
||||
mock(Ec2PortForwardManager.class), mock(AmiImageResolver.class), mock(Ec2ImageCatalog.class),
|
||||
new Ec2InstanceTypeCatalog(),
|
||||
new InMemoryStorageFactory(Map.of("ec2-instances.json", instanceStore)), mock(VpcNetworkManager.class));
|
||||
|
||||
service.stopManagedContainers();
|
||||
|
||||
verify(containerManager).stopForShutdown(argThat(stopped -> "i-other".equals(stopped.getInstanceId())));
|
||||
List<AccountAwareStorageBackend.AccountEntry<Instance>> stored =
|
||||
instanceStore.scanAllAccountEntries(key -> key.endsWith("i-other"));
|
||||
assertEquals(List.of("111122223333"), stored.stream().map(AccountAwareStorageBackend.AccountEntry::accountId).toList());
|
||||
assertEquals("stopped", stored.get(0).value().getState().getName());
|
||||
}
|
||||
|
||||
@Test
|
||||
void restoreSurvivesAnAddressItCannotReserve() {
|
||||
AccountAwareStorageBackend<Instance> instanceStore = AccountAwareStorageBackend.inMemory("000000000000");
|
||||
|
||||
Reference in new issue
Block a user