diff --git a/src/test/java/io/github/hectorvent/floci/services/cloudformation/ApiGatewayV2StackTestSupport.java b/src/test/java/io/github/hectorvent/floci/services/cloudformation/ApiGatewayV2StackTestSupport.java index 40eaaa7130..8c62b7f03a 100644 --- a/src/test/java/io/github/hectorvent/floci/services/cloudformation/ApiGatewayV2StackTestSupport.java +++ b/src/test/java/io/github/hectorvent/floci/services/cloudformation/ApiGatewayV2StackTestSupport.java @@ -1,6 +1,5 @@ package io.github.hectorvent.floci.services.cloudformation; -import io.github.hectorvent.floci.core.common.AwsException; import io.github.hectorvent.floci.services.apigatewayv2.ApiGatewayV2Service; import io.github.hectorvent.floci.services.apigatewayv2.model.Integration; import io.github.hectorvent.floci.services.cloudformation.model.Stack; @@ -11,7 +10,7 @@ import static io.restassured.RestAssured.given; /** * What the AWS::ApiGatewayV2 stack integration tests share: driving a stack through the query API, - * waiting for its status, and reading back the stack and its {@code Integration} resource. + * and reading back the stack and its {@code Integration} resource. */ final class ApiGatewayV2StackTestSupport { @@ -45,27 +44,4 @@ final class ApiGatewayV2StackTestSupport { } request.when().post("/").then().statusCode(200); } - - static void waitForStackStatus(CloudFormationService cloudFormationService, String stackName, - String expected) { - String status = null; - for (int attempt = 0; attempt < 200; attempt++) { - try { - status = cloudFormationService.describeStacks(stackName, REGION).getFirst().getStatus(); - } catch (AwsException e) { - // A deleted stack can no longer be described by name. - status = "DELETE_COMPLETE"; - } - if (expected.equals(status)) { - return; - } - try { - Thread.sleep(25); - } catch (InterruptedException e) { - Thread.currentThread().interrupt(); - throw new AssertionError("Interrupted while waiting for stack status", e); - } - } - throw new AssertionError("Stack did not reach " + expected + ", last status " + status); - } } diff --git a/src/test/java/io/github/hectorvent/floci/services/cloudformation/CloudFormationApiGatewayV2CleanupIntegrationTest.java b/src/test/java/io/github/hectorvent/floci/services/cloudformation/CloudFormationApiGatewayV2CleanupIntegrationTest.java index 654b70bd06..bfc1101d15 100644 --- a/src/test/java/io/github/hectorvent/floci/services/cloudformation/CloudFormationApiGatewayV2CleanupIntegrationTest.java +++ b/src/test/java/io/github/hectorvent/floci/services/cloudformation/CloudFormationApiGatewayV2CleanupIntegrationTest.java @@ -4,6 +4,7 @@ import io.github.hectorvent.floci.core.common.AwsException; import io.github.hectorvent.floci.services.apigatewayv2.ApiGatewayV2Service; import io.github.hectorvent.floci.services.apigatewayv2.model.Route; import io.github.hectorvent.floci.services.cloudformation.model.StackResource; +import io.github.hectorvent.floci.testing.CfnStackWaits; import io.github.hectorvent.floci.testing.RestAssuredJsonUtils; import io.quarkus.test.junit.QuarkusTest; import io.quarkus.test.junit.mockito.InjectSpy; @@ -67,7 +68,7 @@ class CloudFormationApiGatewayV2CleanupIntegrationTest { String apiName = "http-api-cleanup-" + suffix; createStack(httpApiTemplate(apiName, List.of("/before"))); - waitForStackStatus("CREATE_COMPLETE"); + CfnStackWaits.awaitStatus(stackName, "CREATE_COMPLETE"); String apiId = apiIdForName(apiName); String oldRouteId = apiGatewayV2Service.findRouteByKey(REGION, apiId, "GET /before").getRouteId(); @@ -93,7 +94,7 @@ class CloudFormationApiGatewayV2CleanupIntegrationTest { }).when(apiGatewayV2Service).deleteRoute(eq(REGION), eq(apiId), anyString()); updateStack(httpApiTemplate(apiName, List.of("/survivor", "/fail"))); - waitForStackStatus("UPDATE_ROLLBACK_COMPLETE"); + CfnStackWaits.awaitStatus(stackName, "UPDATE_ROLLBACK_COMPLETE"); StackResource persisted = cloudFormationService.describeStacks(stackName, REGION) .getFirst().getResources().get("HttpApi"); @@ -109,7 +110,7 @@ class CloudFormationApiGatewayV2CleanupIntegrationTest { String apiName = "http-api-restore-failure-" + suffix; createStack(httpApiTemplate(apiName, List.of("/before"))); - waitForStackStatus("CREATE_COMPLETE"); + CfnStackWaits.awaitStatus(stackName, "CREATE_COMPLETE"); String apiId = apiIdForName(apiName); String oldRouteId = apiGatewayV2Service.findRouteByKey(REGION, apiId, "GET /before").getRouteId(); @@ -127,7 +128,7 @@ class CloudFormationApiGatewayV2CleanupIntegrationTest { }).when(apiGatewayV2Service).createRoute(eq(REGION), eq(apiId), anyMap()); updateStack(httpApiTemplate(apiName, List.of("/fail"))); - waitForStackStatus("UPDATE_ROLLBACK_FAILED"); + CfnStackWaits.awaitStatus(stackName, "UPDATE_ROLLBACK_FAILED"); Route independent = apiGatewayV2Service.findRouteByKey(REGION, apiId, "GET /before"); assertEquals(independentRouteId.get(), independent.getRouteId()); @@ -159,22 +160,6 @@ class CloudFormationApiGatewayV2CleanupIntegrationTest { .statusCode(200); } - private void waitForStackStatus(String expected) { - for (int attempt = 0; attempt < 100; attempt++) { - String status = cloudFormationService.describeStacks(stackName, REGION).getFirst().getStatus(); - if (expected.equals(status)) { - return; - } - try { - Thread.sleep(25); - } catch (InterruptedException e) { - Thread.currentThread().interrupt(); - throw new AssertionError("Interrupted while waiting for stack status", e); - } - } - throw new AssertionError("Stack did not reach " + expected); - } - private static String apiIdForName(String apiName) { return given() .when() diff --git a/src/test/java/io/github/hectorvent/floci/services/cloudformation/CloudFormationApiGatewayV2IntegrationSettingsIntegrationTest.java b/src/test/java/io/github/hectorvent/floci/services/cloudformation/CloudFormationApiGatewayV2IntegrationSettingsIntegrationTest.java index e2468a37c6..fe0a802b7c 100644 --- a/src/test/java/io/github/hectorvent/floci/services/cloudformation/CloudFormationApiGatewayV2IntegrationSettingsIntegrationTest.java +++ b/src/test/java/io/github/hectorvent/floci/services/cloudformation/CloudFormationApiGatewayV2IntegrationSettingsIntegrationTest.java @@ -3,6 +3,7 @@ package io.github.hectorvent.floci.services.cloudformation; import io.github.hectorvent.floci.services.apigatewayv2.ApiGatewayV2Service; import io.github.hectorvent.floci.services.apigatewayv2.model.Integration; import io.github.hectorvent.floci.services.cloudformation.model.Stack; +import io.github.hectorvent.floci.testing.CfnStackWaits; import io.github.hectorvent.floci.testing.RestAssuredJsonUtils; import io.quarkus.test.junit.QuarkusTest; import io.restassured.response.ValidatableResponse; @@ -17,7 +18,6 @@ import static io.github.hectorvent.floci.services.cloudformation.ApiGatewayV2Sta import static io.github.hectorvent.floci.services.cloudformation.ApiGatewayV2StackTestSupport.integration; import static io.github.hectorvent.floci.services.cloudformation.ApiGatewayV2StackTestSupport.stack; import static io.github.hectorvent.floci.services.cloudformation.ApiGatewayV2StackTestSupport.stackAction; -import static io.github.hectorvent.floci.services.cloudformation.ApiGatewayV2StackTestSupport.waitForStackStatus; import static io.restassured.RestAssured.given; import static org.hamcrest.Matchers.equalTo; import static org.hamcrest.Matchers.nullValue; @@ -51,13 +51,13 @@ class CloudFormationApiGatewayV2IntegrationSettingsIntegrationTest { void removingSettingsFromTheTemplateRemovesThemFromTheIntegration() { String stackName = "integration-settings-removal-stack"; stackAction("CreateStack", stackName, template(PARAMETERS.formatted("value-1") + TIMEOUT, "")); - waitForStackStatus(cloudFormationService, stackName, "CREATE_COMPLETE"); + CfnStackWaits.awaitStatus(stackName, "CREATE_COMPLETE"); Integration created = integration(apiGatewayV2Service, stack(cloudFormationService, stackName)); assertEquals(Map.of("overwrite:header.x-example", "value-1"), created.getRequestParameters()); assertEquals(15000, created.getTimeoutInMillis()); stackAction("UpdateStack", stackName, template(TIMEOUT, "")); - waitForStackStatus(cloudFormationService, stackName, "UPDATE_COMPLETE"); + CfnStackWaits.awaitStatus(stackName, "UPDATE_COMPLETE"); Stack updated = stack(cloudFormationService, stackName); Integration integration = integration(apiGatewayV2Service, updated); @@ -70,7 +70,7 @@ class CloudFormationApiGatewayV2IntegrationSettingsIntegrationTest { .body("timeoutInMillis", equalTo(15000)); stackAction("UpdateStack", stackName, template("", "")); - waitForStackStatus(cloudFormationService, stackName, "UPDATE_COMPLETE"); + CfnStackWaits.awaitStatus(stackName, "UPDATE_COMPLETE"); Stack withoutTimeout = stack(cloudFormationService, stackName); assertEquals(0, integration(apiGatewayV2Service, withoutTimeout).getTimeoutInMillis()); @@ -81,12 +81,12 @@ class CloudFormationApiGatewayV2IntegrationSettingsIntegrationTest { void aTimeoutThatBecomesNoValueIsRemoved() { String stackName = "integration-settings-novalue-stack"; stackAction("CreateStack", stackName, template(TIMEOUT, "")); - waitForStackStatus(cloudFormationService, stackName, "CREATE_COMPLETE"); + CfnStackWaits.awaitStatus(stackName, "CREATE_COMPLETE"); assertEquals(15000, integration(apiGatewayV2Service, stack(cloudFormationService, stackName)).getTimeoutInMillis()); stackAction("UpdateStack", stackName, template( ",\"TimeoutInMillis\":{\"Fn::If\":[\"Off\",{\"Ref\":\"AWS::NoValue\"},15000]}", "")); - waitForStackStatus(cloudFormationService, stackName, "UPDATE_COMPLETE"); + CfnStackWaits.awaitStatus(stackName, "UPDATE_COMPLETE"); assertEquals(0, integration(apiGatewayV2Service, stack(cloudFormationService, stackName)).getTimeoutInMillis()); } @@ -95,10 +95,10 @@ class CloudFormationApiGatewayV2IntegrationSettingsIntegrationTest { void failedUpdateRestoresTheEarlierRequestParameters() { String stackName = "integration-settings-rollback-stack"; stackAction("CreateStack", stackName, template(PARAMETERS.formatted("original") + TIMEOUT, "")); - waitForStackStatus(cloudFormationService, stackName, "CREATE_COMPLETE"); + CfnStackWaits.awaitStatus(stackName, "CREATE_COMPLETE"); stackAction("UpdateStack", stackName, template(PARAMETERS.formatted("changed"), FAILING_RESOURCE)); - waitForStackStatus(cloudFormationService, stackName, "UPDATE_ROLLBACK_COMPLETE"); + CfnStackWaits.awaitStatus(stackName, "UPDATE_ROLLBACK_COMPLETE"); Stack rolledBack = stack(cloudFormationService, stackName); Integration integration = integration(apiGatewayV2Service, rolledBack); diff --git a/src/test/java/io/github/hectorvent/floci/services/cloudformation/CloudFormationApiGatewayV2VpcLinkIntegrationTest.java b/src/test/java/io/github/hectorvent/floci/services/cloudformation/CloudFormationApiGatewayV2VpcLinkIntegrationTest.java index ff8ffc31b5..b244cced97 100644 --- a/src/test/java/io/github/hectorvent/floci/services/cloudformation/CloudFormationApiGatewayV2VpcLinkIntegrationTest.java +++ b/src/test/java/io/github/hectorvent/floci/services/cloudformation/CloudFormationApiGatewayV2VpcLinkIntegrationTest.java @@ -5,6 +5,7 @@ import io.github.hectorvent.floci.services.apigatewayv2.ApiGatewayV2Service; import io.github.hectorvent.floci.services.apigatewayv2.model.Integration; import io.github.hectorvent.floci.services.apigatewayv2.model.VpcLink; import io.github.hectorvent.floci.services.cloudformation.model.Stack; +import io.github.hectorvent.floci.testing.CfnStackWaits; import io.github.hectorvent.floci.testing.RestAssuredJsonUtils; import io.quarkus.test.junit.QuarkusTest; import jakarta.inject.Inject; @@ -19,7 +20,6 @@ import static io.github.hectorvent.floci.services.cloudformation.ApiGatewayV2Sta import static io.github.hectorvent.floci.services.cloudformation.ApiGatewayV2StackTestSupport.integration; import static io.github.hectorvent.floci.services.cloudformation.ApiGatewayV2StackTestSupport.stack; import static io.github.hectorvent.floci.services.cloudformation.ApiGatewayV2StackTestSupport.stackAction; -import static io.github.hectorvent.floci.services.cloudformation.ApiGatewayV2StackTestSupport.waitForStackStatus; import static org.junit.jupiter.api.Assertions.assertEquals; import static org.junit.jupiter.api.Assertions.assertNotEquals; import static org.junit.jupiter.api.Assertions.assertNull; @@ -52,7 +52,7 @@ class CloudFormationApiGatewayV2VpcLinkIntegrationTest { @Test void vpcLinkLifecycleFollowsTheCloudFormationUpdateRules() { stackAction("CreateStack", STACK_NAME, template("first-name", "subnet-aaaa1111")); - waitForStackStatus(cloudFormationService, STACK_NAME, "CREATE_COMPLETE"); + CfnStackWaits.awaitStatus(STACK_NAME, "CREATE_COMPLETE"); Stack created = stack(cloudFormationService, STACK_NAME); String vpcLinkId = created.getOutputs().get("VpcLinkRef"); @@ -66,12 +66,12 @@ class CloudFormationApiGatewayV2VpcLinkIntegrationTest { assertEquals(vpcLinkId, integration.getConnectionId()); stackAction("UpdateStack", STACK_NAME, template("second-name", "subnet-aaaa1111")); - waitForStackStatus(cloudFormationService, STACK_NAME, "UPDATE_COMPLETE"); + CfnStackWaits.awaitStatus(STACK_NAME, "UPDATE_COMPLETE"); assertEquals(vpcLinkId, stack(cloudFormationService, STACK_NAME).getOutputs().get("VpcLinkRef"), "a Name change keeps the same link"); assertEquals("second-name", apiGatewayV2Service.getVpcLink(REGION, vpcLinkId).getName()); stackAction("UpdateStack", STACK_NAME, template("second-name", "subnet-bbbb2222")); - waitForStackStatus(cloudFormationService, STACK_NAME, "UPDATE_COMPLETE"); + CfnStackWaits.awaitStatus(STACK_NAME, "UPDATE_COMPLETE"); Stack replaced = stack(cloudFormationService, STACK_NAME); String newVpcLinkId = replaced.getOutputs().get("VpcLinkRef"); assertNotEquals(vpcLinkId, newVpcLinkId, "a SubnetIds change replaces the link"); @@ -80,7 +80,7 @@ class CloudFormationApiGatewayV2VpcLinkIntegrationTest { assertEquals(newVpcLinkId, integration(apiGatewayV2Service, replaced).getConnectionId()); stackAction("DeleteStack", STACK_NAME, null); - waitForStackStatus(cloudFormationService, STACK_NAME, "DELETE_COMPLETE"); + CfnStackWaits.awaitStackDeleted(STACK_NAME); assertNotFound(newVpcLinkId); } @@ -89,12 +89,12 @@ class CloudFormationApiGatewayV2VpcLinkIntegrationTest { String stackName = "vpclink-rollback-stack"; stackAction("CreateStack", stackName, template("rollback-link", "subnet-aaaa1111", "{}", VPC_LINK_CONNECTION, "")); - waitForStackStatus(cloudFormationService, stackName, "CREATE_COMPLETE"); + CfnStackWaits.awaitStatus(stackName, "CREATE_COMPLETE"); String vpcLinkId = stack(cloudFormationService, stackName).getOutputs().get("VpcLinkRef"); stackAction("UpdateStack", stackName, template("rollback-link", "subnet-bbbb2222", "{}", VPC_LINK_CONNECTION, FAILING_RESOURCE)); - waitForStackStatus(cloudFormationService, stackName, "UPDATE_ROLLBACK_COMPLETE"); + CfnStackWaits.awaitStatus(stackName, "UPDATE_ROLLBACK_COMPLETE"); Stack rolledBack = stack(cloudFormationService, stackName); assertEquals(vpcLinkId, rolledBack.getOutputs().get("VpcLinkRef")); @@ -112,10 +112,10 @@ class CloudFormationApiGatewayV2VpcLinkIntegrationTest { String stackName = "vpclink-connection-stack"; stackAction("CreateStack", stackName, template("connection-link", "subnet-aaaa1111", "{}", VPC_LINK_CONNECTION, "")); - waitForStackStatus(cloudFormationService, stackName, "CREATE_COMPLETE"); + CfnStackWaits.awaitStatus(stackName, "CREATE_COMPLETE"); stackAction("UpdateStack", stackName, template("connection-link", "subnet-aaaa1111", "{}", "", "")); - waitForStackStatus(cloudFormationService, stackName, "UPDATE_COMPLETE"); + CfnStackWaits.awaitStatus(stackName, "UPDATE_COMPLETE"); Integration integration = integration(apiGatewayV2Service, stack(cloudFormationService, stackName)); assertEquals("INTERNET", integration.getConnectionType()); @@ -127,11 +127,11 @@ class CloudFormationApiGatewayV2VpcLinkIntegrationTest { String stackName = "vpclink-reserved-tag-stack"; String tags = "{\"floci:note\":\"kept\",\"Env\":\"test\"}"; stackAction("CreateStack", stackName, template("reserved-v1", "subnet-aaaa1111", tags, "", "")); - waitForStackStatus(cloudFormationService, stackName, "CREATE_COMPLETE"); + CfnStackWaits.awaitStatus(stackName, "CREATE_COMPLETE"); String vpcLinkId = stack(cloudFormationService, stackName).getOutputs().get("VpcLinkRef"); stackAction("UpdateStack", stackName, template("reserved-v2", "subnet-aaaa1111", tags, "", "")); - waitForStackStatus(cloudFormationService, stackName, "UPDATE_COMPLETE"); + CfnStackWaits.awaitStatus(stackName, "UPDATE_COMPLETE"); VpcLink link = apiGatewayV2Service.getVpcLink(REGION, vpcLinkId); assertEquals("reserved-v2", link.getName());