fix: support SSM PutParameter tags and ECS container healthCheck (#2567) (#3550)

* fix(ssm): support Tags and preserve tags on Overwrite in PutParameter (#2567)

* fix(ecs): round-trip containerDefinition healthCheck (#2567)

* style(ecs): replace em-dash with comma in test doc per AGENTS.md

* fix(ssm,ecs): address review feedback on description, ARN parsing and ECS healthCheck command

* test(ecs): pass HostVolumePolicy to EcsJsonHandler in healthCheck tests
This commit is contained in:
Vũ Thế Huy authored and GitHub committed 2026-09-13 21:23:52 -04:00
1 parent 19917ed9ed
commit 7d6e9588cd
10 files changed
+518 -4

No files matched your search

@@ -1239,4 +1239,30 @@ class EcsIntegrationTest {
.body("taskDefinition.containerDefinitions[1]", not(hasKey("entryPoint")))
.body("taskDefinition.containerDefinitions[1]", not(hasKey("command")));
}
@Test
@Order(71)
void registerTaskDefinitionWithHealthCheckMissingCommandReturns400() {
ecs("RegisterTaskDefinition")
.body("""
{
"family": "invalid-hc-missing-cmd",
"containerDefinitions": [
{
"name": "app",
"image": "nginx:latest",
"healthCheck": {
"interval": 30
}
}
]
}
""")
.when()
.post("/")
.then()
.statusCode(400)
.body("__type", containsString("ClientException"))
.body("message", containsString("HealthCheck command is required."));
}
}
@@ -0,0 +1,194 @@
package io.github.hectorvent.floci.services.ecs;
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.AwsException;
import io.github.hectorvent.floci.services.ecs.container.HostVolumePolicy;
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.assertThrows;
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.mock;
import static org.mockito.Mockito.when;
/**
* Unit tests for the RegisterTaskDefinition JSON wire path in {@link EcsJsonHandler}:
* container-level {@code healthCheck} must be parsed from the request, stored on the
* container definition, and serialized back in the response with the AWS wire shape,
* and must stay absent from the response when the request omitted it.
*/
class EcsJsonHandlerHealthCheckTest {
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;
});
EmulatorConfig config = mock(EmulatorConfig.class, RETURNS_DEEP_STUBS);
handler = new EcsJsonHandler(service, objectMapper, new HostVolumePolicy(config));
}
@Test
void registerTaskDefinitionRoundTripsHealthCheck() throws Exception {
String requestJson = """
{
"family": "healthcheck-demo",
"containerDefinitions": [
{
"name": "app",
"image": "public.ecr.aws/docker/library/nginx:alpine",
"essential": true,
"healthCheck": {
"command": ["CMD-SHELL", "curl -f http://localhost/ || exit 1"],
"interval": 30,
"timeout": 5,
"retries": 3,
"startPeriod": 10
}
}
]
}
""";
JsonNode request = objectMapper.readTree(requestJson);
Response response = handler.handle("RegisterTaskDefinition", request, "us-east-1");
JsonNode td = objectMapper.valueToTree(response.getEntity()).path("taskDefinition");
JsonNode container = td.path("containerDefinitions").get(0);
JsonNode hc = container.path("healthCheck");
assertTrue(hc.isObject(), "healthCheck should be an object");
assertTrue(hc.path("command").isArray(), "command should be an array");
assertEquals("CMD-SHELL", hc.path("command").get(0).asText());
assertEquals("curl -f http://localhost/ || exit 1", hc.path("command").get(1).asText());
assertEquals(30, hc.path("interval").asInt());
assertEquals(5, hc.path("timeout").asInt());
assertEquals(3, hc.path("retries").asInt());
assertEquals(10, hc.path("startPeriod").asInt());
}
@Test
void registerTaskDefinitionOmitsHealthCheckWhenNotProvided() 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");
JsonNode container = td.path("containerDefinitions").get(0);
assertTrue(container.path("healthCheck").isMissingNode(),
"healthCheck must stay absent when omitted");
}
@Test
void registerTaskDefinitionHandlesPartialHealthCheckFields() throws Exception {
String requestJson = """
{
"family": "partial-hc-family",
"containerDefinitions": [
{
"name": "app",
"image": "alpine:latest",
"healthCheck": {
"command": ["CMD", "/bin/check.sh"]
}
}
]
}
""";
JsonNode request = objectMapper.readTree(requestJson);
Response response = handler.handle("RegisterTaskDefinition", request, "us-east-1");
JsonNode td = objectMapper.valueToTree(response.getEntity()).path("taskDefinition");
JsonNode container = td.path("containerDefinitions").get(0);
JsonNode hc = container.path("healthCheck");
assertTrue(hc.isObject());
assertEquals(2, hc.path("command").size());
assertEquals("CMD", hc.path("command").get(0).asText());
assertEquals("/bin/check.sh", hc.path("command").get(1).asText());
assertTrue(hc.path("interval").isMissingNode(), "interval should be absent");
assertTrue(hc.path("timeout").isMissingNode(), "timeout should be absent");
assertTrue(hc.path("retries").isMissingNode(), "retries should be absent");
assertTrue(hc.path("startPeriod").isMissingNode(), "startPeriod should be absent");
}
@Test
void registerTaskDefinitionWithMissingHealthCheckCommandThrowsClientException() throws Exception {
String requestJson = """
{
"family": "missing-hc-command",
"containerDefinitions": [
{
"name": "app",
"image": "alpine:latest",
"healthCheck": {
"interval": 30
}
}
]
}
""";
JsonNode request = objectMapper.readTree(requestJson);
AwsException ex = assertThrows(AwsException.class, () ->
handler.handle("RegisterTaskDefinition", request, "us-east-1"));
assertEquals("ClientException", ex.getErrorCode());
assertEquals(400, ex.getHttpStatus());
}
@Test
void registerTaskDefinitionWithEmptyHealthCheckCommandThrowsClientException() throws Exception {
String requestJson = """
{
"family": "empty-hc-command",
"containerDefinitions": [
{
"name": "app",
"image": "alpine:latest",
"healthCheck": {
"command": []
}
}
]
}
""";
JsonNode request = objectMapper.readTree(requestJson);
AwsException ex = assertThrows(AwsException.class, () ->
handler.handle("RegisterTaskDefinition", request, "us-east-1"));
assertEquals("ClientException", ex.getErrorCode());
assertEquals(400, ex.getHttpStatus());
}
}
@@ -1408,4 +1408,106 @@ class SsmIntegrationTest {
.then()
.statusCode(200);
}
@Test
@Order(17)
void putParameterWithTagsAndListTagsForResource() {
given()
.header("X-Amz-Target", "AmazonSSM.PutParameter")
.contentType(SSM_CONTENT_TYPE)
.body("""
{
"Name": "/demo/tagged-param",
"Value": "hello",
"Type": "String",
"Tags": [
{"Key": "Project", "Value": "demo"},
{"Key": "Env", "Value": "test"}
]
}
""")
.when()
.post("/")
.then()
.statusCode(200)
.body("Version", equalTo(1));
given()
.header("X-Amz-Target", "AmazonSSM.ListTagsForResource")
.contentType(SSM_CONTENT_TYPE)
.body("""
{
"ResourceType": "Parameter",
"ResourceId": "/demo/tagged-param"
}
""")
.when()
.post("/")
.then()
.statusCode(200)
.body("TagList", hasSize(2))
.body("TagList.find { it.Key == 'Project' }.Value", equalTo("demo"))
.body("TagList.find { it.Key == 'Env' }.Value", equalTo("test"));
}
@Test
@Order(18)
void putParameterOverwritePreservesExistingTags() {
given()
.header("X-Amz-Target", "AmazonSSM.PutParameter")
.contentType(SSM_CONTENT_TYPE)
.body("""
{
"Name": "/demo/tagged-param",
"Value": "world",
"Type": "String",
"Overwrite": true
}
""")
.when()
.post("/")
.then()
.statusCode(200)
.body("Version", equalTo(2));
given()
.header("X-Amz-Target", "AmazonSSM.ListTagsForResource")
.contentType(SSM_CONTENT_TYPE)
.body("""
{
"ResourceType": "Parameter",
"ResourceId": "/demo/tagged-param"
}
""")
.when()
.post("/")
.then()
.statusCode(200)
.body("TagList", hasSize(2))
.body("TagList.find { it.Key == 'Project' }.Value", equalTo("demo"));
}
@Test
@Order(19)
void putParameterOverwriteWithTagsReturns400() {
given()
.header("X-Amz-Target", "AmazonSSM.PutParameter")
.contentType(SSM_CONTENT_TYPE)
.body("""
{
"Name": "/demo/tagged-param",
"Value": "updated",
"Type": "String",
"Overwrite": true,
"Tags": [
{"Key": "Project", "Value": "demo2"}
]
}
""")
.when()
.post("/")
.then()
.statusCode(400)
.body("__type", equalTo("ValidationException"));
}
}
@@ -843,4 +843,73 @@ class SsmServiceTest {
assertEquals("AssociationDoesNotExist", ex.getErrorCode());
assertEquals(400, ex.getHttpStatus());
}
@Test
void putParameterWithTags() {
String region = "eu-west-1";
Map<String, String> tags = Map.of("Environment", "Production", "Project", "Floci");
ssmService.putParameter("/app/tagged", "value", "String", null, false, tags, region);
Map<String, String> retrievedTags = ssmService.listTagsForResource("/app/tagged", region);
assertEquals(2, retrievedTags.size());
assertEquals("Production", retrievedTags.get("Environment"));
assertEquals("Floci", retrievedTags.get("Project"));
}
@Test
void putParameterOverwritePreservesTags() {
String region = "eu-west-1";
Map<String, String> tags = Map.of("Project", "demo");
ssmService.putParameter("/app/param", "hello", "String", null, false, tags, region);
ssmService.putParameter("/app/param", "world", "String", null, true, null, region);
Map<String, String> retrievedTags = ssmService.listTagsForResource("/app/param", region);
assertEquals(1, retrievedTags.size());
assertEquals("demo", retrievedTags.get("Project"));
}
@Test
void putParameterOverwriteWithTagsThrowsValidationException() {
String region = "eu-west-1";
Map<String, String> tags = Map.of("Project", "demo");
AwsException ex = assertThrows(AwsException.class, () ->
ssmService.putParameter("/app/conflict", "val", "String", null, true, tags, region));
assertEquals("ValidationException", ex.getErrorCode());
assertEquals(400, ex.getHttpStatus());
}
@Test
void listTagsForResourceWithArnNormalized() {
String region = "eu-west-1";
Map<String, String> tags = Map.of("Project", "demo");
ssmService.putParameter("/app/arn-test", "val", "String", null, false, tags, region);
String arn = "arn:aws:ssm:" + region + ":000000000000:parameter/app/arn-test";
Map<String, String> retrievedTags = ssmService.listTagsForResource(arn, region);
assertEquals(1, retrievedTags.size());
assertEquals("demo", retrievedTags.get("Project"));
}
@Test
void listTagsForResourceWithNonAwsPartitionArnNormalized() {
String region = "us-gov-west-1";
Map<String, String> tags = Map.of("GovProject", "mission");
ssmService.putParameter("/app/gov-test", "val", "String", null, false, tags, region);
String arn = "arn:aws-us-gov:ssm:" + region + ":000000000000:parameter/app/gov-test";
Map<String, String> retrievedTags = ssmService.listTagsForResource(arn, region);
assertEquals(1, retrievedTags.size());
assertEquals("mission", retrievedTags.get("GovProject"));
}
@Test
void putParameterOverwriteClearsDescriptionWhenOmitted() {
String region = "eu-west-1";
ssmService.putParameter("/app/desc-test", "val1", "String", "Initial description", false, null, region);
assertEquals("Initial description", ssmService.getParameter("/app/desc-test", region).getDescription());
ssmService.putParameter("/app/desc-test", "val2", "String", null, true, null, region);
assertNull(ssmService.getParameter("/app/desc-test", region).getDescription());
}
}