diff --git a/src/main/java/io/floci/aws/services/cloudformation/CloudFormationService.java b/src/main/java/io/floci/aws/services/cloudformation/CloudFormationService.java index 605394306a..9baa36aea9 100644 --- a/src/main/java/io/floci/aws/services/cloudformation/CloudFormationService.java +++ b/src/main/java/io/floci/aws/services/cloudformation/CloudFormationService.java @@ -1847,7 +1847,7 @@ public class CloudFormationService implements ResourceProvider { List events, Map changeSets, Map changeSetStates, - String stackPolicyBody) {} + String stackPolicyBody, String originalTemplateBody) {} // compute() holds the stack's per-key lock for the whole claim, so only one racing execution can win. private Future claimAndSubmitExecution(String stackNameOrArn, String changeSetName, String region, @@ -1898,6 +1898,11 @@ public class CloudFormationService implements ResourceProvider { if (isCreate && cs.getStackPolicyBody() != null) { existing.setStackPolicyBody(cs.getStackPolicyBody()); } + if (isCreate) { + // The stack has its template once CreateStack answers, as GetTemplate and GetTemplateSummary read + // it on AWS, not only once its creation starts, which waits for a free operation thread. + existing.setOriginalTemplateBody(cs.getTemplateBody()); + } existing.setClientRequestToken(clientRequestToken); existing.setStatus(isCreate ? "CREATE_IN_PROGRESS" : "UPDATE_IN_PROGRESS"); existing.setLastUpdatedTime(now()); @@ -2260,7 +2265,8 @@ public class CloudFormationService implements ResourceProvider { states.put(changeSet, new ChangeSetState(changeSet.getStatus(), changeSet.getExecutionStatus())); } return new StackMutationSnapshot(stack.getStatus(), stack.getLastUpdatedTime(), - stack.eventsSnapshot(), stack.changeSetsSnapshot(), states, stack.getStackPolicyBody()); + stack.eventsSnapshot(), stack.changeSetsSnapshot(), states, stack.getStackPolicyBody(), + stack.getOriginalTemplateBody()); } private void restore(Stack stack, StackMutationSnapshot snapshot) { @@ -2270,6 +2276,7 @@ public class CloudFormationService implements ResourceProvider { stack.setStatus(snapshot.status()); stack.setLastUpdatedTime(snapshot.lastUpdatedTime()); stack.setStackPolicyBody(snapshot.stackPolicyBody()); + stack.setOriginalTemplateBody(snapshot.originalTemplateBody()); stack.setEvents(new ArrayList<>(snapshot.events())); stack.setChangeSets(new LinkedHashMap<>(snapshot.changeSets())); snapshot.changeSetStates().forEach((changeSet, state) -> { diff --git a/src/test/java/io/floci/aws/services/cloudformation/CloudFormationTemplateOnCreateTest.java b/src/test/java/io/floci/aws/services/cloudformation/CloudFormationTemplateOnCreateTest.java new file mode 100644 index 0000000000..2e53a316bc --- /dev/null +++ b/src/test/java/io/floci/aws/services/cloudformation/CloudFormationTemplateOnCreateTest.java @@ -0,0 +1,83 @@ +package io.floci.aws.services.cloudformation; + +import io.floci.aws.services.cloudformation.model.StackResource; +import io.floci.aws.services.cloudformation.provisioners.CfnResourceDispatcher; +import org.junit.jupiter.api.Test; + +import java.util.ArrayList; +import java.util.List; +import java.util.Map; +import java.util.concurrent.CountDownLatch; +import java.util.concurrent.Future; +import java.util.concurrent.Semaphore; +import java.util.concurrent.ThreadPoolExecutor; +import java.util.concurrent.TimeUnit; + +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertTrue; +import static org.mockito.Mockito.RETURNS_DEFAULTS; +import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.withSettings; + +/** + * A created stack has its template once CreateStack answers, as on AWS, though its creation waits for a free operation + * thread: GetTemplate and GetTemplateSummary read it while every thread runs another stack's creation. + */ +class CloudFormationTemplateOnCreateTest { + + private static final String REGION = "us-east-1"; + private static final String ACCOUNT = "000000000000"; + private static final String HELD = "{\"Resources\":{\"Held\":{\"Type\":\"AWS::SNS::Topic\",\"Properties\":{}}}}"; + private static final String QUEUED = + "{\"Resources\":{\"Queued\":{\"Type\":\"AWS::SQS::Queue\",\"Properties\":{}}}}"; + + @Test + void aStackWaitingForAnOperationThreadHasTheTemplateItWasCreatedWith() throws Exception { + Semaphore provisioning = new Semaphore(0); + CountDownLatch release = new CountDownLatch(1); + CfnResourceDispatcher dispatcher = mock(CfnResourceDispatcher.class, + withSettings().defaultAnswer(invocation -> "provision".equals(invocation.getMethod().getName()) + ? heldResource(invocation.getArgument(0), invocation.getArgument(1), provisioning, release) + : RETURNS_DEFAULTS.answer(invocation))); + CloudFormationService service = CloudFormationServiceFixture.create(dispatcher); + ThreadPoolExecutor pool = CloudFormationService.newOperationExecutor(); + int threads = pool.getMaximumPoolSize(); + pool.shutdown(); + List> runs = new ArrayList<>(); + try { + for (int i = 0; i < threads; i++) { + runs.add(create(service, "held-" + i, HELD)); + } + assertTrue(provisioning.tryAcquire(threads, 30, TimeUnit.SECONDS), "not every operation thread is held"); + runs.add(create(service, "queued", QUEUED)); + + assertEquals(QUEUED, service.getTemplate("queued", null, REGION)); + assertEquals(List.of("AWS::SQS::Queue"), + service.getTemplateSummary("queued", null, null, REGION).resourceTypes()); + } finally { + release.countDown(); + } + for (Future run : runs) { + run.get(30, TimeUnit.SECONDS); + } + } + + /** A resource created once {@code release} lets it, counted on {@code provisioning} when its creation starts. */ + private static StackResource heldResource(String logicalId, String type, Semaphore provisioning, + CountDownLatch release) throws InterruptedException { + provisioning.release(); + assertTrue(release.await(30, TimeUnit.SECONDS), "the held creations were not released"); + StackResource resource = new StackResource(); + resource.setLogicalId(logicalId); + resource.setResourceType(type); + resource.setPhysicalId(logicalId + "-id"); + resource.setStatus("CREATE_COMPLETE"); + return resource; + } + + private static Future create(CloudFormationService service, String stackName, String template) { + service.createChangeSet(stackName, "create", "CREATE", template, null, Map.of(), List.of(), Map.of(), REGION, + ACCOUNT); + return service.executeChangeSet(stackName, "create", REGION, ACCOUNT); + } +}