Commit 6c7bc49f authored by mamunozgil's avatar mamunozgil
Browse files

Modified paths

No related merge requests found
Pipeline #10775 passed with stage
in 19 seconds
Showing with 82 additions and 64 deletions
+82 -64
...@@ -19,6 +19,7 @@ import org.springframework.stereotype.Component; ...@@ -19,6 +19,7 @@ import org.springframework.stereotype.Component;
import java.io.IOException; import java.io.IOException;
import java.util.Arrays; import java.util.Arrays;
import java.util.UUID;
import java.util.stream.Collectors; import java.util.stream.Collectors;
@Component @Component
...@@ -95,59 +96,74 @@ public class DockerUtil { ...@@ -95,59 +96,74 @@ public class DockerUtil {
public int runContainerWithVolumes(String image, Volume... volumes) throws InterruptedException, IOException { public int runContainerWithVolumes(String image, Volume... volumes) throws InterruptedException, IOException {
LOG.debug(String.format("Pulling image: %s", image)); LOG.debug(String.format("Pulling image: %s", image));
// Pull the Docker image or log and proceed with local image
try { try {
dockerClient.pullImageCmd(image) dockerClient.pullImageCmd(image).start().awaitCompletion();
.start() LOG.debug(String.format("Image %s pulled successfully", image));
.awaitCompletion()
.close();
} catch (DockerException e) { } catch (DockerException e) {
LOG.error(String.format( LOG.warn(String.format(
"Pulling Docker image %s failed with %s, trying with local image", "Failed to pull image %s from remote: %s. Attempting to use local image.",
image, image, e.getMessage()));
e.getMessage()));
if (dockerClient.inspectImageCmd(image).exec() == null) {
LOG.error(String.format("Image %s not found locally. Aborting.", image));
throw new IOException("Image not available locally or remotely: " + image);
}
} }
LOG.debug("Creating container"); String containerId = null;
CreateContainerResponse containerResponse;
try { try {
// Prepare the host configuration with volumes LOG.debug("Configuring container with volumes");
// Prepare the host configuration for volumes
HostConfig hostConfig = HostConfig.newHostConfig() HostConfig hostConfig = HostConfig.newHostConfig()
.withMounts( .withMounts(Arrays.stream(volumes)
Arrays.stream(volumes) .map(volume -> new Mount()
.map(volume -> new Mount().withTarget(volume.getPath()).withType(MountType.VOLUME)) .withTarget(volume.getPath())
.collect(Collectors.toList()) .withType(MountType.VOLUME))
); .collect(Collectors.toList()));
// Create the container with the configured volumes // Create the container
containerResponse = dockerClient.createContainerCmd("testcontainer") LOG.debug("Creating container");
.withImage(image) CreateContainerResponse containerResponse = dockerClient.createContainerCmd(image)
.withHostConfig(hostConfig) .withHostConfig(hostConfig)
.withName("testcontainer-" + UUID.randomUUID())
.exec(); .exec();
} catch (DockerException e) {
LOG.error(String.format(
"Creating Docker Testrunner container failed with %s", e.getMessage()));
throw e;
}
LOG.debug(String.format("Container created: %s", containerResponse.getId())); containerId = containerResponse.getId();
LOG.debug(String.format("Container created with ID: %s", containerId));
LOG.debug(String.format("Starting container %s", containerResponse.getId())); // Start the container
dockerClient.startContainerCmd(containerResponse.getId()).exec(); LOG.debug(String.format("Starting container %s", containerId));
dockerClient.startContainerCmd(containerId).exec();
LOG.debug(String.format("Waiting for completion of container %s", containerResponse.getId())); // Wait for container completion
int ret = dockerClient LOG.debug(String.format("Waiting for completion of container %s", containerId));
.waitContainerCmd(containerResponse.getId()) int statusCode = dockerClient.waitContainerCmd(containerId)
.start() .start()
.awaitCompletion() .awaitCompletion()
.awaitStatusCode(); .awaitStatusCode();
LOG.debug(String.format("Container completed with status %d", ret));
LOG.debug(String.format("Deleting container %s", containerResponse.getId())); LOG.debug(String.format("Container %s completed with status code %d", containerId, statusCode));
dockerClient.removeContainerCmd(containerResponse.getId()) return statusCode;
.withRemoveVolumes(true)
.exec();
return ret; } catch (DockerException | InterruptedException e) {
} LOG.error(String.format("An error occurred: %s", e.getMessage()), e);
throw e;
} finally {
if (containerId != null) {
try {
LOG.debug(String.format("Cleaning up container %s", containerId));
dockerClient.removeContainerCmd(containerId).withRemoveVolumes(false).exec();
LOG.debug(String.format("Container %s removed successfully", containerId));
} catch (DockerException cleanupException) {
LOG.error(String.format(
"Failed to clean up container %s: %s",
containerId, cleanupException.getMessage()), cleanupException);
}
}
}
}
} }
...@@ -6,7 +6,6 @@ import com.fasterxml.jackson.databind.ObjectMapper; ...@@ -6,7 +6,6 @@ import com.fasterxml.jackson.databind.ObjectMapper;
import com.github.dockerjava.api.model.Volume; import com.github.dockerjava.api.model.Volume;
import de.hftstuttgart.dtabackend.models.ExerciseCompetencyProfile; import de.hftstuttgart.dtabackend.models.ExerciseCompetencyProfile;
import de.hftstuttgart.dtabackend.models.ICompetencyProfile;
import de.hftstuttgart.dtabackend.models.Recommendation; import de.hftstuttgart.dtabackend.models.Recommendation;
import de.hftstuttgart.dtabackend.models.ResultSummary; import de.hftstuttgart.dtabackend.models.ResultSummary;
import de.hftstuttgart.dtabackend.models.TestCompetencyProfile; import de.hftstuttgart.dtabackend.models.TestCompetencyProfile;
...@@ -35,6 +34,7 @@ public class ExecuteTestUtil { ...@@ -35,6 +34,7 @@ public class ExecuteTestUtil {
private final DockerUtil dockerUtil; private final DockerUtil dockerUtil;
private final String assignmentBasePath; private final String assignmentBasePath;
private final String containerTestDir;
public ExecuteTestUtil( public ExecuteTestUtil(
Environment env, Environment env,
...@@ -44,17 +44,19 @@ public class ExecuteTestUtil { ...@@ -44,17 +44,19 @@ public class ExecuteTestUtil {
// set base path for assignments to be stored // set base path for assignments to be stored
Path p = Paths.get( Path p = Paths.get(
env.getProperty("data.dir"), env.getProperty("data.dir"), ///data
env.getProperty("data.dir.test.folder.name")); env.getProperty("data.dir.test.folder.name")); //UnitTests
this.assignmentBasePath = p.toAbsolutePath().toString(); this.assignmentBasePath = p.toAbsolutePath().toString();
this.containerTestDir = env.getProperty( "data.dir");
} }
public ResultSummary runTests(String assignmentId, Path workDirectory) throws IOException, InterruptedException { public ResultSummary runTests(String assignmentId, Path workDirectory) throws IOException, InterruptedException {
// Define paths for the test, the submission, and where the result is expected afterwards // Define paths for the test, the submission, and where the result is expected afterwards
Path testPath = Paths.get("/data/test"); // Volume mount path in the container String containerTestDir = this.containerTestDir;
Path srcPath = Paths.get("/data/src"); // Volume mount path in the container Path resultPath = Paths.get(containerTestDir, "result");
Path resultPath = Paths.get("/data/result"); // Volume mount path in the container Path testPath = Paths.get(containerTestDir, "test");
Path srcPath = Paths.get(containerTestDir, "src");
// Clone stored test to testPath // Clone stored test to testPath
LOG.debug("Copying pre-downloaded unit test repo"); LOG.debug("Copying pre-downloaded unit test repo");
...@@ -95,9 +97,9 @@ public class ExecuteTestUtil { ...@@ -95,9 +97,9 @@ public class ExecuteTestUtil {
// Start the test-container with professor-given image and volume mounts for test, submission, and result // Start the test-container with professor-given image and volume mounts for test, submission, and result
dockerUtil.runContainerWithVolumes( dockerUtil.runContainerWithVolumes(
image, image,
new Volume("/data/test"), new Volume("test_volume: /data/test"),
new Volume("/data/src"), new Volume("src_volume: /data/src"),
new Volume("/data/result") new Volume("result_volume: /data/result")
); );
ResultSummary resultSummary = generateResult(assignmentId, resultPath, testPath); ResultSummary resultSummary = generateResult(assignmentId, resultPath, testPath);
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment