mirror of
https://github.com/floci-io/floci.git
synced 2026-12-01 01:12:48 -05:00
fix(ecs): round-trip runtimePlatform and logConfiguration on task definitions (#2443)
RegisterTaskDefinition parsed a fixed field list and dropped everything else, so a task definition registered with a runtimePlatform or a container logConfiguration came back from DescribeTaskDefinition without them. A client that verifies its own write — Terraform, or a deploy tool comparing the registered revision against desired state — saw drift that was not there. Both are now parsed, stored on the task definition, and serialized back with the AWS wire shape. A request that omits them still produces a response without the keys, so nothing changes for existing callers. The two fields are set on the task definition returned by EcsService.registerTaskDefinition, following the pattern already used for task-level volumes. That mutation happens after the value was stored, so a backend that serializes on put (persistent, hybrid, WAL) had captured the pre-mutation value; the definition is now written back, which also makes the existing volumes round-trip survive a restart.
This commit is contained in:
1 parent
30f965d056
commit
a2389e7733
10 files changed
+480
-2
No files matched your search
@@ -563,6 +563,82 @@ class EcsIntegrationTest {
|
||||
.body("taskArns", empty());
|
||||
}
|
||||
|
||||
@Test
|
||||
@Order(25)
|
||||
void registerTaskDefinitionRoundTripsRuntimePlatformAndLogConfiguration() {
|
||||
String platformTaskDefArn = ecs("RegisterTaskDefinition")
|
||||
.body("""
|
||||
{
|
||||
"family": "task-with-runtime-platform",
|
||||
"requiresCompatibilities": ["FARGATE"],
|
||||
"networkMode": "awsvpc",
|
||||
"cpu": "256",
|
||||
"memory": "512",
|
||||
"runtimePlatform": {"cpuArchitecture": "ARM64", "operatingSystemFamily": "LINUX"},
|
||||
"containerDefinitions": [
|
||||
{
|
||||
"name": "app",
|
||||
"image": "nginx:latest",
|
||||
"essential": true,
|
||||
"logConfiguration": {
|
||||
"logDriver": "awslogs",
|
||||
"options": {
|
||||
"awslogs-group": "/ecs/task-with-runtime-platform",
|
||||
"awslogs-region": "%s",
|
||||
"awslogs-stream-prefix": "ecs"
|
||||
},
|
||||
"secretOptions": []
|
||||
}
|
||||
}
|
||||
]
|
||||
}
|
||||
""".formatted(REGION))
|
||||
.when()
|
||||
.post("/")
|
||||
.then()
|
||||
.statusCode(200)
|
||||
.body("taskDefinition.runtimePlatform.cpuArchitecture", equalTo("ARM64"))
|
||||
.body("taskDefinition.runtimePlatform.operatingSystemFamily", equalTo("LINUX"))
|
||||
.body("taskDefinition.containerDefinitions[0].logConfiguration.logDriver", equalTo("awslogs"))
|
||||
.body("taskDefinition.containerDefinitions[0].logConfiguration.options.'awslogs-group'",
|
||||
equalTo("/ecs/task-with-runtime-platform"))
|
||||
.body("taskDefinition.containerDefinitions[0].logConfiguration.options.'awslogs-region'", equalTo(REGION))
|
||||
.body("taskDefinition.containerDefinitions[0].logConfiguration.options.'awslogs-stream-prefix'", equalTo("ecs"))
|
||||
.extract()
|
||||
.path("taskDefinition.taskDefinitionArn");
|
||||
|
||||
ecs("DescribeTaskDefinition")
|
||||
.body("""
|
||||
{"taskDefinition": "%s"}
|
||||
""".formatted(platformTaskDefArn))
|
||||
.when()
|
||||
.post("/")
|
||||
.then()
|
||||
.statusCode(200)
|
||||
.body("taskDefinition.runtimePlatform.cpuArchitecture", equalTo("ARM64"))
|
||||
.body("taskDefinition.runtimePlatform.operatingSystemFamily", equalTo("LINUX"))
|
||||
.body("taskDefinition.containerDefinitions[0].logConfiguration.logDriver", equalTo("awslogs"))
|
||||
.body("taskDefinition.containerDefinitions[0].logConfiguration.options.'awslogs-group'",
|
||||
equalTo("/ecs/task-with-runtime-platform"))
|
||||
.body("taskDefinition.containerDefinitions[0].logConfiguration.options.'awslogs-region'", equalTo(REGION))
|
||||
.body("taskDefinition.containerDefinitions[0].logConfiguration.options.'awslogs-stream-prefix'", equalTo("ecs"));
|
||||
}
|
||||
|
||||
@Test
|
||||
@Order(26)
|
||||
void describeTaskDefinitionWithoutRuntimePlatformOrLogConfigurationOmitsBothKeys() {
|
||||
ecs("DescribeTaskDefinition")
|
||||
.body("""
|
||||
{"taskDefinition": "%s:1"}
|
||||
""".formatted(TASK_DEF_FAMILY))
|
||||
.when()
|
||||
.post("/")
|
||||
.then()
|
||||
.statusCode(200)
|
||||
.body("taskDefinition", not(hasKey("runtimePlatform")))
|
||||
.body("taskDefinition.containerDefinitions[0]", not(hasKey("logConfiguration")));
|
||||
}
|
||||
|
||||
// ── Services ──────────────────────────────────────────────────────────────
|
||||
|
||||
@Test
|
||||
|
||||
+148
@@ -0,0 +1,148 @@
|
||||
package io.github.hectorvent.floci.services.ecs;
|
||||
|
||||
import com.fasterxml.jackson.databind.JsonNode;
|
||||
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||
import io.github.hectorvent.floci.services.ecs.model.TaskDefinition;
|
||||
import jakarta.ws.rs.core.Response;
|
||||
import org.junit.jupiter.api.BeforeEach;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
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.mock;
|
||||
import static org.mockito.Mockito.when;
|
||||
|
||||
/**
|
||||
* Unit tests for the RegisterTaskDefinition JSON wire path in {@link EcsJsonHandler}:
|
||||
* the task-level {@code runtimePlatform} and per-container {@code logConfiguration} must be
|
||||
* parsed from the request, stored on the task definition, and serialized back in the response
|
||||
* with the AWS wire shape — and must stay absent from the response when the request omitted
|
||||
* them, so existing callers see no new keys.
|
||||
*
|
||||
* <p>{@link EcsService} is mocked to echo the parsed container definitions back inside a
|
||||
* task definition, so the test exercises parse + serialize without any Docker/Quarkus context.
|
||||
*/
|
||||
class EcsJsonHandlerRuntimePlatformLogConfigurationTest {
|
||||
|
||||
private ObjectMapper objectMapper;
|
||||
private EcsJsonHandler handler;
|
||||
|
||||
@BeforeEach
|
||||
void setUp() {
|
||||
objectMapper = new ObjectMapper();
|
||||
EcsService service = mock(EcsService.class);
|
||||
when(service.registerTaskDefinition(anyString(), any(), any(), any(), any(), any(), any(), any(), any(), anyString()))
|
||||
.thenAnswer(inv -> {
|
||||
TaskDefinition td = new TaskDefinition();
|
||||
td.setFamily(inv.getArgument(0));
|
||||
td.setRevision(1);
|
||||
td.setStatus("ACTIVE");
|
||||
td.setContainerDefinitions(inv.getArgument(1, List.class));
|
||||
return td;
|
||||
});
|
||||
|
||||
handler = new EcsJsonHandler(service, objectMapper);
|
||||
}
|
||||
|
||||
@Test
|
||||
void registerTaskDefinitionRoundTripsRuntimePlatformAndLogConfiguration() throws Exception {
|
||||
String requestJson = """
|
||||
{
|
||||
"family": "arm-family",
|
||||
"runtimePlatform": {"cpuArchitecture": "ARM64", "operatingSystemFamily": "LINUX"},
|
||||
"containerDefinitions": [
|
||||
{
|
||||
"name": "app",
|
||||
"image": "alpine:latest",
|
||||
"logConfiguration": {
|
||||
"logDriver": "awslogs",
|
||||
"options": {
|
||||
"awslogs-group": "/ecs/arm-family",
|
||||
"awslogs-region": "us-east-1",
|
||||
"awslogs-stream-prefix": "ecs"
|
||||
},
|
||||
"secretOptions": [
|
||||
{"name": "splunk-token", "valueFrom": "arn:aws:ssm:us-east-1:000000000000:parameter/splunk"}
|
||||
]
|
||||
}
|
||||
}
|
||||
]
|
||||
}
|
||||
""";
|
||||
JsonNode request = objectMapper.readTree(requestJson);
|
||||
|
||||
Response response = handler.handle("RegisterTaskDefinition", request, "us-east-1");
|
||||
JsonNode td = objectMapper.valueToTree(response.getEntity()).path("taskDefinition");
|
||||
|
||||
JsonNode platform = td.path("runtimePlatform");
|
||||
assertEquals("ARM64", platform.path("cpuArchitecture").asText());
|
||||
assertEquals("LINUX", platform.path("operatingSystemFamily").asText());
|
||||
|
||||
JsonNode logConfig = td.path("containerDefinitions").get(0).path("logConfiguration");
|
||||
assertEquals("awslogs", logConfig.path("logDriver").asText());
|
||||
assertEquals("/ecs/arm-family", logConfig.path("options").path("awslogs-group").asText());
|
||||
assertEquals("us-east-1", logConfig.path("options").path("awslogs-region").asText());
|
||||
assertEquals("ecs", logConfig.path("options").path("awslogs-stream-prefix").asText());
|
||||
JsonNode secretOptions = logConfig.path("secretOptions");
|
||||
assertTrue(secretOptions.isArray() && secretOptions.size() == 1, "one secretOption expected");
|
||||
assertEquals("splunk-token", secretOptions.get(0).path("name").asText());
|
||||
assertEquals("arn:aws:ssm:us-east-1:000000000000:parameter/splunk",
|
||||
secretOptions.get(0).path("valueFrom").asText());
|
||||
}
|
||||
|
||||
@Test
|
||||
void registerTaskDefinitionSerializesOnlyTheRuntimePlatformKeysThatWereSent() throws Exception {
|
||||
String requestJson = """
|
||||
{
|
||||
"family": "arch-only-family",
|
||||
"runtimePlatform": {"cpuArchitecture": "X86_64"},
|
||||
"containerDefinitions": [
|
||||
{
|
||||
"name": "app",
|
||||
"image": "alpine:latest",
|
||||
"logConfiguration": {"logDriver": "json-file"}
|
||||
}
|
||||
]
|
||||
}
|
||||
""";
|
||||
JsonNode request = objectMapper.readTree(requestJson);
|
||||
|
||||
Response response = handler.handle("RegisterTaskDefinition", request, "us-east-1");
|
||||
JsonNode td = objectMapper.valueToTree(response.getEntity()).path("taskDefinition");
|
||||
|
||||
JsonNode platform = td.path("runtimePlatform");
|
||||
assertEquals("X86_64", platform.path("cpuArchitecture").asText());
|
||||
assertTrue(platform.path("operatingSystemFamily").isMissingNode(),
|
||||
"operatingSystemFamily must not be invented when the request omitted it");
|
||||
|
||||
JsonNode logConfig = td.path("containerDefinitions").get(0).path("logConfiguration");
|
||||
assertEquals("json-file", logConfig.path("logDriver").asText());
|
||||
assertTrue(logConfig.path("options").isMissingNode(), "options must not be emitted when omitted");
|
||||
assertTrue(logConfig.path("secretOptions").isMissingNode(), "secretOptions must not be emitted when omitted");
|
||||
}
|
||||
|
||||
@Test
|
||||
void registerTaskDefinitionWithoutRuntimePlatformOrLogConfigurationEmitsNeitherKey() throws Exception {
|
||||
String requestJson = """
|
||||
{
|
||||
"family": "plain-family",
|
||||
"containerDefinitions": [
|
||||
{"name": "app", "image": "alpine:latest"}
|
||||
]
|
||||
}
|
||||
""";
|
||||
JsonNode request = objectMapper.readTree(requestJson);
|
||||
|
||||
Response response = handler.handle("RegisterTaskDefinition", request, "us-east-1");
|
||||
JsonNode td = objectMapper.valueToTree(response.getEntity()).path("taskDefinition");
|
||||
|
||||
assertTrue(td.path("runtimePlatform").isMissingNode(),
|
||||
"runtimePlatform must stay absent for a task definition that never set it");
|
||||
assertTrue(td.path("containerDefinitions").get(0).path("logConfiguration").isMissingNode(),
|
||||
"logConfiguration must stay absent for a container that never set it");
|
||||
}
|
||||
}
|
||||
+124
@@ -0,0 +1,124 @@
|
||||
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.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.TaskDefinition;
|
||||
import jakarta.ws.rs.core.Response;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.junit.jupiter.api.io.TempDir;
|
||||
|
||||
import java.nio.file.Path;
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
import static org.junit.jupiter.api.Assertions.assertNotNull;
|
||||
import static org.mockito.Mockito.RETURNS_DEEP_STUBS;
|
||||
import static org.mockito.Mockito.mock;
|
||||
import static org.mockito.Mockito.when;
|
||||
|
||||
/**
|
||||
* RegisterTaskDefinition sets {@code volumes} and {@code runtimePlatform} on the task definition
|
||||
* <em>after</em> {@link EcsService#registerTaskDefinition} has already stored it. A storage backend
|
||||
* that serializes on {@code put} (persistent, hybrid, WAL) captured the pre-mutation value, so
|
||||
* without an explicit write-back those fields are lost on restart.
|
||||
*
|
||||
* <p>Backed by a real {@link PersistentStorage} over a temp directory, so a second service instance
|
||||
* reloads from the file the first one wrote — the in-memory backend used elsewhere keeps the live
|
||||
* object reference and cannot detect the miss.
|
||||
*/
|
||||
class EcsJsonHandlerTaskDefinitionPersistenceTest {
|
||||
|
||||
private static final String REGION = "us-east-1";
|
||||
|
||||
@Test
|
||||
void taskDefinitionFieldsSetAfterRegistrationSurviveRestart(@TempDir Path dataDir) throws Exception {
|
||||
FileStorageFactory storage = new FileStorageFactory(dataDir);
|
||||
ObjectMapper objectMapper = new ObjectMapper();
|
||||
|
||||
EcsJsonHandler handler = new EcsJsonHandler(serviceWithStorage(storage), objectMapper);
|
||||
JsonNode request = objectMapper.readTree("""
|
||||
{
|
||||
"family": "restart-family",
|
||||
"runtimePlatform": {"cpuArchitecture": "ARM64", "operatingSystemFamily": "LINUX"},
|
||||
"containerDefinitions": [
|
||||
{
|
||||
"name": "app",
|
||||
"image": "alpine:latest",
|
||||
"mountPoints": [{"sourceVolume": "data", "containerPath": "/data", "readOnly": false}],
|
||||
"logConfiguration": {
|
||||
"logDriver": "awslogs",
|
||||
"options": {"awslogs-group": "/ecs/restart-family"}
|
||||
}
|
||||
}
|
||||
],
|
||||
"volumes": [{"name": "data", "host": {"sourcePath": "/host/data"}}]
|
||||
}
|
||||
""");
|
||||
Response response = handler.handle("RegisterTaskDefinition", request, REGION);
|
||||
assertEquals(200, response.getStatus());
|
||||
|
||||
// Simulate a restart: a fresh service reloading from the files the first one wrote.
|
||||
EcsService reloaded = serviceWithStorage(new FileStorageFactory(dataDir));
|
||||
TaskDefinition td = reloaded.describeTaskDefinition("restart-family:1", REGION);
|
||||
|
||||
assertNotNull(td.getRuntimePlatform(), "runtimePlatform must survive a restart");
|
||||
assertEquals("ARM64", td.getRuntimePlatform().cpuArchitecture());
|
||||
assertEquals("LINUX", td.getRuntimePlatform().operatingSystemFamily());
|
||||
|
||||
var logConfiguration = td.getContainerDefinitions().getFirst().getLogConfiguration();
|
||||
assertNotNull(logConfiguration, "logConfiguration must survive a restart");
|
||||
assertEquals("awslogs", logConfiguration.logDriver());
|
||||
assertEquals("/ecs/restart-family", logConfiguration.options().get("awslogs-group"));
|
||||
|
||||
assertEquals(1, td.getVolumes().size(), "task-level volumes must survive a restart");
|
||||
assertEquals("/host/data", td.getVolumes().getFirst().hostSourcePath());
|
||||
}
|
||||
|
||||
private static EcsService serviceWithStorage(StorageFactory storage) {
|
||||
EmulatorConfig config = mock(EmulatorConfig.class, RETURNS_DEEP_STUBS);
|
||||
when(config.services().ecs().mock()).thenReturn(true);
|
||||
when(config.effectiveBaseUrl()).thenReturn("http://localhost:4566");
|
||||
|
||||
EcsService service = new EcsService(
|
||||
new RegionResolver(REGION, "000000000000"),
|
||||
mock(EcsContainerManager.class),
|
||||
config,
|
||||
mock(EcsLoadBalancerRegistrar.class),
|
||||
storage);
|
||||
service.initializeStorage();
|
||||
return service;
|
||||
}
|
||||
|
||||
/** Backs every store with a real {@link PersistentStorage} file, so values are serialized on put. */
|
||||
private static final class FileStorageFactory extends StorageFactory {
|
||||
|
||||
private final Path dataDir;
|
||||
private final Map<String, AccountAwareStorageBackend<?>> stores = new HashMap<>();
|
||||
|
||||
private FileStorageFactory(Path dataDir) {
|
||||
super(null, null);
|
||||
this.dataDir = dataDir;
|
||||
}
|
||||
|
||||
@Override
|
||||
@SuppressWarnings("unchecked")
|
||||
public <V> AccountAwareStorageBackend<V> create(String serviceName,
|
||||
String fileName,
|
||||
TypeReference<Map<String, V>> typeReference) {
|
||||
return (AccountAwareStorageBackend<V>) stores.computeIfAbsent(fileName, ignored -> {
|
||||
PersistentStorage<String, V> persistent =
|
||||
new PersistentStorage<>(dataDir.resolve(fileName), typeReference);
|
||||
persistent.load();
|
||||
return new AccountAwareStorageBackend<>(persistent, null, "000000000000");
|
||||
});
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user