mirror of
https://github.com/floci-io/floci.git
synced 2026-12-01 01:12:48 -05:00
fix(cloudformation): read an ECS task definition's properties as the ECS handler does (#6018)
Measured on CloudFormation (eu-west-1): an AWS::ECS::TaskDefinition gets
no template validation. CloudFormation hands the ECS handler every
scalar as text, and the handler converts each to the type the resource
schema declares: an integer is a sign and digits once trimmed, within
32 bits ("+128", " 128", "0128"), and a boolean is true or false in
lower, upper or title case once trimmed. A value it cannot convert
("abc", 4.5 or true for an integer, "yes", "tRuE" or "1" for a
boolean), and an object, a list and a scalar standing for one another,
fail the resource with every violation of the unconverted model, where
every scalar is a String, so a Memory of 128 is listed beside the value
that failed. At each object its missing required members come first,
then its members in the schema's order. When every value converts, the
required members the model lacks fail it alone, grouped where their
paths meet as the schema validator groups them.
Floci read the properties as plain JSON: a Memory of "abc" and a
container without an image were registered, and an Essential of
"False" registered an essential container.
AWS::ECS::TaskDefinition joins the generated cfn-property-types.json,
whose structures now carry the members the schema requires, read into
RequestShapeModel's shapes. CfnPropertyTypes' walk over the shapes is
shared by the template validation Reader and the new HandlerReader,
which differ in how a scalar converts and in what a mismatch does.
This commit is contained in:
1 parent
e1d102f06b
commit
fcb41966a8
12 files changed
+1596
-162
No files matched your search
@@ -23,7 +23,8 @@ class RequestShapeModelTest {
|
||||
"_source": {"generator": "test"},
|
||||
"operations": {"Send": "SendRequest"},
|
||||
"shapes": {
|
||||
"SendRequest": {"type": "structure", "members": {"To": "Addresses", "Tags": "TagMap", "From": "Address"}},
|
||||
"SendRequest": {"type": "structure", "members": {"To": "Addresses", "Tags": "TagMap", "From": "Address"},
|
||||
"required": ["To", "From"]},
|
||||
"Addresses": {"type": "list", "member": "Address"},
|
||||
"TagMap": {"type": "map", "value": "Address"},
|
||||
"Address": {"type": "string"}
|
||||
@@ -35,7 +36,9 @@ class RequestShapeModelTest {
|
||||
Shape request = model.shapes().get("SendRequest");
|
||||
assertEquals("structure", request.type());
|
||||
assertEquals(List.of("To", "Tags", "From"), List.copyOf(request.members().keySet()));
|
||||
assertEquals(List.of("To", "From"), request.required());
|
||||
assertEquals("Address", model.shapes().get("Addresses").element());
|
||||
assertTrue(model.shapes().get("Addresses").required().isEmpty());
|
||||
assertEquals("Address", model.shapes().get("TagMap").element());
|
||||
Shape scalar = model.shapes().get("Address");
|
||||
assertEquals("string", scalar.type());
|
||||
@@ -59,6 +62,7 @@ class RequestShapeModelTest {
|
||||
assertThrows(UnsupportedOperationException.class, () -> model.shapes().remove("Address"));
|
||||
assertThrows(UnsupportedOperationException.class,
|
||||
() -> model.shapes().get("SendRequest").members().put("Cc", "Addresses"));
|
||||
assertThrows(UnsupportedOperationException.class, () -> model.shapes().get("SendRequest").required().add("To"));
|
||||
}
|
||||
|
||||
@Test
|
||||
|
||||
+149
@@ -0,0 +1,149 @@
|
||||
package io.github.hectorvent.floci.services.cloudformation;
|
||||
|
||||
import io.github.hectorvent.floci.testing.PartitionCleanup;
|
||||
import io.github.hectorvent.floci.testing.RestAssuredJsonUtils;
|
||||
import io.quarkus.test.junit.QuarkusTest;
|
||||
import io.restassured.specification.RequestSpecification;
|
||||
import org.junit.jupiter.api.BeforeAll;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.junit.jupiter.api.extension.RegisterExtension;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
import static io.restassured.RestAssured.given;
|
||||
import static org.hamcrest.MatcherAssert.assertThat;
|
||||
import static org.hamcrest.Matchers.equalTo;
|
||||
import static org.hamcrest.Matchers.hasItem;
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
|
||||
/**
|
||||
* An {@code AWS::ECS::TaskDefinition} read as the ECS handler reads it. Measured on AWS (eu-west-1,
|
||||
* 2026-10-19): CloudFormation hands the properties over without template validation, every scalar
|
||||
* as text, and the handler converts them to its model's types. A value it cannot convert fails the
|
||||
* resource with every violation of the unconverted model, the Memory it could convert included;
|
||||
* a model that converts but lacks a required member fails with that member alone.
|
||||
*/
|
||||
@QuarkusTest
|
||||
class CloudFormationEcsTaskDefinitionModelIntegrationTest {
|
||||
|
||||
private static final String CFN_AUTH =
|
||||
"AWS4-HMAC-SHA256 Credential=test/20260205/us-east-1/cloudformation/aws4_request";
|
||||
private static final String ECS_TARGET = "AmazonEC2ContainerServiceV20141113";
|
||||
private static final String MEMORY = "#/ContainerDefinitions/0/Memory: expected type: Integer, found: String."
|
||||
+ " This may occur when a numeric value exceeds 32-bit integer max (2147483647). Consider using 'number'"
|
||||
+ " type or ensure value fits in integer range.";
|
||||
/** A String and a CommaDelimitedList parameter of Linux capabilities. */
|
||||
private static final String PARAMETERS = """
|
||||
"Parameters": {
|
||||
"Str": {"Type": "String", "Default": "SYS_PTRACE"},
|
||||
"Csv": {"Type": "CommaDelimitedList", "Default": "SYS_PTRACE,NET_ADMIN"}
|
||||
},
|
||||
""";
|
||||
|
||||
@RegisterExtension
|
||||
final PartitionCleanup cleanup = new PartitionCleanup();
|
||||
|
||||
@BeforeAll
|
||||
static void configureRestAssured() {
|
||||
RestAssuredJsonUtils.configureAwsContentTypes();
|
||||
}
|
||||
|
||||
@Test
|
||||
void aValueTheHandlerCannotConvertFailsTheCreateWithEveryViolation() {
|
||||
String memory = stackName("memory");
|
||||
submit("CreateStack", memory, template("", memory, "\"Name\": \"app\", \"Image\": \"busybox\", \"Memory\": \"abc\""));
|
||||
assertEquals("ROLLBACK_COMPLETE", CfnStackWaits.awaitTerminal(memory, CFN_AUTH).status());
|
||||
assertEventReason(memory, "Model validation failed (" + MEMORY + ")");
|
||||
|
||||
String add = stackName("add");
|
||||
submit("CreateStack", add, template(PARAMETERS, add, "\"Name\": \"app\", \"Image\": \"busybox\", \"Memory\": 128,"
|
||||
+ " \"LinuxParameters\": {\"Capabilities\": {\"Add\": {\"Ref\": \"Str\"}}}"));
|
||||
assertEquals("ROLLBACK_COMPLETE", CfnStackWaits.awaitTerminal(add, CFN_AUTH).status());
|
||||
assertEventReason(add, "Model validation failed (" + MEMORY + "\n#/ContainerDefinitions/0/LinuxParameters"
|
||||
+ "/Capabilities/Add: expected type: JSONArray, found: String)");
|
||||
}
|
||||
|
||||
@Test
|
||||
void aMissingRequiredMemberFailsTheCreateAlone() {
|
||||
String image = stackName("image");
|
||||
submit("CreateStack", image, template("", image, "\"Name\": \"app\", \"Memory\": 128"));
|
||||
assertEquals("ROLLBACK_COMPLETE", CfnStackWaits.awaitTerminal(image, CFN_AUTH).status());
|
||||
assertEventReason(image, "Model validation failed (#/ContainerDefinitions/0: required key [Image] not found)");
|
||||
}
|
||||
|
||||
@Test
|
||||
void valuesTheHandlerConvertsAreRegisteredConverted() {
|
||||
String converted = stackName("converted");
|
||||
submit("CreateStack", converted, template(PARAMETERS, converted, "\"Name\": \"app\", \"Image\": \"busybox\","
|
||||
+ " \"Memory\": \"+128\", \"LinuxParameters\": {\"Capabilities\": {\"Add\": {\"Ref\": \"Csv\"}}}},"
|
||||
+ " {\"Name\": \"side\", \"Image\": \"busybox\", \"Memory\": 64, \"Essential\": \"False\""));
|
||||
assertEquals("CREATE_COMPLETE", CfnStackWaits.awaitTerminal(converted, CFN_AUTH).status());
|
||||
|
||||
RestAssuredJsonUtils.awsAction(ECS_TARGET, "DescribeTaskDefinition", "{\"taskDefinition\": \"" + converted + "\"}")
|
||||
.then()
|
||||
.statusCode(200)
|
||||
.body("taskDefinition.containerDefinitions.find { it.name == 'app' }.memory", equalTo(128))
|
||||
.body("taskDefinition.containerDefinitions.find { it.name == 'side' }.essential", equalTo(false));
|
||||
}
|
||||
|
||||
private String stackName(String purpose) {
|
||||
String name = "cfn-ecs-model-" + purpose + "-" + Long.toString(System.nanoTime(), 36);
|
||||
cleanup.register(() -> {
|
||||
submit("DeleteStack", name, null);
|
||||
CfnStackWaits.awaitStackDeleted(name, CFN_AUTH);
|
||||
});
|
||||
return name;
|
||||
}
|
||||
|
||||
/**
|
||||
* A task definition of {@code family} whose containers are {@code containers}, the members of
|
||||
* each without its braces, after the template sections {@code sections}.
|
||||
*/
|
||||
private static String template(String sections, String family, String containers) {
|
||||
return """
|
||||
{
|
||||
%s
|
||||
"Resources": {
|
||||
"TaskDef": {
|
||||
"Type": "AWS::ECS::TaskDefinition",
|
||||
"Properties": {
|
||||
"Family": "%s",
|
||||
"ContainerDefinitions": [{%s}]
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
""".formatted(sections, family, containers);
|
||||
}
|
||||
|
||||
private static void submit(String action, String stackName, String template) {
|
||||
RequestSpecification request = given()
|
||||
.contentType("application/x-www-form-urlencoded")
|
||||
.header("Authorization", CFN_AUTH)
|
||||
.formParam("Action", action)
|
||||
.formParam("StackName", stackName);
|
||||
if (template != null) {
|
||||
request.formParam("TemplateBody", template);
|
||||
}
|
||||
request
|
||||
.when()
|
||||
.post("/")
|
||||
.then()
|
||||
.statusCode(200);
|
||||
}
|
||||
|
||||
/** The stack has an event whose reason is {@code reason}, read from the XML so its escapes are undone. */
|
||||
private static void assertEventReason(String stackName, String reason) {
|
||||
List<String> reasons = given()
|
||||
.contentType("application/x-www-form-urlencoded")
|
||||
.header("Authorization", CFN_AUTH)
|
||||
.formParam("Action", "DescribeStackEvents")
|
||||
.formParam("StackName", stackName)
|
||||
.when()
|
||||
.post("/")
|
||||
.then()
|
||||
.statusCode(200)
|
||||
.extract().xmlPath().getList("**.findAll { it.name() == 'ResourceStatusReason' }", String.class);
|
||||
assertThat(reasons, hasItem(reason));
|
||||
}
|
||||
}
|
||||
+168
-1
@@ -7,6 +7,11 @@ import io.github.hectorvent.floci.core.common.AwsException;
|
||||
import io.github.hectorvent.floci.core.common.RequestShapeModel;
|
||||
import io.github.hectorvent.floci.services.cloudformation.CloudFormationTemplateEngine;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.junit.jupiter.params.ParameterizedTest;
|
||||
import org.junit.jupiter.params.provider.Arguments;
|
||||
import org.junit.jupiter.params.provider.MethodSource;
|
||||
|
||||
import java.util.stream.Stream;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
import static org.junit.jupiter.api.Assertions.assertNull;
|
||||
@@ -14,7 +19,7 @@ import static org.junit.jupiter.api.Assertions.assertThrows;
|
||||
import static org.junit.jupiter.api.Assertions.assertTrue;
|
||||
|
||||
/**
|
||||
* The property types vendored from the resource schemas, and the reader's walk over the shapes the
|
||||
* The property types vendored from the resource schemas, and the readers' walk over the shapes the
|
||||
* generator writes: structures, lists, maps, scalars and values the schema leaves free.
|
||||
*/
|
||||
class CfnPropertyTypesTest {
|
||||
@@ -50,6 +55,8 @@ class CfnPropertyTypesTest {
|
||||
CfnPropertyTypes.propertyShape("AWS::Lambda::LayerVersion", "CompatibleRuntimes"));
|
||||
assertEquals("AWS::AutoScaling::LaunchConfiguration/MetadataOptions",
|
||||
CfnPropertyTypes.propertyShape("AWS::AutoScaling::LaunchConfiguration", "MetadataOptions"));
|
||||
assertEquals("AWS::ECS::TaskDefinition.ContainerDefinitions",
|
||||
CfnPropertyTypes.propertyShape("AWS::ECS::TaskDefinition", "ContainerDefinitions"));
|
||||
}
|
||||
|
||||
@Test
|
||||
@@ -91,6 +98,166 @@ class CfnPropertyTypesTest {
|
||||
.read("Config", json("{\"Ref\": \"AWS::NoValue\"}"), "AWS::Test::Thing/Config"));
|
||||
}
|
||||
|
||||
private static final String HINT = ". This may occur when a numeric value exceeds 32-bit integer max"
|
||||
+ " (2147483647). Consider using 'number' type or ensure value fits in integer range.";
|
||||
private static final String CONTAINER = "#/ContainerDefinitions/0";
|
||||
private static final String MEMORY = CONTAINER + "/Memory: expected type: Integer, found: String" + HINT;
|
||||
private static final String LINUX = CONTAINER + "/LinuxParameters";
|
||||
|
||||
/**
|
||||
* Measured on CloudFormation (eu-west-1, 2026-10-19): an AWS::ECS::TaskDefinition gets no
|
||||
* template validation, and the ECS handler fails it with every violation of the unconverted
|
||||
* model when a value does not convert, Memory 128 included, in the schema's order whatever the
|
||||
* template's; when every value converts, with the required members the model lacks, grouped
|
||||
* where their paths meet.
|
||||
*/
|
||||
@ParameterizedTest(name = "{0}")
|
||||
@MethodSource("ecsTaskDefinitions")
|
||||
void anEcsTaskDefinitionFailsWhereTheEcsHandlerDoes(String measured, String properties, String violations) {
|
||||
AwsException e = assertThrows(AwsException.class, () -> new CfnPropertyTypes.HandlerReader(engine())
|
||||
.read("AWS::ECS::TaskDefinition", json(properties)));
|
||||
|
||||
assertEquals("InvalidRequest", e.getErrorCode());
|
||||
assertEquals("Model validation failed (" + violations, e.getMessage());
|
||||
}
|
||||
|
||||
static Stream<Arguments> ecsTaskDefinitions() {
|
||||
return Stream.of(
|
||||
Arguments.of("Add a string", container("\"Memory\": 128, \"LinuxParameters\": {\"Capabilities\": {\"Add\": \"SYS_PTRACE\"}}"),
|
||||
MEMORY + "\n" + LINUX + "/Capabilities/Add: expected type: JSONArray, found: String)"),
|
||||
Arguments.of("Add an object item", container("\"Memory\": 128, \"LinuxParameters\": {\"Capabilities\": {\"Add\": [{}]}}"),
|
||||
MEMORY + "\n" + LINUX + "/Capabilities/Add/0: expected type: String, found: JSONObject)"),
|
||||
Arguments.of("Capabilities a string", container("\"Memory\": 128, \"LinuxParameters\": {\"Capabilities\": \"x\"}"),
|
||||
MEMORY + "\n" + LINUX + "/Capabilities: expected type: JSONObject, found: String)"),
|
||||
Arguments.of("LinuxParameters a string", container("\"Memory\": 128, \"LinuxParameters\": \"x\""),
|
||||
MEMORY + "\n" + LINUX + ": expected type: JSONObject, found: String)"),
|
||||
Arguments.of("LinuxParameters a list", container("\"Memory\": 128, \"LinuxParameters\": [\"x\"]"),
|
||||
MEMORY + "\n" + LINUX + ": expected type: JSONObject, found: JSONArray)"),
|
||||
Arguments.of("InitProcessEnabled yes", container("\"Memory\": 128, \"LinuxParameters\": {\"InitProcessEnabled\": \"yes\"}"),
|
||||
MEMORY + "\n" + LINUX + "/InitProcessEnabled: expected type: Boolean, found: String)"),
|
||||
Arguments.of("InitProcessEnabled an object", container("\"Memory\": 128, \"LinuxParameters\": {\"InitProcessEnabled\": {}}"),
|
||||
MEMORY + "\n" + LINUX + "/InitProcessEnabled: expected type: Boolean, found: JSONObject)"),
|
||||
Arguments.of("SharedMemorySize abc", container("\"Memory\": 128, \"LinuxParameters\": {\"SharedMemorySize\": \"abc\"}"),
|
||||
MEMORY + "\n" + LINUX + "/SharedMemorySize: expected type: Integer, found: String" + HINT + ")"),
|
||||
Arguments.of("SharedMemorySize 4.5", container("\"Memory\": 128, \"LinuxParameters\": {\"SharedMemorySize\": 4.5}"),
|
||||
MEMORY + "\n" + LINUX + "/SharedMemorySize: expected type: Integer, found: String" + HINT + ")"),
|
||||
Arguments.of("SharedMemorySize true", container("\"Memory\": 128, \"LinuxParameters\": {\"SharedMemorySize\": true}"),
|
||||
MEMORY + "\n" + LINUX + "/SharedMemorySize: expected type: Integer, found: String" + HINT + ")"),
|
||||
Arguments.of("MaxSwap beyond 32 bits", container("\"Memory\": 128, \"LinuxParameters\": {\"MaxSwap\": 3000000000}"),
|
||||
MEMORY + "\n" + LINUX + "/MaxSwap: expected type: Integer, found: String" + HINT + ")"),
|
||||
Arguments.of("Devices a string", container("\"Memory\": 128, \"LinuxParameters\": {\"Devices\": \"x\"}"),
|
||||
MEMORY + "\n" + LINUX + "/Devices: expected type: JSONArray, found: String)"),
|
||||
Arguments.of("HostPath an object", container("\"Memory\": 128, \"LinuxParameters\": {\"Devices\": [{\"HostPath\": {}}]}"),
|
||||
MEMORY + "\n" + LINUX + "/Devices/0/HostPath: expected type: String, found: JSONObject)"),
|
||||
Arguments.of("MountOptions a string", container("\"Memory\": 128, \"LinuxParameters\": {\"Tmpfs\": [{\"ContainerPath\": \"/t\", \"Size\": 64, \"MountOptions\": \"rw\"}]}"),
|
||||
MEMORY + "\n" + LINUX + "/Tmpfs/0/Size: expected type: Integer, found: String" + HINT + "\n" + LINUX
|
||||
+ "/Tmpfs/0/MountOptions: expected type: JSONArray, found: String)"),
|
||||
Arguments.of("MountOptions before Size in the template", container("\"Memory\": 128, \"LinuxParameters\": {\"Tmpfs\": [{\"MountOptions\": \"rw\", \"ContainerPath\": \"/t\", \"Size\": 64}]}"),
|
||||
MEMORY + "\n" + LINUX + "/Tmpfs/0/Size: expected type: Integer, found: String" + HINT + "\n" + LINUX
|
||||
+ "/Tmpfs/0/MountOptions: expected type: JSONArray, found: String)"),
|
||||
Arguments.of("LinuxParameters before Memory in the template", "{\"ContainerDefinitions\": [{\"LinuxParameters\": {\"Capabilities\": {\"Add\": \"X\"}}, \"Memory\": 128, \"Name\": \"app\", \"Image\": \"busybox\"}]}",
|
||||
MEMORY + "\n" + LINUX + "/Capabilities/Add: expected type: JSONArray, found: String)"),
|
||||
Arguments.of("Memory abc", container("\"Memory\": \"abc\", \"LinuxParameters\": {}"), MEMORY + ")"),
|
||||
Arguments.of("Memory an object", container("\"Memory\": {}"),
|
||||
CONTAINER + "/Memory: expected type: Integer, found: JSONObject)"),
|
||||
Arguments.of("Essential tRuE", container("\"Memory\": 128, \"Essential\": \"tRuE\""),
|
||||
MEMORY + "\n" + CONTAINER + "/Essential: expected type: Boolean, found: String)"),
|
||||
Arguments.of("Essential 1", container("\"Memory\": 128, \"Essential\": \"1\""),
|
||||
MEMORY + "\n" + CONTAINER + "/Essential: expected type: Boolean, found: String)"),
|
||||
Arguments.of("Name an object", "{\"ContainerDefinitions\": [{\"Name\": {\"a\": 1}, \"Image\": \"busybox\", \"Memory\": 128}]}",
|
||||
MEMORY + "\n" + CONTAINER + "/Name: expected type: String, found: JSONObject)"),
|
||||
Arguments.of("ContainerDefinitions a string", "{\"ContainerDefinitions\": \"x\"}",
|
||||
"#/ContainerDefinitions: expected type: JSONArray, found: String)"),
|
||||
Arguments.of("a number for text", container("\"Memory\": 128, \"Environment\": [{\"Name\": \"A\", \"Value\": 5}], \"LinuxParameters\": {\"Devices\": \"x\"}"),
|
||||
MEMORY + "\n" + LINUX + "/Devices: expected type: JSONArray, found: String)"),
|
||||
Arguments.of("every integer and boolean, in the schema's order", container("\"Memory\": 128, \"PortMappings\": [{\"ContainerPort\": 80}], \"Essential\": true, \"LinuxParameters\": {\"Devices\": \"x\"}"),
|
||||
MEMORY + "\n" + CONTAINER + "/Essential: expected type: Boolean, found: String\n" + LINUX
|
||||
+ "/Devices: expected type: JSONArray, found: String\n" + CONTAINER
|
||||
+ "/PortMappings/0/ContainerPort: expected type: Integer, found: String" + HINT + ")"),
|
||||
Arguments.of("two containers", "{\"ContainerDefinitions\": [{\"Name\": \"a\", \"Image\": \"busybox\", \"Memory\": 128, \"Essential\": true},"
|
||||
+ " {\"Name\": \"b\", \"Image\": \"busybox\", \"Memory\": 64, \"Essential\": false, \"LinuxParameters\": {\"Capabilities\": {\"Add\": \"X\"}}}]}",
|
||||
MEMORY + "\n" + CONTAINER + "/Essential: expected type: Boolean, found: String\n#/ContainerDefinitions/1/Memory:"
|
||||
+ " expected type: Integer, found: String" + HINT + "\n#/ContainerDefinitions/1/Essential: expected type:"
|
||||
+ " Boolean, found: String\n#/ContainerDefinitions/1/LinuxParameters/Capabilities/Add: expected type:"
|
||||
+ " JSONArray, found: String)"),
|
||||
Arguments.of("the task level", "{\"Memory\": \"512\", \"EphemeralStorage\": {\"SizeInGiB\": 30}, \"ContainerDefinitions\": [{\"Name\": \"app\","
|
||||
+ " \"Image\": \"busybox\", \"Cpu\": 10, \"LinuxParameters\": {\"Capabilities\": {\"Add\": \"X\"}, \"InitProcessEnabled\": true}}]}",
|
||||
CONTAINER + "/Cpu: expected type: Integer, found: String" + HINT + "\n" + LINUX
|
||||
+ "/Capabilities/Add: expected type: JSONArray, found: String\n" + LINUX
|
||||
+ "/InitProcessEnabled: expected type: Boolean, found: String\n#/EphemeralStorage/SizeInGiB: expected"
|
||||
+ " type: Integer, found: String" + HINT + ")"),
|
||||
Arguments.of("a missing required member beside a type", container("\"Memory\": 128, \"LinuxParameters\": {\"Capabilities\": {\"Add\": \"X\"}, \"Tmpfs\": [{\"ContainerPath\": \"/t\"}]}"),
|
||||
MEMORY + "\n" + LINUX + "/Capabilities/Add: expected type: JSONArray, found: String\n" + LINUX
|
||||
+ "/Tmpfs/0: required key [Size] not found)"),
|
||||
Arguments.of("required members before the object's members", container("\"Memory\": 128, \"LinuxParameters\": {\"Tmpfs\": [{\"ContainerPath\": \"/t\", \"MountOptions\": \"rw\"}]}"),
|
||||
MEMORY + "\n" + LINUX + "/Tmpfs/0: required key [Size] not found\n" + LINUX
|
||||
+ "/Tmpfs/0/MountOptions: expected type: JSONArray, found: String)"),
|
||||
Arguments.of("no Name and no Image beside a type", "{\"ContainerDefinitions\": [{\"Memory\": 128, \"LinuxParameters\": {\"Capabilities\": {\"Add\": \"X\"}}}]}",
|
||||
CONTAINER + ": required key [Name] not found\n" + CONTAINER + ": required key [Image] not found\n" + MEMORY
|
||||
+ "\n" + LINUX + "/Capabilities/Add: expected type: JSONArray, found: String)"),
|
||||
Arguments.of("Tmpfs without Size", container("\"Memory\": 128, \"LinuxParameters\": {\"Tmpfs\": [{\"ContainerPath\": \"/t\"}]}"),
|
||||
LINUX + "/Tmpfs/0: required key [Size] not found)"),
|
||||
Arguments.of("no Name", "{\"ContainerDefinitions\": [{\"Image\": \"busybox\", \"Memory\": 128}]}",
|
||||
CONTAINER + ": required key [Name] not found)"),
|
||||
Arguments.of("no Name and no Image", "{\"ContainerDefinitions\": [{\"Memory\": 128, \"Essential\": true}]}",
|
||||
CONTAINER + ": 2 schema violations found)\n" + CONTAINER + ": required key [Name] not found (" + CONTAINER
|
||||
+ ")\n" + CONTAINER + ": required key [Image] not found (" + CONTAINER + ")"),
|
||||
Arguments.of("no Name and Tmpfs without Size", "{\"ContainerDefinitions\": [{\"Image\": \"busybox\", \"Memory\": 128,"
|
||||
+ " \"LinuxParameters\": {\"Tmpfs\": [{\"ContainerPath\": \"/t\"}]}}]}",
|
||||
CONTAINER + ": 2 schema violations found)\n" + CONTAINER + ": required key [Name] not found (" + CONTAINER
|
||||
+ ")\n" + LINUX + "/Tmpfs/0: required key [Size] not found (" + LINUX + "/Tmpfs/0)"),
|
||||
Arguments.of("an empty ulimit", container("\"Memory\": 128, \"Ulimits\": [{}]"),
|
||||
CONTAINER + "/Ulimits/0: 3 schema violations found)\n" + CONTAINER + "/Ulimits/0: required key [HardLimit] not"
|
||||
+ " found (" + CONTAINER + "/Ulimits/0)\n" + CONTAINER + "/Ulimits/0: required key [Name] not found ("
|
||||
+ CONTAINER + "/Ulimits/0)\n" + CONTAINER + "/Ulimits/0: required key [SoftLimit] not found ("
|
||||
+ CONTAINER + "/Ulimits/0)"),
|
||||
Arguments.of("no Name and an empty ulimit", "{\"ContainerDefinitions\": [{\"Image\": \"busybox\", \"Memory\": 128, \"Ulimits\": [{}]}]}",
|
||||
CONTAINER + ": 4 schema violations found)\n" + CONTAINER + ": required key [Name] not found (" + CONTAINER
|
||||
+ ")\n" + CONTAINER + "/Ulimits/0: 3 schema violations found (" + CONTAINER + "/Ulimits/0)"),
|
||||
Arguments.of("two containers without Name", "{\"ContainerDefinitions\": [{\"Image\": \"busybox\", \"Memory\": 128},"
|
||||
+ " {\"Image\": \"busybox\", \"Memory\": 64}]}",
|
||||
"#/ContainerDefinitions: 2 schema violations found)\n" + CONTAINER + ": required key [Name] not found ("
|
||||
+ CONTAINER + ")\n#/ContainerDefinitions/1: required key [Name] not found (#/ContainerDefinitions/1)"),
|
||||
Arguments.of("the task level and a container", "{\"ContainerDefinitions\": [{\"Image\": \"busybox\", \"Memory\": 128}],"
|
||||
+ " \"ProxyConfiguration\": {\"Type\": \"APPMESH\"}}",
|
||||
"#: 2 schema violations found)\n#/ProxyConfiguration: required key [ContainerName] not found"
|
||||
+ " (#/ProxyConfiguration)\n" + CONTAINER + ": required key [Name] not found (" + CONTAINER + ")"));
|
||||
}
|
||||
|
||||
/** Measured: the values the ECS handler converts, and a number where text is expected, read as text. */
|
||||
@Test
|
||||
void anEcsTaskDefinitionIsConvertedAsTheEcsHandlerConvertsIt() {
|
||||
JsonNode read = new CfnPropertyTypes.HandlerReader(engine()).read("AWS::ECS::TaskDefinition", json("""
|
||||
{"Family": "web", "Memory": 512, "ContainerDefinitions": [
|
||||
{"Name": "a", "Image": "busybox", "Memory": "+128", "Essential": "True",
|
||||
"Environment": [{"Name": "A", "Value": 5}],
|
||||
"LinuxParameters": {"InitProcessEnabled": "TRUE", "SharedMemorySize": "64",
|
||||
"Devices": [{"HostPath": 5, "ContainerPath": "/dev/x"}]}},
|
||||
{"Name": "b", "Image": "busybox", "Memory": " 128", "Essential": " true"},
|
||||
{"Name": "c", "Image": "busybox", "Memory": "0128", "Cpu": {"Ref": "AWS::NoValue"}}]}
|
||||
"""));
|
||||
|
||||
assertEquals(json("""
|
||||
{"Family": "web", "Memory": "512", "ContainerDefinitions": [
|
||||
{"Name": "a", "Image": "busybox", "Memory": 128, "Essential": true,
|
||||
"Environment": [{"Name": "A", "Value": "5"}],
|
||||
"LinuxParameters": {"InitProcessEnabled": true, "SharedMemorySize": 64,
|
||||
"Devices": [{"HostPath": "5", "ContainerPath": "/dev/x"}]}},
|
||||
{"Name": "b", "Image": "busybox", "Memory": 128, "Essential": true},
|
||||
{"Name": "c", "Image": "busybox", "Memory": 128}]}
|
||||
"""), read);
|
||||
}
|
||||
|
||||
@Test
|
||||
void aResourceTypeTheVendoredFileLacksIsAnErrorOfTheBuild() {
|
||||
assertThrows(IllegalStateException.class,
|
||||
() -> new CfnPropertyTypes.HandlerReader(engine()).read("AWS::No::Type", json("{}")));
|
||||
}
|
||||
|
||||
private static String container(String members) {
|
||||
return "{\"ContainerDefinitions\": [{\"Name\": \"app\", \"Image\": \"busybox\", " + members + "}]}";
|
||||
}
|
||||
|
||||
private static JsonNode read(String value, String shape) {
|
||||
CfnPropertyTypes.Reader reader = new CfnPropertyTypes.Reader(engine(), MODEL);
|
||||
JsonNode read = reader.read("Config", json(value), shape);
|
||||
|
||||
+47
@@ -20,6 +20,7 @@ import org.junit.jupiter.api.Test;
|
||||
import org.mockito.ArgumentCaptor;
|
||||
import org.mockito.InOrder;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.HashMap;
|
||||
import java.util.List;
|
||||
import java.util.Set;
|
||||
@@ -42,6 +43,7 @@ import static org.mockito.Mockito.mock;
|
||||
import static org.mockito.Mockito.never;
|
||||
import static org.mockito.Mockito.times;
|
||||
import static org.mockito.Mockito.verify;
|
||||
import static org.mockito.Mockito.verifyNoMoreInteractions;
|
||||
import static org.mockito.Mockito.when;
|
||||
|
||||
/**
|
||||
@@ -71,9 +73,15 @@ class EcsCfnProvisionerTest {
|
||||
return node == null || node.isMissingNode() || node.isNull() ? null : node.asText();
|
||||
});
|
||||
when(engine.resolveNode(any())).thenAnswer(inv -> inv.getArgument(0));
|
||||
when(engine.omitNoValue(any())).thenAnswer(inv -> inv.getArgument(0));
|
||||
// resolveStringList delegates to the real engine method; for the literal arrays these
|
||||
// tests use it just walks the array and calls resolve(...) per element, stubbed above.
|
||||
when(engine.resolveStringList(any())).thenCallRealMethod();
|
||||
when(engine.resolveLiteralList(any())).thenAnswer(inv -> {
|
||||
List<String> values = new ArrayList<>();
|
||||
((JsonNode) inv.getArgument(0)).forEach(value -> values.add(value.asText()));
|
||||
return values;
|
||||
});
|
||||
return new ProvisionContext(engine, REGION, "000000000000", "my-stack", priorPhysicalId);
|
||||
}
|
||||
|
||||
@@ -184,6 +192,45 @@ class EcsCfnProvisionerTest {
|
||||
assertEquals("fluentbit", def.getFirelensConfiguration().type());
|
||||
}
|
||||
|
||||
/**
|
||||
* Measured (eu-west-1, 2026-10-19): the ECS handler converts text to the integers and booleans
|
||||
* its model declares, and a value it cannot convert fails the resource with its model
|
||||
* validation message before anything is registered.
|
||||
*/
|
||||
@Test
|
||||
void taskDefinitionValuesAreReadAsTheEcsHandlerConvertsThem() {
|
||||
TaskDefinition td = new TaskDefinition();
|
||||
td.setTaskDefinitionArn(TASK_DEF_ARN);
|
||||
when(ecs.registerTaskDefinition(eq("web"), anyList(), isNull(), isNull(), isNull(), isNull(), isNull(),
|
||||
anyList(), eq(REGION))).thenReturn(td);
|
||||
ObjectNode props = mapper.createObjectNode().put("Family", "web");
|
||||
props.putArray("ContainerDefinitions").addObject()
|
||||
.put("Name", "app").put("Image", "nginx:1").put("Cpu", " 64").put("Memory", "+128")
|
||||
.put("Essential", "False");
|
||||
|
||||
provisioner.provision(resource("AWS::ECS::TaskDefinition", "TaskDef"), props, ctx());
|
||||
|
||||
@SuppressWarnings("unchecked")
|
||||
ArgumentCaptor<List<ContainerDefinition>> defs = ArgumentCaptor.forClass(List.class);
|
||||
verify(ecs).registerTaskDefinition(eq("web"), defs.capture(), isNull(), isNull(), isNull(), isNull(),
|
||||
isNull(), anyList(), eq(REGION));
|
||||
ContainerDefinition def = defs.getValue().get(0);
|
||||
assertEquals(64, def.getCpu());
|
||||
assertEquals(128, def.getMemory());
|
||||
assertFalse(def.isEssential());
|
||||
|
||||
((ObjectNode) props.get("ContainerDefinitions").get(0)).put("Memory", "abc");
|
||||
AwsException e = assertThrows(AwsException.class,
|
||||
() -> provisioner.provision(resource("AWS::ECS::TaskDefinition", "TaskDef"), props, ctx()));
|
||||
assertEquals("Model validation failed (#/ContainerDefinitions/0/Memory: expected type: Integer, found: String."
|
||||
+ " This may occur when a numeric value exceeds 32-bit integer max (2147483647). Consider using"
|
||||
+ " 'number' type or ensure value fits in integer range.\n#/ContainerDefinitions/0/Cpu: expected"
|
||||
+ " type: Integer, found: String. This may occur when a numeric value exceeds 32-bit integer max"
|
||||
+ " (2147483647). Consider using 'number' type or ensure value fits in integer range.\n"
|
||||
+ "#/ContainerDefinitions/0/Essential: expected type: Boolean, found: String)", e.getMessage());
|
||||
verifyNoMoreInteractions(ecs);
|
||||
}
|
||||
|
||||
@Test
|
||||
void taskDefinitionWithoutFamilyGeneratesOneAndIgnoresAnUnknownNetworkMode() {
|
||||
TaskDefinition td = new TaskDefinition();
|
||||
|
||||
Reference in new issue
Block a user