Search in sources :

Example 1 with Node

use of io.kestra.core.tasks.scripts.Node 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)

Example 2 with Node

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

the class NodeTest method args.

@Test
void args() throws Exception {
    RunContext runContext = runContextFactory.of(ImmutableMap.of("test", "value"));
    Map<String, String> files = new HashMap<>();
    files.put("main.js", "console.log('::{\"outputs\": {\"extract\":\"' + (process.argv.slice(2).join(' ')) + '\"}}::')");
    Node node = Node.builder().id("test-node-task").nodePath("node").inputFiles(files).args(Arrays.asList("test", "param", "{{test}}")).build();
    ScriptOutput run = node.run(runContext);
    assertThat(run.getVars().get("extract"), is("test param value"));
}
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)

Example 3 with Node

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

the class NodeTest method outputs.

@Test
void outputs() throws Exception {
    RunContext runContext = runContextFactory.of(ImmutableMap.of("test", "value"));
    Map<String, String> files = new HashMap<>();
    files.put("main.js", "const Kestra = require(\"./kestra\");" + "Kestra.outputs({test: 'value', int: 2, bool: true, float: 3.65});" + "Kestra.counter('count', 1, {tag1: 'i', tag2: 'win'});" + "Kestra.counter('count2', 2);" + "Kestra.timer('timer1', (callback) => { setTimeout(callback, 1000) }, {tag1: 'i', tag2: 'lost'});" + "Kestra.timer('timer2', 2.12, {tag1: 'i', tag2: 'destroy'});");
    Node node = Node.builder().id("test-node-task").nodePath("node").inputFiles(files).build();
    ScriptOutput run = node.run(runContext);
    AbstractBashTest.controlOutputs(runContext, run);
}
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)

Example 4 with Node

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

the class NodeTest method requirements.

@Test
void requirements() throws Exception {
    RunContext runContext = runContextFactory.of();
    Map<String, String> files = new HashMap<>();
    files.put("main.js", "require('axios').get('http://google.com').then(r => { console.log('::{\"outputs\": {\"extract\":\"' + r.status + '\"}}::') })");
    files.put("package.json", "{\"dependencies\":{\"axios\":\"^0.20.0\"}}");
    Node node = Node.builder().id("test-node-task").nodePath("node").npmPath("npm").inputFiles(files).build();
    ScriptOutput run = node.run(runContext);
    assertThat(run.getExitCode(), is(0));
    assertThat(run.getVars().get("extract"), is("200"));
}
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)

Example 5 with Node

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

the class NodeTest method failed.

@Test
void failed() throws Exception {
    RunContext runContext = runContextFactory.of();
    Map<String, String> files = new HashMap<>();
    files.put("main.js", "process.exit(1)");
    Node node = Node.builder().id("test-node-task").nodePath("node").inputFiles(files).build();
    Bash.BashException nodeException = assertThrows(Bash.BashException.class, () -> {
        node.run(runContext);
    });
    assertThat(nodeException.getExitCode(), is(1));
    assertThat(nodeException.getStdOutSize(), is(0));
    assertThat(nodeException.getStdErrSize(), equalTo(0));
}
Also used : HashMap(java.util.HashMap) Node(io.kestra.core.tasks.scripts.Node) RunContext(io.kestra.core.runners.RunContext) Bash(io.kestra.core.tasks.scripts.Bash) Test(org.junit.jupiter.api.Test) MicronautTest(io.micronaut.test.extensions.junit5.annotation.MicronautTest)

Aggregations

RunContext (io.kestra.core.runners.RunContext)7 Node (io.kestra.core.tasks.scripts.Node)7 MicronautTest (io.micronaut.test.extensions.junit5.annotation.MicronautTest)7 HashMap (java.util.HashMap)7 Test (org.junit.jupiter.api.Test)7 ScriptOutput (io.kestra.core.tasks.scripts.ScriptOutput)6 Bash (io.kestra.core.tasks.scripts.Bash)1