mirror of
https://github.com/floci-io/floci.git
synced 2026-12-01 01:12:48 -05:00
feat(cloudformation): add StackSets with account-aware provisioning (#1551)
* feat(cloudformation): add StackSets with account-aware provisioning Implement CloudFormation StackSets so a template can be deployed across target accounts and regions, and make CloudFormation provisioning land resources in the correct account. Provisioning previously ran on a background executor thread with no request scope, so the downstream service calls fell back to the default account — a stack created by account B materialised its resources under 000000000000. executeChangeSet/deleteStack now run the provisioning under a synthetic request scope bound to the deploying account (the caller's account for a single stack, the target account for a StackSet instance), mirroring the pattern other background workers already use. Single-stack resources now land in the caller's account. StackSet actions are wired through CloudFormationQueryHandler and AwsQueryController: CreateStackSet, DescribeStackSet, ListStackSets, UpdateStackSet, DeleteStackSet, CreateStackInstances, ListStackInstances, DeleteStackInstances, ListStackSetOperations. CreateStackInstances drives the existing single-stack engine once per target (account, region), provisioning each instance into that account's namespace; the stack set, its instances and its operation history live in the administration account. Tests: an integration test deploying a StackSet from an admin account into accounts B and C asserts the resources materialise in B and C and are invisible to the admin/default account, exercises describe/list/operations and instance deletion, and covers the not-found / already-exists / not-empty error paths. A single-stack test asserts resources now land in the caller's account. Design aligned with maintainers in floci-io/floci#1550. * fix(cloudformation): await StackSet instance deletion; test UpdateStackSet Make the account-scoped deleteStack overload return its Future so DeleteStackInstances completes synchronously before the operation returns, rather than racing the async resource removal. Add integration coverage for UpdateStackSet re-applying the template to existing instances. * test(cloudformation): add SDK compatibility test for StackSets Adds an sdk-test-java compatibility test exercising StackSets through the real AWS SDK CloudFormationClient (CreateStackSet, CreateStackInstances, DescribeStackSet, ListStackInstances, ListStackSetOperations, DeleteStackInstances, DeleteStackSet). It asserts the management responses unmarshal cleanly and that instances provision into each target account's namespace (verified via per-account SQS clients), per the project's policy of validating management APIs with AWS SDK clients. Runs in the compatibility CI on pull requests. * feat(cloudformation): add DescribeStackInstance and DescribeStackSetOperation Complete the StackSets API surface with the singular describe operations: DescribeStackInstance (used by orchestration that polls a single instance's status) and DescribeStackSetOperation (companion to ListStackSetOperations). Covered by the integration test and the SDK compatibility test, which confirm the StackInstance / StackSetOperation response shapes unmarshal through the real AWS SDK. * feat(cloudformation): resolve TemplateURL and report StackInstanceStatus.DetailedStatus for StackSets StackSet create/update now resolve a TemplateURL (fetched from S3) in addition to an inline TemplateBody, matching how orchestration tools reference templates staged in S3. DescribeStackInstance and ListStackInstances now also report the nested StackInstanceStatus.DetailedStatus that AWS returns and that pollers read to determine when an instance operation has completed. * feat(cloudformation): derive StackSet operation status and honor RetainStacks Address review feedback on #1551: - Operation status is now derived from instance outcomes: CreateStackInstances and UpdateStackSet report FAILED if any instance fails to deploy, instead of always SUCCEEDED. deployInstance also recognizes a rolled-back create (ROLLBACK_COMPLETE, not *_FAILED) as a failed/INOPERABLE instance, which was previously mis-reported as SUCCEEDED. - DeleteStackInstances honors RetainStacks: when true, the instance is detached from the stack set but its underlying stack and resources are left in place. - UpdateStackSet now persists the refreshed instance records. Tests: RetainStacks retention and the FAILED-operation path; docs updated. * fix(cloudformation): restore account on active scope; log StackSets test teardown Greptile review on #1551: - runUnderAccount now saves and restores the previous account ID when it runs inside an already-active request scope, so it can't leave the overridden account behind on a reused thread. (Background workers normally have no active scope, so this only hardens the synchronous-caller path.) - CloudFormationStackSetsTest teardown no longer swallows exceptions silently; it logs them, per the AGENTS.md no-empty-catch rule. * fix(cloudformation): report a failed StackSet instance delete as FAILED deleteStackInstances awaited the underlying deleteStack via a helper that swallows the failure, then removed the instance record unconditionally and hardcoded the operation status to SUCCEEDED. A failed non-retained delete was therefore reported as a success with the instance silently dropped. await() now returns whether the future completed cleanly. On a non-retained delete failure the instance record is retained (marked INOPERABLE/FAILED) and the operation status is derived via deriveOperationStatus, so it reports FAILED — matching AWS, which does not detach an instance whose stack delete failed. Adds StackSetServiceDeleteFailureTest covering the failure, success, and RetainStacks paths. * fix(cloudformation): reject templateless CreateStackSet and restore interrupt in await CreateStackSet now rejects a request with neither TemplateBody nor TemplateURL (ValidationError, matching AWS) instead of storing a null template — which would otherwise make a later CreateStackInstances deploy empty stacks into every target account. Also handle InterruptedException separately in await(Future) and restore the thread's interrupt flag, so a shutdown signal to the backing ExecutorService propagates instead of being swallowed. Adds a CreateStackSet-without-template integration test.
This commit is contained in:
1 parent
a27971c7d5
commit
058e02630a
12 files changed
+1642
-12
No files matched your search
+484
@@ -0,0 +1,484 @@
|
||||
package io.github.hectorvent.floci.services.cloudformation;
|
||||
|
||||
import io.quarkus.test.junit.QuarkusTest;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import java.util.UUID;
|
||||
|
||||
import static io.restassured.RestAssured.given;
|
||||
import static org.hamcrest.Matchers.containsString;
|
||||
import static org.hamcrest.Matchers.not;
|
||||
|
||||
/**
|
||||
* Integration tests for cross-account CloudFormation provisioning:
|
||||
* <ul>
|
||||
* <li>single-stack resources land in the caller's account namespace; and</li>
|
||||
* <li>StackSet instances materialize resources in each target account's namespace.</li>
|
||||
* </ul>
|
||||
*/
|
||||
@QuarkusTest
|
||||
class CloudFormationStackSetsIntegrationTest {
|
||||
|
||||
private static final String ADMIN = "111111111111";
|
||||
private static final String ACCOUNT_B = "222222222222";
|
||||
private static final String ACCOUNT_C = "333333333333";
|
||||
private static final String REGION = "us-east-1";
|
||||
|
||||
private static String auth(String account, String service) {
|
||||
return "AWS4-HMAC-SHA256 Credential=" + account + "/20260215/" + REGION + "/" + service
|
||||
+ "/aws4_request, SignedHeaders=host, Signature=abc";
|
||||
}
|
||||
|
||||
private static String queueTemplate(String queueName) {
|
||||
return """
|
||||
{"Resources":{"Q":{"Type":"AWS::SQS::Queue","Properties":{"QueueName":"%s"}}}}
|
||||
""".formatted(queueName);
|
||||
}
|
||||
|
||||
private void assertQueueVisible(String account, String queueName) {
|
||||
given()
|
||||
.contentType("application/x-www-form-urlencoded")
|
||||
.formParam("Action", "GetQueueUrl")
|
||||
.formParam("QueueName", queueName)
|
||||
.header("Authorization", auth(account, "sqs"))
|
||||
.when().post("/")
|
||||
.then().statusCode(200)
|
||||
.body(containsString("/" + account + "/" + queueName));
|
||||
}
|
||||
|
||||
private void assertQueueAbsent(String account, String queueName) {
|
||||
given()
|
||||
.contentType("application/x-www-form-urlencoded")
|
||||
.formParam("Action", "GetQueueUrl")
|
||||
.formParam("QueueName", queueName)
|
||||
.header("Authorization", auth(account, "sqs"))
|
||||
.when().post("/")
|
||||
.then().body(containsString("QueueDoesNotExist"));
|
||||
}
|
||||
|
||||
@Test
|
||||
void stackSetFromTemplateUrlProvisionsAndReportsDetailedStatus() {
|
||||
String setName = "url-set-" + UUID.randomUUID().toString().substring(0, 8);
|
||||
String queue = "url-q-" + UUID.randomUUID().toString().substring(0, 8);
|
||||
String bucket = "tpl-bucket-" + UUID.randomUUID().toString().substring(0, 8);
|
||||
String tkey = "template.json";
|
||||
|
||||
// Stage the template in S3, the way DPS does, then reference it by TemplateURL.
|
||||
given().header("Authorization", auth(ADMIN, "s3")).when().put("/" + bucket)
|
||||
.then().statusCode(200);
|
||||
given().header("Authorization", auth(ADMIN, "s3")).body(queueTemplate(queue))
|
||||
.when().put("/" + bucket + "/" + tkey).then().statusCode(200);
|
||||
String templateUrl = "http://localhost:4566/" + bucket + "/" + tkey;
|
||||
|
||||
given()
|
||||
.contentType("application/x-www-form-urlencoded")
|
||||
.formParam("Action", "CreateStackSet")
|
||||
.formParam("StackSetName", setName)
|
||||
.formParam("TemplateURL", templateUrl)
|
||||
.header("Authorization", auth(ADMIN, "cloudformation"))
|
||||
.when().post("/")
|
||||
.then().statusCode(200).body(containsString("<StackSetId>"));
|
||||
|
||||
given()
|
||||
.contentType("application/x-www-form-urlencoded")
|
||||
.formParam("Action", "CreateStackInstances")
|
||||
.formParam("StackSetName", setName)
|
||||
.formParam("Accounts.member.1", ACCOUNT_B)
|
||||
.formParam("Regions.member.1", REGION)
|
||||
.header("Authorization", auth(ADMIN, "cloudformation"))
|
||||
.when().post("/")
|
||||
.then().statusCode(200);
|
||||
|
||||
// The template fetched from S3 was provisioned into account B.
|
||||
assertQueueVisible(ACCOUNT_B, queue);
|
||||
|
||||
// DescribeStackInstance reports the nested StackInstanceStatus.DetailedStatus DPS polls.
|
||||
given()
|
||||
.contentType("application/x-www-form-urlencoded")
|
||||
.formParam("Action", "DescribeStackInstance")
|
||||
.formParam("StackSetName", setName)
|
||||
.formParam("StackInstanceAccount", ACCOUNT_B)
|
||||
.formParam("StackInstanceRegion", REGION)
|
||||
.header("Authorization", auth(ADMIN, "cloudformation"))
|
||||
.when().post("/")
|
||||
.then().statusCode(200)
|
||||
.body(containsString("<DetailedStatus>SUCCEEDED</DetailedStatus>"));
|
||||
}
|
||||
|
||||
@Test
|
||||
void singleStackProvisioningLandsInCallerAccount() {
|
||||
String queue = "single-" + UUID.randomUUID().toString().substring(0, 8);
|
||||
|
||||
given()
|
||||
.contentType("application/x-www-form-urlencoded")
|
||||
.formParam("Action", "CreateStack")
|
||||
.formParam("StackName", "stack-" + queue)
|
||||
.formParam("TemplateBody", queueTemplate(queue))
|
||||
.header("Authorization", auth(ACCOUNT_B, "cloudformation"))
|
||||
.when().post("/")
|
||||
.then().statusCode(200);
|
||||
|
||||
// Resource lands in the caller's account, not the default account.
|
||||
assertQueueVisible(ACCOUNT_B, queue);
|
||||
assertQueueAbsent("000000000000", queue);
|
||||
}
|
||||
|
||||
@Test
|
||||
void createStackSetWithoutTemplateIsRejected() {
|
||||
// AWS rejects CreateStackSet when neither TemplateBody nor TemplateURL is supplied; otherwise
|
||||
// a later CreateStackInstances would deploy empty stacks into every target account.
|
||||
given()
|
||||
.contentType("application/x-www-form-urlencoded")
|
||||
.formParam("Action", "CreateStackSet")
|
||||
.formParam("StackSetName", "set-" + UUID.randomUUID().toString().substring(0, 8))
|
||||
.header("Authorization", auth(ADMIN, "cloudformation"))
|
||||
.when().post("/")
|
||||
.then().statusCode(400)
|
||||
.body(containsString("ValidationError"));
|
||||
}
|
||||
|
||||
@Test
|
||||
void stackSetProvisionsInstancesIntoTargetAccounts() {
|
||||
String setName = "set-" + UUID.randomUUID().toString().substring(0, 8);
|
||||
String queue = "ss-" + UUID.randomUUID().toString().substring(0, 8);
|
||||
|
||||
// 1. Admin creates the StackSet.
|
||||
given()
|
||||
.contentType("application/x-www-form-urlencoded")
|
||||
.formParam("Action", "CreateStackSet")
|
||||
.formParam("StackSetName", setName)
|
||||
.formParam("TemplateBody", queueTemplate(queue))
|
||||
.header("Authorization", auth(ADMIN, "cloudformation"))
|
||||
.when().post("/")
|
||||
.then().statusCode(200)
|
||||
.body(containsString("<StackSetId>"));
|
||||
|
||||
// 2. Create instances into accounts B and C.
|
||||
given()
|
||||
.contentType("application/x-www-form-urlencoded")
|
||||
.formParam("Action", "CreateStackInstances")
|
||||
.formParam("StackSetName", setName)
|
||||
.formParam("Accounts.member.1", ACCOUNT_B)
|
||||
.formParam("Accounts.member.2", ACCOUNT_C)
|
||||
.formParam("Regions.member.1", REGION)
|
||||
.header("Authorization", auth(ADMIN, "cloudformation"))
|
||||
.when().post("/")
|
||||
.then().statusCode(200)
|
||||
.body(containsString("<OperationId>"));
|
||||
|
||||
// 3. The queue materializes in B and C, but not in the admin or default account.
|
||||
assertQueueVisible(ACCOUNT_B, queue);
|
||||
assertQueueVisible(ACCOUNT_C, queue);
|
||||
assertQueueAbsent(ADMIN, queue);
|
||||
assertQueueAbsent("000000000000", queue);
|
||||
|
||||
// 4. DescribeStackSet returns the definition.
|
||||
given()
|
||||
.contentType("application/x-www-form-urlencoded")
|
||||
.formParam("Action", "DescribeStackSet")
|
||||
.formParam("StackSetName", setName)
|
||||
.header("Authorization", auth(ADMIN, "cloudformation"))
|
||||
.when().post("/")
|
||||
.then().statusCode(200)
|
||||
.body(containsString("<StackSetName>" + setName + "</StackSetName>"))
|
||||
.body(containsString("<Status>ACTIVE</Status>"));
|
||||
|
||||
// 5. ListStackInstances shows both target accounts.
|
||||
given()
|
||||
.contentType("application/x-www-form-urlencoded")
|
||||
.formParam("Action", "ListStackInstances")
|
||||
.formParam("StackSetName", setName)
|
||||
.header("Authorization", auth(ADMIN, "cloudformation"))
|
||||
.when().post("/")
|
||||
.then().statusCode(200)
|
||||
.body(containsString("<Account>" + ACCOUNT_B + "</Account>"))
|
||||
.body(containsString("<Account>" + ACCOUNT_C + "</Account>"));
|
||||
|
||||
// 5b. DescribeStackInstance (singular) returns one target's instance.
|
||||
given()
|
||||
.contentType("application/x-www-form-urlencoded")
|
||||
.formParam("Action", "DescribeStackInstance")
|
||||
.formParam("StackSetName", setName)
|
||||
.formParam("StackInstanceAccount", ACCOUNT_B)
|
||||
.formParam("StackInstanceRegion", REGION)
|
||||
.header("Authorization", auth(ADMIN, "cloudformation"))
|
||||
.when().post("/")
|
||||
.then().statusCode(200)
|
||||
.body(containsString("<Account>" + ACCOUNT_B + "</Account>"))
|
||||
.body(containsString("<Region>" + REGION + "</Region>"));
|
||||
|
||||
// 6. ListStackSetOperations records the CREATE operation.
|
||||
String operationId = given()
|
||||
.contentType("application/x-www-form-urlencoded")
|
||||
.formParam("Action", "ListStackSetOperations")
|
||||
.formParam("StackSetName", setName)
|
||||
.header("Authorization", auth(ADMIN, "cloudformation"))
|
||||
.when().post("/")
|
||||
.then().statusCode(200)
|
||||
.body(containsString("<Action>CREATE</Action>"))
|
||||
.extract().path("ListStackSetOperationsResponse.ListStackSetOperationsResult.Summaries.member.OperationId");
|
||||
|
||||
// 6b. DescribeStackSetOperation returns that operation.
|
||||
given()
|
||||
.contentType("application/x-www-form-urlencoded")
|
||||
.formParam("Action", "DescribeStackSetOperation")
|
||||
.formParam("StackSetName", setName)
|
||||
.formParam("OperationId", operationId)
|
||||
.header("Authorization", auth(ADMIN, "cloudformation"))
|
||||
.when().post("/")
|
||||
.then().statusCode(200)
|
||||
.body(containsString("<OperationId>" + operationId + "</OperationId>"))
|
||||
.body(containsString("<Status>SUCCEEDED</Status>"));
|
||||
|
||||
// 7. Delete the instances; resources and instance records go away.
|
||||
given()
|
||||
.contentType("application/x-www-form-urlencoded")
|
||||
.formParam("Action", "DeleteStackInstances")
|
||||
.formParam("StackSetName", setName)
|
||||
.formParam("Accounts.member.1", ACCOUNT_B)
|
||||
.formParam("Accounts.member.2", ACCOUNT_C)
|
||||
.formParam("Regions.member.1", REGION)
|
||||
.header("Authorization", auth(ADMIN, "cloudformation"))
|
||||
.when().post("/")
|
||||
.then().statusCode(200)
|
||||
.body(containsString("<OperationId>"));
|
||||
|
||||
assertQueueAbsent(ACCOUNT_B, queue);
|
||||
assertQueueAbsent(ACCOUNT_C, queue);
|
||||
|
||||
given()
|
||||
.contentType("application/x-www-form-urlencoded")
|
||||
.formParam("Action", "ListStackInstances")
|
||||
.formParam("StackSetName", setName)
|
||||
.header("Authorization", auth(ADMIN, "cloudformation"))
|
||||
.when().post("/")
|
||||
.then().statusCode(200)
|
||||
.body(not(containsString("<Account>" + ACCOUNT_B + "</Account>")));
|
||||
|
||||
// 8. With no instances left, the StackSet can be deleted.
|
||||
given()
|
||||
.contentType("application/x-www-form-urlencoded")
|
||||
.formParam("Action", "DeleteStackSet")
|
||||
.formParam("StackSetName", setName)
|
||||
.header("Authorization", auth(ADMIN, "cloudformation"))
|
||||
.when().post("/")
|
||||
.then().statusCode(200);
|
||||
}
|
||||
|
||||
@Test
|
||||
void updateStackSetReappliesToExistingInstances() {
|
||||
String setName = "upd-" + UUID.randomUUID().toString().substring(0, 8);
|
||||
String q1 = "u1-" + UUID.randomUUID().toString().substring(0, 8);
|
||||
String q2 = "u2-" + UUID.randomUUID().toString().substring(0, 8);
|
||||
|
||||
given()
|
||||
.contentType("application/x-www-form-urlencoded")
|
||||
.formParam("Action", "CreateStackSet")
|
||||
.formParam("StackSetName", setName)
|
||||
.formParam("TemplateBody", queueTemplate(q1))
|
||||
.header("Authorization", auth(ADMIN, "cloudformation"))
|
||||
.when().post("/")
|
||||
.then().statusCode(200);
|
||||
|
||||
given()
|
||||
.contentType("application/x-www-form-urlencoded")
|
||||
.formParam("Action", "CreateStackInstances")
|
||||
.formParam("StackSetName", setName)
|
||||
.formParam("Accounts.member.1", ACCOUNT_B)
|
||||
.formParam("Regions.member.1", REGION)
|
||||
.header("Authorization", auth(ADMIN, "cloudformation"))
|
||||
.when().post("/")
|
||||
.then().statusCode(200);
|
||||
|
||||
assertQueueVisible(ACCOUNT_B, q1);
|
||||
assertQueueAbsent(ACCOUNT_B, q2);
|
||||
|
||||
// Update the template to add a second queue; existing instances are re-applied.
|
||||
String twoQueues = """
|
||||
{"Resources":{
|
||||
"Q1":{"Type":"AWS::SQS::Queue","Properties":{"QueueName":"%s"}},
|
||||
"Q2":{"Type":"AWS::SQS::Queue","Properties":{"QueueName":"%s"}}}}
|
||||
""".formatted(q1, q2);
|
||||
given()
|
||||
.contentType("application/x-www-form-urlencoded")
|
||||
.formParam("Action", "UpdateStackSet")
|
||||
.formParam("StackSetName", setName)
|
||||
.formParam("TemplateBody", twoQueues)
|
||||
.header("Authorization", auth(ADMIN, "cloudformation"))
|
||||
.when().post("/")
|
||||
.then().statusCode(200)
|
||||
.body(containsString("<OperationId>"));
|
||||
|
||||
// The newly added resource now exists in the target account.
|
||||
assertQueueVisible(ACCOUNT_B, q2);
|
||||
|
||||
// The operation history records the update.
|
||||
given()
|
||||
.contentType("application/x-www-form-urlencoded")
|
||||
.formParam("Action", "ListStackSetOperations")
|
||||
.formParam("StackSetName", setName)
|
||||
.header("Authorization", auth(ADMIN, "cloudformation"))
|
||||
.when().post("/")
|
||||
.then().statusCode(200)
|
||||
.body(containsString("<Action>UPDATE</Action>"));
|
||||
}
|
||||
|
||||
@Test
|
||||
void stackSetErrorPaths() {
|
||||
String setName = "errset-" + UUID.randomUUID().toString().substring(0, 8);
|
||||
String queue = "errq-" + UUID.randomUUID().toString().substring(0, 8);
|
||||
|
||||
// Describe a non-existent stack set → StackSetNotFoundException.
|
||||
given()
|
||||
.contentType("application/x-www-form-urlencoded")
|
||||
.formParam("Action", "DescribeStackSet")
|
||||
.formParam("StackSetName", "does-not-exist-" + setName)
|
||||
.header("Authorization", auth(ADMIN, "cloudformation"))
|
||||
.when().post("/")
|
||||
.then().statusCode(404)
|
||||
.body(containsString("StackSetNotFoundException"));
|
||||
|
||||
given()
|
||||
.contentType("application/x-www-form-urlencoded")
|
||||
.formParam("Action", "CreateStackSet")
|
||||
.formParam("StackSetName", setName)
|
||||
.formParam("TemplateBody", queueTemplate(queue))
|
||||
.header("Authorization", auth(ADMIN, "cloudformation"))
|
||||
.when().post("/")
|
||||
.then().statusCode(200);
|
||||
|
||||
// Duplicate create → NameAlreadyExistsException.
|
||||
given()
|
||||
.contentType("application/x-www-form-urlencoded")
|
||||
.formParam("Action", "CreateStackSet")
|
||||
.formParam("StackSetName", setName)
|
||||
.formParam("TemplateBody", queueTemplate(queue))
|
||||
.header("Authorization", auth(ADMIN, "cloudformation"))
|
||||
.when().post("/")
|
||||
.then().statusCode(409)
|
||||
.body(containsString("NameAlreadyExistsException"));
|
||||
|
||||
// Add an instance, then attempt to delete the non-empty set → StackSetNotEmptyException.
|
||||
given()
|
||||
.contentType("application/x-www-form-urlencoded")
|
||||
.formParam("Action", "CreateStackInstances")
|
||||
.formParam("StackSetName", setName)
|
||||
.formParam("Accounts.member.1", ACCOUNT_B)
|
||||
.formParam("Regions.member.1", REGION)
|
||||
.header("Authorization", auth(ADMIN, "cloudformation"))
|
||||
.when().post("/")
|
||||
.then().statusCode(200);
|
||||
|
||||
given()
|
||||
.contentType("application/x-www-form-urlencoded")
|
||||
.formParam("Action", "DeleteStackSet")
|
||||
.formParam("StackSetName", setName)
|
||||
.header("Authorization", auth(ADMIN, "cloudformation"))
|
||||
.when().post("/")
|
||||
.then().statusCode(409)
|
||||
.body(containsString("StackSetNotEmptyException"));
|
||||
}
|
||||
|
||||
@Test
|
||||
void deleteStackInstancesWithRetainStacksKeepsUnderlyingResources() {
|
||||
String setName = "retain-" + UUID.randomUUID().toString().substring(0, 8);
|
||||
String queue = "retain-q-" + UUID.randomUUID().toString().substring(0, 8);
|
||||
|
||||
given()
|
||||
.contentType("application/x-www-form-urlencoded")
|
||||
.formParam("Action", "CreateStackSet")
|
||||
.formParam("StackSetName", setName)
|
||||
.formParam("TemplateBody", queueTemplate(queue))
|
||||
.header("Authorization", auth(ADMIN, "cloudformation"))
|
||||
.when().post("/")
|
||||
.then().statusCode(200);
|
||||
|
||||
given()
|
||||
.contentType("application/x-www-form-urlencoded")
|
||||
.formParam("Action", "CreateStackInstances")
|
||||
.formParam("StackSetName", setName)
|
||||
.formParam("Accounts.member.1", ACCOUNT_B)
|
||||
.formParam("Regions.member.1", REGION)
|
||||
.header("Authorization", auth(ADMIN, "cloudformation"))
|
||||
.when().post("/")
|
||||
.then().statusCode(200);
|
||||
assertQueueVisible(ACCOUNT_B, queue);
|
||||
|
||||
// RetainStacks=true (the SDK always sends it) detaches the instance but keeps the stack.
|
||||
given()
|
||||
.contentType("application/x-www-form-urlencoded")
|
||||
.formParam("Action", "DeleteStackInstances")
|
||||
.formParam("StackSetName", setName)
|
||||
.formParam("Accounts.member.1", ACCOUNT_B)
|
||||
.formParam("Regions.member.1", REGION)
|
||||
.formParam("RetainStacks", "true")
|
||||
.header("Authorization", auth(ADMIN, "cloudformation"))
|
||||
.when().post("/")
|
||||
.then().statusCode(200)
|
||||
.body(containsString("<OperationId>"));
|
||||
|
||||
// The instance is no longer part of the StackSet...
|
||||
given()
|
||||
.contentType("application/x-www-form-urlencoded")
|
||||
.formParam("Action", "ListStackInstances")
|
||||
.formParam("StackSetName", setName)
|
||||
.header("Authorization", auth(ADMIN, "cloudformation"))
|
||||
.when().post("/")
|
||||
.then().statusCode(200)
|
||||
.body(not(containsString("<Account>" + ACCOUNT_B + "</Account>")));
|
||||
|
||||
// ...but its underlying resources were retained in the target account.
|
||||
assertQueueVisible(ACCOUNT_B, queue);
|
||||
}
|
||||
|
||||
@Test
|
||||
void operationReportsFailedWhenAnInstanceRollsBack() {
|
||||
String setName = "failset-" + UUID.randomUUID().toString().substring(0, 8);
|
||||
// A nested stack with no TemplateURL fails to provision, rolling the instance stack back.
|
||||
String badTemplate =
|
||||
"{\"Resources\":{\"Nested\":{\"Type\":\"AWS::CloudFormation::Stack\",\"Properties\":{}}}}";
|
||||
|
||||
given()
|
||||
.contentType("application/x-www-form-urlencoded")
|
||||
.formParam("Action", "CreateStackSet")
|
||||
.formParam("StackSetName", setName)
|
||||
.formParam("TemplateBody", badTemplate)
|
||||
.header("Authorization", auth(ADMIN, "cloudformation"))
|
||||
.when().post("/")
|
||||
.then().statusCode(200);
|
||||
|
||||
String operationId = given()
|
||||
.contentType("application/x-www-form-urlencoded")
|
||||
.formParam("Action", "CreateStackInstances")
|
||||
.formParam("StackSetName", setName)
|
||||
.formParam("Accounts.member.1", ACCOUNT_B)
|
||||
.formParam("Regions.member.1", REGION)
|
||||
.header("Authorization", auth(ADMIN, "cloudformation"))
|
||||
.when().post("/")
|
||||
.then().statusCode(200)
|
||||
.extract().path("CreateStackInstancesResponse.CreateStackInstancesResult.OperationId");
|
||||
|
||||
// The rolled-back instance is INOPERABLE / FAILED...
|
||||
given()
|
||||
.contentType("application/x-www-form-urlencoded")
|
||||
.formParam("Action", "DescribeStackInstance")
|
||||
.formParam("StackSetName", setName)
|
||||
.formParam("StackInstanceAccount", ACCOUNT_B)
|
||||
.formParam("StackInstanceRegion", REGION)
|
||||
.header("Authorization", auth(ADMIN, "cloudformation"))
|
||||
.when().post("/")
|
||||
.then().statusCode(200)
|
||||
.body(containsString("<DetailedStatus>FAILED</DetailedStatus>"));
|
||||
|
||||
// ...so the operation reports FAILED rather than a false SUCCEEDED.
|
||||
given()
|
||||
.contentType("application/x-www-form-urlencoded")
|
||||
.formParam("Action", "DescribeStackSetOperation")
|
||||
.formParam("StackSetName", setName)
|
||||
.formParam("OperationId", operationId)
|
||||
.header("Authorization", auth(ADMIN, "cloudformation"))
|
||||
.when().post("/")
|
||||
.then().statusCode(200)
|
||||
.body(containsString("<Status>FAILED</Status>"));
|
||||
}
|
||||
}
|
||||
+109
@@ -0,0 +1,109 @@
|
||||
package io.github.hectorvent.floci.services.cloudformation;
|
||||
|
||||
import com.fasterxml.jackson.core.type.TypeReference;
|
||||
import io.github.hectorvent.floci.core.storage.InMemoryStorage;
|
||||
import io.github.hectorvent.floci.core.storage.StorageBackend;
|
||||
import io.github.hectorvent.floci.core.storage.StorageFactory;
|
||||
import io.github.hectorvent.floci.services.cloudformation.model.Stack;
|
||||
import io.github.hectorvent.floci.services.cloudformation.model.StackInstance;
|
||||
import io.github.hectorvent.floci.services.cloudformation.model.StackSetOperation;
|
||||
import org.junit.jupiter.api.BeforeEach;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.concurrent.CompletableFuture;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
import static org.junit.jupiter.api.Assertions.assertTrue;
|
||||
import static org.mockito.ArgumentMatchers.anyString;
|
||||
import static org.mockito.Mockito.mock;
|
||||
import static org.mockito.Mockito.when;
|
||||
|
||||
/**
|
||||
* Focused coverage for how {@link StackSetService#deleteStackInstances} treats a failing underlying
|
||||
* {@code deleteStack}. A non-retained delete whose stack delete fails must report the operation as
|
||||
* FAILED and retain the instance record (AWS semantics) rather than swallowing the failure, dropping
|
||||
* the instance, and reporting a false SUCCEEDED.
|
||||
*/
|
||||
class StackSetServiceDeleteFailureTest {
|
||||
|
||||
private static final String SET = "del-set";
|
||||
private static final String ACCOUNT = "222222222222";
|
||||
private static final String REGION = "us-east-1";
|
||||
|
||||
private CloudFormationService cfnService;
|
||||
private StackSetService service;
|
||||
|
||||
@BeforeEach
|
||||
void setUp() {
|
||||
cfnService = mock(CloudFormationService.class);
|
||||
// deployInstance drives the single-stack engine; stub it to a clean CREATE so the seeded
|
||||
// instance starts SUCCEEDED, isolating this test to the delete path.
|
||||
when(cfnService.executeChangeSet(anyString(), anyString(), anyString(), anyString()))
|
||||
.thenReturn(CompletableFuture.completedFuture(null));
|
||||
Stack created = new Stack();
|
||||
created.setStackName("StackSet-" + SET + "-" + ACCOUNT);
|
||||
created.setStackId("stack-id");
|
||||
created.setStatus("CREATE_COMPLETE");
|
||||
when(cfnService.describeStacks(anyString(), anyString())).thenReturn(List.of(created));
|
||||
|
||||
service = new StackSetService(cfnService, new InMemoryStorageFactory());
|
||||
service.createStackSet(SET, "{\"Resources\":{}}", null, null, null, null);
|
||||
service.createStackInstances(SET, List.of(ACCOUNT), List.of(REGION));
|
||||
}
|
||||
|
||||
@Test
|
||||
void nonRetainedDeleteFailureReportsFailedAndRetainsInstance() {
|
||||
when(cfnService.deleteStack(anyString(), anyString(), anyString()))
|
||||
.thenReturn(CompletableFuture.failedFuture(new RuntimeException("boom")));
|
||||
|
||||
StackSetOperation op = service.deleteStackInstances(SET, List.of(ACCOUNT), List.of(REGION), false);
|
||||
|
||||
// The operation must surface the failure, not a hardcoded SUCCEEDED.
|
||||
assertEquals("FAILED", op.getStatus());
|
||||
// The instance record is retained (AWS does not detach an instance whose stack delete failed)
|
||||
// and marked INOPERABLE/FAILED.
|
||||
List<StackInstance> remaining = service.listStackInstances(SET, null, null);
|
||||
assertEquals(1, remaining.size(), "failed-delete instance must be retained");
|
||||
assertEquals("INOPERABLE", remaining.get(0).getStatus());
|
||||
assertEquals("FAILED", remaining.get(0).getDetailedStatus());
|
||||
}
|
||||
|
||||
@Test
|
||||
void nonRetainedDeleteSuccessReportsSucceededAndRemovesInstance() {
|
||||
when(cfnService.deleteStack(anyString(), anyString(), anyString()))
|
||||
.thenReturn(CompletableFuture.completedFuture(null));
|
||||
|
||||
StackSetOperation op = service.deleteStackInstances(SET, List.of(ACCOUNT), List.of(REGION), false);
|
||||
|
||||
assertEquals("SUCCEEDED", op.getStatus());
|
||||
assertTrue(service.listStackInstances(SET, null, null).isEmpty(),
|
||||
"successful delete must remove the instance");
|
||||
}
|
||||
|
||||
@Test
|
||||
void retainStacksRemovesInstanceWithoutDeletingStack() {
|
||||
StackSetOperation op = service.deleteStackInstances(SET, List.of(ACCOUNT), List.of(REGION), true);
|
||||
|
||||
assertEquals("SUCCEEDED", op.getStatus());
|
||||
assertTrue(service.listStackInstances(SET, null, null).isEmpty(),
|
||||
"RetainStacks detaches the instance from the StackSet");
|
||||
// RetainStacks=true must never invoke the underlying stack delete.
|
||||
org.mockito.Mockito.verify(cfnService, org.mockito.Mockito.never())
|
||||
.deleteStack(anyString(), anyString(), anyString());
|
||||
}
|
||||
|
||||
private static final class InMemoryStorageFactory extends StorageFactory {
|
||||
private InMemoryStorageFactory() {
|
||||
super(null, null);
|
||||
}
|
||||
|
||||
@Override
|
||||
public <V> StorageBackend<String, V> create(String serviceName,
|
||||
String fileName,
|
||||
TypeReference<Map<String, V>> typeReference) {
|
||||
return new InMemoryStorage<>();
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user