use of io.kestra.core.tasks.scripts.Bash in project kestra by kestra-io.
the class AbstractBashTest method preventRelativeFile.
@Test
void preventRelativeFile() throws Exception {
URL resource = AbstractBashTest.class.getClassLoader().getResource("application.yml");
URI put = storageInterface.put(new URI("/file/storage/get.yml"), new FileInputStream(Objects.requireNonNull(resource).getFile()));
assertThrows(IllegalArgumentException.class, () -> {
Bash bash = configure(Bash.builder().commands(new String[] { "echo 1" }).inputFiles(Map.of("{{ inputs.vars }}", put.toString()))).build();
RunContext runContext = TestsUtils.mockRunContext(runContextFactory, bash, ImmutableMap.of("vars", "../../test.txt"));
bash.run(runContext);
});
assertThrows(IllegalArgumentException.class, () -> {
Bash bash = configure(Bash.builder().commands(new String[] { "echo 1" }).inputFiles(Map.of("{{ inputs.vars }}", put.toString()))).build();
RunContext runContext = TestsUtils.mockRunContext(runContextFactory, bash, ImmutableMap.of("vars", "../../test.txt"));
bash.run(runContext);
});
// we allow dot file starting with a .
Bash bash = configure(Bash.builder().commands(new String[] { "echo 1" }).inputFiles(Map.of("{{ inputs.vars }}", put.toString()))).build();
RunContext runContext = TestsUtils.mockRunContext(runContextFactory, bash, ImmutableMap.of("vars", ".test.txt"));
ScriptOutput run = bash.run(runContext);
assertThat(run.getExitCode(), is(0));
}
use of io.kestra.core.tasks.scripts.Bash in project kestra by kestra-io.
the class AbstractBashTest method stopOnFirstFailed.
@Test
@DisabledIfEnvironmentVariable(named = "GITHUB_WORKFLOW", matches = ".*")
void stopOnFirstFailed() {
Bash bash = configure(Bash.builder().commands(new String[] { "unknown", "echo 1" })).build();
RunContext runContext = TestsUtils.mockRunContext(runContextFactory, bash, ImmutableMap.of());
Bash.BashException bashException = assertThrows(Bash.BashException.class, () -> {
bash.run(runContext);
});
assertThat(bashException.getExitCode(), is(127));
assertThat(bashException.getStdOutSize(), is(0));
assertThat(bashException.getStdErrSize(), is(1));
}
use of io.kestra.core.tasks.scripts.Bash in project kestra by kestra-io.
the class AbstractBashTest method files.
@Test
void files() throws Exception {
Bash bash = configure(Bash.builder().outputFiles(Arrays.asList("xml", "csv")).inputFiles(ImmutableMap.of("files/in/in.txt", "I'm here")).commands(new String[] { "echo '::{\"outputs\": {\"extract\":\"'$(cat files/in/in.txt)'\"}}::'", "echo 1 >> {{ outputFiles.xml }}", "echo 2 >> {{ outputFiles.csv }}", "echo 3 >> {{ outputFiles.xml }}" })).build();
RunContext runContext = TestsUtils.mockRunContext(runContextFactory, bash, ImmutableMap.of());
ScriptOutput run = bash.run(runContext);
assertThat(run.getExitCode(), is(0));
assertThat(run.getStdErrLineCount(), is(0));
assertThat(run.getStdOutLineCount(), is(1));
assertThat(run.getVars().get("extract"), is("I'm here"));
InputStream get = storageInterface.get(run.getOutputFiles().get("xml"));
assertThat(CharStreams.toString(new InputStreamReader(get)), is("1\n3\n"));
get = storageInterface.get(run.getOutputFiles().get("csv"));
assertThat(CharStreams.toString(new InputStreamReader(get)), is("2\n"));
}
use of io.kestra.core.tasks.scripts.Bash in project kestra by kestra-io.
the class AbstractBashTest method failed.
@Test
@DisabledIfEnvironmentVariable(named = "GITHUB_WORKFLOW", matches = ".*")
void failed() {
Bash bash = configure(Bash.builder().commands(new String[] { "echo 1 1>&2", "exit 66", "echo 2" })).build();
RunContext runContext = TestsUtils.mockRunContext(runContextFactory, bash, ImmutableMap.of());
Bash.BashException bashException = assertThrows(Bash.BashException.class, () -> {
bash.run(runContext);
});
assertThat(bashException.getExitCode(), is(66));
assertThat(bashException.getStdOutSize(), is(0));
assertThat(bashException.getStdErrSize(), is(1));
}
use of io.kestra.core.tasks.scripts.Bash in project kestra by kestra-io.
the class AbstractBashTest method useInputFiles.
@Test
void useInputFiles() throws Exception {
Map<String, String> files = new HashMap<>();
files.put("test.sh", "tst() { echo '::{\"outputs\": {\"extract\":\"testbash\"}}::' ; echo '{{workingDir}}'; }");
List<String> commands = new ArrayList<>();
commands.add("source {{workingDir}}/test.sh && tst");
Bash bash = configure(Bash.builder().interpreter("/bin/bash").commands(commands.toArray(String[]::new)).inputFiles(files)).build();
RunContext runContext = TestsUtils.mockRunContext(runContextFactory, bash, ImmutableMap.of());
ScriptOutput run = bash.run(runContext);
assertThat(run.getExitCode(), is(0));
assertThat(run.getVars().get("extract"), is("testbash"));
}
Aggregations