From 15038c17bcde154e3a4ab46a919b9c4e5fea5152 Mon Sep 17 00:00:00 2001 From: Sean-DNAstack Date: Tue, 24 Oct 2023 14:13:49 -0400 Subject: [PATCH] Optimize & clean up e2e tests --- .../com/dnastack/wes/service/WesE2ETest.java | 83 ++++++++++--------- .../local/LocalBlobStorageClientTest.java | 27 +++--- 2 files changed, 58 insertions(+), 52 deletions(-) diff --git a/e2e-tests/src/main/java/com/dnastack/wes/service/WesE2ETest.java b/e2e-tests/src/main/java/com/dnastack/wes/service/WesE2ETest.java index e183fcd..b0e9eeb 100644 --- a/e2e-tests/src/main/java/com/dnastack/wes/service/WesE2ETest.java +++ b/e2e-tests/src/main/java/com/dnastack/wes/service/WesE2ETest.java @@ -8,15 +8,20 @@ import io.restassured.builder.MultiPartSpecBuilder; import io.restassured.http.ContentType; import io.restassured.specification.MultiPartSpecification; +import org.awaitility.Awaitility; import org.awaitility.core.ConditionFactory; import org.junit.jupiter.api.*; import org.junit.jupiter.api.TestInstance.Lifecycle; +import org.junit.jupiter.params.ParameterizedTest; +import org.junit.jupiter.params.provider.Arguments; +import org.junit.jupiter.params.provider.MethodSource; import java.io.ByteArrayInputStream; import java.io.IOException; import java.nio.charset.StandardCharsets; import java.time.Duration; import java.util.*; +import java.util.stream.Stream; import static io.restassured.RestAssured.given; import static org.awaitility.Awaitility.with; @@ -431,7 +436,6 @@ public void uploadWorkflowAttachmentWithRunSubmission() throws Exception { public class RunMethodsWithExistingJobs { String workflowJobId; - String workflowJobIdWithAllOutputTypes; @BeforeAll public void setup() throws InterruptedException { @@ -451,22 +455,6 @@ public void setup() throws InterruptedException { .multiPart(getJsonMultipart("tags", tags)) .multiPart(getJsonMultipart("workflow_params", inputs)) .post(path) - .then() - .assertThat() - .statusCode(200) - .body("run_id",is(notNullValue())) - .extract() - .jsonPath() - .getString("run_id"); - - workflowJobIdWithAllOutputTypes = given() - .log().uri() - .log().method() - .header(getHeader(getResource(path))) - .multiPart(getWorkflowUrlMultipart("echo.wdl")) - .multiPart(getMultipartAttachment("echo.wdl",supplier.getFileContent(WdlSupplier.WORKFLOW_WITH_ALL_OUTPUT_TYPES).getBytes())) - .multiPart(getJsonMultipart("workflow_params", inputs)) - .post(path) .then() .assertThat() .statusCode(200) @@ -585,17 +573,17 @@ public void listRunsReturnsReturnsNonEmptyCollection() { } - @Test + @ParameterizedTest + @MethodSource("completeWorkflowWithFilesProvider") @DisplayName("Get Run Files for existing run returns all files") - public void getRunFilesReturnsNonEmptyCollection() throws Exception { - String path = getRootPath() + "/runs/" + workflowJobIdWithAllOutputTypes + "/files"; - pollUntilJobCompletes(workflowJobIdWithAllOutputTypes); + public void getRunFilesReturnsNonEmptyCollection(String runId) { + String path = getRootPath() + "/runs/" + runId + "/files"; //@formatter:off given() .log().uri() .log().method() - .header(getHeader(getResource(getRootPath() + "/runs/" + workflowJobIdWithAllOutputTypes))) + .header(getHeader(getResource(getRootPath() + "/runs/" + runId))) .accept(ContentType.JSON) .get(path) .then() @@ -627,18 +615,17 @@ public void getRunFilesForNonExistentRunShouldFail() { } - @Test + @ParameterizedTest + @MethodSource("completeWorkflowWithFilesProvider") @DisplayName("Delete Run Files for existing run returns all deleted files") - public void deleteRunFilesReturnsNonEmptyCollection() throws Exception { - reRunWorkflowWithAllOutputTypes(); - String path = getRootPath() + "/runs/" + workflowJobIdWithAllOutputTypes + "/files"; - pollUntilJobCompletes(workflowJobIdWithAllOutputTypes); + public void deleteRunFilesReturnsNonEmptyCollection(String runId) { + String path = getRootPath() + "/runs/" + runId + "/files"; //@formatter:off given() .log().uri() .log().method() - .header(getHeader(getResource(getRootPath() + "/runs/" + workflowJobIdWithAllOutputTypes))) + .header(getHeader(getResource(getRootPath() + "/runs/" + runId))) .accept(ContentType.JSON) .delete(path) .then() @@ -650,18 +637,17 @@ public void deleteRunFilesReturnsNonEmptyCollection() throws Exception { } - @Test + @ParameterizedTest + @MethodSource("completeWorkflowWithFilesProvider") @DisplayName("Delete Run Files for existing run asynchronously returns all deleted files") - public void deleteRunFilesAsyncReturnsNonEmptyCollection() throws Exception { - reRunWorkflowWithAllOutputTypes(); - String path = getRootPath() + "/runs/" + workflowJobIdWithAllOutputTypes + "/files"; - pollUntilJobCompletes(workflowJobIdWithAllOutputTypes); + public void deleteRunFilesAsyncReturnsNonEmptyCollection(String runId) { + String path = getRootPath() + "/runs/" + runId + "/files"; //@formatter:off given() .log().uri() .log().method() - .header(getHeader(getResource(getRootPath() + "/runs/" + workflowJobIdWithAllOutputTypes))) + .header(getHeader(getResource(getRootPath() + "/runs/" + runId))) .accept(ContentType.JSON) .queryParam("async", true) .delete(path) @@ -671,6 +657,24 @@ public void deleteRunFilesAsyncReturnsNonEmptyCollection() throws Exception { .body("deletions.size()", greaterThan(0)) .body("deletions.every { it.path != null && it.file_type == 'SECONDARY' && it.state == 'ASYNC' }", equalTo(true)); //@formatter:on + + Awaitility.await() + .atMost(Duration.ofSeconds(30)) + .pollInterval(Duration.ofSeconds(5)) + .untilAsserted(() -> + //@formatter:off + given() + .log().uri() + .log().method() + .header(getHeader(getResource(getRootPath() + "/runs/" + runId))) + .accept(ContentType.JSON) + .get(path) + .then() + .assertThat() + .statusCode(200) + .body("runFiles.size()", greaterThan(0)) + .body("runFiles.every { it.path != null && it.file_type in ['FINAL', 'LOG'] }", equalTo(true))); + //@formatter:on } @@ -694,27 +698,30 @@ public void deleteRunFilesForNonExistentRunShouldFail() { } - private void reRunWorkflowWithAllOutputTypes() { + private Stream completeWorkflowWithFilesProvider() throws Exception { String path = getRootPath() + "/runs"; Map inputs = Collections.singletonMap("hello_world.name", "Some sort of String"); //@formatter:off - workflowJobIdWithAllOutputTypes = given() + String workflowJobIdWithAllOutputTypes = given() .log().uri() .log().method() .header(getHeader(getResource(path))) .multiPart(getWorkflowUrlMultipart("echo.wdl")) - .multiPart(getMultipartAttachment("echo.wdl",supplier.getFileContent(WdlSupplier.WORKFLOW_WITH_ALL_OUTPUT_TYPES).getBytes())) + .multiPart(getMultipartAttachment("echo.wdl", supplier.getFileContent(WdlSupplier.WORKFLOW_WITH_ALL_OUTPUT_TYPES).getBytes())) .multiPart(getJsonMultipart("workflow_params", inputs)) .post(path) .then() .assertThat() .statusCode(200) - .body("run_id",is(notNullValue())) + .body("run_id", is(notNullValue())) .extract() .jsonPath() .getString("run_id"); //@formatter:on + + pollUntilJobCompletes(workflowJobIdWithAllOutputTypes); + return Stream.of(Arguments.of(workflowJobIdWithAllOutputTypes)); } } diff --git a/src/test/java/com/dnastack/wes/storage/client/local/LocalBlobStorageClientTest.java b/src/test/java/com/dnastack/wes/storage/client/local/LocalBlobStorageClientTest.java index 80f832f..cd57938 100644 --- a/src/test/java/com/dnastack/wes/storage/client/local/LocalBlobStorageClientTest.java +++ b/src/test/java/com/dnastack/wes/storage/client/local/LocalBlobStorageClientTest.java @@ -62,9 +62,7 @@ public void testWritingBytesToFile_existingFileThrowsError() throws IOException @Test public void testReadingFile() throws IOException { LocalBlobStorageClient storageClient = new LocalBlobStorageClient(); - Path targetPath = Path.of(storageClient.getStagingPath() + "/" + directory + "/" + fileName); - Files.createDirectory(targetPath.getParent()); - Files.write(targetPath, toWrite.getBytes(), StandardOpenOption.CREATE_NEW); + Path targetPath = createFile(); ByteArrayOutputStream outputStream = new ByteArrayOutputStream(); storageClient.readBytes(outputStream, targetPath.toString(), HttpRange.createByteRange(0L,toWrite.length())); @@ -76,9 +74,7 @@ public void testReadingFile() throws IOException { @Test public void testReadingFile_withTruncation() throws IOException { LocalBlobStorageClient storageClient = new LocalBlobStorageClient(); - Path targetPath = Path.of(storageClient.getStagingPath() + "/" + directory + "/" + fileName); - Files.createDirectory(targetPath.getParent()); - Files.write(targetPath, toWrite.getBytes(), StandardOpenOption.CREATE_NEW); + Path targetPath = createFile(); ByteArrayOutputStream outputStream = new ByteArrayOutputStream(); @@ -90,9 +86,7 @@ public void testReadingFile_withTruncation() throws IOException { @Test public void testIsFile() throws IOException { LocalBlobStorageClient storageClient = new LocalBlobStorageClient(); - Path targetPath = Path.of(storageClient.getStagingPath() + "/" + directory + "/" + fileName); - Files.createDirectory(targetPath.getParent()); - Files.write(targetPath, toWrite.getBytes(), StandardOpenOption.CREATE_NEW); + Path targetPath = createFile(); Assertions.assertTrue(storageClient.isFile(targetPath.toString())); } @@ -100,7 +94,7 @@ public void testIsFile() throws IOException { @Test public void testIsFile_noFileExists() throws IOException { LocalBlobStorageClient storageClient = new LocalBlobStorageClient(); - Path targetPath = Path.of(storageClient.getStagingPath() + "/" + directory + "/" + fileName); + Path targetPath = Path.of(directory + "/" + fileName); Assertions.assertFalse(storageClient.isFile(targetPath.toString())); } @@ -108,9 +102,7 @@ public void testIsFile_noFileExists() throws IOException { @Test public void testDeletingFile() throws IOException { LocalBlobStorageClient storageClient = new LocalBlobStorageClient(); - Path targetPath = Path.of(storageClient.getStagingPath() + "/" + directory + "/" + fileName); - Files.createDirectory(targetPath.getParent()); - Files.write(targetPath, toWrite.getBytes(), StandardOpenOption.CREATE_NEW); + Path targetPath = createFile(); Assertions.assertTrue(Files.exists(targetPath)); storageClient.deleteFile(targetPath.toString()); @@ -120,10 +112,17 @@ public void testDeletingFile() throws IOException { @Test public void testDeletingFile_throwsError() throws IOException { LocalBlobStorageClient storageClient = new LocalBlobStorageClient(); - Path targetPath = Path.of(storageClient.getStagingPath() + "/" + directory + "/" + fileName); + Path targetPath = Path.of(directory + "/" + fileName); Assertions.assertFalse(Files.exists(targetPath)); Assertions.assertThrows(IOException.class, () -> storageClient.deleteFile(targetPath.toString())); } + private Path createFile() throws IOException { + Path tempDir = Files.createTempDirectory(directory); + Path targetPath = tempDir.resolve(fileName); + Files.write(targetPath, toWrite.getBytes(), StandardOpenOption.CREATE_NEW); + return targetPath; + } + } \ No newline at end of file