From ad2ee51d061042d34857966d27a8665b80697bbb Mon Sep 17 00:00:00 2001 From: Aqua-sc <108478185+Aqua-sc@users.noreply.github.com> Date: Tue, 7 May 2024 22:42:38 +0200 Subject: [PATCH] Updates too checks --- .../pidgeon/controllers/TestController.java | 49 +++++++++++-------- .../com/ugent/pidgeon/util/Filehandler.java | 10 +++- .../java/com/ugent/pidgeon/util/TestUtil.java | 21 +++++++- 3 files changed, 56 insertions(+), 24 deletions(-) diff --git a/backend/app/src/main/java/com/ugent/pidgeon/controllers/TestController.java b/backend/app/src/main/java/com/ugent/pidgeon/controllers/TestController.java index 2712b85c..c2cbb01f 100644 --- a/backend/app/src/main/java/com/ugent/pidgeon/controllers/TestController.java +++ b/backend/app/src/main/java/com/ugent/pidgeon/controllers/TestController.java @@ -61,7 +61,7 @@ public ResponseEntity updateTests( @RequestParam(name = "structuretest", required = false) String structureTest, @PathVariable("projectid") long projectId, Auth auth) { - return alterTests(projectId, auth.getUserEntity(), dockerImage, dockerTest, structureTest, dockerTemplate, HttpMethod.POST); + return alterTests(projectId, auth.getUserEntity(), dockerImage, dockerTest, dockerTemplate, structureTest, HttpMethod.POST); } @PatchMapping(ApiRoutes.PROJECT_BASE_PATH + "/{projectid}/tests") @@ -73,7 +73,7 @@ public ResponseEntity patchTests( @RequestParam(name = "structuretest", required = false) String structureTest, @PathVariable("projectid") long projectId, Auth auth) { - return alterTests(projectId, auth.getUserEntity(), dockerImage, dockerTest, structureTest, dockerTemplate, HttpMethod.PATCH); + return alterTests(projectId, auth.getUserEntity(), dockerImage, dockerTest, dockerTemplate, structureTest, HttpMethod.PATCH); } @PutMapping(ApiRoutes.PROJECT_BASE_PATH + "/{projectid}/tests") @@ -85,7 +85,7 @@ public ResponseEntity putTests( @RequestParam(name = "structuretest", required = false) String structureTest, @PathVariable("projectid") long projectId, Auth auth) { - return alterTests(projectId, auth.getUserEntity(), dockerImage, dockerTest, structureTest, dockerTemplate, HttpMethod.PUT); + return alterTests(projectId, auth.getUserEntity(), dockerImage, dockerTest, dockerTemplate, structureTest, HttpMethod.PUT); } @@ -99,6 +99,9 @@ private ResponseEntity alterTests( HttpMethod httpMethod ) { + + + if (dockerImage != null && dockerImage.isBlank()) { dockerImage = null; } @@ -112,7 +115,13 @@ private ResponseEntity alterTests( structureTemplate = null; } - CheckResult> updateCheckResult = testUtil.checkForTestUpdate(projectId, user, dockerImage, null, null, httpMethod); + /* LOg arguments even if null */ + System.out.println("dockerImage: " + dockerImage); + System.out.println("dockerScript: " + dockerScript); + System.out.println("dockerTemplate: " + dockerTemplate); + System.out.println("structureTemplate: " + structureTemplate); + + CheckResult> updateCheckResult = testUtil.checkForTestUpdate(projectId, user, dockerImage, dockerScript, dockerTemplate, httpMethod); if (!updateCheckResult.getStatus().equals(HttpStatus.OK)) { @@ -142,25 +151,23 @@ private ResponseEntity alterTests( return ResponseEntity.ok().build(); } - // Docker test - if(dockerImage != null){ - // update/install image if possible. - DockerSubmissionTestModel.installImage(dockerImage); - testEntity.setDockerImage(dockerImage); - - testEntity.setDockerTestScript(dockerScript); - testEntity.setDockerTestTemplate(dockerTemplate); // If present, the test is in template mode + //Update fields + if (dockerImage != null || !httpMethod.equals(HttpMethod.PATCH)) { + testEntity.setDockerImage(dockerImage); + if (dockerImage == null && !testRepository.imageIsUsed(dockerImage)) { + DockerSubmissionTestModel.removeDockerImage(dockerImage); //TODO: move this to different thread if takes a while + } } - - // save structure template - if (!httpMethod.equals(HttpMethod.PATCH) || (structureTemplate != null && !structureTemplate.isBlank())) { - if (structureTemplate != null && structureTemplate.isBlank()) { - structureTemplate = null; - } - } else { - structureTemplate = testEntity.getStructureTemplate(); + if (dockerScript != null || !httpMethod.equals(HttpMethod.PATCH)) { + testEntity.setDockerTestScript(dockerScript); + } + if (dockerTemplate != null || !httpMethod.equals(HttpMethod.PATCH)) { + testEntity.setDockerTestTemplate(dockerTemplate); } - testEntity.setStructureTemplate(structureTemplate); + if (structureTemplate != null || !httpMethod.equals(HttpMethod.PATCH)) { + testEntity.setStructureTemplate(structureTemplate); + } + // save test entity testEntity = testRepository.save(testEntity); diff --git a/backend/app/src/main/java/com/ugent/pidgeon/util/Filehandler.java b/backend/app/src/main/java/com/ugent/pidgeon/util/Filehandler.java index e060ee3f..7a4a96ef 100644 --- a/backend/app/src/main/java/com/ugent/pidgeon/util/Filehandler.java +++ b/backend/app/src/main/java/com/ugent/pidgeon/util/Filehandler.java @@ -253,7 +253,15 @@ public static void copyFilesAsZip(List files, Path path) throws IOExceptio // Write directly to a zip file in the path variable File zipFile = new File(path.toString()); - // Create a ZIP file + Logger.getGlobal().info("Filexists: " + zipFile.exists()); + if (zipFile.exists() && !zipFile.canWrite()) { + Logger.getGlobal().info("Setting writable"); + boolean res = zipFile.setWritable(true); + if (!res) { + throw new IOException("Cannot write to zip file"); + } + } + try (ZipOutputStream zipOutputStream = new ZipOutputStream(new FileOutputStream(zipFile))) { for (File file : files) { // add file to zip diff --git a/backend/app/src/main/java/com/ugent/pidgeon/util/TestUtil.java b/backend/app/src/main/java/com/ugent/pidgeon/util/TestUtil.java index 6307098d..fd534447 100644 --- a/backend/app/src/main/java/com/ugent/pidgeon/util/TestUtil.java +++ b/backend/app/src/main/java/com/ugent/pidgeon/util/TestUtil.java @@ -7,6 +7,7 @@ import com.ugent.pidgeon.postgre.models.TestEntity; import com.ugent.pidgeon.postgre.models.UserEntity; import com.ugent.pidgeon.postgre.repository.TestRepository; +import java.util.logging.Level; import java.util.logging.Logger; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.http.HttpMethod; @@ -51,7 +52,15 @@ public CheckResult> checkForTestUpdate( String dockerTemplate, HttpMethod httpMethod ) { - + /* Log arguments */ + Logger logger = Logger.getGlobal(); + logger.log(Level.INFO, "=========="); + logger.log(Level.INFO, "projectId: " + projectId); + logger.log(Level.INFO, "user: " + user); + logger.log(Level.INFO, "dockerImage: " + dockerImage); + logger.log(Level.INFO, "dockerScript: " + dockerScript); + logger.log(Level.INFO, "dockerTemplate: " + dockerTemplate); + logger.log(Level.INFO, "httpMethod: " + httpMethod); CheckResult projectCheck = projectUtil.getProjectIfAdmin(projectId, user); if (!projectCheck.getStatus().equals(HttpStatus.OK)) { @@ -72,10 +81,18 @@ public CheckResult> checkForTestUpdate( return new CheckResult<>(HttpStatus.CONFLICT, "Tests already exist for this project", null); } - if(httpMethod.equals(HttpMethod.POST) && dockerImage != null && dockerScript == null) { + if(!httpMethod.equals(HttpMethod.PATCH) && dockerImage != null && dockerScript == null) { return new CheckResult<>(HttpStatus.BAD_REQUEST, "A test script is required in a docker test.", null); } + if(!httpMethod.equals(HttpMethod.PATCH) && dockerScript != null && dockerImage == null) { + return new CheckResult<>(HttpStatus.BAD_REQUEST, "A docker image is required in a docker test.", null); + } + + if (!httpMethod.equals(HttpMethod.PATCH) && dockerTemplate != null && dockerScript == null) { + return new CheckResult<>(HttpStatus.BAD_REQUEST, "A test script and image are required in a docker template test.", null); + } + if(httpMethod.equals(HttpMethod.PATCH) && dockerScript != null && testEntity.getDockerImage() == null && dockerImage == null) { return new CheckResult<>(HttpStatus.BAD_REQUEST, "No docker image is configured for this test", null); }