Search in sources :

Example 6 with Python

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

the class PythonTest method docker.

@Test
void docker() throws Exception {
    Map<String, String> files = new HashMap<>();
    files.put("main.py", "import requests; print('::{\"outputs\": {\"extract\":\"' + str(requests.get('http://google.com').status_code) + '\"}}::')");
    Python python = Python.builder().id("test-python-task").type(Python.class.getName()).inputFiles(files).runner(AbstractBash.Runner.DOCKER).dockerOptions(AbstractBash.DockerOptions.builder().image("python").build()).requirements(Collections.singletonList("requests")).build();
    RunContext runContext = TestsUtils.mockRunContext(runContextFactory, python, ImmutableMap.of());
    ScriptOutput run = python.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) 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 7 with Python

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

the class PythonTest method run.

@Test
void run() throws Exception {
    RunContext runContext = runContextFactory.of();
    Map<String, String> files = new HashMap<>();
    files.put("main.py", "print('::{\"outputs\": {\"extract\":\"hello world\"}}::')");
    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.getStdOutLineCount(), is(1));
    assertThat(run.getVars().get("extract"), is("hello world"));
}
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 8 with Python

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

the class PythonTest method failed.

@Test
void failed() {
    RunContext runContext = runContextFactory.of();
    Map<String, String> files = new HashMap<>();
    files.put("main.py", "import sys; sys.exit(1)");
    Python python = Python.builder().id("test-python-task").pythonPath("python3").inputFiles(files).build();
    Bash.BashException pythonException = assertThrows(Bash.BashException.class, () -> {
        python.run(runContext);
    });
    assertThat(pythonException.getExitCode(), is(1));
    assertThat(pythonException.getStdOutSize(), is(0));
}
Also used : HashMap(java.util.HashMap) RunContext(io.kestra.core.runners.RunContext) Bash(io.kestra.core.tasks.scripts.Bash) AbstractBash(io.kestra.core.tasks.scripts.AbstractBash) Python(io.kestra.core.tasks.scripts.Python) Test(org.junit.jupiter.api.Test) MicronautTest(io.micronaut.test.extensions.junit5.annotation.MicronautTest)

Example 9 with Python

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

the class PythonTest method requirements.

@Test
void requirements() throws Exception {
    RunContext runContext = runContextFactory.of();
    Map<String, String> files = new HashMap<>();
    files.put("main.py", "import requests; print('::{\"outputs\": {\"extract\":\"' + str(requests.get('http://google.com').status_code) + '\"}}::')");
    Python python = Python.builder().id("test-python-task").pythonPath("python3").inputFiles(files).requirements(Collections.singletonList("requests")).build();
    ScriptOutput run = python.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) 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