Skip to content

Commit

Permalink
Small refactoring of helm:init, add tests for fallbackBinaryDownload
Browse files Browse the repository at this point in the history
  • Loading branch information
sschnabe committed Jul 29, 2023
1 parent 8de9b94 commit 5f55306
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 25 deletions.
22 changes: 8 additions & 14 deletions src/main/java/io/kokuwa/maven/helm/InitMojo.java
Original file line number Diff line number Diff line change
Expand Up @@ -128,8 +128,8 @@ public class InitMojo extends AbstractHelmMojo {
private String helmDownloadServerId;

/**
* Controls whether a download should occur when local helm binary is not found/verified.
* This property has no effect unless "helm.useLocalHelmBinary" is set to <code>true</code>.
* Controls whether a download should occur when local helm binary is not found/verified. This property has no
* effect unless "helm.useLocalHelmBinary" is set to <code>true</code>.
*
* @since 6.8.1
*/
Expand All @@ -155,22 +155,20 @@ public void execute() throws MojoExecutionException {
}
}

boolean performDownload = true;
if (isUseLocalHelmBinary()) {
try {
verifyLocalHelmBinary();
helm().arguments("version").execute("Unable to verify local HELM binary");
getLog().info("Using local HELM binary [" + getHelmExecutablePath() + "]");
performDownload = false;
} catch (Exception e) {
} catch (MojoExecutionException e) {
if (fallbackBinaryDownload) {
getLog().info("Local HELM not verified => falling back to binary download");
getLog().info("Local HELM binary not found => falling back to binary download");
downloadAndUnpackHelm();
} else {
getLog().debug("Skipping fallback binary download");
getLog().error("Local HELM binary not found.");
throw e;
}
}
}
if (performDownload) {
} else {
downloadAndUnpackHelm();
}

Expand Down Expand Up @@ -329,10 +327,6 @@ private void addExecPermission(Path helm) throws IOException {
}
}

private void verifyLocalHelmBinary() throws MojoExecutionException {
helm().arguments("version").execute("Unable to verify local HELM binary");
}

private ArchiveInputStream createArchiverInputStream(InputStream is) throws MojoExecutionException {

// Stream must support mark to allow for auto detection of compressor
Expand Down
53 changes: 42 additions & 11 deletions src/test/java/io/kokuwa/maven/helm/InitMojoTest.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package io.kokuwa.maven.helm;

import static org.junit.jupiter.api.Assertions.assertDoesNotThrow;
import static org.junit.jupiter.api.Assertions.assertThrows;
import static org.junit.jupiter.api.Assertions.assertTrue;

import java.io.File;
Expand All @@ -8,6 +10,7 @@
import java.nio.file.Files;
import java.nio.file.Path;

import org.apache.maven.plugin.MojoExecutionException;
import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.condition.DisabledOnOs;
Expand Down Expand Up @@ -80,37 +83,55 @@ void addDefaultRepo(InitMojo mojo) {
@DisplayName("executable: local")
@Test
void localHelm(InitMojo mojo) {
mojo.setAutoDetectLocalHelmBinary(false);
mojo.setUseLocalHelmBinary(true);
mojo.setFallbackBinaryDownload(false);
mojo.setHelmVersion(null);
mojo.setHelmExecutableDirectory(new File("src/it"));
assertHelm(mojo, "version", "repo add stable " + InitMojo.STABLE_HELM_REPO);
}

@DisplayName("executable: local missing and fallback enabled")
@Test
void localHelmMissingWithFallbackEnabled(InitMojo mojo) {
mojo.setAutoDetectLocalHelmBinary(false);
mojo.setUseLocalHelmBinary(true);
mojo.setFallbackBinaryDownload(true);
mojo.setHelmVersion("3.12.0");
mojo.setHelmExecutableDirectory(createTempDirectory());
assertHelm(mojo, "repo add stable " + InitMojo.STABLE_HELM_REPO);
assertHelmExecuteable(mojo);
}

@DisplayName("executable: local missing and fallback disabled")
@Test
void localHelmMissingWithFallbackDisabled(InitMojo mojo) {
mojo.setAutoDetectLocalHelmBinary(false);
mojo.setUseLocalHelmBinary(true);
mojo.setFallbackBinaryDownload(false);
mojo.setHelmVersion("3.12.0");
mojo.setHelmExecutableDirectory(createTempDirectory());
assertThrows(MojoExecutionException.class, mojo::execute);
}

@DisplayName("executable: download with version")
@Test
void downloadHelmWithVersion(InitMojo mojo) throws IOException {
Path helmExecutableDirectory = Files.createTempDirectory("helm-maven-plugin-test");
Path helmExecutable = helmExecutableDirectory.resolve(HELM);
mojo.setHelmExecutableDirectory(helmExecutableDirectory.toFile());
void downloadHelmWithVersion(InitMojo mojo) {
mojo.setHelmExecutableDirectory(createTempDirectory());
mojo.setHelmVersion("3.10.1");
mojo.setUseLocalHelmBinary(false);
assertHelm(mojo, "repo add stable " + InitMojo.STABLE_HELM_REPO);
assertTrue(Files.isRegularFile(helmExecutable), "executable not found");
assertTrue(Files.isExecutable(helmExecutable), "executable not executable");
assertHelmExecuteable(mojo);
}

@DisplayName("executable: download with url")
@DisabledOnOs(OS.WINDOWS)
@Test
void downloadHelmWithUrl(InitMojo mojo) throws IOException {
Path helmExecutableDirectory = Files.createTempDirectory("helm-maven-plugin-test");
Path helmExecutable = helmExecutableDirectory.resolve("helm");
mojo.setHelmExecutableDirectory(helmExecutableDirectory.toFile());
mojo.setHelmExecutableDirectory(createTempDirectory());
mojo.setHelmVersion(null);
mojo.setHelmDownloadUrl(new URL("https://get.helm.sh/helm-v3.10.1-linux-amd64.tar.gz"));
assertHelm(mojo, "repo add stable " + InitMojo.STABLE_HELM_REPO);
assertTrue(Files.isRegularFile(helmExecutable), "executable not found");
assertTrue(Files.isExecutable(helmExecutable), "executable not executable");
}

@DisplayName("repository: extra repo without authentication")
Expand Down Expand Up @@ -261,4 +282,14 @@ void allKindsOfRepositories(InitMojo mojo) {
"repo add extra2 https://example.org/extra2 --username user-extra2 --password secret-extra2",
"repo add extra3 https://example.org/extra3");
}

private File createTempDirectory() {
return assertDoesNotThrow(() -> Files.createTempDirectory("helm-maven-plugin-test")).toFile();
}

private void assertHelmExecuteable(InitMojo mojo) {
Path helmExecutable = mojo.getHelmExecutableDirectory().resolve(HELM);
assertTrue(Files.isRegularFile(helmExecutable), "executable not found");
assertTrue(Files.isExecutable(helmExecutable), "executable not executable");
}
}

0 comments on commit 5f55306

Please sign in to comment.