Search in sources :

Example 1 with Python

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

the class PythonTest method args.

@Test
void args() throws Exception {
    RunContext runContext = runContextFactory.of(ImmutableMap.of("test", "value"));
    Map<String, String> files = new HashMap<>();
    files.put("main.py", "import sys; print('::{\"outputs\": {\"extract\":\"' + ' '.join(sys.argv) + '\"}}::')");
    Python python = Python.builder().id("test-python-task").pythonPath("python3").inputFiles(files).args(Arrays.asList("test", "param", "{{test}}")).build();
    ScriptOutput run = python.run(runContext);
    assertThat(run.getVars().get("extract"), is("main.py test param value"));
}
Also used : ScriptOutput(io.kestra.core.tasks.scripts.ScriptOutput) HashMap(java.util.HashMap) RunContext(io.kestra.core.runners.RunContext) Python(io.kestra.core.tasks.scripts.Python) Test(org.junit.jupiter.api.Test) MicronautTest(io.micronaut.test.extensions.junit5.annotation.MicronautTest)

Example 2 with Python

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

the class PythonTest method fileInSubFolders.

@Test
void fileInSubFolders() throws Exception {
    RunContext runContext = runContextFactory.of();
    Map<String, String> files = new HashMap<>();
    files.put("main.py", "print('::{\"outputs\": {\"extract\":\"' + open('sub/folder/file/test.txt').read() + '\"}}::')");
    files.put("sub/folder/file/test.txt", "OK");
    files.put("sub/folder/file/test1.txt", "OK");
    Python python = Python.builder().id("test-python-task").pythonPath("python3").inputFiles(files).build();
    ScriptOutput run = python.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) RunContext(io.kestra.core.runners.RunContext) Python(io.kestra.core.tasks.scripts.Python) Test(org.junit.jupiter.api.Test) MicronautTest(io.micronaut.test.extensions.junit5.annotation.MicronautTest)

Example 3 with Python

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

the class PythonTest method pipConf.

@Test
void pipConf() throws Exception {
    RunContext runContext = runContextFactory.of();
    Map<String, String> files = new HashMap<>();
    files.put("main.py", "print('::{\"outputs\": {\"extract\":\"' + str('#it worked !' in open('pip.conf').read()) + '\"}}::')");
    files.put("pip.conf", "[global]\nno-cache-dir = false\n#it worked !");
    Python python = Python.builder().id("test-python-task").pythonPath("python3").inputFiles(files).build();
    ScriptOutput run = python.run(runContext);
    assertThat(run.getExitCode(), is(0));
    assertThat(run.getVars().get("extract"), is("True"));
}
Also used : ScriptOutput(io.kestra.core.tasks.scripts.ScriptOutput) HashMap(java.util.HashMap) RunContext(io.kestra.core.runners.RunContext) Python(io.kestra.core.tasks.scripts.Python) Test(org.junit.jupiter.api.Test) MicronautTest(io.micronaut.test.extensions.junit5.annotation.MicronautTest)

Example 4 with Python

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

the class PythonTest method manyFiles.

@Test
void manyFiles() throws Exception {
    RunContext runContext = runContextFactory.of();
    Map<String, String> files = new HashMap<>();
    files.put("main.py", "import otherfile; otherfile.test()");
    files.put("otherfile.py", "def test(): print('::{\"outputs\": {\"extract\":\"success\"}}::')");
    Python python = Python.builder().id("test-python-task").pythonPath("python3").inputFiles(files).build();
    ScriptOutput run = python.run(runContext);
    assertThat(run.getExitCode(), is(0));
    assertThat(run.getVars().get("extract"), is("success"));
}
Also used : ScriptOutput(io.kestra.core.tasks.scripts.ScriptOutput) HashMap(java.util.HashMap) RunContext(io.kestra.core.runners.RunContext) Python(io.kestra.core.tasks.scripts.Python) Test(org.junit.jupiter.api.Test) MicronautTest(io.micronaut.test.extensions.junit5.annotation.MicronautTest)

Example 5 with Python

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

the class PythonTest method outputs.

@Test
void outputs() throws Exception {
    RunContext runContext = runContextFactory.of(ImmutableMap.of("test", "value"));
    Map<String, String> files = new HashMap<>();
    files.put("main.py", "from kestra import Kestra\n" + "import time\n" + "Kestra.outputs({'test': 'value', 'int': 2, 'bool': True, 'float': 3.65})\n" + "Kestra.counter('count', 1, {'tag1': 'i', 'tag2': 'win'})\n" + "Kestra.counter('count2', 2)\n" + "Kestra.timer('timer1', lambda: time.sleep(1), {'tag1': 'i', 'tag2': 'lost'})\n" + "Kestra.timer('timer2', 2.12, {'tag1': 'i', 'tag2': 'destroy'})\n");
    Python node = Python.builder().id("test-node-task").pythonPath("python3").inputFiles(files).build();
    ScriptOutput run = node.run(runContext);
    assertThat(run.getVars().get("test"), is("value"));
    assertThat(run.getVars().get("int"), is(2));
    assertThat(run.getVars().get("bool"), is(true));
    assertThat(run.getVars().get("float"), is(3.65));
    AbstractBashTest.controlOutputs(runContext, run);
}
Also used : ScriptOutput(io.kestra.core.tasks.scripts.ScriptOutput) HashMap(java.util.HashMap) RunContext(io.kestra.core.runners.RunContext) Python(io.kestra.core.tasks.scripts.Python) Test(org.junit.jupiter.api.Test) MicronautTest(io.micronaut.test.extensions.junit5.annotation.MicronautTest)

Aggregations

RunContext (io.kestra.core.runners.RunContext)9 Python (io.kestra.core.tasks.scripts.Python)9 MicronautTest (io.micronaut.test.extensions.junit5.annotation.MicronautTest)9 HashMap (java.util.HashMap)9 Test (org.junit.jupiter.api.Test)9 ScriptOutput (io.kestra.core.tasks.scripts.ScriptOutput)8 AbstractBash (io.kestra.core.tasks.scripts.AbstractBash)1 Bash (io.kestra.core.tasks.scripts.Bash)1