diff --git a/src/main/java/com/dajudge/kindcontainer/helm/InstallFluent.java b/src/main/java/com/dajudge/kindcontainer/helm/InstallFluent.java index 63aecec..5dd6d25 100644 --- a/src/main/java/com/dajudge/kindcontainer/helm/InstallFluent.java +++ b/src/main/java/com/dajudge/kindcontainer/helm/InstallFluent.java @@ -2,12 +2,11 @@ import com.dajudge.kindcontainer.BaseSidecarContainer.ExecInContainer; import com.dajudge.kindcontainer.exception.ExecutionException; +import org.testcontainers.containers.ContainerState; +import org.testcontainers.utility.MountableFile; import java.io.IOException; -import java.util.ArrayList; -import java.util.HashMap; -import java.util.List; -import java.util.Map; +import java.util.*; import static java.util.Arrays.asList; @@ -17,6 +16,7 @@ public class InstallFluent

{ private final P parent; private String namespace; private boolean createNamespace; + private String version; private final Map params = new HashMap<>(); private final List values = new ArrayList<>(); @@ -26,31 +26,77 @@ public InstallFluent(final ExecInContainer c, final P parent) { this.parent = parent; } + /** + * Adds the given key/value as --set parameter to the Helm install command. + * @param key required + * @param value required + * @return The fluent API + */ public InstallFluent

set(final String key, final String value) { params.put(key, value); return this; } + /** + * Adds the given values file as -f parameter to the Helm install command. + * Make sure the values file is available in the Helm container. + * @see ContainerState#copyFileToContainer(MountableFile, String) + * @param path Path to the values file in the Helm container. + * @return The fluent API + */ public InstallFluent

values(final String path) { values.add(path); return this; } + /** + * Sets the given namespace as target namespace (--namespace parameter) for the Helm install command. + * @param namespace required + * @return The fluent API + */ public InstallFluent

namespace(final String namespace) { this.namespace = namespace; return this; } + /** + * Enables or disables the creation of the target namespace for the Helm install command. (--create-namespace parameter) + * @param createNamespace true to enable creation, false otherwise + * @return The fluent API + */ public InstallFluent

createNamespace(final boolean createNamespace) { this.createNamespace = createNamespace; return this; } + /** + * Enables the creation of the target namespace for the Helm install command. + * @return The fluent API + */ public InstallFluent

createNamespace() { return createNamespace(true); } + /** + * Sets the version for the Helm chart to be installed. (--version parameter) + * @param version required + * @return The fluent API + */ + public InstallFluent

version(final String version) { + this.version = version; + return this; + } + + /** + * Runs the Helm install command. + * @param releaseName The release name of the Helm installation + * @param chart The chart name of the Helm installation + * @return Parent container + * @throws IOException + * @throws InterruptedException + * @throws ExecutionException + */ public P run(final String releaseName, final String chart) throws IOException, InterruptedException, ExecutionException { try { final List cmdline = new ArrayList<>(asList("helm", "install")); @@ -60,6 +106,9 @@ public P run(final String releaseName, final String chart) throws IOException, I if (createNamespace) { cmdline.add("--create-namespace"); } + if (version != null) { + cmdline.addAll(asList("--version", version)); + } params.forEach((k, v) -> cmdline.addAll(asList("--set", String.format("%s=%s", k, v)))); cmdline.addAll(asList(releaseName, chart)); values.forEach(v -> { @@ -70,6 +119,7 @@ public P run(final String releaseName, final String chart) throws IOException, I } finally { createNamespace = false; namespace = null; + version = null; params.clear(); } } diff --git a/src/test/java/com/dajudge/kindcontainer/Helm3Test.java b/src/test/java/com/dajudge/kindcontainer/Helm3Test.java index 483a8ce..6725cfc 100644 --- a/src/test/java/com/dajudge/kindcontainer/Helm3Test.java +++ b/src/test/java/com/dajudge/kindcontainer/Helm3Test.java @@ -3,37 +3,57 @@ import com.dajudge.kindcontainer.util.ContainerVersionHelpers.KubernetesTestPackage; import org.junit.jupiter.api.DynamicTest; import org.junit.jupiter.api.TestFactory; +import org.testcontainers.utility.MountableFile; import java.util.stream.Stream; -import org.testcontainers.utility.MountableFile; import static com.dajudge.kindcontainer.util.ContainerVersionHelpers.allContainers; import static com.dajudge.kindcontainer.util.ContainerVersionHelpers.runWithK8s; import static com.dajudge.kindcontainer.util.TestUtils.runWithClient; import static org.junit.jupiter.api.Assertions.assertFalse; +import static org.junit.jupiter.api.Assertions.assertThrows; public class Helm3Test { + private static final String NAMESPACE = "hello"; + @TestFactory public Stream can_install_something() { return allContainers(this::assertCanInstallSomething); } + @TestFactory + public Stream assertInvalidVersionFails() { + return allContainers(this::assertInvalidVersionFails); + } + private void assertCanInstallSomething(final KubernetesTestPackage> testPkg) { - runWithK8s(configureContainer(testPkg.newContainer()), k8s -> runWithClient(k8s, client -> { - assertFalse(client.apps().deployments().inNamespace("hello").list().getItems().isEmpty()); + assertCanInstallVersion(testPkg, "0.1.0"); + } + + private void assertInvalidVersionFails(final KubernetesTestPackage> testPkg) { + assertThrows(RuntimeException.class, () -> assertCanInstallVersion(testPkg, "42.42.42")); + } + + private void assertCanInstallVersion( + final KubernetesTestPackage> testPkg, + final String version + ) { + runWithK8s(configureContainer(testPkg.newContainer(), version), k8s -> runWithClient(k8s, client -> { + assertFalse(client.apps().deployments().inNamespace(NAMESPACE).list().getItems().isEmpty()); })); } - private KubernetesContainer configureContainer(KubernetesContainer container) { + private KubernetesContainer configureContainer(final KubernetesContainer container, final String version) { return container.withHelm3(helm -> { helm.copyFileToContainer(MountableFile.forClasspathResource("hello-values.yaml"), "/apps/values.yaml"); helm.repo.add.run("examples", "https://helm.github.io/examples"); helm.repo.update.run(); helm.install - .namespace("hello") + .namespace(NAMESPACE) .createNamespace() .values("/apps/values.yaml") + .version(version) .run("hello", "examples/hello-world"); }); }