Skip to content

Commit

Permalink
Reviewed docker cp handling and added tests. (#249)
Browse files Browse the repository at this point in the history
  • Loading branch information
sschnabe authored Aug 9, 2023
1 parent 47aed89 commit 0fc91a2
Show file tree
Hide file tree
Showing 2 changed files with 61 additions and 7 deletions.
24 changes: 17 additions & 7 deletions src/main/java/io/kokuwa/maven/k3s/mojo/ApplyMojo.java
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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

Expand Down Expand Up @@ -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"));
Expand All @@ -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);
}
Expand Down Expand Up @@ -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) {
Expand Down
44 changes: 44 additions & 0 deletions src/test/java/io/kokuwa/maven/k3s/util/DockerTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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));
}
}

0 comments on commit 0fc91a2

Please sign in to comment.