mirror of
https://github.com/floci-io/floci.git
synced 2026-12-01 01:12:48 -05:00
feat(cloudformation): support SAM's optional AutoPublishAlias version properties (#5823)
* feat(cloudformation): support SAM's optional AutoPublishAlias version properties With AutoPublishAlias, read the three function properties SAM uses to shape the version it publishes: - AutoPublishCodeSha256: a string names the version by its first ten characters instead of the hash, so new code under the same value publishes nothing. A Ref to a parameter resolves first. - AutoPublishAliasAllProperties: the hash covers every property the transform gives the function, plus its SnapStart. - VersionDeletionPolicy: the version's DeletionPolicy when the function sets none; a function with a CapacityProviderConfig defaults to Delete, any other to Retain. An intrinsic value is copied unresolved, as SAM copies it. Each gets SAM's type checks and messages: AutoPublishCodeSha256 must be a string and make an alphanumeric id, AutoPublishAliasAllProperties a boolean, and VersionDeletionPolicy Delete or Retain, only with AutoPublishAlias. Signed-off-by: Jackson Price <59149601+jprice8@users.noreply.github.com> * test(cloudformation): pin the default Ref hash and AutoPublishAliasAllProperties coverage Keep the check that a Ref in the code changes the version id without AutoPublishAliasAllProperties, and show that with it a Timeout change names a new version and false hashes as if the property were absent. Pin that an AutoPublishCodeSha256 Ref resolving to an empty value leaves the hash in place. The docs say that ids under AutoPublishAliasAllProperties are floci's own and that a Ref to a parameter works for AutoPublishCodeSha256. Signed-off-by: Jackson Price <59149601+jprice8@users.noreply.github.com> * fix(cloudformation): refuse AutoPublishCodeSha256 in SAM Globals SAM refuses a Globals property it does not list for a section, and Globals.Function does not list AutoPublishCodeSha256. Now that the property names the version, merging it from Globals would give every function the same version id, so a code change would never publish. Refuse the template with SAM's message, without the list of supported properties that SAM appends. Signed-off-by: Jackson Price <59149601+jprice8@users.noreply.github.com> * test(cloudformation): pin the AutoPublishAliasAllProperties ids and SAM version updates Pin two version ids under AutoPublishAliasAllProperties to the SHA-1 of the generated function's properties, worked out by hand, and that a given AutoPublishCodeSha256 still names the version there. Show that Globals.Function carries AutoPublishAliasAllProperties and VersionDeletionPolicy. On a stack, a Timeout change under AutoPublishAliasAllProperties publishes version 2 with the new timeout, and VersionDeletionPolicy: Delete reports the replaced version DELETE_COMPLETE instead of DELETE_SKIPPED. An intrinsic VersionDeletionPolicy is copied unresolved, which the assertion now says. Signed-off-by: Jackson Price <59149601+jprice8@users.noreply.github.com> * docs(cloudformation): say when a new SAM version id publishes Say which statements about the version id hold by default, that AutoPublishCodeSha256 outranks AutoPublishAliasAllProperties and cannot come from Globals, and when a new id publishes nothing: SnapStart, a new AutoPublishCodeSha256 alone, or, under AutoPublishAliasAllProperties, a Tags or ReservedConcurrentExecutions change, which AWS fails with "A version for this Lambda function exists". A hot-reloaded function publishes for every new id. State the DeletionPolicy order, and that CloudFormation takes intrinsic functions only in resource properties, outputs, metadata and update policies. The javadocs of the version id and of AutoPublishAlias say the same. Signed-off-by: Jackson Price <59149601+jprice8@users.noreply.github.com> * docs(cloudformation): note IgnoreGlobals and LanguageExtensions for SAM versions Say that floci does not honour IgnoreGlobals, so a function cannot opt out of a VersionDeletionPolicy in Globals, and that an intrinsic VersionDeletionPolicy reaches CloudFormation only through a transform such as AWS::LanguageExtensions, which floci does not implement. Under AutoPublishAliasAllProperties the code's parameter references are still resolved, and any SnapStart change publishes nothing. Signed-off-by: Jackson Price <59149601+jprice8@users.noreply.github.com> * test(cloudformation): pin the SnapStart id under AutoPublishAliasAllProperties Pin the id a SnapStart with ApplyOn None gives under AutoPublishAliasAllProperties, worked out by hand like the others, and show that a version VersionDeletionPolicy: Delete reports deleted is still listed, as the docs say floci keeps every version it publishes. Signed-off-by: Jackson Price <59149601+jprice8@users.noreply.github.com> * docs(cloudformation): say that IgnoreGlobals is the opt-out floci ignores A function can still clear a Globals VersionDeletionPolicy with a null of its own; what floci does not honour is IgnoreGlobals. Signed-off-by: Jackson Price <59149601+jprice8@users.noreply.github.com> --------- Signed-off-by: Jackson Price <59149601+jprice8@users.noreply.github.com>
This commit is contained in:
1 parent
c33823bd48
commit
f22c2a4db1
4 files changed
+527
-41
No files matched your search
+134
@@ -265,6 +265,140 @@ class SamTransformIntegrationTest {
|
||||
.body("release", equalTo("second"));
|
||||
}
|
||||
|
||||
@Test
|
||||
void samAutoPublishCodeSha256DecidesWhenANewVersionPublishes() {
|
||||
String stackName = "sam-code-sha-stack";
|
||||
String function = "sam-code-sha-func";
|
||||
stacksToDelete.add(stackName);
|
||||
String template = """
|
||||
Transform: AWS::Serverless-2016-10-31
|
||||
Resources:
|
||||
Function:
|
||||
Type: AWS::Serverless::Function
|
||||
Properties:
|
||||
FunctionName: sam-code-sha-func
|
||||
Handler: index.handler
|
||||
Runtime: nodejs20.x
|
||||
AutoPublishAlias: live
|
||||
AutoPublishCodeSha256: "%s"
|
||||
InlineCode: "exports.handler = async () => ({ release: '%s' });"
|
||||
""";
|
||||
given().contentType("application/x-www-form-urlencoded")
|
||||
.formParam("Action", "CreateStack").formParam("StackName", stackName)
|
||||
.formParam("TemplateBody", template.formatted("aaaaaaaaaa", "first"))
|
||||
.formParam("Capabilities.member.1", "CAPABILITY_IAM")
|
||||
.when().post("/").then().statusCode(200);
|
||||
waitForStackStatus(stackName, "CREATE_COMPLETE");
|
||||
|
||||
// As in SAM, the given hash names the version, so new code under the same value is not published.
|
||||
given().contentType("application/x-www-form-urlencoded")
|
||||
.formParam("Action", "UpdateStack").formParam("StackName", stackName)
|
||||
.formParam("TemplateBody", template.formatted("aaaaaaaaaa", "second"))
|
||||
.formParam("Capabilities.member.1", "CAPABILITY_IAM")
|
||||
.when().post("/").then().statusCode(200);
|
||||
waitForStackStatus(stackName, "UPDATE_COMPLETE");
|
||||
given().when().get("/2015-03-31/functions/" + function + "/aliases/live")
|
||||
.then().statusCode(200).body("FunctionVersion", equalTo("1"));
|
||||
|
||||
given().contentType("application/x-www-form-urlencoded")
|
||||
.formParam("Action", "UpdateStack").formParam("StackName", stackName)
|
||||
.formParam("TemplateBody", template.formatted("bbbbbbbbbb", "second"))
|
||||
.formParam("Capabilities.member.1", "CAPABILITY_IAM")
|
||||
.when().post("/").then().statusCode(200);
|
||||
waitForStackStatus(stackName, "UPDATE_COMPLETE");
|
||||
given().when().get("/2015-03-31/functions/" + function + "/aliases/live")
|
||||
.then().statusCode(200).body("FunctionVersion", equalTo("2"));
|
||||
given().when().get("/2015-03-31/functions/" + function + "/versions")
|
||||
.then().statusCode(200).body("Versions.Version", equalTo(List.of("$LATEST", "1", "2")));
|
||||
}
|
||||
|
||||
/** Under AutoPublishAliasAllProperties a property outside the default hash, here Timeout, publishes. */
|
||||
@Test
|
||||
void samAutoPublishAliasAllPropertiesPublishesATimeoutChange() {
|
||||
String stackName = "sam-all-properties-stack";
|
||||
String function = "sam-all-properties-func";
|
||||
stacksToDelete.add(stackName);
|
||||
String template = """
|
||||
Transform: AWS::Serverless-2016-10-31
|
||||
Resources:
|
||||
Function:
|
||||
Type: AWS::Serverless::Function
|
||||
Properties:
|
||||
FunctionName: sam-all-properties-func
|
||||
Handler: index.handler
|
||||
Runtime: nodejs20.x
|
||||
Timeout: %d
|
||||
AutoPublishAlias: live
|
||||
AutoPublishAliasAllProperties: true
|
||||
InlineCode: "exports.handler = async () => ({});"
|
||||
""";
|
||||
given().contentType("application/x-www-form-urlencoded")
|
||||
.formParam("Action", "CreateStack").formParam("StackName", stackName)
|
||||
.formParam("TemplateBody", template.formatted(3)).formParam("Capabilities.member.1", "CAPABILITY_IAM")
|
||||
.when().post("/").then().statusCode(200);
|
||||
waitForStackStatus(stackName, "CREATE_COMPLETE");
|
||||
|
||||
given().contentType("application/x-www-form-urlencoded")
|
||||
.formParam("Action", "UpdateStack").formParam("StackName", stackName)
|
||||
.formParam("TemplateBody", template.formatted(9)).formParam("Capabilities.member.1", "CAPABILITY_IAM")
|
||||
.when().post("/").then().statusCode(200);
|
||||
waitForStackStatus(stackName, "UPDATE_COMPLETE");
|
||||
|
||||
given().when().get("/2015-03-31/functions/" + function + "/aliases/live")
|
||||
.then().statusCode(200).body("FunctionVersion", equalTo("2"));
|
||||
given().queryParam("Qualifier", "live")
|
||||
.when().get("/2015-03-31/functions/" + function + "/configuration")
|
||||
.then().statusCode(200).body("Version", equalTo("2")).body("Timeout", equalTo(9));
|
||||
}
|
||||
|
||||
/** With VersionDeletionPolicy: Delete, the version a code change replaces is reported deleted. */
|
||||
@Test
|
||||
void samVersionDeletionPolicyDeleteAppliesToTheReplacedVersion() {
|
||||
String stackName = "sam-version-delete-stack";
|
||||
String function = "sam-version-delete-func";
|
||||
stacksToDelete.add(stackName);
|
||||
String template = """
|
||||
Transform: AWS::Serverless-2016-10-31
|
||||
Resources:
|
||||
Function:
|
||||
Type: AWS::Serverless::Function
|
||||
Properties:
|
||||
FunctionName: sam-version-delete-func
|
||||
Handler: index.handler
|
||||
Runtime: nodejs20.x
|
||||
AutoPublishAlias: live
|
||||
VersionDeletionPolicy: Delete
|
||||
InlineCode: "exports.handler = async () => ({ release: '%s' });"
|
||||
""";
|
||||
given().contentType("application/x-www-form-urlencoded")
|
||||
.formParam("Action", "CreateStack").formParam("StackName", stackName)
|
||||
.formParam("TemplateBody", template.formatted("first")).formParam("Capabilities.member.1", "CAPABILITY_IAM")
|
||||
.when().post("/").then().statusCode(200);
|
||||
waitForStackStatus(stackName, "CREATE_COMPLETE");
|
||||
|
||||
given().contentType("application/x-www-form-urlencoded")
|
||||
.formParam("Action", "UpdateStack").formParam("StackName", stackName)
|
||||
.formParam("TemplateBody", template.formatted("second")).formParam("Capabilities.member.1", "CAPABILITY_IAM")
|
||||
.when().post("/").then().statusCode(200);
|
||||
waitForStackStatus(stackName, "UPDATE_COMPLETE");
|
||||
|
||||
given().when().get("/2015-03-31/functions/" + function + "/aliases/live")
|
||||
.then().statusCode(200).body("FunctionVersion", equalTo("2"));
|
||||
String events = given().contentType("application/x-www-form-urlencoded")
|
||||
.formParam("Action", "DescribeStackEvents").formParam("StackName", stackName)
|
||||
.when().post("/").then().statusCode(200).extract().asString();
|
||||
// FunctionVersion6a52d0f87f is the id SAM gives the first InlineCode.
|
||||
List<String> oldVersionStatuses = XmlParser.extractGroups(events, "member").stream()
|
||||
.filter(event -> "FunctionVersion6a52d0f87f".equals(event.get("LogicalResourceId")))
|
||||
.map(event -> event.get("ResourceStatus"))
|
||||
.toList();
|
||||
assertThat(oldVersionStatuses, hasItem("DELETE_COMPLETE"));
|
||||
assertThat(oldVersionStatuses, not(hasItem("DELETE_SKIPPED")));
|
||||
// floci reports the version deleted but keeps every version it publishes; AWS deletes it.
|
||||
given().when().get("/2015-03-31/functions/" + function + "/versions")
|
||||
.then().statusCode(200).body("Versions.Version", equalTo(List.of("$LATEST", "1", "2")));
|
||||
}
|
||||
|
||||
/**
|
||||
* A code change names a new version resource: the change set adds it, modifies the alias and
|
||||
* removes the old version, which, retained as SAM retains it, stays published.
|
||||
|
||||
+228
-8
@@ -181,7 +181,8 @@ class SamTransformProcessorTest {
|
||||
|
||||
/**
|
||||
* SAM also hashes a function's SnapStart when its ApplyOn is anything but "None", so turning
|
||||
* SnapStart on names a new version; absent, empty or "None" leaves the id unchanged.
|
||||
* SnapStart on names a new version; absent, empty or "None" leaves the id unchanged. With
|
||||
* AutoPublishAliasAllProperties it is hashed like every other property.
|
||||
*/
|
||||
@Test
|
||||
void expandSamTemplate_autoPublishAliasVersionIdIncludesAppliedSnapStart() throws Exception {
|
||||
@@ -210,6 +211,20 @@ class SamTransformProcessorTest {
|
||||
for (String off : List.of("", ", \"SnapStart\": {\"ApplyOn\": \"None\"}", ", \"SnapStart\": {}")) {
|
||||
assertEquals("MyFuncVersion102360dfc3", publishedVersionId(template.formatted(off)), off);
|
||||
}
|
||||
String allProperties = ", \"AutoPublishAliasAllProperties\": true";
|
||||
// The SHA-1 of the properties the transform gives the function, as sorted compact JSON:
|
||||
// {"Code":{"ZipFile":"..."},"Handler":"index.handler","Role":{"Fn::GetAtt":["MyFuncRole","Arn"]},
|
||||
// "Runtime":"java21"}
|
||||
assertEquals("MyFuncVersionef5795a086", publishedVersionId(template.formatted(allProperties)));
|
||||
assertEquals("MyFuncVersion60f02511cd",
|
||||
publishedVersionId(template.formatted(allProperties + ", \"Timeout\": 9")),
|
||||
"every property is hashed, Timeout included");
|
||||
assertEquals("MyFuncVersion6c94e3ab8d",
|
||||
publishedVersionId(template.formatted(allProperties + ", \"SnapStart\": {\"ApplyOn\": \"None\"}")),
|
||||
"SnapStart is hashed whatever its ApplyOn");
|
||||
assertEquals("MyFuncVersion102360dfc3",
|
||||
publishedVersionId(template.formatted(", \"AutoPublishAliasAllProperties\": false")),
|
||||
"false hashes as if the property were absent");
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -268,11 +283,197 @@ class SamTransformProcessorTest {
|
||||
}
|
||||
|
||||
/**
|
||||
* SAM retains each version it publishes unless the function sets a {@code DeletionPolicy}, which
|
||||
* the version and alias both take.
|
||||
* A given {@code AutoPublishCodeSha256} names the version: SAM takes its first ten characters
|
||||
* and hashes nothing else, so the code alone no longer names the version.
|
||||
*/
|
||||
@Test
|
||||
void expandSamTemplate_autoPublishAliasVersionIsRetainedUnlessTheFunctionSaysOtherwise() throws Exception {
|
||||
void expandSamTemplate_autoPublishCodeSha256TakesTheHashesPlace() throws Exception {
|
||||
String template = """
|
||||
{
|
||||
"Transform": "AWS::Serverless-2016-10-31",
|
||||
"Resources": {
|
||||
"MyFunc": {
|
||||
"Type": "AWS::Serverless::Function",
|
||||
"Properties": {
|
||||
"Handler": "index.handler", "Runtime": "nodejs20.x", "AutoPublishAlias": "live",
|
||||
"InlineCode": "%s", "AutoPublishCodeSha256": "%s"
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
""";
|
||||
String code = "exports.handler = async () => ({});";
|
||||
String sha = "6b86b273ff34fce19d6b804eff5a3f5747ada4eaa22f1d49c01e52ddb7875b4b";
|
||||
|
||||
assertEquals("MyFuncVersion6b86b273ff", publishedVersionId(template.formatted(code, sha)));
|
||||
assertEquals("MyFuncVersion6b86b273ff",
|
||||
publishedVersionId(template.formatted("exports.handler = async () => ({ changed: true });", sha)),
|
||||
"a code change under the same AutoPublishCodeSha256 keeps the version");
|
||||
assertEquals("MyFuncVersionabc", publishedVersionId(template.formatted(code, "abc")));
|
||||
assertEquals("MyFuncVersion102360dfc3", publishedVersionId(template.formatted(code, "")),
|
||||
"an empty AutoPublishCodeSha256 gives no hash, so the code is hashed");
|
||||
}
|
||||
|
||||
/**
|
||||
* SAM refuses an AutoPublishCodeSha256 that is not a string: on any function when it is neither a
|
||||
* string nor a one-key object, and with AutoPublishAlias when it does not resolve to a string.
|
||||
*/
|
||||
@Test
|
||||
void expandSamTemplate_autoPublishCodeSha256MustBeAString() throws Exception {
|
||||
String template = """
|
||||
{
|
||||
"Transform": "AWS::Serverless-2016-10-31",
|
||||
"Resources": {
|
||||
"Bucket": {"Type": "AWS::S3::Bucket"},
|
||||
"MyFunc": {
|
||||
"Type": "AWS::Serverless::Function",
|
||||
"Properties": {
|
||||
"Handler": "index.handler", "Runtime": "nodejs20.x",%s
|
||||
"InlineCode": "exports.handler = async () => ({});", "AutoPublishCodeSha256": %s
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
""";
|
||||
String alias = " \"AutoPublishAlias\": \"live\",";
|
||||
|
||||
AwsException number = assertThrows(AwsException.class,
|
||||
() -> processor.expandSamTemplate(objectMapper.readTree(template.formatted("", "12"))));
|
||||
assertEquals("ValidationError", number.getErrorCode());
|
||||
assertEquals("Resource with id [MyFunc] is invalid. Type of property 'AutoPublishCodeSha256' is invalid.",
|
||||
number.getMessage(), "checked whether or not the function publishes a version");
|
||||
assertEquals("Resource with id [MyFunc] is invalid. Type of property 'AutoPublishCodeSha256' is invalid.",
|
||||
assertThrows(AwsException.class, () -> publishedVersionId(template.formatted(alias,
|
||||
"{\"Fn::Sub\": \"a\", \"Fn::Join\": [\"\", []]}"))).getMessage());
|
||||
for (String unresolved : List.of("{\"Fn::GetAtt\": [\"Bucket\", \"Arn\"]}", "{\"Sha\": \"abc\"}")) {
|
||||
assertEquals("Resource with id [MyFunc] is invalid. AutoPublishCodeSha256 must be a string",
|
||||
assertThrows(AwsException.class, () -> publishedVersionId(template.formatted(alias, unresolved)))
|
||||
.getMessage(), unresolved);
|
||||
}
|
||||
assertEquals("Resource with id [MyFuncVersion1.2.3-rc] is invalid. Logical ids must be alphanumeric.",
|
||||
assertThrows(AwsException.class, () -> publishedVersionId(template.formatted(alias, "\"1.2.3-rc\"")))
|
||||
.getMessage());
|
||||
assertEquals("MyFuncVersionabcdefghij", publishedVersionId(template.formatted(alias, "\"abcdefghij-x\"")),
|
||||
"only the first ten characters name the version");
|
||||
}
|
||||
|
||||
/** SAM's types for the other version properties, and VersionDeletionPolicy needs AutoPublishAlias. */
|
||||
@Test
|
||||
void expandSamTemplate_versionPropertiesHaveSamsTypes() throws Exception {
|
||||
String template = """
|
||||
{
|
||||
"Transform": "AWS::Serverless-2016-10-31",
|
||||
"Parameters": {"Policy": {"Type": "String", "Default": "Retain"}},
|
||||
"Resources": {
|
||||
"MyFunc": {
|
||||
"Type": "AWS::Serverless::Function",
|
||||
"Properties": {
|
||||
"Handler": "index.handler", "Runtime": "nodejs20.x",
|
||||
"InlineCode": "exports.handler = async () => ({});"%s
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
""";
|
||||
String alias = ", \"AutoPublishAlias\": \"live\"";
|
||||
|
||||
for (String allProperties : List.of("\"true\"", "1", "{\"Ref\": \"HashAll\"}")) {
|
||||
assertEquals("Resource with id [MyFunc] is invalid. Type of property 'AutoPublishAliasAllProperties' "
|
||||
+ "is invalid.",
|
||||
assertThrows(AwsException.class, () -> processor.expandSamTemplate(objectMapper.readTree(
|
||||
template.formatted(alias + ", \"AutoPublishAliasAllProperties\": " + allProperties))))
|
||||
.getMessage(), allProperties);
|
||||
}
|
||||
assertEquals("Resource with id [MyFunc] is invalid. Property 'VersionDeletionPolicy' should be a one of the "
|
||||
+ "strings: 'Delete' or 'Retain'.",
|
||||
assertThrows(AwsException.class, () -> processor.expandSamTemplate(objectMapper.readTree(
|
||||
template.formatted(alias + ", \"VersionDeletionPolicy\": \"retain\"")))).getMessage());
|
||||
assertEquals("Resource with id [MyFunc] is invalid. 'VersionDeletionPolicy' requires 'AutoPublishAlias'.",
|
||||
assertThrows(AwsException.class, () -> processor.expandSamTemplate(objectMapper.readTree(
|
||||
template.formatted(", \"VersionDeletionPolicy\": \"Retain\"")))).getMessage());
|
||||
assertEquals("{\"Ref\":\"Policy\"}", processor.expandSamTemplate(objectMapper.readTree(
|
||||
template.formatted(alias + ", \"VersionDeletionPolicy\": {\"Ref\": \"Policy\"}")))
|
||||
.path("Resources").path("MyFuncVersion102360dfc3").path("DeletionPolicy").toString(),
|
||||
"an intrinsic function is copied unresolved, as SAM copies it");
|
||||
}
|
||||
|
||||
/** As in SAM, a given AutoPublishCodeSha256 names the version under AutoPublishAliasAllProperties too. */
|
||||
@Test
|
||||
void expandSamTemplate_autoPublishCodeSha256OutranksAutoPublishAliasAllProperties() throws Exception {
|
||||
String template = """
|
||||
{
|
||||
"Transform": "AWS::Serverless-2016-10-31",
|
||||
"Resources": {
|
||||
"MyFunc": {
|
||||
"Type": "AWS::Serverless::Function",
|
||||
"Properties": {
|
||||
"Handler": "index.handler", "Runtime": "nodejs20.x", "AutoPublishAlias": "live",
|
||||
"AutoPublishAliasAllProperties": true, "AutoPublishCodeSha256": "abc123",
|
||||
"InlineCode": "exports.handler = async () => ({});", "Timeout": %d
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
""";
|
||||
|
||||
assertEquals("MyFuncVersionabc123", publishedVersionId(template.formatted(3)));
|
||||
assertEquals("MyFuncVersionabc123", publishedVersionId(template.formatted(9)));
|
||||
}
|
||||
|
||||
/** Globals.Function carries AutoPublishAliasAllProperties and VersionDeletionPolicy, as SAM's do. */
|
||||
@Test
|
||||
void expandSamTemplate_versionPropertiesComeFromGlobals() throws Exception {
|
||||
JsonNode resources = processor.expandSamTemplate(objectMapper.readTree("""
|
||||
{
|
||||
"Transform": "AWS::Serverless-2016-10-31",
|
||||
"Globals": {"Function": {"AutoPublishAlias": "live", "AutoPublishAliasAllProperties": true,
|
||||
"VersionDeletionPolicy": "Delete"}},
|
||||
"Resources": {
|
||||
"MyFunc": {
|
||||
"Type": "AWS::Serverless::Function",
|
||||
"Properties": {"Handler": "index.handler", "Runtime": "nodejs20.x",
|
||||
"InlineCode": "exports.handler = async () => ({});"}
|
||||
}
|
||||
}
|
||||
}
|
||||
""")).path("Resources");
|
||||
|
||||
// Without AutoPublishAliasAllProperties the id is MyFuncVersion102360dfc3.
|
||||
assertEquals("[\"MyFuncVersionb6788accf2\",\"Version\"]", resources.path("MyFuncAliasLive")
|
||||
.path("Properties").path("FunctionVersion").path("Fn::GetAtt").toString());
|
||||
assertEquals("Delete", resources.path("MyFuncVersionb6788accf2").path("DeletionPolicy").asText());
|
||||
}
|
||||
|
||||
/** SAM's Globals do not take AutoPublishCodeSha256, which would name every function's version. */
|
||||
@Test
|
||||
void expandSamTemplate_globalsRefuseAutoPublishCodeSha256() throws Exception {
|
||||
JsonNode template = objectMapper.readTree("""
|
||||
{
|
||||
"Transform": "AWS::Serverless-2016-10-31",
|
||||
"Globals": {"Function": {"AutoPublishAlias": "live", "AutoPublishCodeSha256": "release1234"}},
|
||||
"Resources": {
|
||||
"MyFunc": {
|
||||
"Type": "AWS::Serverless::Function",
|
||||
"Properties": {"Handler": "index.handler", "Runtime": "nodejs20.x",
|
||||
"InlineCode": "exports.handler = async () => ({});"}
|
||||
}
|
||||
}
|
||||
}
|
||||
""");
|
||||
|
||||
AwsException refused = assertThrows(AwsException.class, () -> processor.expandSamTemplate(template));
|
||||
assertEquals("ValidationError", refused.getErrorCode());
|
||||
assertEquals("'Globals' section is invalid. 'AutoPublishCodeSha256' is not a supported property of "
|
||||
+ "'Function'.", refused.getMessage());
|
||||
}
|
||||
|
||||
/**
|
||||
* SAM retains each version it publishes unless the function sets a {@code DeletionPolicy}, which
|
||||
* the version and alias both take, or a {@code VersionDeletionPolicy}; a function on a capacity
|
||||
* provider defaults to {@code Delete}.
|
||||
*/
|
||||
@Test
|
||||
void expandSamTemplate_autoPublishAliasVersionDeletionPolicyFollowsSam() throws Exception {
|
||||
String template = """
|
||||
{
|
||||
"Transform": "AWS::Serverless-2016-10-31",
|
||||
@@ -293,7 +494,16 @@ class SamTransformProcessorTest {
|
||||
assertEquals("Retain", retained.path("MyFuncVersion102360dfc3").path("DeletionPolicy").asText());
|
||||
assertTrue(retained.path("MyFuncAliasLive").path("DeletionPolicy").isMissingNode(),
|
||||
"the alias takes only what the function declares");
|
||||
assertEquals("Delete", versionDeletionPolicy(template.formatted(" \"DeletionPolicy\": \"Delete\",", "")));
|
||||
assertEquals("Delete", versionDeletionPolicy(template.formatted(
|
||||
" \"DeletionPolicy\": \"Delete\",", ", \"VersionDeletionPolicy\": \"Retain\"")));
|
||||
assertEquals("Delete", versionDeletionPolicy(template.formatted(
|
||||
"", ", \"VersionDeletionPolicy\": \"Delete\"")));
|
||||
assertEquals("Retain", versionDeletionPolicy(template.formatted(
|
||||
"", ", \"VersionDeletionPolicy\": \"Retain\"")));
|
||||
assertEquals("Delete", versionDeletionPolicy(template.formatted(
|
||||
"", ", \"CapacityProviderConfig\": {\"Arn\": \"capacity-provider-arn\"}")));
|
||||
assertEquals("Retain", versionDeletionPolicy(template.formatted("", ", \"VersionDeletionPolicy\": \"Retain\", "
|
||||
+ "\"CapacityProviderConfig\": {\"Arn\": \"capacity-provider-arn\"}")));
|
||||
}
|
||||
|
||||
private String versionDeletionPolicy(String template) throws Exception {
|
||||
@@ -332,9 +542,10 @@ class SamTransformProcessorTest {
|
||||
}
|
||||
|
||||
/**
|
||||
* A parameter reaches the hash the way SAM resolves it: as a {@code Ref}, and as a
|
||||
* {@code ${Name}} in an {@code Fn::Sub}. Otherwise a parameter-only update changes the function
|
||||
* but publishes no version, and the alias keeps the old code.
|
||||
* A parameter reaches the hash the way SAM resolves it: as a {@code Ref}, as a {@code ${Name}}
|
||||
* in an {@code Fn::Sub}, under {@code AutoPublishAliasAllProperties} too, and in
|
||||
* {@code AutoPublishCodeSha256}. Otherwise a parameter-only update changes the function but
|
||||
* publishes no version, and the alias keeps the old code.
|
||||
*/
|
||||
@Test
|
||||
void expandSamTemplate_autoPublishAliasVersionIdFollowsParametersInEveryForm() throws Exception {
|
||||
@@ -363,6 +574,15 @@ class SamTransformProcessorTest {
|
||||
publishedVersionId(template.formatted(sub, ""), Map.of("Release", "2")));
|
||||
assertNotEquals(publishedVersionId(template.formatted(ref, ""), Map.of("Release", "1")),
|
||||
publishedVersionId(template.formatted(ref, ""), Map.of("Release", "2")));
|
||||
String allProperties = ", \"AutoPublishAliasAllProperties\": true";
|
||||
assertNotEquals(publishedVersionId(template.formatted(ref, allProperties), Map.of("Release", "1")),
|
||||
publishedVersionId(template.formatted(ref, allProperties), Map.of("Release", "2")));
|
||||
String codeSha = ", \"AutoPublishCodeSha256\": {\"Ref\": \"Release\"}";
|
||||
assertEquals("MyFuncVersion2",
|
||||
publishedVersionId(template.formatted("\"app.zip\"", codeSha), Map.of("Release", "2")));
|
||||
assertEquals(publishedVersionId(template.formatted("\"app.zip\"", ""), Map.of()),
|
||||
publishedVersionId(template.formatted("\"app.zip\"", codeSha), Map.of("Release", "")),
|
||||
"a Ref resolving to an empty value leaves the hash in place");
|
||||
}
|
||||
|
||||
private String publishedVersionId(String template) throws Exception {
|
||||
|
||||
Reference in new issue
Block a user