Skip to content

Commit

Permalink
Updated several files to improve and fix functionality
Browse files Browse the repository at this point in the history
- Fixed regular expression in VolumeName.kt
- Updated library versions in pom.xml
- Changed default value of affix in Functions.kt
- Improved determineLocalPath function and handled null case by generating a temp directory in DockerRuntime.kt
- Made several additions to ContainerVolumeMountTests.kt
- Simplified Volume.kt constructor
  • Loading branch information
thomas-muller666 committed Apr 18, 2024
1 parent 3f9803b commit aef0f9f
Show file tree
Hide file tree
Showing 6 changed files with 71 additions and 35 deletions.
4 changes: 2 additions & 2 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@

<!-- Library versions -->
<spring-boot.version>3.2.2</spring-boot.version>
<kubernetes-client.version>6.12.0</kubernetes-client.version>
<kubernetes-client.version>6.12.1</kubernetes-client.version>
<docker-java.version>3.3.6</docker-java.version>
<awaitility.version>4.2.1</awaitility.version>
<commons.lang3.version>3.14.0</commons.lang3.version>
Expand All @@ -34,7 +34,7 @@
<jsch.version>0.2.17</jsch.version>
<junit.version>5.10.1</junit.version>
<slf4j.version>2.0.10</slf4j.version>
<logback.version>1.5.5</logback.version>
<logback.version>1.5.6</logback.version>
<junit.version>5.10.2</junit.version>
<khttp.version>1.6.1</khttp.version>

Expand Down
14 changes: 4 additions & 10 deletions src/main/kotlin/no/acntech/easycontainers/docker/DockerRuntime.kt
Original file line number Diff line number Diff line change
Expand Up @@ -555,16 +555,10 @@ internal class DockerRuntime(
}
}

