Search in sources :

Example 1 with ScriptOutput

use of io.kestra.core.tasks.scripts.ScriptOutput 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));
}
Also used : ScriptOutput(io.kestra.core.tasks.scripts.ScriptOutput) RunContext(io.kestra.core.runners.RunContext) Bash(io.kestra.core.tasks.scripts.Bash) URI(java.net.URI) URL(java.net.URL) FileInputStream(java.io.FileInputStream) MicronautTest(io.micronaut.test.extensions.junit5.annotation.MicronautTest) Test(org.junit.jupiter.api.Test)

Example 2 with ScriptOutput

use of io.kestra.core.tasks.scripts.ScriptOutput 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"));
}
Also used : ScriptOutput(io.kestra.core.tasks.scripts.ScriptOutput) InputStreamReader(java.io.InputStreamReader) FileInputStream(java.io.FileInputStream) InputStream(java.io.InputStream) RunContext(io.kestra.core.runners.RunContext) Bash(io.kestra.core.tasks.scripts.Bash) MicronautTest(io.micronaut.test.extensions.junit5.annotation.MicronautTest) Test(org.junit.jupiter.api.Test)

Example 3 with ScriptOutput

use of io.kestra.core.tasks.scripts.ScriptOutput 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"));
}
Also used : ScriptOutput(io.kestra.core.tasks.scripts.ScriptOutput) RunContext(io.kestra.core.runners.RunContext) Bash(io.kestra.core.tasks.scripts.Bash) MicronautTest(io.micronaut.test.extensions.junit5.annotation.MicronautTest) Test(org.junit.jupiter.api.Test)

Example 4 with ScriptOutput

use of io.kestra.core.tasks.scripts.ScriptOutput in project kestra by kestra-io.

the class AbstractBashTest method run.

@Test
void run() throws Exception {
    Bash bash = configure(Bash.builder().commands(new String[] { "echo 0", "echo 1", ">&2 echo 2", ">&2 echo 3" })).build();
    RunContext runContext = TestsUtils.mockRunContext(runContextFactory, bash, ImmutableMap.of());
    ScriptOutput run = bash.run(runContext);
    assertThat(run.getExitCode(), is(0));
    assertThat(run.getStdOutLineCount(), is(2));
    assertThat(run.getStdErrLineCount(), is(2));
}
Also used : ScriptOutput(io.kestra.core.tasks.scripts.ScriptOutput) RunContext(io.kestra.core.runners.RunContext) Bash(io.kestra.core.tasks.scripts.Bash) MicronautTest(io.micronaut.test.extensions.junit5.annotation.MicronautTest) Test(org.junit.jupiter.api.Test)

Example 5 with ScriptOutput

use of io.kestra.core.tasks.scripts.ScriptOutput in project kestra by kestra-io.

the class NodeTest method fileInSubFolders.

@Test
void fileInSubFolders() throws Exception {
    RunContext runContext = runContextFactory.of();
    Map<String, String> files = new HashMap<>();
    files.put("main.js", "console.log('::{\"outputs\": {\"extract\":\"' + (require('fs').readFileSync('./sub/folder/file/test.txt', 'utf-8')) + '\"}}::')");
    files.put("sub/folder/file/test.txt", "OK");
    files.put("sub/folder/file/test1.txt", "OK");
    Node node = Node.builder().id("test-node-task").nodePath("node").inputFiles(files).build();
    ScriptOutput run = node.run(runContext);
    assertThat(run.getExitCode(), is(0));
    assertThat(run.getVars().get("extract"), is("OK"));
}
Also used : ScriptOutput(io.kestra.core.tasks.scripts.ScriptOutput) HashMap(java.util.HashMap) Node(io.kestra.core.tasks.scripts.Node) RunContext(io.kestra.core.runners.RunContext) Test(org.junit.jupiter.api.Test) MicronautTest(io.micronaut.test.extensions.junit5.annotation.MicronautTest)

Aggregations

RunContext (io.kestra.core.runners.RunContext)21 ScriptOutput (io.kestra.core.tasks.scripts.ScriptOutput)21 MicronautTest (io.micronaut.test.extensions.junit5.annotation.MicronautTest)21 Test (org.junit.jupiter.api.Test)21 HashMap (java.util.HashMap)14 Python (io.kestra.core.tasks.scripts.Python)8 Bash (io.kestra.core.tasks.scripts.Bash)7 Node (io.kestra.core.tasks.scripts.Node)6 FileInputStream (java.io.FileInputStream)3 InputStream (java.io.InputStream)2 InputStreamReader (java.io.InputStreamReader)2 URI (java.net.URI)2 URL (java.net.URL)2 File (java.io.File)1 DisabledIfEnvironmentVariable (org.junit.jupiter.api.condition.DisabledIfEnvironmentVariable)1