Skip to content

Commit

Permalink
Updates too checks
Browse files Browse the repository at this point in the history
  • Loading branch information
Aqua-sc committed May 7, 2024
1 parent b65e433 commit ad2ee51
Show file tree
Hide file tree
Showing 3 changed files with 56 additions and 24 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand All @@ -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")
Expand All @@ -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);
}


Expand All @@ -99,6 +99,9 @@ private ResponseEntity<?> alterTests(
HttpMethod httpMethod
) {




if (dockerImage != null && dockerImage.isBlank()) {
dockerImage = null;
}
Expand All @@ -112,7 +115,13 @@ private ResponseEntity<?> alterTests(
structureTemplate = null;
}

CheckResult<Pair<TestEntity, ProjectEntity>> 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<Pair<TestEntity, ProjectEntity>> updateCheckResult = testUtil.checkForTestUpdate(projectId, user, dockerImage, dockerScript, dockerTemplate, httpMethod);


if (!updateCheckResult.getStatus().equals(HttpStatus.OK)) {
Expand Down Expand Up @@ -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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -253,7 +253,15 @@ public static void copyFilesAsZip(List<File> 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
Expand Down
21 changes: 19 additions & 2 deletions backend/app/src/main/java/com/ugent/pidgeon/util/TestUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -51,7 +52,15 @@ public CheckResult<Pair<TestEntity, ProjectEntity>> 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<ProjectEntity> projectCheck = projectUtil.getProjectIfAdmin(projectId, user);
if (!projectCheck.getStatus().equals(HttpStatus.OK)) {
Expand All @@ -72,10 +81,18 @@ public CheckResult<Pair<TestEntity, ProjectEntity>> 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);
}
Expand Down

0 comments on commit ad2ee51

Please sign in to comment.