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.
This commit is contained in:
Hector Ventura authored and GitHub committed 2026-09-06 16:26:16 -04:00
1 parent 17af9590c7
commit 691453086a
1 file changed
+49 -1
@@ -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<String> 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<String> 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);
}