diff --git a/src/main/java/io/github/hectorvent/floci/services/cloudformation/provisioners/DynamoDbCfnProvisioner.java b/src/main/java/io/github/hectorvent/floci/services/cloudformation/provisioners/DynamoDbCfnProvisioner.java index a29a7de17d..3af673c657 100644 --- a/src/main/java/io/github/hectorvent/floci/services/cloudformation/provisioners/DynamoDbCfnProvisioner.java +++ b/src/main/java/io/github/hectorvent/floci/services/cloudformation/provisioners/DynamoDbCfnProvisioner.java @@ -562,6 +562,17 @@ public class DynamoDbCfnProvisioner implements CfnResourceProvisioner { return resource.getAttributes().containsKey(CfnRollback.DYNAMODB_TABLE_UPDATE_SNAPSHOT_ATTR); } + /** + * The engine calls this for every resource once a stack update commits. A committed update is + * never rolled back, so its snapshot is dropped here: left on the resource, a retried rollback + * of a later update would put the table back to its state before this one. + */ + @Override + public UpdateCleanupResult completeUpdate(StackResource resource) { + resource.getAttributes().remove(CfnRollback.DYNAMODB_TABLE_UPDATE_SNAPSHOT_ATTR); + return UpdateCleanupResult.notApplicable(); + } + /** * Puts back the tags, stream setting, TTL setting and Kinesis streaming destinations an in-place * table update changed. All four go back or none is claimed: restoring one alone would report a diff --git a/src/test/java/io/github/hectorvent/floci/services/cloudformation/CloudFormationDynamoDbTimeToLiveIntegrationTest.java b/src/test/java/io/github/hectorvent/floci/services/cloudformation/CloudFormationDynamoDbTimeToLiveIntegrationTest.java index f8d14846d4..95b5fe9853 100644 --- a/src/test/java/io/github/hectorvent/floci/services/cloudformation/CloudFormationDynamoDbTimeToLiveIntegrationTest.java +++ b/src/test/java/io/github/hectorvent/floci/services/cloudformation/CloudFormationDynamoDbTimeToLiveIntegrationTest.java @@ -15,6 +15,7 @@ import static org.hamcrest.Matchers.contains; import static org.hamcrest.Matchers.containsString; import static org.hamcrest.Matchers.equalTo; import static org.hamcrest.Matchers.nullValue; +import static org.junit.jupiter.api.Assertions.assertAll; import static org.junit.jupiter.api.Assertions.assertEquals; import static org.mockito.ArgumentMatchers.anyBoolean; import static org.mockito.ArgumentMatchers.anyString; @@ -185,6 +186,69 @@ class CloudFormationDynamoDbTimeToLiveIntegrationTest { deleteStack(stack); } + /** + * A committed in-place update is never rolled back. A later replacing update fails and cannot + * delete the table it created, so its rollback fails; retrying that rollback must keep the tags + * the committed update set rather than put back the ones from before it. + */ + @Test + void retryingAFailedReplacementRollbackKeepsTheCommittedTags() { + String suffix = Long.toString(System.nanoTime(), 36); + String stack = "cfn-ddb-ttl-retry-" + suffix; + String table = "ttl-retry-table-" + suffix; + String replacement = "ttl-retry-new-" + suffix; + + Throwable testFailure = null; + try { + cloudFormation(stack, "CreateStack", TEMPLATE.formatted(table, "dev", "", "")); + assertEquals("CREATE_COMPLETE", CfnStackWaits.awaitTerminal(stack, CFN_AUTH).status()); + String tableArn = dynamoDb("DescribeTable", "{\"TableName\": \"" + table + "\"}") + .statusCode(200) + .extract().path("Table.TableArn"); + cloudFormation(stack, "UpdateStack", TEMPLATE.formatted(table, "prod", "", "")); + assertEquals("UPDATE_COMPLETE", CfnStackWaits.awaitTerminal(stack, CFN_AUTH).status()); + + failNextUpdateTimeToLive(); + Mockito.doThrow(new IllegalStateException("simulated DeleteTable failure")) + .doCallRealMethod() + .when(dynamoDbService) + .deleteTable(anyString(), anyString()); + try { + cloudFormation(stack, "UpdateStack", TEMPLATE.formatted(replacement, "prod", TTL_ENABLED, "")); + assertEquals("UPDATE_ROLLBACK_FAILED", CfnStackWaits.awaitTerminal(stack, CFN_AUTH).status()); + } finally { + Mockito.doCallRealMethod().when(dynamoDbService) + .updateTimeToLive(anyString(), anyString(), anyBoolean(), anyString()); + Mockito.doCallRealMethod().when(dynamoDbService).deleteTable(anyString(), anyString()); + } + + CfnStackWaits.continueUpdateRollback(stack, CFN_AUTH); + assertEquals("UPDATE_ROLLBACK_FAILED", CfnStackWaits.awaitTerminal(stack, CFN_AUTH).status(), + "a replacing table update still has no rollback, so the retry fails again"); + + dynamoDb("ListTagsOfResource", "{\"ResourceArn\": \"" + tableArn + "\"}") + .statusCode(200) + .body("Tags.Value", contains("prod")); + + CfnStackWaits.continueUpdateRollback(stack, CFN_AUTH, "Orders"); + assertEquals("UPDATE_ROLLBACK_COMPLETE", CfnStackWaits.awaitTerminal(stack, CFN_AUTH).status()); + } catch (RuntimeException | Error failure) { + testFailure = failure; + throw failure; + } finally { + try { + assertAll("retry test cleanup", + () -> dynamoDb("DeleteTable", "{\"TableName\": \"" + replacement + "\"}"), + () -> deleteStack(stack)); + } catch (RuntimeException | Error cleanupFailure) { + if (testFailure == null) { + throw cleanupFailure; + } + testFailure.addSuppressed(cleanupFailure); + } + } + } + /** A create that fails after the table exists rolls back to no table, not to an orphan. */ @Test void aCreateThatFailsAfterTheTableExistsLeavesNoTable() { diff --git a/src/test/java/io/github/hectorvent/floci/testing/CfnStackWaits.java b/src/test/java/io/github/hectorvent/floci/testing/CfnStackWaits.java index 02d7862726..157915f722 100644 --- a/src/test/java/io/github/hectorvent/floci/testing/CfnStackWaits.java +++ b/src/test/java/io/github/hectorvent/floci/testing/CfnStackWaits.java @@ -178,6 +178,25 @@ public final class CfnStackWaits { awaitStackDeleted(stackName, authorization); } + /** + * Sends ContinueUpdateRollback and checks it was accepted; the caller waits for the outcome. + * {@code authorization} may be null, and each of {@code resourcesToSkip} is sent as a + * ResourcesToSkip member. + */ + public static void continueUpdateRollback(String stackName, String authorization, String... resourcesToSkip) { + RequestSpecification request = given() + .contentType("application/x-www-form-urlencoded") + .formParam("Action", "ContinueUpdateRollback") + .formParam("StackName", stackName); + if (authorization != null) { + request.header("Authorization", authorization); + } + for (int i = 0; i < resourcesToSkip.length; i++) { + request.formParam("ResourcesToSkip.member." + (i + 1), resourcesToSkip[i]); + } + request.when().post("/").then().statusCode(200); + } + /** * Waits until DescribeStacks answers 400, which is how a missing stack is reported. Fails on * DELETE_FAILED or after ten seconds.