From 691453086a35db844e007647d0b64f9008afd7e9 Mon Sep 17 00:00:00 2001 From: Hector Ventura Date: Sun, 6 Sep 2026 15:26:16 -0500 Subject: [PATCH] fix(test): tear down the organizations the service-managed StackSets test creates (#3158) Since #3055 merged, main fails one shard on OrganizationsCfnIntegrationTest with AlreadyInOrganizationException: CloudFormationServiceManagedStackSetsIntegrationTest created an organization under management account 666666666666, the account the Organizations CloudFormation test also uses, and never deleted it. Organizations state is shared by every test in the JVM and keyed by the management account, so the next class to create one under that account fails, and which shard shows it depends on class distribution. The StackSets test now records every management account it creates an organization under and dismantles each after every test: member accounts out, organizational units bottom-up, then DeleteOrganization. The one test that used 666666666666 uses an account of its own, like its siblings. Both classes pass in one JVM in the order that failed, and the whole CloudFormation package is green again apart from the ACM schema-gap row that predates this. --- ...erviceManagedStackSetsIntegrationTest.java | 50 ++++++++++++++++++- 1 file changed, 49 insertions(+), 1 deletion(-) diff --git a/src/test/java/io/github/hectorvent/floci/services/cloudformation/CloudFormationServiceManagedStackSetsIntegrationTest.java b/src/test/java/io/github/hectorvent/floci/services/cloudformation/CloudFormationServiceManagedStackSetsIntegrationTest.java index 6de0324a36..ba517ec76b 100644 --- a/src/test/java/io/github/hectorvent/floci/services/cloudformation/CloudFormationServiceManagedStackSetsIntegrationTest.java +++ b/src/test/java/io/github/hectorvent/floci/services/cloudformation/CloudFormationServiceManagedStackSetsIntegrationTest.java @@ -4,9 +4,13 @@ import io.github.hectorvent.floci.testing.RestAssuredJsonUtils; import io.quarkus.test.junit.QuarkusTest; import io.restassured.path.json.JsonPath; import io.restassured.specification.RequestSpecification; +import org.junit.jupiter.api.AfterEach; import org.junit.jupiter.api.BeforeAll; import org.junit.jupiter.api.Test; +import java.util.LinkedHashSet; +import java.util.List; +import java.util.Set; import java.util.UUID; import static io.restassured.RestAssured.given; @@ -22,6 +26,8 @@ class CloudFormationServiceManagedStackSetsIntegrationTest { private static final String REGION = "us-east-1"; private static final String ORG_TARGET = "AWSOrganizationsV20161128."; + private final Set organizationOwners = new LinkedHashSet<>(); + @BeforeAll static void configureRestAssured() { RestAssuredJsonUtils.configureAwsContentTypes(); @@ -377,7 +383,7 @@ class CloudFormationServiceManagedStackSetsIntegrationTest { @Test void activateOrganizationsAccessRequiresAllFeatures() { - String management = "666666666666"; + String management = "939393939393"; organizations(management, "CreateOrganization", "{\"FeatureSet\":\"CONSOLIDATED_BILLING\"}") .post("/").then().statusCode(200); @@ -408,6 +414,9 @@ class CloudFormationServiceManagedStackSetsIntegrationTest { } private RequestSpecification organizations(String account, String action, String body) { + if ("CreateOrganization".equals(action)) { + organizationOwners.add(account); + } return given() .header("Authorization", auth(account, "organizations")) .header("X-Amz-Target", ORG_TARGET + action) @@ -415,6 +424,45 @@ class CloudFormationServiceManagedStackSetsIntegrationTest { .body(body); } + /** + * Organizations state is shared by every test in the JVM and keyed by the management account, + * so an organization left behind here makes the next class that creates one under the same + * account fail with AlreadyInOrganizationException. Dismantle each one this test created: + * member accounts out, organizational units bottom-up, then the organization. + */ + @AfterEach + void deleteTheOrganizationsThisTestCreated() { + for (String owner : organizationOwners) { + if (organizations(owner, "DescribeOrganization", "{}").post("/").statusCode() != 200) { + continue; + } + JsonPath accounts = organizations(owner, "ListAccounts", "{}").post("/").jsonPath(); + for (String accountId : accounts.getList("Accounts.Id", String.class)) { + if (!owner.equals(accountId)) { + organizations(owner, "RemoveAccountFromOrganization", "{\"AccountId\":\"" + accountId + "\"}") + .post("/").then().statusCode(200); + } + } + JsonPath roots = organizations(owner, "ListRoots", "{}").post("/").jsonPath(); + for (String rootId : roots.getList("Roots.Id", String.class)) { + deleteOrganizationalUnitsUnder(owner, rootId); + } + organizations(owner, "DeleteOrganization", "{}").post("/").then().statusCode(200); + } + organizationOwners.clear(); + } + + private void deleteOrganizationalUnitsUnder(String owner, String parentId) { + JsonPath units = organizations(owner, "ListOrganizationalUnitsForParent", "{\"ParentId\":\"" + parentId + "\"}") + .post("/").jsonPath(); + List ouIds = units.getList("OrganizationalUnits.Id", String.class); + for (String ouId : ouIds) { + deleteOrganizationalUnitsUnder(owner, ouId); + organizations(owner, "DeleteOrganizationalUnit", "{\"OrganizationalUnitId\":\"" + ouId + "\"}") + .post("/").then().statusCode(200); + } + } + private RequestSpecification cloudFormation(String action) { return cloudFormation(MANAGEMENT, action); }