mirror of
https://github.com/floci-io/floci.git
synced 2026-12-03 02:07:58 -05:00
feat(ecs): honor UpdateService forceNewDeployment and emit EventBridge lifecycle events (#2938)
* feat(ecs): honor UpdateService forceNewDeployment (#2557) * feat(ecs): add EcsEventPublisher for aws.ecs lifecycle events (#2557) * feat(ecs): emit ECS Task State Change events on task start/stop (#2557) * feat(ecs): emit ECS Deployment State Change events (#2557) * test(ecs): end-to-end ECS EventBridge lifecycle events + docs (#2557) * fix(ecs): log diagnostic when task ARN region parse fails (#2557) * fix(ecs): synthesize lifecycle events from a task snapshot, not the shared instance emitTaskLadder toggled the live task's lastStatus in place to synthesize each phase, so a concurrent DescribeTasks/ListTasks could observe a status the task never actually occupied while the four-phase ladder was in flight. Build the ladder from a shallow copy instead. Also bump the ARN region-parse fallback log from debug to warn, since debug is off by default and made the fallback effectively invisible.
This commit is contained in:
1 parent
58b2e5ff96
commit
14ca62d9f7
15 files changed
+772
-22
No files matched your search
+211
@@ -0,0 +1,211 @@
|
||||
package io.github.hectorvent.floci.services.ecs;
|
||||
|
||||
import com.fasterxml.jackson.core.type.TypeReference;
|
||||
import com.fasterxml.jackson.databind.JsonNode;
|
||||
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||
import io.github.hectorvent.floci.config.EmulatorConfig;
|
||||
import io.github.hectorvent.floci.core.common.RegionResolver;
|
||||
import io.github.hectorvent.floci.core.storage.AccountAwareStorageBackend;
|
||||
import io.github.hectorvent.floci.core.storage.StorageBackend;
|
||||
import io.github.hectorvent.floci.core.storage.StorageFactory;
|
||||
import io.github.hectorvent.floci.services.ecs.container.EcsContainerManager;
|
||||
import io.github.hectorvent.floci.services.ecs.model.ContainerDefinition;
|
||||
import io.github.hectorvent.floci.services.ecs.model.EcsTask;
|
||||
import io.github.hectorvent.floci.services.ecs.model.LaunchType;
|
||||
import io.github.hectorvent.floci.services.ecs.model.TaskDefinition;
|
||||
import io.github.hectorvent.floci.services.eventbridge.EventBridgeInvoker;
|
||||
import io.github.hectorvent.floci.services.eventbridge.EventBridgeService;
|
||||
import io.github.hectorvent.floci.services.eventbridge.model.RuleState;
|
||||
import io.github.hectorvent.floci.services.eventbridge.model.Target;
|
||||
import org.junit.jupiter.api.BeforeEach;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import java.util.HashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.concurrent.CopyOnWriteArrayList;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
import static org.junit.jupiter.api.Assertions.assertTrue;
|
||||
import static org.mockito.ArgumentMatchers.any;
|
||||
import static org.mockito.ArgumentMatchers.anyString;
|
||||
import static org.mockito.Mockito.RETURNS_DEEP_STUBS;
|
||||
import static org.mockito.Mockito.doAnswer;
|
||||
import static org.mockito.Mockito.mock;
|
||||
import static org.mockito.Mockito.when;
|
||||
|
||||
class EcsEventBridgeIntegrationTest {
|
||||
|
||||
private static final String REGION = "us-east-1";
|
||||
private final ObjectMapper objectMapper = new ObjectMapper();
|
||||
|
||||
private EventBridgeService eventBridgeService;
|
||||
private EventBridgeInvoker invoker;
|
||||
private EcsService ecsService;
|
||||
private List<String> deliveredEvents;
|
||||
|
||||
@BeforeEach
|
||||
void setUp() {
|
||||
deliveredEvents = new CopyOnWriteArrayList<>();
|
||||
invoker = mock(EventBridgeInvoker.class);
|
||||
doAnswer(invocation -> {
|
||||
String eventJson = invocation.getArgument(1);
|
||||
deliveredEvents.add(eventJson);
|
||||
return null;
|
||||
}).when(invoker).invokeTarget(any(), anyString(), anyString());
|
||||
|
||||
RegionResolver regionResolver = new RegionResolver(REGION, "000000000000");
|
||||
InMemoryStorageFactory storageFactory = new InMemoryStorageFactory();
|
||||
EmulatorConfig config = mock(EmulatorConfig.class, RETURNS_DEEP_STUBS);
|
||||
when(config.services().ecs().mock()).thenReturn(true);
|
||||
when(config.effectiveBaseUrl()).thenReturn("http://localhost:4566");
|
||||
|
||||
eventBridgeService = new EventBridgeService(
|
||||
storageFactory,
|
||||
config,
|
||||
regionResolver,
|
||||
objectMapper,
|
||||
null,
|
||||
invoker,
|
||||
null,
|
||||
null
|
||||
);
|
||||
|
||||
EcsEventPublisher eventPublisher = new EcsEventPublisher(eventBridgeService, regionResolver, objectMapper);
|
||||
|
||||
ecsService = new EcsService(
|
||||
regionResolver,
|
||||
mock(EcsContainerManager.class),
|
||||
config,
|
||||
mock(EcsLoadBalancerRegistrar.class),
|
||||
storageFactory,
|
||||
eventPublisher
|
||||
);
|
||||
ecsService.initializeStorage();
|
||||
}
|
||||
|
||||
private static TaskDefinition registerTaskDef(EcsService service, String family, String image) {
|
||||
ContainerDefinition cd = new ContainerDefinition();
|
||||
cd.setName("app");
|
||||
cd.setImage(image);
|
||||
return service.registerTaskDefinition(family, List.of(cd), null, null, null,
|
||||
null, null, List.of(), REGION);
|
||||
}
|
||||
|
||||
@Test
|
||||
void deploymentStateChangeDeliveredToEventBridgeOnServiceReconcile() {
|
||||
eventBridgeService.putRule(
|
||||
"ecs-deploy-rule",
|
||||
null,
|
||||
"{\"source\":[\"aws.ecs\"],\"detail-type\":[\"ECS Deployment State Change\"]}",
|
||||
null,
|
||||
RuleState.ENABLED,
|
||||
"ECS deployment rule",
|
||||
null,
|
||||
null,
|
||||
REGION
|
||||
);
|
||||
|
||||
Target target = new Target();
|
||||
target.setId("deploy-target-1");
|
||||
target.setArn("arn:aws:sqs:us-east-1:000000000000:deploy-queue");
|
||||
eventBridgeService.putTargets("ecs-deploy-rule", null, List.of(target), REGION);
|
||||
|
||||
ecsService.createCluster("deploy-cluster", REGION);
|
||||
registerTaskDef(ecsService, "deploy-fam", "app:1");
|
||||
|
||||
ecsService.createService("deploy-cluster", "deploy-svc", "deploy-fam", 1,
|
||||
LaunchType.FARGATE, List.of(), null, REGION);
|
||||
|
||||
// Tick 1: task launched, desiredCount is being satisfied
|
||||
ecsService.reconcileServices();
|
||||
// Tick 2: converged to steady state
|
||||
ecsService.reconcileServices();
|
||||
|
||||
List<JsonNode> events = deliveredEvents.stream().map(json -> {
|
||||
try {
|
||||
return objectMapper.readTree(json);
|
||||
} catch (Exception e) {
|
||||
throw new RuntimeException(e);
|
||||
}
|
||||
}).toList();
|
||||
|
||||
assertTrue(events.stream().anyMatch(evt ->
|
||||
"aws.ecs".equals(evt.path("source").asText())
|
||||
&& "ECS Deployment State Change".equals(evt.path("detail-type").asText())
|
||||
&& "SERVICE_DEPLOYMENT_COMPLETED".equals(evt.path("detail").path("eventName").asText())
|
||||
&& "INFO".equals(evt.path("detail").path("eventType").asText())
|
||||
&& evt.path("detail").path("deploymentId").asText().startsWith("ecs-svc/")
|
||||
), "Expected SERVICE_DEPLOYMENT_COMPLETED event in delivered events");
|
||||
}
|
||||
|
||||
@Test
|
||||
void taskStateChangeDeliveredToEventBridgeOnTaskStop() {
|
||||
eventBridgeService.putRule(
|
||||
"ecs-task-rule",
|
||||
null,
|
||||
"{\"source\":[\"aws.ecs\"],\"detail-type\":[\"ECS Task State Change\"]}",
|
||||
null,
|
||||
RuleState.ENABLED,
|
||||
"ECS task rule",
|
||||
null,
|
||||
null,
|
||||
REGION
|
||||
);
|
||||
|
||||
Target target = new Target();
|
||||
target.setId("task-target-1");
|
||||
target.setArn("arn:aws:sqs:us-east-1:000000000000:task-queue");
|
||||
eventBridgeService.putTargets("ecs-task-rule", null, List.of(target), REGION);
|
||||
|
||||
ecsService.createCluster("task-cluster", REGION);
|
||||
registerTaskDef(ecsService, "task-fam", "app:1");
|
||||
|
||||
ecsService.createService("task-cluster", "task-svc", "task-fam", 1,
|
||||
LaunchType.FARGATE, List.of(), null, REGION);
|
||||
|
||||
ecsService.reconcileServices();
|
||||
|
||||
List<EcsTask> runningTasks = ecsService.describeTasks(
|
||||
"task-cluster",
|
||||
ecsService.listTasks("task-cluster", null, null, null, REGION),
|
||||
REGION
|
||||
);
|
||||
assertEquals(1, runningTasks.size());
|
||||
String taskArn = runningTasks.getFirst().getTaskArn();
|
||||
|
||||
ecsService.stopTask("task-cluster", taskArn, "test-stop", REGION);
|
||||
|
||||
List<JsonNode> events = deliveredEvents.stream().map(json -> {
|
||||
try {
|
||||
return objectMapper.readTree(json);
|
||||
} catch (Exception e) {
|
||||
throw new RuntimeException(e);
|
||||
}
|
||||
}).toList();
|
||||
|
||||
assertTrue(events.stream().anyMatch(evt ->
|
||||
"aws.ecs".equals(evt.path("source").asText())
|
||||
&& "ECS Task State Change".equals(evt.path("detail-type").asText())
|
||||
&& "STOPPED".equals(evt.path("detail").path("lastStatus").asText())
|
||||
&& taskArn.equals(evt.path("detail").path("taskArn").asText())
|
||||
), "Expected STOPPED ECS Task State Change event with matching taskArn in delivered events");
|
||||
}
|
||||
|
||||
private static final class InMemoryStorageFactory extends StorageFactory {
|
||||
private final Map<String, StorageBackend<String, ?>> stores = new HashMap<>();
|
||||
|
||||
private InMemoryStorageFactory() {
|
||||
super(null, null);
|
||||
}
|
||||
|
||||
@Override
|
||||
@SuppressWarnings("unchecked")
|
||||
public <V> AccountAwareStorageBackend<V> create(String serviceName,
|
||||
String fileName,
|
||||
TypeReference<Map<String, V>> typeReference) {
|
||||
return (AccountAwareStorageBackend<V>) stores.computeIfAbsent(fileName,
|
||||
ignored -> AccountAwareStorageBackend.inMemory("000000000000"));
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,101 @@
|
||||
package io.github.hectorvent.floci.services.ecs;
|
||||
|
||||
import com.fasterxml.jackson.databind.JsonNode;
|
||||
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||
import io.github.hectorvent.floci.core.common.RegionResolver;
|
||||
import io.github.hectorvent.floci.services.ecs.model.Container;
|
||||
import io.github.hectorvent.floci.services.ecs.model.EcsTask;
|
||||
import io.github.hectorvent.floci.services.ecs.model.TaskStatus;
|
||||
import io.github.hectorvent.floci.services.eventbridge.EventBridgeService;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.mockito.ArgumentCaptor;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
import static org.junit.jupiter.api.Assertions.assertTrue;
|
||||
import static org.mockito.ArgumentMatchers.any;
|
||||
import static org.mockito.ArgumentMatchers.eq;
|
||||
import static org.mockito.Mockito.mock;
|
||||
import static org.mockito.Mockito.times;
|
||||
import static org.mockito.Mockito.verify;
|
||||
import static org.mockito.Mockito.when;
|
||||
|
||||
class EcsEventPublisherTest {
|
||||
|
||||
private static final String REGION = "us-east-1";
|
||||
private final ObjectMapper objectMapper = new ObjectMapper();
|
||||
|
||||
private EcsTask runningTask() {
|
||||
EcsTask t = new EcsTask();
|
||||
t.setTaskArn("arn:aws:ecs:us-east-1:000000000000:task/c/abc");
|
||||
t.setClusterArn("arn:aws:ecs:us-east-1:000000000000:cluster/c");
|
||||
t.setTaskDefinitionArn("arn:aws:ecs:us-east-1:000000000000:task-definition/fam:1");
|
||||
t.setLastStatus("RUNNING");
|
||||
t.setDesiredStatus("RUNNING");
|
||||
Container c = new Container();
|
||||
c.setName("app");
|
||||
c.setLastStatus("RUNNING");
|
||||
c.setExitCode(0);
|
||||
t.setContainers(List.of(c));
|
||||
return t;
|
||||
}
|
||||
|
||||
@SuppressWarnings("unchecked")
|
||||
private List<Map<String, Object>> capture(EventBridgeService eb, int times) {
|
||||
ArgumentCaptor<List<Map<String, Object>>> cap = ArgumentCaptor.forClass(List.class);
|
||||
verify(eb, times(times)).putEvents(cap.capture(), eq(REGION));
|
||||
return cap.getAllValues().stream().map(List::getFirst).toList();
|
||||
}
|
||||
|
||||
@Test
|
||||
void startLadderEmitsProvisioningPendingActivatingRunning() throws Exception {
|
||||
EventBridgeService eb = mock(EventBridgeService.class);
|
||||
RegionResolver rr = new RegionResolver(REGION, "000000000000");
|
||||
EcsEventPublisher publisher = new EcsEventPublisher(eb, rr, objectMapper);
|
||||
|
||||
EcsTask task = runningTask();
|
||||
publisher.emitTaskLadder(task, TaskStatus.PENDING, TaskStatus.RUNNING, REGION);
|
||||
|
||||
List<Map<String, Object>> entries = capture(eb, 4);
|
||||
assertEquals(List.of("PROVISIONING", "PENDING", "ACTIVATING", "RUNNING"),
|
||||
entries.stream().map(e -> {
|
||||
try {
|
||||
return objectMapper.readTree((String) e.get("Detail")).path("lastStatus").asText();
|
||||
} catch (Exception ex) { throw new RuntimeException(ex); }
|
||||
}).toList());
|
||||
Map<String, Object> first = entries.getFirst();
|
||||
assertEquals("aws.ecs", first.get("Source"));
|
||||
assertEquals("ECS Task State Change", first.get("DetailType"));
|
||||
assertEquals("RUNNING", task.getLastStatus(), "task status is restored after the ladder");
|
||||
}
|
||||
|
||||
@Test
|
||||
void stopLadderEmitsDeactivatingStoppingDeprovisioningStopped() throws Exception {
|
||||
EventBridgeService eb = mock(EventBridgeService.class);
|
||||
RegionResolver rr = new RegionResolver(REGION, "000000000000");
|
||||
EcsEventPublisher publisher = new EcsEventPublisher(eb, rr, objectMapper);
|
||||
|
||||
EcsTask task = runningTask();
|
||||
task.setLastStatus("STOPPED");
|
||||
task.getContainers().getFirst().setExitCode(137);
|
||||
publisher.emitTaskLadder(task, TaskStatus.RUNNING, TaskStatus.STOPPED, REGION);
|
||||
|
||||
List<Map<String, Object>> entries = capture(eb, 4);
|
||||
JsonNode lastDetail = objectMapper.readTree((String) entries.get(3).get("Detail"));
|
||||
assertEquals("STOPPED", lastDetail.path("lastStatus").asText());
|
||||
assertEquals(137, lastDetail.path("containers").path(0).path("exitCode").asInt());
|
||||
}
|
||||
|
||||
@Test
|
||||
void putEventsFailureIsSwallowed() {
|
||||
EventBridgeService eb = mock(EventBridgeService.class);
|
||||
when(eb.putEvents(any(), any())).thenThrow(new RuntimeException("boom"));
|
||||
RegionResolver rr = new RegionResolver(REGION, "000000000000");
|
||||
EcsEventPublisher publisher = new EcsEventPublisher(eb, rr, objectMapper);
|
||||
|
||||
publisher.emitTaskLadder(runningTask(), TaskStatus.PENDING, TaskStatus.RUNNING, REGION);
|
||||
// no exception propagates
|
||||
}
|
||||
}
|
||||
+35
-1
@@ -3,12 +3,14 @@ package io.github.hectorvent.floci.services.ecs;
|
||||
import com.fasterxml.jackson.core.type.TypeReference;
|
||||
import com.fasterxml.jackson.databind.JsonNode;
|
||||
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||
import com.fasterxml.jackson.databind.node.ObjectNode;
|
||||
import io.github.hectorvent.floci.config.EmulatorConfig;
|
||||
import io.github.hectorvent.floci.core.common.RegionResolver;
|
||||
import io.github.hectorvent.floci.core.storage.AccountAwareStorageBackend;
|
||||
import io.github.hectorvent.floci.core.storage.PersistentStorage;
|
||||
import io.github.hectorvent.floci.core.storage.StorageFactory;
|
||||
import io.github.hectorvent.floci.services.ecs.container.EcsContainerManager;
|
||||
import io.github.hectorvent.floci.services.ecs.model.LaunchType;
|
||||
import io.github.hectorvent.floci.services.ecs.model.TaskDefinition;
|
||||
import jakarta.ws.rs.core.Response;
|
||||
import org.junit.jupiter.api.Test;
|
||||
@@ -16,9 +18,11 @@ import org.junit.jupiter.api.io.TempDir;
|
||||
|
||||
import java.nio.file.Path;
|
||||
import java.util.HashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
import static org.junit.jupiter.api.Assertions.assertNotEquals;
|
||||
import static org.junit.jupiter.api.Assertions.assertNotNull;
|
||||
import static org.mockito.Mockito.RETURNS_DEEP_STUBS;
|
||||
import static org.mockito.Mockito.mock;
|
||||
@@ -82,6 +86,35 @@ class EcsJsonHandlerTaskDefinitionPersistenceTest {
|
||||
assertEquals("/host/data", td.getVolumes().getFirst().hostSourcePath());
|
||||
}
|
||||
|
||||
@Test
|
||||
void updateServiceForceNewDeploymentMintsNewDeploymentId(@TempDir Path dataDir) throws Exception {
|
||||
FileStorageFactory storage = new FileStorageFactory(dataDir);
|
||||
ObjectMapper objectMapper = new ObjectMapper();
|
||||
EcsService service = serviceWithStorage(storage);
|
||||
EcsJsonHandler handler = new EcsJsonHandler(service, objectMapper);
|
||||
|
||||
JsonNode registerReq = objectMapper.readTree("""
|
||||
{
|
||||
"family": "force-fam",
|
||||
"containerDefinitions": [{"name": "app", "image": "alpine:latest"}]
|
||||
}
|
||||
""");
|
||||
handler.handle("RegisterTaskDefinition", registerReq, REGION);
|
||||
|
||||
var created = service.createService(null, "force-svc", "force-fam:1", 0,
|
||||
LaunchType.FARGATE, List.of(), null, REGION);
|
||||
String firstId = created.getDeploymentId();
|
||||
|
||||
ObjectNode req = objectMapper.createObjectNode();
|
||||
req.put("service", "force-svc");
|
||||
req.put("forceNewDeployment", true);
|
||||
Response resp = handler.handle("UpdateService", req, REGION);
|
||||
|
||||
JsonNode svc = objectMapper.readTree(resp.getEntity().toString()).path("service");
|
||||
String newId = svc.path("deployments").path(0).path("id").asText();
|
||||
assertNotEquals(firstId, newId);
|
||||
}
|
||||
|
||||
private static EcsService serviceWithStorage(StorageFactory storage) {
|
||||
EmulatorConfig config = mock(EmulatorConfig.class, RETURNS_DEEP_STUBS);
|
||||
when(config.services().ecs().mock()).thenReturn(true);
|
||||
@@ -92,7 +125,8 @@ class EcsJsonHandlerTaskDefinitionPersistenceTest {
|
||||
mock(EcsContainerManager.class),
|
||||
config,
|
||||
mock(EcsLoadBalancerRegistrar.class),
|
||||
storage);
|
||||
storage,
|
||||
null);
|
||||
service.initializeStorage();
|
||||
return service;
|
||||
}
|
||||
|
||||
+1
-1
@@ -39,7 +39,7 @@ class EcsServiceContainerSecretsTest {
|
||||
when(config.effectiveBaseUrl()).thenReturn("http://localhost:4566");
|
||||
|
||||
EcsService service = new EcsService(new RegionResolver("us-east-1", "000000000000"),
|
||||
containerManager, config, mock(EcsLoadBalancerRegistrar.class), null);
|
||||
containerManager, config, mock(EcsLoadBalancerRegistrar.class), null, null);
|
||||
service.createCluster("test-cluster", "us-east-1");
|
||||
|
||||
ContainerDefinition app = new ContainerDefinition();
|
||||
|
||||
+3
-2
@@ -77,7 +77,7 @@ class EcsServiceDaemonSchedulingTest {
|
||||
when(containerManager.startTask(any(), any(), any(), anyString()))
|
||||
.thenThrow(new RuntimeException("no docker here"));
|
||||
EcsService service = new EcsService(new RegionResolver(REGION, "000000000000"), containerManager,
|
||||
config, mock(EcsLoadBalancerRegistrar.class), new InMemoryStorageFactory());
|
||||
config, mock(EcsLoadBalancerRegistrar.class), new InMemoryStorageFactory(), null);
|
||||
service.initializeStorage();
|
||||
service.createCluster("daemon-fail", REGION);
|
||||
service.registerContainerInstance("daemon-fail", null, List.of(), REGION);
|
||||
@@ -161,7 +161,8 @@ class EcsServiceDaemonSchedulingTest {
|
||||
mock(EcsContainerManager.class),
|
||||
config,
|
||||
mock(EcsLoadBalancerRegistrar.class),
|
||||
new InMemoryStorageFactory());
|
||||
new InMemoryStorageFactory(),
|
||||
null);
|
||||
service.initializeStorage();
|
||||
return service;
|
||||
}
|
||||
|
||||
+2
-1
@@ -114,7 +114,8 @@ class EcsServicePersistenceTest {
|
||||
mock(EcsContainerManager.class),
|
||||
config,
|
||||
mock(EcsLoadBalancerRegistrar.class),
|
||||
storage);
|
||||
storage,
|
||||
null);
|
||||
service.initializeStorage();
|
||||
return service;
|
||||
}
|
||||
|
||||
@@ -11,6 +11,7 @@ import io.github.hectorvent.floci.services.ecs.model.ContainerDefinition;
|
||||
import io.github.hectorvent.floci.services.ecs.model.EcsTask;
|
||||
import io.github.hectorvent.floci.services.ecs.model.LaunchType;
|
||||
import io.github.hectorvent.floci.services.ecs.model.TaskDefinition;
|
||||
import io.github.hectorvent.floci.services.ecs.model.TaskStatus;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import java.util.HashMap;
|
||||
@@ -18,9 +19,16 @@ import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
import static org.junit.jupiter.api.Assertions.assertNotEquals;
|
||||
import static org.junit.jupiter.api.Assertions.assertNotNull;
|
||||
import static org.junit.jupiter.api.Assertions.assertTrue;
|
||||
import static org.mockito.ArgumentMatchers.any;
|
||||
import static org.mockito.ArgumentMatchers.eq;
|
||||
import static org.mockito.Mockito.RETURNS_DEEP_STUBS;
|
||||
import static org.mockito.Mockito.atLeastOnce;
|
||||
import static org.mockito.Mockito.mock;
|
||||
import static org.mockito.Mockito.times;
|
||||
import static org.mockito.Mockito.verify;
|
||||
import static org.mockito.Mockito.when;
|
||||
|
||||
/**
|
||||
@@ -122,6 +130,81 @@ class EcsServiceRolloutTest {
|
||||
second.stream().map(EcsTask::getTaskArn).sorted().toList());
|
||||
}
|
||||
|
||||
@Test
|
||||
void forceNewDeploymentSameTaskDefinitionMintsNewIdAndReplacesTasks() {
|
||||
EcsService service = newMockModeService();
|
||||
service.createCluster("force-cluster", REGION);
|
||||
TaskDefinition rev1 = registerTaskDef(service, "force-fam", "app:1");
|
||||
var created = service.createService("force-cluster", "force-svc", "force-fam", 1,
|
||||
LaunchType.FARGATE, List.of(), null, REGION);
|
||||
String firstDeploymentId = created.getDeploymentId();
|
||||
assertNotNull(firstDeploymentId, "createService assigns a deploymentId");
|
||||
|
||||
service.reconcileServices();
|
||||
List<EcsTask> before = runningTasks(service);
|
||||
assertEquals(1, before.size());
|
||||
assertEquals(firstDeploymentId, before.getFirst().getDeploymentId());
|
||||
|
||||
// Same task definition, force flag set.
|
||||
var updated = service.updateService("force-cluster", "force-svc", null, null, null,
|
||||
null, true, REGION);
|
||||
assertNotEquals(firstDeploymentId, updated.getDeploymentId(),
|
||||
"forceNewDeployment mints a new deployment id");
|
||||
|
||||
// Tick 1: replacement on the new deployment comes up while desiredCount is still met.
|
||||
service.reconcileServices();
|
||||
assertEquals(2, runningTasks(service).size());
|
||||
|
||||
// Tick 2: the task from the old deployment is drained.
|
||||
service.reconcileServices();
|
||||
List<EcsTask> after = runningTasks(service);
|
||||
assertEquals(1, after.size());
|
||||
assertEquals(updated.getDeploymentId(), after.getFirst().getDeploymentId());
|
||||
|
||||
EcsTask stopped = service.describeTasks("force-cluster",
|
||||
List.of(before.getFirst().getTaskArn()), REGION).getFirst();
|
||||
assertEquals("STOPPED", stopped.getLastStatus());
|
||||
}
|
||||
|
||||
@Test
|
||||
void serviceTaskStartAndStopEmitTaskStateChangeLadders() {
|
||||
EcsEventPublisher publisher = mock(EcsEventPublisher.class);
|
||||
EcsService service = newMockModeService(publisher);
|
||||
service.createCluster("evt-cluster", REGION);
|
||||
registerTaskDef(service, "evt-fam", "app:1");
|
||||
service.createService("evt-cluster", "evt-svc", "evt-fam", 1,
|
||||
LaunchType.FARGATE, List.of(), null, REGION);
|
||||
|
||||
service.reconcileServices();
|
||||
verify(publisher).emitTaskLadder(any(), eq(TaskStatus.PENDING), eq(TaskStatus.RUNNING), eq(REGION));
|
||||
|
||||
String taskArn = runningTasks(service).getFirst().getTaskArn();
|
||||
service.stopTask("evt-cluster", taskArn, "test", REGION);
|
||||
verify(publisher).emitTaskLadder(any(), eq(TaskStatus.RUNNING), eq(TaskStatus.STOPPED), eq(REGION));
|
||||
}
|
||||
|
||||
@Test
|
||||
void deploymentEmitsStartedThenCompleted() {
|
||||
EcsEventPublisher publisher = mock(EcsEventPublisher.class);
|
||||
EcsService service = newMockModeService(publisher);
|
||||
service.createCluster("dep-cluster", REGION);
|
||||
registerTaskDef(service, "dep-fam", "app:1");
|
||||
|
||||
service.createService("dep-cluster", "dep-svc", "dep-fam", 1,
|
||||
LaunchType.FARGATE, List.of(), null, REGION);
|
||||
verify(publisher).emitDeploymentStateChange(any(), eq("SERVICE_DEPLOYMENT_STARTED"), any(), eq(REGION));
|
||||
|
||||
service.reconcileServices(); // task starts, still converging or converged same tick
|
||||
service.reconcileServices(); // converged
|
||||
verify(publisher, atLeastOnce())
|
||||
.emitDeploymentStateChange(any(), eq("SERVICE_DEPLOYMENT_COMPLETED"), any(), eq(REGION));
|
||||
|
||||
// COMPLETED is not re-emitted on further steady-state ticks.
|
||||
service.reconcileServices();
|
||||
verify(publisher, times(1))
|
||||
.emitDeploymentStateChange(any(), eq("SERVICE_DEPLOYMENT_COMPLETED"), any(), eq(REGION));
|
||||
}
|
||||
|
||||
private static List<EcsTask> runningTasks(EcsService service) {
|
||||
return service.describeTasks(null, service.listTasks(null, null, null, null, REGION), REGION).stream()
|
||||
.filter(t -> "RUNNING".equals(t.getLastStatus()))
|
||||
@@ -137,6 +220,10 @@ class EcsServiceRolloutTest {
|
||||
}
|
||||
|
||||
private static EcsService newMockModeService() {
|
||||
return newMockModeService(null);
|
||||
}
|
||||
|
||||
private static EcsService newMockModeService(EcsEventPublisher publisher) {
|
||||
EmulatorConfig config = mock(EmulatorConfig.class, RETURNS_DEEP_STUBS);
|
||||
when(config.services().ecs().mock()).thenReturn(true);
|
||||
when(config.effectiveBaseUrl()).thenReturn("http://localhost:4566");
|
||||
@@ -145,7 +232,8 @@ class EcsServiceRolloutTest {
|
||||
mock(EcsContainerManager.class),
|
||||
config,
|
||||
mock(EcsLoadBalancerRegistrar.class),
|
||||
new InMemoryStorageFactory());
|
||||
new InMemoryStorageFactory(),
|
||||
publisher);
|
||||
service.initializeStorage();
|
||||
return service;
|
||||
}
|
||||
|
||||
+2
-1
@@ -225,7 +225,8 @@ class EcsServiceTaskOwnershipTest {
|
||||
mock(EcsContainerManager.class),
|
||||
config,
|
||||
mock(EcsLoadBalancerRegistrar.class),
|
||||
new InMemoryStorageFactory());
|
||||
new InMemoryStorageFactory(),
|
||||
null);
|
||||
service.initializeStorage();
|
||||
return service;
|
||||
}
|
||||
|
||||
@@ -52,7 +52,8 @@ class EcsServiceTeardownTest {
|
||||
containerManager,
|
||||
config,
|
||||
mock(EcsLoadBalancerRegistrar.class),
|
||||
new SingleUseStorageFactory());
|
||||
new SingleUseStorageFactory(),
|
||||
null);
|
||||
service.initializeStorage();
|
||||
|
||||
ContainerDefinition cd = new ContainerDefinition();
|
||||
@@ -98,7 +99,8 @@ class EcsServiceTeardownTest {
|
||||
containerManager,
|
||||
config,
|
||||
mock(EcsLoadBalancerRegistrar.class),
|
||||
new SingleUseStorageFactory());
|
||||
new SingleUseStorageFactory(),
|
||||
null);
|
||||
service.initializeStorage();
|
||||
|
||||
ContainerDefinition cd = new ContainerDefinition();
|
||||
|
||||
Reference in new issue
Block a user