From 0fc91a20aaf979214b6d164c8e1976b493e3bdb8 Mon Sep 17 00:00:00 2001 From: Stephan Schnabel Date: Wed, 9 Aug 2023 15:10:55 +0200 Subject: [PATCH] Reviewed docker cp handling and added tests. (#249) --- .../io/kokuwa/maven/k3s/mojo/ApplyMojo.java | 24 +++++++--- .../io/kokuwa/maven/k3s/util/DockerTest.java | 44 +++++++++++++++++++ 2 files changed, 61 insertions(+), 7 deletions(-) diff --git a/src/main/java/io/kokuwa/maven/k3s/mojo/ApplyMojo.java b/src/main/java/io/kokuwa/maven/k3s/mojo/ApplyMojo.java index 21f7f093..c7eebabe 100644 --- a/src/main/java/io/kokuwa/maven/k3s/mojo/ApplyMojo.java +++ b/src/main/java/io/kokuwa/maven/k3s/mojo/ApplyMojo.java @@ -42,12 +42,20 @@ public class ApplyMojo extends K3sMojo { private Path manifests; /** - * Subdir of {@link #manifests} to execute + * Path for {@link #manifests} inside the k3s container. + * + * @since 1.0.0 + */ + @Parameter(readonly = true, defaultValue = "/tmp/manifests") + private Path path; + + /** + * Subdir of {@link #manifests} to execute inside the k3s container. * * @since 1.0.0 */ @Parameter(property = "k3s.subdir") - private String subdir; + private Path subdir; /** * Timeout in seconds to wait for resources getting ready. @@ -72,11 +80,12 @@ public void execute() throws MojoExecutionException { return; } - // verify container + // verify container and copy manifests if (getDocker().getContainer().isEmpty()) { throw new MojoExecutionException("No k3s container found"); } + getDocker().copyToContainer(manifests, path); // wait for service account, see https://github.com/kubernetes/kubernetes/issues/66689 @@ -121,9 +130,7 @@ public void execute() throws MojoExecutionException { private Task apply() throws MojoExecutionException { - var path = Paths.get("/tmp/manifests"); var subPath = subdir == null ? path : path.resolve(subdir); - var kustomizePath = subdir == null ? manifests : manifests.resolve(subdir); var kustomize = Files.isRegularFile(kustomizePath.resolve("kustomization.yml")) || Files.isRegularFile(kustomizePath.resolve("kustomization.yaml")); @@ -138,7 +145,6 @@ private Task apply() throws MojoExecutionException { command.add("--recursive"); } - getDocker().copyToContainer(manifests, path); getLog().info(command.stream().collect(Collectors.joining(" "))); return getDocker().execWithoutVerify(timeout, command); } @@ -194,8 +200,12 @@ public void setManifests(File manifests) { this.manifests = manifests.toPath().toAbsolutePath(); } + public void setPath(String path) { + this.path = Paths.get(path); + } + public void setSubdir(String subdir) { - this.subdir = subdir; + this.subdir = subdir == null ? null : Paths.get(subdir); } public void setTimeout(int timeout) { diff --git a/src/test/java/io/kokuwa/maven/k3s/util/DockerTest.java b/src/test/java/io/kokuwa/maven/k3s/util/DockerTest.java index 18c827c2..a706c1b1 100644 --- a/src/test/java/io/kokuwa/maven/k3s/util/DockerTest.java +++ b/src/test/java/io/kokuwa/maven/k3s/util/DockerTest.java @@ -4,8 +4,12 @@ import static org.junit.jupiter.api.Assertions.assertFalse; import static org.junit.jupiter.api.Assertions.assertTrue; +import java.io.IOException; +import java.nio.file.Files; +import java.nio.file.Paths; import java.time.Duration; import java.util.List; +import java.util.UUID; import java.util.function.BiConsumer; import org.apache.maven.plugin.MojoExecutionException; @@ -96,4 +100,44 @@ void normalizeImage() { assertImage.accept("quay.io/hello/world:0.1.23", "quay.io/hello/world:0.1.23"); assertImage.accept("quay.io/hello/world@sha256:XYZ", "quay.io/hello/world@sha256:XYZ"); } + + @DisplayName("copy()") + @Test + void copy() throws MojoExecutionException, IOException { + + // start container + + docker.createVolume(); + docker.createContainer("rancher/k3s", List.of(), List.of("server")); + + // define test data + + var containerDir = Paths.get("/k3s-data"); + var sourceDir = Paths.get("target", "docker-copy", "source"); + var sourceFile = sourceDir.resolve("test.txt"); + var returnDir = Paths.get("target", "docker-copy", "return"); + var returnFile = sourceDir.resolve("test.txt"); + Files.createDirectories(sourceDir); + Files.createDirectories(returnDir); + + // write initial file and copy to container + + var initialContent = UUID.randomUUID().toString(); + Files.deleteIfExists(sourceFile); + Files.deleteIfExists(returnFile); + Files.write(sourceFile, initialContent.toString().getBytes()); + docker.copyToContainer(sourceDir, containerDir); + docker.copyFromContainer(containerDir, returnDir); + assertEquals(initialContent, Files.readString(returnFile)); + + // write changed file and copy to container + + var changedContent = UUID.randomUUID().toString(); + Files.deleteIfExists(sourceFile); + Files.deleteIfExists(returnFile); + Files.write(sourceFile, changedContent.toString().getBytes()); + docker.copyToContainer(sourceDir, containerDir); + docker.copyFromContainer(containerDir, returnDir); + assertEquals(changedContent, Files.readString(returnFile)); + } }