Search in sources :

Example 16 with ScriptOutput

use of io.kestra.core.tasks.scripts.ScriptOutput 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 17 with ScriptOutput

use of io.kestra.core.tasks.scripts.ScriptOutput 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 18 with ScriptOutput

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

Example 19 with ScriptOutput

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

the class AbstractBashTest method useInputFilesFromKestraFs.

@Test
void useInputFilesFromKestraFs() 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()));
    Map<String, String> files = new HashMap<>();
    files.put("test.sh", "cat fscontent.txt");
    files.put("fscontent.txt", put.toString());
    List<String> commands = new ArrayList<>();
    commands.add("cat fscontent.txt > {{ outputFiles.out }} ");
    Bash bash = configure(Bash.builder().interpreter("/bin/bash").commands(commands.toArray(String[]::new)).inputFiles(files).outputFiles(Collections.singletonList("out"))).build();
    RunContext runContext = TestsUtils.mockRunContext(runContextFactory, bash, ImmutableMap.of());
    ScriptOutput run = bash.run(runContext);
    assertThat(run.getExitCode(), is(0));
    InputStream get = storageInterface.get(run.getOutputFiles().get("out"));
    String outputContent = CharStreams.toString(new InputStreamReader(get));
    String fileContent = String.join("\n", Files.readAllLines(new File(resource.getPath()).toPath(), StandardCharsets.UTF_8));
    assertThat(outputContent, is(fileContent));
}
Also used : InputStreamReader(java.io.InputStreamReader) FileInputStream(java.io.FileInputStream) InputStream(java.io.InputStream) Bash(io.kestra.core.tasks.scripts.Bash) URI(java.net.URI) URL(java.net.URL) FileInputStream(java.io.FileInputStream) ScriptOutput(io.kestra.core.tasks.scripts.ScriptOutput) RunContext(io.kestra.core.runners.RunContext) File(java.io.File) MicronautTest(io.micronaut.test.extensions.junit5.annotation.MicronautTest) Test(org.junit.jupiter.api.Test)

Example 20 with ScriptOutput

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

the class AbstractBashTest method dontStopOnFirstFailed.

@Test
@DisabledIfEnvironmentVariable(named = "GITHUB_WORKFLOW", matches = ".*")
void dontStopOnFirstFailed() throws Exception {
    Bash bash = configure(Bash.builder().commands(new String[] { "unknown", "echo 1" }).exitOnFailed(false)).build();
    RunContext runContext = TestsUtils.mockRunContext(runContextFactory, bash, ImmutableMap.of());
    ScriptOutput run = bash.run(runContext);
    assertThat(run.getExitCode(), is(0));
    assertThat(run.getStdOutLineCount(), is(1));
    assertThat(run.getStdErrLineCount(), is(1));
}
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) DisabledIfEnvironmentVariable(org.junit.jupiter.api.condition.DisabledIfEnvironmentVariable)

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