mirror of
https://github.com/floci-io/floci.git
synced 2026-12-05 11:07:59 -05:00
fix(codepipeline): preserve external actions while stopping (#3726)
* fix(codepipeline): preserve external actions while stopping * fix(codepipeline): publish stop mode across threads --------- Co-authored-by: xujiantop-crypto <265865031+xujiantop-crypto@users.noreply.github.com>
This commit is contained in:
1 parent
bd005ac9c7
commit
b3d46d5024
3 files changed
+310
-13
No files matched your search
+264
@@ -7,7 +7,9 @@ import io.github.hectorvent.floci.core.common.AwsException;
|
||||
import io.github.hectorvent.floci.core.storage.AccountAwareStorageBackend;
|
||||
import io.github.hectorvent.floci.core.storage.StorageFactory;
|
||||
import io.github.hectorvent.floci.services.codebuild.CodeBuildService;
|
||||
import io.github.hectorvent.floci.services.codebuild.model.Build;
|
||||
import io.github.hectorvent.floci.services.codedeploy.CodeDeployService;
|
||||
import io.github.hectorvent.floci.services.codedeploy.model.Deployment;
|
||||
import io.github.hectorvent.floci.services.codepipeline.model.CodePipelinePipeline;
|
||||
import io.github.hectorvent.floci.services.lambda.LambdaService;
|
||||
import io.github.hectorvent.floci.services.s3.S3Service;
|
||||
@@ -19,12 +21,17 @@ import java.util.concurrent.CompletableFuture;
|
||||
import java.util.concurrent.CountDownLatch;
|
||||
import java.util.concurrent.TimeUnit;
|
||||
import java.util.concurrent.atomic.AtomicInteger;
|
||||
import java.util.concurrent.atomic.AtomicReference;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
import static org.junit.jupiter.api.Assertions.assertFalse;
|
||||
import static org.junit.jupiter.api.Assertions.assertThrows;
|
||||
import static org.junit.jupiter.api.Assertions.assertTrue;
|
||||
import static org.mockito.ArgumentMatchers.any;
|
||||
import static org.mockito.ArgumentMatchers.anyString;
|
||||
import static org.mockito.Mockito.mock;
|
||||
import static org.mockito.Mockito.never;
|
||||
import static org.mockito.Mockito.verify;
|
||||
import static org.mockito.Mockito.when;
|
||||
|
||||
class CodePipelineServiceTest {
|
||||
@@ -170,6 +177,169 @@ class CodePipelineServiceTest {
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
void stoppingCodeBuildWaitsForSuccessWithoutStoppingTheBuild() throws Exception {
|
||||
CodeBuildService codeBuildService = mock(CodeBuildService.class);
|
||||
AtomicReference<Build> currentBuild = new AtomicReference<>(build("IN_PROGRESS", false));
|
||||
CountDownLatch buildStarted = new CountDownLatch(1);
|
||||
when(codeBuildService.startBuild(anyString(), anyString(), anyString(),
|
||||
any(), any(), any(), any(), any(), any(), any())).thenAnswer(invocation -> {
|
||||
buildStarted.countDown();
|
||||
return currentBuild.get();
|
||||
});
|
||||
when(codeBuildService.getBuild(anyString(), anyString(), anyString()))
|
||||
.thenAnswer(invocation -> currentBuild.get());
|
||||
CodePipelineService pipelineService = serviceWith(codeBuildService, mock(CodeDeployService.class));
|
||||
String pipelineName = "codebuild-stop-wait";
|
||||
|
||||
try {
|
||||
createExternalActionPipeline(pipelineService, pipelineName, """
|
||||
{
|
||||
"name": "BuildAction",
|
||||
"actionTypeId": {
|
||||
"category": "Build",
|
||||
"owner": "AWS",
|
||||
"provider": "CodeBuild",
|
||||
"version": "1"
|
||||
},
|
||||
"configuration": {"ProjectName": "build-project"}
|
||||
}
|
||||
""");
|
||||
String executionId = startExecution(pipelineService, pipelineName);
|
||||
assertTrue(buildStarted.await(2, TimeUnit.SECONDS));
|
||||
|
||||
stopExecution(pipelineService, pipelineName, executionId, false);
|
||||
waitForExecutionStatus(pipelineService, pipelineName, executionId, "Stopping");
|
||||
currentBuild.set(build("SUCCEEDED", true));
|
||||
|
||||
waitForExecutionStatus(pipelineService, pipelineName, executionId, "Stopped");
|
||||
JsonNode state = pipelineState(pipelineService, pipelineName);
|
||||
assertEquals("Stopped", state.path("stageStates").path(0)
|
||||
.path("latestExecution").path("status").asText());
|
||||
assertEquals("Succeeded", state.path("stageStates").path(0).path("actionStates").path(0)
|
||||
.path("latestExecution").path("status").asText());
|
||||
verify(codeBuildService, never()).stopBuild(anyString(), anyString(), anyString());
|
||||
} finally {
|
||||
pipelineService.shutdown();
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
void stoppingCodeDeployWaitsForFailureWithoutFailingThePipeline() throws Exception {
|
||||
CodeDeployService codeDeployService = mock(CodeDeployService.class);
|
||||
AtomicReference<Deployment> currentDeployment = new AtomicReference<>(deployment("InProgress"));
|
||||
CountDownLatch deploymentStarted = new CountDownLatch(1);
|
||||
when(codeDeployService.createDeployment(anyString(), anyString(), anyString(),
|
||||
any(), any(), anyString())).thenAnswer(invocation -> {
|
||||
deploymentStarted.countDown();
|
||||
return "deployment-1";
|
||||
});
|
||||
when(codeDeployService.getDeployment(anyString(), anyString()))
|
||||
.thenAnswer(invocation -> currentDeployment.get());
|
||||
CodePipelineService pipelineService = serviceWith(mock(CodeBuildService.class), codeDeployService);
|
||||
String pipelineName = "codedeploy-stop-wait";
|
||||
|
||||
try {
|
||||
createExternalActionPipeline(pipelineService, pipelineName, """
|
||||
{
|
||||
"name": "DeployAction",
|
||||
"actionTypeId": {
|
||||
"category": "Deploy",
|
||||
"owner": "AWS",
|
||||
"provider": "CodeDeploy",
|
||||
"version": "1"
|
||||
},
|
||||
"configuration": {
|
||||
"ApplicationName": "application",
|
||||
"DeploymentGroupName": "deployment-group"
|
||||
}
|
||||
}
|
||||
""");
|
||||
String executionId = startExecution(pipelineService, pipelineName);
|
||||
assertTrue(deploymentStarted.await(2, TimeUnit.SECONDS));
|
||||
|
||||
stopExecution(pipelineService, pipelineName, executionId, false);
|
||||
waitForExecutionStatus(pipelineService, pipelineName, executionId, "Stopping");
|
||||
currentDeployment.set(deployment("Failed"));
|
||||
|
||||
waitForExecutionStatus(pipelineService, pipelineName, executionId, "Stopped");
|
||||
JsonNode state = pipelineState(pipelineService, pipelineName);
|
||||
assertEquals("Failed", state.path("stageStates").path(0)
|
||||
.path("latestExecution").path("status").asText());
|
||||
assertEquals("Failed", state.path("stageStates").path(0).path("actionStates").path(0)
|
||||
.path("latestExecution").path("status").asText());
|
||||
verify(codeDeployService, never()).stopDeployment(anyString(), anyString());
|
||||
} finally {
|
||||
pipelineService.shutdown();
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
void abandoningExternalActionsStopsWaitingWithoutStoppingThem() throws Exception {
|
||||
CodeBuildService codeBuildService = mock(CodeBuildService.class);
|
||||
CodeDeployService codeDeployService = mock(CodeDeployService.class);
|
||||
CountDownLatch actionsStarted = new CountDownLatch(2);
|
||||
Build runningBuild = build("IN_PROGRESS", false);
|
||||
Deployment runningDeployment = deployment("InProgress");
|
||||
when(codeBuildService.startBuild(anyString(), anyString(), anyString(),
|
||||
any(), any(), any(), any(), any(), any(), any())).thenAnswer(invocation -> {
|
||||
actionsStarted.countDown();
|
||||
return runningBuild;
|
||||
});
|
||||
when(codeBuildService.getBuild(anyString(), anyString(), anyString())).thenReturn(runningBuild);
|
||||
when(codeDeployService.createDeployment(anyString(), anyString(), anyString(),
|
||||
any(), any(), anyString())).thenAnswer(invocation -> {
|
||||
actionsStarted.countDown();
|
||||
return "deployment-1";
|
||||
});
|
||||
when(codeDeployService.getDeployment(anyString(), anyString())).thenReturn(runningDeployment);
|
||||
CodePipelineService pipelineService = serviceWith(codeBuildService, codeDeployService);
|
||||
String pipelineName = "external-actions-abandon";
|
||||
|
||||
try {
|
||||
createExternalActionPipeline(pipelineService, pipelineName, """
|
||||
{
|
||||
"name": "BuildAction",
|
||||
"actionTypeId": {
|
||||
"category": "Build",
|
||||
"owner": "AWS",
|
||||
"provider": "CodeBuild",
|
||||
"version": "1"
|
||||
},
|
||||
"configuration": {"ProjectName": "build-project"}
|
||||
},
|
||||
{
|
||||
"name": "DeployAction",
|
||||
"actionTypeId": {
|
||||
"category": "Deploy",
|
||||
"owner": "AWS",
|
||||
"provider": "CodeDeploy",
|
||||
"version": "1"
|
||||
},
|
||||
"configuration": {
|
||||
"ApplicationName": "application",
|
||||
"DeploymentGroupName": "deployment-group"
|
||||
}
|
||||
}
|
||||
""");
|
||||
String executionId = startExecution(pipelineService, pipelineName);
|
||||
assertTrue(actionsStarted.await(2, TimeUnit.SECONDS));
|
||||
|
||||
stopExecution(pipelineService, pipelineName, executionId, true);
|
||||
|
||||
waitForExecutionStatus(pipelineService, pipelineName, executionId, "Stopped");
|
||||
JsonNode state = pipelineState(pipelineService, pipelineName);
|
||||
assertEquals("Abandoned", state.path("stageStates").path(0).path("actionStates").path(0)
|
||||
.path("latestExecution").path("status").asText());
|
||||
assertEquals("Abandoned", state.path("stageStates").path(0).path("actionStates").path(1)
|
||||
.path("latestExecution").path("status").asText());
|
||||
verify(codeBuildService, never()).stopBuild(anyString(), anyString(), anyString());
|
||||
verify(codeDeployService, never()).stopDeployment(anyString(), anyString());
|
||||
} finally {
|
||||
pipelineService.shutdown();
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
void aStartAfterShutdownIsRefusedAndTheExecutionIsPersistedAsFailed() throws Exception {
|
||||
handle("CreatePipeline", """
|
||||
@@ -221,6 +391,100 @@ class CodePipelineServiceTest {
|
||||
return service.handle(action, mapper.readTree(body), REGION, ACCOUNT);
|
||||
}
|
||||
|
||||
private CodePipelineService serviceWith(CodeBuildService codeBuildService,
|
||||
CodeDeployService codeDeployService) {
|
||||
return new CodePipelineService(
|
||||
new InMemoryStorageFactory(), mapper, codeBuildService, codeDeployService,
|
||||
mock(LambdaService.class), mock(S3Service.class));
|
||||
}
|
||||
|
||||
private void createExternalActionPipeline(CodePipelineService pipelineService,
|
||||
String pipelineName, String actions) throws Exception {
|
||||
handle(pipelineService, "CreatePipeline", """
|
||||
{
|
||||
"pipeline": {
|
||||
"name": "%s",
|
||||
"roleArn": "arn:aws:iam::000000000000:role/codepipeline-role",
|
||||
"artifactStore": {"type": "S3", "location": "codepipeline-artifacts"},
|
||||
"stages": [{
|
||||
"name": "External",
|
||||
"actions": [%s]
|
||||
}, {
|
||||
"name": "Complete",
|
||||
"actions": [{
|
||||
"name": "ManualApproval",
|
||||
"actionTypeId": {
|
||||
"category": "Approval",
|
||||
"owner": "AWS",
|
||||
"provider": "Manual",
|
||||
"version": "1"
|
||||
}
|
||||
}]
|
||||
}]
|
||||
}
|
||||
}
|
||||
""".formatted(pipelineName, actions));
|
||||
}
|
||||
|
||||
private String startExecution(CodePipelineService pipelineService, String pipelineName) throws Exception {
|
||||
return handle(pipelineService, "StartPipelineExecution", """
|
||||
{"name": "%s"}
|
||||
""".formatted(pipelineName)).path("pipelineExecutionId").asText();
|
||||
}
|
||||
|
||||
private void stopExecution(CodePipelineService pipelineService, String pipelineName,
|
||||
String executionId, boolean abandon) throws Exception {
|
||||
handle(pipelineService, "StopPipelineExecution", """
|
||||
{
|
||||
"pipelineName": "%s",
|
||||
"pipelineExecutionId": "%s",
|
||||
"abandon": %s
|
||||
}
|
||||
""".formatted(pipelineName, executionId, abandon));
|
||||
}
|
||||
|
||||
private JsonNode waitForExecutionStatus(CodePipelineService pipelineService, String pipelineName,
|
||||
String executionId, String expectedStatus) throws Exception {
|
||||
long deadline = System.nanoTime() + TimeUnit.SECONDS.toNanos(3);
|
||||
JsonNode execution;
|
||||
do {
|
||||
execution = handle(pipelineService, "GetPipelineExecution", """
|
||||
{"pipelineName": "%s", "pipelineExecutionId": "%s"}
|
||||
""".formatted(pipelineName, executionId)).path("pipelineExecution");
|
||||
if (expectedStatus.equals(execution.path("status").asText())) {
|
||||
return execution;
|
||||
}
|
||||
Thread.sleep(25);
|
||||
} while (System.nanoTime() < deadline);
|
||||
throw new AssertionError("Pipeline did not reach " + expectedStatus
|
||||
+ "; last status was " + execution.path("status").asText());
|
||||
}
|
||||
|
||||
private JsonNode pipelineState(CodePipelineService pipelineService, String pipelineName) throws Exception {
|
||||
return handle(pipelineService, "GetPipelineState", """
|
||||
{"name": "%s"}
|
||||
""".formatted(pipelineName));
|
||||
}
|
||||
|
||||
private JsonNode handle(CodePipelineService pipelineService, String action, String body) throws Exception {
|
||||
return pipelineService.handle(action, mapper.readTree(body), REGION, ACCOUNT);
|
||||
}
|
||||
|
||||
private Build build(String status, boolean complete) {
|
||||
Build build = new Build();
|
||||
build.setId("build-1");
|
||||
build.setBuildStatus(status);
|
||||
build.setBuildComplete(complete);
|
||||
return build;
|
||||
}
|
||||
|
||||
private Deployment deployment(String status) {
|
||||
Deployment deployment = new Deployment();
|
||||
deployment.setDeploymentId("deployment-1");
|
||||
deployment.setStatus(status);
|
||||
return deployment;
|
||||
}
|
||||
|
||||
private static final class InMemoryStorageFactory extends StorageFactory {
|
||||
private InMemoryStorageFactory() {
|
||||
super(null, null);
|
||||
|
||||
Reference in new issue
Block a user