Skip to content
GitLab
Projects
Groups
Snippets
/
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Sign in
Toggle navigation
Menu
Open sidebar
CoTA
cota-backend
Commits
6c7bc49f
Commit
6c7bc49f
authored
3 months ago
by
mamunozgil
Browse files
Options
Download
Email Patches
Plain Diff
Modified paths
parent
aaf14b36
Pipeline
#10775
passed with stage
in 19 seconds
Changes
2
Pipelines
1
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
src/main/java/de/hftstuttgart/dtabackend/utils/DockerUtil.java
+71
-55
...ain/java/de/hftstuttgart/dtabackend/utils/DockerUtil.java
src/main/java/de/hftstuttgart/dtabackend/utils/ExecuteTestUtil.java
+11
-9
...ava/de/hftstuttgart/dtabackend/utils/ExecuteTestUtil.java
with
82 additions
and
64 deletions
+82
-64
src/main/java/de/hftstuttgart/dtabackend/utils/DockerUtil.java
+
71
-
55
View file @
6c7bc49f
...
@@ -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
...
@@ -94,60 +95,75 @@ public class DockerUtil {
...
@@ -94,60 +95,75 @@ 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
));
try
{
dockerClient
.
pullImageCmd
(
image
)
// Pull the Docker image or log and proceed with local image
.
start
()
try
{
.
awaitCompletion
()
dockerClient
.
pullImageCmd
(
image
).
start
().
awaitCompletion
();
.
close
();
LOG
.
debug
(
String
.
format
(
"Image %s pulled successfully"
,
image
));
}
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
));
LOG
.
debug
(
"Creating container"
);
throw
new
IOException
(
"Image not available locally or remotely: "
+
image
);
CreateContainerResponse
containerResponse
;
}
try
{
}
// Prepare the host configuration with volumes
HostConfig
hostConfig
=
HostConfig
.
newHostConfig
()
String
containerId
=
null
;
.
withMounts
(
try
{
Arrays
.
stream
(
volumes
)
LOG
.
debug
(
"Configuring container with volumes"
);
.
map
(
volume
->
new
Mount
().
withTarget
(
volume
.
getPath
()).
withType
(
MountType
.
VOLUME
))
.
collect
(
Collectors
.
toList
())
// Prepare the host configuration for volumes
);
HostConfig
hostConfig
=
HostConfig
.
newHostConfig
()
.
withMounts
(
Arrays
.
stream
(
volumes
)
// Create the container with the configured volumes
.
map
(
volume
->
new
Mount
()
containerResponse
=
dockerClient
.
createContainerCmd
(
"testcontainer"
)
.
withTarget
(
volume
.
getPath
())
.
withImage
(
image
)
.
withType
(
MountType
.
VOLUME
))
.
withHostConfig
(
hostConfig
)
.
collect
(
Collectors
.
toList
()));
.
exec
();
}
catch
(
DockerException
e
)
{
// Create the container
LOG
.
error
(
String
.
format
(
LOG
.
debug
(
"Creating container"
);
"Creating Docker Testrunner container failed with %s"
,
e
.
getMessage
()));
CreateContainerResponse
containerResponse
=
dockerClient
.
createContainerCmd
(
image
)
throw
e
;
.
withHostConfig
(
hostConfig
)
}
.
withName
(
"testcontainer-"
+
UUID
.
randomUUID
())
.
exec
();
LOG
.
debug
(
String
.
format
(
"Container created: %s"
,
containerResponse
.
getId
()));
containerId
=
containerResponse
.
getId
();
LOG
.
debug
(
String
.
format
(
"Starting container %s"
,
containerResponse
.
getId
()));
LOG
.
debug
(
String
.
format
(
"Container created with ID: %s"
,
containerId
));
dockerClient
.
startContainerCmd
(
containerResponse
.
getId
()).
exec
();
// Start the container
LOG
.
debug
(
String
.
format
(
"Waiting for completion of container %s"
,
containerResponse
.
getId
()));
LOG
.
debug
(
String
.
format
(
"Starting container %s"
,
containerId
));
int
ret
=
dockerClient
dockerClient
.
startContainerCmd
(
containerId
).
exec
();
.
waitContainerCmd
(
containerResponse
.
getId
())
.
start
()
// Wait for container completion
.
awaitCompletion
()
LOG
.
debug
(
String
.
format
(
"Waiting for completion of container %s"
,
containerId
));
.
awaitStatusCode
();
int
statusCode
=
dockerClient
.
waitContainerCmd
(
containerId
)
LOG
.
debug
(
String
.
format
(
"Container completed with status %d"
,
ret
));
.
start
()
.
awaitCompletion
()
LOG
.
debug
(
String
.
format
(
"Deleting container %s"
,
containerResponse
.
getId
()));
.
awaitStatusCode
();
dockerClient
.
removeContainerCmd
(
containerResponse
.
getId
())
.
withRemoveVolumes
(
true
)
LOG
.
debug
(
String
.
format
(
"Container %s completed with status code %d"
,
containerId
,
statusCode
));
.
exec
();
return
statusCode
;
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
);
}
}
}
}
}
}
This diff is collapsed.
Click to expand it.
src/main/java/de/hftstuttgart/dtabackend/utils/ExecuteTestUtil.java
+
11
-
9
View file @
6c7bc49f
...
@@ -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
);
...
...
This diff is collapsed.
Click to expand it.
Write
Preview
Supports
Markdown
0%
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment