Skip to content

Commit

Permalink
fix: env not rendered correctly
Browse files Browse the repository at this point in the history
  • Loading branch information
mgabelle committed Oct 3, 2024
1 parent 18fee80 commit 81f28d2
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 11 deletions.
16 changes: 6 additions & 10 deletions src/main/java/io/kestra/plugin/modal/cli/ModalCLI.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import io.kestra.core.models.annotations.Example;
import io.kestra.core.models.annotations.Plugin;
import io.kestra.core.models.property.Property;
import io.kestra.core.models.tasks.*;
import io.kestra.core.models.tasks.runners.ScriptService;
import io.kestra.core.models.tasks.runners.TaskRunner;
Expand All @@ -19,10 +20,8 @@

import jakarta.validation.constraints.NotEmpty;
import jakarta.validation.constraints.NotNull;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Optional;

import java.util.*;

import static io.kestra.core.utils.Rethrow.throwFunction;

Expand Down Expand Up @@ -106,11 +105,8 @@ public class ModalCLI extends Task implements RunnableTask<ScriptOutput>, Namesp
@Schema(
title = "Additional environment variables for the current process."
)
@PluginProperty(
additionalProperties = String.class,
dynamic = true
)
protected Map<String, String> env;
@Builder.Default
protected Property<Map<String, String>> env = Property.of(new HashMap<>());

@Schema(
title = "Deprecated, use 'taskRunner' instead"
Expand Down Expand Up @@ -146,7 +142,7 @@ public ScriptOutput run(RunContext runContext) throws Exception {
.withDockerOptions(injectDefaults(getDocker()))
.withTaskRunner(this.taskRunner)
.withContainerImage(this.containerImage)
.withEnv(Optional.ofNullable(env).orElse(new HashMap<>()))
.withEnv(this.env.asMap(runContext, String.class, String.class))
.withNamespaceFiles(namespaceFiles)
.withInputFiles(inputFiles)
.withOutputFiles(outputFiles)
Expand Down
37 changes: 36 additions & 1 deletion src/test/java/io/kestra/plugin/modal/cli/ModalCLITest.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package io.kestra.plugin.modal.cli;

import io.kestra.core.models.property.Property;
import io.kestra.core.utils.IdUtils;
import io.kestra.core.utils.TestsUtils;
import io.kestra.plugin.modal.cli.ModalCLI;
Expand Down Expand Up @@ -45,7 +46,7 @@ void run() throws Exception {
assertThat(scriptOutput.getExitCode(), is(0));

runner = terraformBuilder
.env(Map.of("{{ inputs.environmentKey }}", "{{ inputs.environmentValue }}"))
.env(Property.of(Map.of("{{ inputs.environmentKey }}", "{{ inputs.environmentValue }}")))
.commands(List.of(
"echo \"::{\\\"outputs\\\":{" +
"\\\"customEnv\\\":\\\"$" + environmentKey + "\\\"" +
Expand All @@ -58,4 +59,38 @@ void run() throws Exception {
assertThat(scriptOutput.getExitCode(), is(0));
assertThat(scriptOutput.getVars().get("customEnv"), is(environmentValue));
}

@Test
@SuppressWarnings("unchecked")
void testModal() throws Exception {
String environmentKey = "MY_KEY";
String environmentValue = "MY_VALUE";

ModalCLI.ModalCLIBuilder<?, ?> terraformBuilder = ModalCLI.builder()
.id(IdUtils.create())
.type(ModalCLI.class.getName())
.docker(DockerOptions.builder().image("ghcr.io/kestra-io/modal").entryPoint(Collections.emptyList()).build())
.commands(List.of("modal --version"));

ModalCLI runner = terraformBuilder.build();

RunContext runContext = TestsUtils.mockRunContext(runContextFactory, runner, Map.of("environmentKey", environmentKey, "environmentValue", environmentValue));

ScriptOutput scriptOutput = runner.run(runContext);
assertThat(scriptOutput.getExitCode(), is(0));

runner = terraformBuilder
.env(Property.of(Map.of("{{ inputs.environmentKey }}", "{{ inputs.environmentValue }}")))
.commands(List.of(
"echo \"::{\\\"outputs\\\":{" +
"\\\"customEnv\\\":\\\"$" + environmentKey + "\\\"" +
"}}::\"",
"modal --version | tr -d ' \n' | xargs -0 -I {} echo '::{\"outputs\":{}}::'"
))
.build();

scriptOutput = runner.run(runContext);
assertThat(scriptOutput.getExitCode(), is(0));
assertThat(scriptOutput.getVars().get("customEnv"), is(environmentValue));
}
}

0 comments on commit 81f28d2

Please sign in to comment.