private fun determineLocalPath(localPath: Path?, remoteFilename: String): Path {
if (localPath == null) {
return Paths.get(System.getProperty("user.dir"), remoteFilename)
}

return if (Files.isDirectory(localPath)) {
localPath.resolve(remoteFilename)
} else {
localPath
}
private fun determineLocalPath(localPath: Path?, remoteFilename: String): Path = when {
localPath == null -> Files.createTempDirectory("docker-file-transfer-").resolve(remoteFilename)
Files.isDirectory(localPath) -> localPath.resolve(remoteFilename)
else -> localPath
}

private fun getActualHostDir(path: Path): String {
Expand Down
2 changes: 1 addition & 1 deletion src/main/kotlin/no/acntech/easycontainers/model/Volume.kt
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,6 @@ data class Volume(
val memoryBacked: Boolean = false,
val memory: Memory? = null,
) {
constructor(name: String, mountPath: String) : this(VolumeName.of(name), UnixDir.of(mountPath), null, false, null)
constructor(name: String, mountPath: String) : this(VolumeName.of(name), UnixDir.of(mountPath))

}
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ value class VolumeName(val value: String) : SimpleValueObject<String> {

companion object {

private val REGEXP: Regex = "^[a-zA-Z0-9_-]\$".toRegex()
private val REGEXP: Regex = "^[a-zA-Z0-9_-]+$".toRegex()

private val VALIDATOR = StringValueObjectValidator(
minLength = 1,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ fun String.splitOnWhites() = split(WHITESPACE_REGEX)
* affix appended as a suffix.
*
* @param length The maximum length of the string.
* @param affix The prefix or suffix to be added to the truncated string. Default value is "[..]".
* @param affix The prefix or suffix to be added to the truncated string. Default value the empty string.
* @param fromStart Boolean indicating whether the truncation should be performed from the start of the string or from
* the end. Default value is false.
* @return The truncated string with the specified affix appended.
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,14 @@
package test.acntech.easycontainers

import no.acntech.easycontainers.ContainerBuilderCallback
import no.acntech.easycontainers.Environment
import no.acntech.easycontainers.Environment.k8sGeneralDataPvcName
import no.acntech.easycontainers.model.*
import no.acntech.easycontainers.util.platform.PlatformUtils
import no.acntech.easycontainers.util.text.FORWARD_SLASH
import no.acntech.easycontainers.util.text.truncate
import org.apache.commons.io.FileUtils
import org.junit.jupiter.api.Assertions.assertEquals
import org.junit.jupiter.api.extension.ExtensionContext
import org.junit.jupiter.params.ParameterizedTest
import org.junit.jupiter.params.provider.Arguments
Expand All @@ -11,8 +17,9 @@ import org.junit.jupiter.params.provider.ArgumentsSource
import org.slf4j.LoggerFactory
import java.nio.file.Files
import java.nio.file.Path
import java.util.concurrent.TimeUnit
import java.util.*
import java.util.stream.Stream
import kotlin.io.path.readText

class ContainerVolumeMountTests {

Expand All @@ -23,8 +30,8 @@ class ContainerVolumeMountTests {
class ContainerVolumeMountTestsProvider : ArgumentsProvider {
override fun provideArguments(context: ExtensionContext?): Stream<out Arguments> {
return Stream.of(
Arguments.of(ContainerPlatformType.DOCKER, "/tmp"),
// Arguments.of(ContainerPlatformType.KUBERNETES, "/home/thomas/kind/share"),
// Arguments.of(ContainerPlatformType.DOCKER, "/tmp"), // in Docker, any directory can be shared
Arguments.of(ContainerPlatformType.KUBERNETES, Environment.k8sGeneralDataHostDir),
)
}
}
Expand All @@ -33,17 +40,25 @@ class ContainerVolumeMountTests {
@ArgumentsSource(ContainerVolumeMountTestsProvider::class)
fun `Test mounting simple host volume `(containerType: ContainerPlatformType, hostPath: String) {

val name = VolumeName.of("test-volume")
val subDir = "test-mount"
val mountDir = UnixDir.of("/mnt/$subDir")
val hostDir = Path.of("$hostPath/$subDir")
val volumeNameVal = k8sGeneralDataPvcName.substringBefore("-pvc").also {
log.debug("Using the k8s PVC name (less the '-pvc' suffix) as volume name: $it")
}

val name = VolumeName.of(volumeNameVal)

val subDir = UUID.randomUUID().toString().truncate(8)
val mountDir = UnixDir.of("/mnt/test")
val hostDir = Path.of(hostPath).also {
log.debug("Using host directory as a basis for mount: $it")
}

createTestFilesInSharedDir(hostDir)
createTestFiles(hostDir.resolve(subDir))

val callback: ContainerBuilderCallback = object : ContainerBuilderCallback {

override fun configure(builder: ContainerBuilder<*>) {
builder.withVolume(Volume(name, mountDir, hostDir))
val volume = Volume(name, mountDir, hostDir)
builder.withVolume(volume)
}

}
Expand All @@ -57,20 +72,28 @@ class ContainerVolumeMountTests {

log.debug("Container state: ${container.getState()}")

TimeUnit.SECONDS.sleep(10 * 60)
val targetTestDir = mountDir.value + FORWARD_SLASH + subDir

val localFile1 = container.getFile(targetTestDir, "hello-1.txt")
val localFile2 = container.getFile(targetTestDir, "hello-2.txt")

val content1 = localFile1.readText().also {
log.debug("Content of file 1: $it")
}
val content2 = localFile2.readText().also {
log.debug("Content of file 2: $it")
}

assertEquals("Hello, world 1!", content1)
assertEquals("Hello, world 2!", content2)

container.getRuntime().stop()
container.getRuntime().delete()
container.getRuntime().delete(true)
deleteTestFiles(hostDir.resolve(subDir))
}

private fun createTestFilesInSharedDir(hostDir: Path) {
val actualDir = Path.of(
if (PlatformUtils.isWslInstalled()) {
PlatformUtils.convertUnixPathToWindowsWslPath(hostDir.toString())
} else {
hostDir.toString()
}
)
private fun createTestFiles(hostDir: Path) {
val actualDir = getActualDir(hostDir)

Files.createDirectories(actualDir)

Expand All @@ -83,7 +106,26 @@ class ContainerVolumeMountTests {
file1.toFile().writeText("Hello, world 1!")
file2.toFile().writeText("Hello, world 2!")

log.debug("Created test files ${file1.toAbsolutePath()} and ${file2.toAbsolutePath()} in shared host directory: $actualDir")
log.debug(
"Created test files ${file1.toAbsolutePath()} and ${file2.toAbsolutePath()}" +
" in the shared host directory: $actualDir"
)
}

private fun deleteTestFiles(hostDir: Path) {
val actualDir = getActualDir(hostDir)
FileUtils.deleteDirectory(actualDir.toFile())
log.debug("Deleted test files in dir: $actualDir")
}

private fun getActualDir(hostDir: Path): Path {
return Path.of(
if (PlatformUtils.isWslInstalled()) {
PlatformUtils.convertUnixPathToWindowsWslPath(hostDir.toString())
} else {
hostDir.toString()
}
)
}

}

0 comments on commit aef0f9f

Please sign in to comment.