Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 8 additions & 2 deletions AGENTS.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ Multi-module Maven build (parent `pom.xml`) + a sibling Go module under `native/
| `lib/wasi/` | Abandoned WebAssembly experiment (issue [#239](https://github.com/manusa/helm-java/issues/239), PR [#243](https://github.com/manusa/helm-java/pull/243)). Not in parent `pom.xml`. Don't touch; will be removed. |
| `native/main.go` | **CGO bridge only**: C struct definitions + `//export` wrappers that delegate to `internal/helm`. No business logic. |
| `native/internal/helm/` | **Real Go implementations** — one file per verb (`install.go`, `upgrade.go`, …) plus `_test.go` siblings. `helm.go` holds shared config (`NewCfg`), `debug.go` the stdout/stderr capture. |
| `native/internal/test/` | Go test helpers (envtest). |
| `native/internal/test/` | Go test helpers: `SetupIsolatedEnv` (isolated Helm/Docker homes and seeded registry configs, called from each package's `TestMain`) and `RepositoryConfig` (a `repositories.yaml` path in a test temp dir). The envtest (k8s) helper is `internal/helm/envtest_test.go`. |
| `native/wasm/`, `native/out/` | TinyGo/WASI experiment entrypoint (see `lib/wasi/` note); build output for shared libraries. |
| `docs/research/` | Audits and decision records (e.g. native-codebase audit). Snapshots — may be stale; check the date. |
| `scripts/check-authors.sh` | Validates `@author` Javadoc tags (invoked via `make check-authors`). |
Expand Down Expand Up @@ -125,7 +125,7 @@ Verbs with both forms: `install`, `template`, `show`, `upgrade`. Most others are
## Adding a new Helm command (end-to-end)

1. `native/internal/helm/<verb>.go` — write the real Go function (params struct + function returning `(string, error)`).
2. `native/internal/helm/<verb>_test.go` — Go test (use `internal/test/env.go` helpers if k8s needed).
2. `native/internal/helm/<verb>_test.go` — Go test (use the envtest helpers in `internal/helm/envtest_test.go` if k8s is needed, and `test.RepositoryConfig(t, content)` for any `repositories.yaml`, never `os.CreateTemp("", …)`: repo commands create a lock file next to it).
3. `native/main.go`:
- Add `struct <Verb>Options { ... }` in the CGO comment block.
- Add `//export <Verb>` wrapper calling `helm.<Verb>(...)` via `runCommand(...)`.
Expand All @@ -145,6 +145,12 @@ Verbs with both forms: `install`, `template`, `show`, `upgrade`. Most others are
- `@TempDir` as a field for shared dirs; as a parameter for per-test dirs (see `HelmInstallTest`).
- k8s integration: `HelmKubernetesTest` spins up KinD via Testcontainers in `@BeforeAll`; clean releases in `@AfterEach`.
- Surefire sets `KUBECONFIG=/dev/null` in `helm-java/pom.xml` so tests cannot accidentally hit your local cluster — pass `kubeConfigFile`/`kubeConfigContents` explicitly.
- Surefire also isolates the tests from your Helm and Docker config:
- `HELM_CACHE_HOME` and `HELM_DATA_HOME` point at `helm-java/target/helm-home/`; `HELM_CONFIG_HOME` and `DOCKER_CONFIG` at `helm-java/target/test-classes/helm-home/`.
- Every other `HELM_*` variable Helm reads that redirects its files or changes its behavior (`HELM_REPOSITORY_CACHE`, `HELM_NAMESPACE`, `HELM_KUBEASUSER`, …) is not passed to the tests. The list in `helm-java/pom.xml` matches the one in `native/internal/test/env.go`; keep both in sync with the variables Helm reads: those in its `pkg/cli/environment.go`, plus `HELM_DRIVER` (read by `native/internal/helm/helm.go`) and `HELM_DRIVER_SQL_CONNECTION_STRING` (Helm's `pkg/action/action.go`). `HELM_REGISTRY_CONFIG` is excluded defensively: the native registry client doesn't read it.
- The registry configs come from the test resources (`helm-java/src/test/resources/helm-home/`, copied by Maven and IDEs alike) and hold a placeholder entry, so registry logins go to that file and not to an OS credential store (Keychain, Credential Manager, `pass`, Secret Service).
- Trade-off: Testcontainers reads `DOCKER_CONFIG` too, so the KinD and Ryuk image pulls in `HelmKubernetesTest` are anonymous (Docker Hub rate limits apply; an authenticated mirror won't be used).
- Don't make a test depend on the working directory (e.g. packaging `Paths.get("")`): `target/helm-home` lives in it and holds large repository indexes.

```java
class HelmFeatureTest {
Expand Down
35 changes: 35 additions & 0 deletions helm-java/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@

<properties>
<enforcer.skipRules>requireFilesExist</enforcer.skipRules>
<!-- Helm cache and data used by the tests (see the surefire configuration) -->
<helm.test.home>${project.build.directory}/helm-home</helm.test.home>
</properties>

<build>
Expand All @@ -27,7 +29,40 @@
<configuration>
<environmentVariables>
<KUBECONFIG>/dev/null</KUBECONFIG>
<!-- Keep registry credentials, repositories and their cache out of the developer's Helm and Docker config.
The config homes are in the test output directory because they hold the seeded registry configs
(src/test/resources/helm-home), which IDEs copy there too. Without a seeded entry, Helm's registry
client (oras-go DetectDefaultNativeStore) would store logins in the OS credential store
(osxkeychain, wincred, pass, secretservice). -->
<HELM_CACHE_HOME>${helm.test.home}/cache</HELM_CACHE_HOME>
<HELM_CONFIG_HOME>${project.build.testOutputDirectory}/helm-home/config</HELM_CONFIG_HOME>
<HELM_DATA_HOME>${helm.test.home}/data</HELM_DATA_HOME>
<DOCKER_CONFIG>${project.build.testOutputDirectory}/helm-home/docker</DOCKER_CONFIG>
</environmentVariables>
<!-- Every HELM_* variable Helm reads that would redirect its files or change its behavior: those in
helm.sh/helm/v3 pkg/cli/environment.go, plus HELM_DRIVER (read by native/internal/helm/helm.go) and
HELM_DRIVER_SQL_CONNECTION_STRING (pkg/action/action.go); same list as native/internal/test/env.go -->
<excludedEnvironmentVariables>
<excludedEnvironmentVariable>HELM_BURST_LIMIT</excludedEnvironmentVariable>
<excludedEnvironmentVariable>HELM_DEBUG</excludedEnvironmentVariable>
<excludedEnvironmentVariable>HELM_DRIVER</excludedEnvironmentVariable>
<excludedEnvironmentVariable>HELM_DRIVER_SQL_CONNECTION_STRING</excludedEnvironmentVariable>
<excludedEnvironmentVariable>HELM_KUBEAPISERVER</excludedEnvironmentVariable>
<excludedEnvironmentVariable>HELM_KUBEASGROUPS</excludedEnvironmentVariable>
<excludedEnvironmentVariable>HELM_KUBEASUSER</excludedEnvironmentVariable>
<excludedEnvironmentVariable>HELM_KUBECAFILE</excludedEnvironmentVariable>
<excludedEnvironmentVariable>HELM_KUBECONTEXT</excludedEnvironmentVariable>
<excludedEnvironmentVariable>HELM_KUBEINSECURE_SKIP_TLS_VERIFY</excludedEnvironmentVariable>
<excludedEnvironmentVariable>HELM_KUBETLS_SERVER_NAME</excludedEnvironmentVariable>
<excludedEnvironmentVariable>HELM_KUBETOKEN</excludedEnvironmentVariable>
<excludedEnvironmentVariable>HELM_MAX_HISTORY</excludedEnvironmentVariable>
<excludedEnvironmentVariable>HELM_NAMESPACE</excludedEnvironmentVariable>
<excludedEnvironmentVariable>HELM_PLUGINS</excludedEnvironmentVariable>
<excludedEnvironmentVariable>HELM_QPS</excludedEnvironmentVariable>
<excludedEnvironmentVariable>HELM_REGISTRY_CONFIG</excludedEnvironmentVariable>
<excludedEnvironmentVariable>HELM_REPOSITORY_CACHE</excludedEnvironmentVariable>
<excludedEnvironmentVariable>HELM_REPOSITORY_CONFIG</excludedEnvironmentVariable>
</excludedEnvironmentVariables>
</configuration>
</plugin>
</plugins>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.nio.file.StandardOpenOption;
import java.util.Objects;

Expand Down Expand Up @@ -66,8 +65,8 @@ void setUp() throws IOException {

}
@Test
void withoutChart() {
final PackageCommand packageCommand = new Helm(Paths.get("")).packageIt();
void withoutChart() throws IOException {
final PackageCommand packageCommand = new Helm(Files.createDirectory(tempDir.resolve("no-chart"))).packageIt();
assertThatIllegalStateException()
.isThrownBy(packageCommand::call)
.withMessage("Chart.yaml file is missing");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
package com.marcnuri.helm;

import com.marcnuri.helm.jni.RepoServerOptions;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Nested;
import org.junit.jupiter.api.Test;
Expand All @@ -38,6 +39,11 @@ void setUp() {
remoteServer = Helm.HelmLibHolder.INSTANCE.RepoOciServerStart(new RepoServerOptions()).out;
}

@AfterEach
void tearDown() {
Helm.HelmLibHolder.INSTANCE.RepoServerStop(remoteServer);
}

@Nested
class Login {
@Test
Expand Down
11 changes: 11 additions & 0 deletions helm-java/src/test/java/com/marcnuri/helm/HelmShowTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
package com.marcnuri.helm;

import com.marcnuri.helm.jni.RepoServerOptions;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Nested;
import org.junit.jupiter.api.Test;
Expand Down Expand Up @@ -147,6 +148,11 @@ void setUp() {
.call();
}

@AfterEach
void tearDown() {
Helm.HelmLibHolder.INSTANCE.RepoServerStop(remoteServer);
}

@Test
void all() {
final String result = Helm.show("oci://" + remoteServer + "/test")
Expand Down Expand Up @@ -183,6 +189,11 @@ void setUp() {
.call();
}

@AfterEach
void tearDown() {
Helm.HelmLibHolder.INSTANCE.RepoServerStop(remoteServer);
}

@Test
void missingVersionThrowsException() {
final ShowCommand.ShowSubcommand showAll = Helm.show("oci://" + remoteServer + "/test")
Expand Down
16 changes: 10 additions & 6 deletions helm-java/src/test/java/com/marcnuri/helm/HelmTestServerTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -41,8 +41,7 @@ void tearDown() {
@Test
void testRepoServerStart() throws Exception {
final Result result = Helm.HelmLibHolder.INSTANCE.RepoServerStart(new RepoServerOptions());
final HttpURLConnection uc = (HttpURLConnection) new URI(result.out).toURL().openConnection();
uc.setRequestMethod("GET");
final HttpURLConnection uc = get(result.out);
uc.connect();
assertThat(uc.getResponseCode()).isEqualTo(200);
}
Expand All @@ -52,8 +51,7 @@ void testRepoServerStartMultipleTimesReturnsMultipleFunctionalUrls() throws Exce
final Result result1 = Helm.HelmLibHolder.INSTANCE.RepoServerStart(new RepoServerOptions());
final Result result2 = Helm.HelmLibHolder.INSTANCE.RepoServerStart(new RepoServerOptions());
for (Result result : new Result[]{result1, result2}) {
final HttpURLConnection uc = (HttpURLConnection) new URI(result.out).toURL().openConnection();
uc.setRequestMethod("GET");
final HttpURLConnection uc = get(result.out);
uc.connect();
assertThat(uc.getResponseCode()).isEqualTo(200);
}
Expand All @@ -65,10 +63,16 @@ void testRepoServerStopAllStopsAllInstances() throws Exception {
final Result result2 = Helm.HelmLibHolder.INSTANCE.RepoServerStart(new RepoServerOptions());
Helm.HelmLibHolder.INSTANCE.RepoServerStopAll();
for (Result result : new Result[]{result1, result2}) {
final HttpURLConnection uc = (HttpURLConnection) new URI(result.out).toURL().openConnection();
uc.setRequestMethod("GET");
final HttpURLConnection uc = get(result.out);
assertThatThrownBy(uc::connect).isInstanceOf(ConnectException.class);
}
}

private static HttpURLConnection get(String url) throws Exception {
final HttpURLConnection uc = (HttpURLConnection) new URI(url).toURL().openConnection();
uc.setRequestMethod("GET");
uc.setConnectTimeout(10_000);
uc.setReadTimeout(10_000);
return uc;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,8 @@ void getInstanceNotAvailable() {
Thread.currentThread().setContextClassLoader(new URLClassLoader(new java.net.URL[0], null));
assertThatThrownBy(NativeLibrary::getInstance)
.isInstanceOf(IllegalStateException.class)
.hasMessage("No NativeLibrary implementation found, please add one of the supported dependencies to your project");
.hasMessage("No NativeLibrary implementation found, please add one of the supported dependencies to your project")
.hasNoCause();
} finally {
System.clearProperty("com.marcnuri.jkube-helm.skipRemoteJar");
Thread.currentThread().setContextClassLoader(currentClassLoader);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"auths": {
"helm-java.invalid": {}
}
}
5 changes: 5 additions & 0 deletions helm-java/src/test/resources/helm-home/docker/config.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"auths": {
"helm-java.invalid": {}
}
}
25 changes: 16 additions & 9 deletions lib/api/src/main/java/com/marcnuri/helm/jni/NativeLibrary.java
Original file line number Diff line number Diff line change
Expand Up @@ -36,17 +36,24 @@
public interface NativeLibrary {

static NativeLibrary getInstance() {
NativeLibrary nativeLibrary;
ClassLoader remoteJar;
if (
// Load from ClassPath (should work on Maven)
(nativeLibrary = serviceProviderLibrary(null)) != null ||
// Load from remote JAR (should work on Gradle if not air-gapped)
((remoteJar = remoteJar()) != null && (nativeLibrary = serviceProviderLibrary(remoteJar)) != null)
) {
// Load from ClassPath (should work on Maven)
NativeLibrary nativeLibrary = serviceProviderLibrary(null);
if (nativeLibrary != null) {
return nativeLibrary;
}
throw new IllegalStateException("No NativeLibrary implementation found, please add one of the supported dependencies to your project");
// Load from remote JAR (should work on Gradle if not air-gapped)
IOException remoteJarFailure = null;
try {
final ClassLoader remoteJar = remoteJar();
if (remoteJar != null && (nativeLibrary = serviceProviderLibrary(remoteJar)) != null) {
return nativeLibrary;
}
} catch (IOException exception) {
remoteJarFailure = exception;
}
throw new IllegalStateException(
"No NativeLibrary implementation found, please add one of the supported dependencies to your project",
remoteJarFailure);
}

/**
Expand Down
Loading
Loading