diff --git a/src/main/java/io/github/hectorvent/floci/services/codepipeline/CodePipelineService.java b/src/main/java/io/github/hectorvent/floci/services/codepipeline/CodePipelineService.java index e1a4ad5034..d2ce061469 100644 --- a/src/main/java/io/github/hectorvent/floci/services/codepipeline/CodePipelineService.java +++ b/src/main/java/io/github/hectorvent/floci/services/codepipeline/CodePipelineService.java @@ -1244,7 +1244,7 @@ public class CodePipelineService { stored.set("job", job); storeItem(execution.getAccountId(), execution.getRegion(), "job", jobId, "Created", stored); state.setExternalExecutionId(jobId); - while (!execution.isStopRequested()) { + while (!(execution.isStopRequested() && execution.isAbandon())) { CodePipelineStoredItem current = requireItem( execution.getAccountId(), execution.getRegion(), "job", jobId, "JobNotFoundException"); if ("Succeeded".equals(current.getStatus())) { @@ -1256,12 +1256,20 @@ public class CodePipelineService { return; } if ("Failed".equals(current.getStatus())) { - throw new AwsException("ActionExecutionFailed", - current.getData().path("result").path("failureDetails").path("message") - .asText("Custom action failed"), 400); + String message = current.getData().path("result").path("failureDetails").path("message") + .asText("Custom action failed"); + if (execution.isStopRequested()) { + state.setStatus("Failed"); + state.setSummary(message); + state.setErrorDetails(Map.of("code", "ActionExecutionFailed", "message", message)); + return; + } + throw new AwsException("ActionExecutionFailed", message, 400); } TimeUnit.MILLISECONDS.sleep(POLL_INTERVAL_MS); } + state.setStatus("Abandoned"); + state.setSummary("Action abandoned."); } private void applyExecutionMode(CodePipelineExecution execution) { diff --git a/src/test/java/io/github/hectorvent/floci/services/codepipeline/CodePipelineIntegrationTest.java b/src/test/java/io/github/hectorvent/floci/services/codepipeline/CodePipelineIntegrationTest.java index c4f7281166..0b6493c821 100644 --- a/src/test/java/io/github/hectorvent/floci/services/codepipeline/CodePipelineIntegrationTest.java +++ b/src/test/java/io/github/hectorvent/floci/services/codepipeline/CodePipelineIntegrationTest.java @@ -851,6 +851,191 @@ class CodePipelineIntegrationTest { """).then().statusCode(200); } + @Test + void stoppingCustomActionWaitsForWorkerSuccess() throws Exception { + String provider = "StopWaitSuccessWorker"; + String pipelineName = "custom-worker-stop-wait-success"; + createCustomWorkerPipeline(provider, pipelineName); + String executionId = post("StartPipelineExecution", """ + {"name": "%s"} + """.formatted(pipelineName)).then().extract().path("pipelineExecutionId"); + String jobId = waitForJob(provider).path("jobs[0].id"); + + post("StopPipelineExecution", """ + { + "pipelineName": "%s", + "pipelineExecutionId": "%s", + "reason": "Wait for the custom worker" + } + """.formatted(pipelineName, executionId)) + .then() + .statusCode(200); + + assertCustomWorkerIsStopping(pipelineName); + post("PutJobSuccessResult", """ + { + "jobId": "%s", + "executionDetails": { + "summary": "worker completed after stop request", + "externalExecutionId": "stop-wait-success", + "percentComplete": 100 + } + } + """.formatted(jobId)).then().statusCode(200); + + waitForExecution(pipelineName, executionId, "Stopped"); + post("GetPipelineState", """ + {"name": "%s"} + """.formatted(pipelineName)) + .then() + .statusCode(200) + .body("stageStates[0].latestExecution.status", equalTo("Stopped")) + .body("stageStates[0].actionStates[0].latestExecution.status", equalTo("Succeeded")) + .body("stageStates[1].latestExecution", nullValue()); + + deleteCustomWorkerPipeline(provider, pipelineName); + } + + @Test + void stoppingCustomActionWaitsForWorkerFailure() throws Exception { + String provider = "StopWaitFailureWorker"; + String pipelineName = "custom-worker-stop-wait-failure"; + createCustomWorkerPipeline(provider, pipelineName); + String executionId = post("StartPipelineExecution", """ + {"name": "%s"} + """.formatted(pipelineName)).then().extract().path("pipelineExecutionId"); + String jobId = waitForJob(provider).path("jobs[0].id"); + + post("StopPipelineExecution", """ + { + "pipelineName": "%s", + "pipelineExecutionId": "%s", + "reason": "Wait for the failing custom worker" + } + """.formatted(pipelineName, executionId)) + .then() + .statusCode(200); + + assertCustomWorkerIsStopping(pipelineName); + post("PutJobFailureResult", """ + { + "jobId": "%s", + "failureDetails": { + "type": "JobFailed", + "message": "worker failed after stop request", + "externalExecutionId": "stop-wait-failure" + } + } + """.formatted(jobId)).then().statusCode(200); + + waitForExecution(pipelineName, executionId, "Stopped"); + post("GetPipelineState", """ + {"name": "%s"} + """.formatted(pipelineName)) + .then() + .statusCode(200) + .body("stageStates[0].latestExecution.status", equalTo("Failed")) + .body("stageStates[0].actionStates[0].latestExecution.status", equalTo("Failed")) + .body("stageStates[1].latestExecution", nullValue()); + + deleteCustomWorkerPipeline(provider, pipelineName); + } + + @Test + void abandoningCustomActionStopsWaitingImmediately() throws Exception { + String provider = "AbandonWorker"; + String pipelineName = "custom-worker-stop-abandon"; + createCustomWorkerPipeline(provider, pipelineName); + String executionId = post("StartPipelineExecution", """ + {"name": "%s"} + """.formatted(pipelineName)).then().extract().path("pipelineExecutionId"); + waitForJob(provider); + + post("StopPipelineExecution", """ + { + "pipelineName": "%s", + "pipelineExecutionId": "%s", + "abandon": true, + "reason": "Abandon the custom worker" + } + """.formatted(pipelineName, executionId)) + .then() + .statusCode(200); + + waitForExecution(pipelineName, executionId, "Stopped"); + post("GetPipelineState", """ + {"name": "%s"} + """.formatted(pipelineName)) + .then() + .statusCode(200) + .body("stageStates[0].latestExecution.status", equalTo("Stopped")) + .body("stageStates[0].actionStates[0].latestExecution.status", equalTo("Abandoned")) + .body("stageStates[1].latestExecution", nullValue()); + + deleteCustomWorkerPipeline(provider, pipelineName); + } + + private void createCustomWorkerPipeline(String provider, String pipelineName) { + post("CreateCustomActionType", """ + { + "category": "Build", + "provider": "%s", + "version": "1", + "inputArtifactDetails": {"minimumCount": 0, "maximumCount": 0}, + "outputArtifactDetails": {"minimumCount": 0, "maximumCount": 0} + } + """.formatted(provider)) + .then() + .statusCode(200); + + post("CreatePipeline", pipeline(pipelineName, """ + { + "name": "Build", + "actions": [{ + "name": "WorkerBuild", + "actionTypeId": { + "category": "Build", + "owner": "Custom", + "provider": "%s", + "version": "1" + } + }] + }, + { + "name": "Complete", + "actions": [{ + "name": "ManualApproval", + "actionTypeId": { + "category": "Approval", + "owner": "AWS", + "provider": "Manual", + "version": "1" + } + }] + } + """.formatted(provider))).then().statusCode(200); + } + + private void assertCustomWorkerIsStopping(String pipelineName) { + post("GetPipelineState", """ + {"name": "%s"} + """.formatted(pipelineName)) + .then() + .statusCode(200) + .body("stageStates[0].latestExecution.status", equalTo("Stopping")) + .body("stageStates[0].actionStates[0].latestExecution.status", equalTo("InProgress")) + .body("stageStates[1].latestExecution", nullValue()); + } + + private void deleteCustomWorkerPipeline(String provider, String pipelineName) { + post("DeletePipeline", """ + {"name": "%s"} + """.formatted(pipelineName)).then().statusCode(200); + post("DeleteCustomActionType", """ + {"category": "Build", "provider": "%s", "version": "1"} + """.formatted(provider)).then().statusCode(200); + } + @Test void putApprovalResultApprovesAndRejectsManualApprovalActions() throws Exception { String pipelineName = "approval-test-pipeline"; @@ -1429,6 +1614,10 @@ class CodePipelineIntegrationTest { } private Response waitForJob() throws Exception { + return waitForJob("FlociWorker"); + } + + private Response waitForJob(String provider) throws Exception { Instant deadline = Instant.now().plus(Duration.ofSeconds(5)); Response response; do { @@ -1437,11 +1626,11 @@ class CodePipelineIntegrationTest { "actionTypeId": { "category": "Build", "owner": "Custom", - "provider": "FlociWorker", + "provider": "%s", "version": "1" } } - """); + """.formatted(provider)); if (response.jsonPath().getList("jobs").size() == 1) { return response; }