test(cloudformation): share stack resource physical ID lookup (#7641)

This commit is contained in:
Jimmy Liu Jia authored and GitHub committed 2026-11-05 16:49:53 -04:00
1 parent bcbd7c8d9d
commit cf96dadfd9
5 files changed
+33 -58

No files matched your search

@@ -35,7 +35,7 @@ class CloudFormationKmsKeyPropertiesIntegrationTest {
"""));
CfnStackWaits.awaitStatus(stack, "CREATE_COMPLETE", EnforcementFixtures.auth(account, "cloudformation"));
String keyId = physicalId(account, stack);
String keyId = CfnRequests.physicalId(stack, KEY_RESOURCE, EnforcementFixtures.auth(account, "cloudformation"));
JsonPath key = kms(account, "DescribeKey", "{\"KeyId\":\"%s\"}".formatted(keyId));
assertAll(
() -> assertEquals(keyId, key.getString("KeyMetadata.KeyId")),
@@ -50,7 +50,7 @@ class CloudFormationKmsKeyPropertiesIntegrationTest {
createStack(account, stack, template(""));
CfnStackWaits.awaitStatus(stack, "CREATE_COMPLETE", EnforcementFixtures.auth(account, "cloudformation"));
String keyId = physicalId(account, stack);
String keyId = CfnRequests.physicalId(stack, KEY_RESOURCE, EnforcementFixtures.auth(account, "cloudformation"));
JsonPath key = kms(account, "DescribeKey", "{\"KeyId\":\"%s\"}".formatted(keyId));
assertAll(
@@ -85,17 +85,6 @@ class CloudFormationKmsKeyPropertiesIntegrationTest {
.statusCode(200);
}
private static String physicalId(String account, String stack) {
return CfnRequests.cfn("DescribeStackResource", stack)
.header("Authorization", EnforcementFixtures.auth(account, "cloudformation"))
.formParam("LogicalResourceId", KEY_RESOURCE)
.when()
.post("/")
.then()
.statusCode(200)
.extract().xmlPath().getString("**.find { it.name() == 'PhysicalResourceId' }");
}
private static JsonPath kms(String account, String action, String body) {
return given()
.header("Authorization", EnforcementFixtures.auth(account, "kms"))
@@ -1,5 +1,6 @@
package io.github.hectorvent.floci.services.cloudformation;
import io.github.hectorvent.floci.testing.CfnRequests;
import io.github.hectorvent.floci.testing.CfnStackWaits;
import io.github.hectorvent.floci.testing.PartitionCleanup;
import io.quarkus.test.junit.QuarkusTest;
@@ -316,17 +317,7 @@ class CloudFormationLambdaObjectPropertyIntegrationTest {
""".formatted(bucket, key));
assertEquals("String".equals(type) ? "ROLLBACK_COMPLETE" : "CREATE_COMPLETE",
CfnStackWaits.awaitTerminal(parent, CFN_AUTH).status(), type);
String childStack = given()
.contentType("application/x-www-form-urlencoded")
.header("Authorization", CFN_AUTH)
.formParam("Action", "DescribeStackResource")
.formParam("StackName", parent)
.formParam("LogicalResourceId", "Child")
.when()
.post("/")
.then()
.statusCode(200)
.extract().xmlPath().getString("**.find { it.name() == 'PhysicalResourceId' }");
String childStack = CfnRequests.physicalId(parent, "Child", CFN_AUTH);
if ("String".equals(type)) {
assertEventReason(childStack, "Property [Architectures] expected type: JSONArray, found: String");
} else {
@@ -3,6 +3,7 @@ package io.github.hectorvent.floci.services.cloudformation;
import io.github.hectorvent.floci.core.common.AwsException;
import io.github.hectorvent.floci.services.route53.Route53HostedZoneTestSupport;
import io.github.hectorvent.floci.services.route53.Route53Service;
import io.github.hectorvent.floci.testing.CfnRequests;
import io.github.hectorvent.floci.testing.CfnStackWaits;
import io.github.hectorvent.floci.testing.RestAssuredJsonUtils;
import io.quarkus.test.junit.QuarkusTest;
@@ -398,12 +399,12 @@ class CloudFormationRoute53RecordSetIntegrationTest {
String stackName = "r53-hc-replace-" + Long.toString(System.nanoTime(), 36);
createStack(stackName, healthCheckTemplate(30, false, false));
assertEquals("CREATE_COMPLETE", CfnStackWaits.awaitTerminal(stackName).status());
String oldId = physicalId(stackName, "Check");
String oldId = CfnRequests.physicalId(stackName, "Check", CFN_AUTH);
try {
updateStack(stackName, healthCheckTemplate(10, false, false));
assertEquals("UPDATE_COMPLETE", CfnStackWaits.awaitTerminal(stackName).status());
String newId = physicalId(stackName, "Check");
String newId = CfnRequests.physicalId(stackName, "Check", CFN_AUTH);
assertNotEquals(oldId, newId);
assertTrue(healthCheck(newId).contains("<RequestInterval>10</RequestInterval>"));
healthCheckRequest(oldId).then().statusCode(404);
@@ -418,12 +419,12 @@ class CloudFormationRoute53RecordSetIntegrationTest {
String stackName = "r53-hc-retain-" + Long.toString(System.nanoTime(), 36);
createStack(stackName, healthCheckTemplate(30, true, false));
assertEquals("CREATE_COMPLETE", CfnStackWaits.awaitTerminal(stackName).status());
String oldId = physicalId(stackName, "Check");
String oldId = CfnRequests.physicalId(stackName, "Check", CFN_AUTH);
try {
updateStack(stackName, healthCheckTemplate(10, true, false));
assertEquals("UPDATE_COMPLETE", CfnStackWaits.awaitTerminal(stackName).status());
assertNotEquals(oldId, physicalId(stackName, "Check"));
assertNotEquals(oldId, CfnRequests.physicalId(stackName, "Check", CFN_AUTH));
assertTrue(healthCheck(oldId).contains("<RequestInterval>30</RequestInterval>"));
} finally {
deleteStack(stackName);
@@ -438,12 +439,12 @@ class CloudFormationRoute53RecordSetIntegrationTest {
String stackName = "r53-hc-rollback-" + Long.toString(System.nanoTime(), 36);
createStack(stackName, healthCheckTemplate(30, false, false));
assertEquals("CREATE_COMPLETE", CfnStackWaits.awaitTerminal(stackName).status());
String oldId = physicalId(stackName, "Check");
String oldId = CfnRequests.physicalId(stackName, "Check", CFN_AUTH);
try {
updateStack(stackName, healthCheckTemplate(10, false, true));
assertEquals("UPDATE_ROLLBACK_COMPLETE", CfnStackWaits.awaitTerminal(stackName).status());
assertEquals(oldId, physicalId(stackName, "Check"));
assertEquals(oldId, CfnRequests.physicalId(stackName, "Check", CFN_AUTH));
assertTrue(healthCheck(oldId).contains("<RequestInterval>30</RequestInterval>"));
String checks = given().header("Authorization", ROUTE53_AUTH)
.when().get("/2013-04-01/healthcheck")
@@ -473,7 +474,7 @@ class CloudFormationRoute53RecordSetIntegrationTest {
// AWS documents Ref as the group's name, for example MyRecordSetGroup, and Fn::GetAtt Id as
// the ID GetChange takes.
assertEquals("Group", stackOutput(stackName, "GroupRef"));
assertEquals("Group", physicalId(stackName, "Group"));
assertEquals("Group", CfnRequests.physicalId(stackName, "Group", CFN_AUTH));
firstChange = stackOutput(stackName, "GroupChange");
assertChangeInSync(firstChange);
String created = listResourceRecordSets(zoneId);
@@ -540,7 +541,7 @@ class CloudFormationRoute53RecordSetIntegrationTest {
parameterizedStackRequest(stackName, "CreateStack", template, "BlueWeight", "70");
assertEquals("CREATE_COMPLETE", CfnStackWaits.awaitTerminal(stackName).status());
try {
String checkId = physicalId(stackName, "Check");
String checkId = CfnRequests.physicalId(stackName, "Check", CFN_AUTH);
String created = listResourceRecordSets(zoneId);
assertTrue(created.contains("<SetIdentifier>blue</SetIdentifier><Weight>70</Weight>"), created);
assertTrue(created.contains("<SetIdentifier>green</SetIdentifier><Weight>30</Weight>"), created);
@@ -1059,17 +1060,6 @@ class CloudFormationRoute53RecordSetIntegrationTest {
.when().post("/").then().statusCode(200);
}
private String physicalId(String stackName, String logicalId) {
return given()
.contentType("application/x-www-form-urlencoded")
.header("Authorization", CFN_AUTH)
.formParam("Action", "DescribeStackResource")
.formParam("StackName", stackName)
.formParam("LogicalResourceId", logicalId)
.when().post("/").then().statusCode(200)
.extract().xmlPath().getString("**.find { it.name() == 'PhysicalResourceId' }");
}
private Response healthCheckRequest(String id) {
return given().header("Authorization", ROUTE53_AUTH)
.when().get("/2013-04-01/healthcheck/" + id);
@@ -12,6 +12,7 @@ import org.junit.jupiter.api.Test;
import java.util.ArrayList;
import java.util.List;
import static io.github.hectorvent.floci.testing.CfnRequests.physicalIdUnauthenticated;
import static io.restassured.RestAssured.given;
import static org.hamcrest.Matchers.containsString;
import static org.hamcrest.Matchers.equalTo;
@@ -113,7 +114,7 @@ class CloudFormationSamEventInvokeConfigIntegrationTest {
.body("MaximumEventAgeInSeconds", equalTo(21600))
.body("DestinationConfig.OnFailure.Destination", equalTo(queueArn));
getRolePolicy(physicalId(name, "FnRole"), "FnEventInvokeConfigOnFailureSQSPolicy")
getRolePolicy(physicalIdUnauthenticated(name, "FnRole"), "FnEventInvokeConfigOnFailureSQSPolicy")
.body(containsString("sqs:SendMessage"))
.body(containsString(queueArn))
.body(not(containsString("Fn::GetAtt")));
@@ -177,13 +178,13 @@ class CloudFormationSamEventInvokeConfigIntegrationTest {
""".formatted(name));
CfnStackWaits.awaitStatus(name, "CREATE_COMPLETE");
String topicArn = physicalId(name, "FnEventInvokeConfigOnSuccessTopic");
String topicArn = physicalIdUnauthenticated(name, "FnEventInvokeConfigOnSuccessTopic");
given().queryParam("Qualifier", "$LATEST")
.when().get("/2019-09-25/functions/" + name + "/event-invoke-config")
.then()
.statusCode(200)
.body("DestinationConfig.OnSuccess.Destination", equalTo(topicArn));
getRolePolicy(physicalId(name, "FnRole"), "FnEventInvokeConfigOnSuccessSNSPolicy")
getRolePolicy(physicalIdUnauthenticated(name, "FnRole"), "FnEventInvokeConfigOnSuccessSNSPolicy")
.body(containsString("sns:publish"))
.body(containsString(topicArn));
}
@@ -300,7 +301,7 @@ class CloudFormationSamEventInvokeConfigIntegrationTest {
.statusCode(200);
CfnStackWaits.awaitStatus(name, "CREATE_COMPLETE");
String topicArn = physicalId(name, "FnEventInvokeConfigOnFailureTopic");
String topicArn = physicalIdUnauthenticated(name, "FnEventInvokeConfigOnFailureTopic");
given().queryParam("Qualifier", "$LATEST")
.when().get("/2019-09-25/functions/" + name + "/event-invoke-config")
.then()
@@ -345,7 +346,7 @@ class CloudFormationSamEventInvokeConfigIntegrationTest {
""".formatted(name, parameterName));
CfnStackWaits.awaitStatus(name, "CREATE_COMPLETE");
String topicArn = physicalId(name, "FnEventInvokeConfigOnFailureTopic");
String topicArn = physicalIdUnauthenticated(name, "FnEventInvokeConfigOnFailureTopic");
given().queryParam("Qualifier", "$LATEST")
.when().get("/2019-09-25/functions/" + name + "/event-invoke-config")
.then()
@@ -401,16 +402,6 @@ class CloudFormationSamEventInvokeConfigIntegrationTest {
.statusCode(200);
}
private static String physicalId(String stackName, String logicalId) {
XmlPath xml = given()
.contentType("application/x-www-form-urlencoded")
.formParam("Action", "DescribeStackResource")
.formParam("StackName", stackName)
.formParam("LogicalResourceId", logicalId)
.when().post("/").then().statusCode(200).extract().xmlPath();
return xml.getString("**.find { it.name() == 'PhysicalResourceId' }");
}
private static ValidatableResponse getRolePolicy(String roleName, String policyName) {
return given()
.contentType("application/x-www-form-urlencoded")
@@ -17,6 +17,20 @@ public final class CfnRequests {
return stackName == null ? request : request.formParam("StackName", stackName);
}
public static String physicalIdUnauthenticated(String stackName, String logicalId) {
return physicalId(stackName, logicalId, null);
}
public static String physicalId(String stackName, String logicalId, String authorization) {
RequestSpecification request = cfn("DescribeStackResource", stackName)
.formParam("LogicalResourceId", logicalId);
if (authorization != null) {
request.header("Authorization", authorization);
}
return request.when().post("/").then().statusCode(200)
.extract().xmlPath().getString("**.find { it.name() == 'PhysicalResourceId' }");
}
/** Returns the stack-level tags reported by DescribeStacks. */
public static Map<String, String> tags(String stackName) {
String xml = cfn("DescribeStacks", stackName).when().post("/")