Search in sources :

Example 6 with Bash

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

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

the class WorkerTest method workerTask.

private WorkerTask workerTask(String sleep) {
    Bash bash = Bash.builder().type(Bash.class.getName()).id("unit-test").commands(new String[] { "for i in $(seq 1 " + sleep + "); do echo $i; sleep 1; done" }).build();
    Flow flow = Flow.builder().id(IdUtils.create()).namespace("io.kestra.unit-test").tasks(Collections.singletonList(bash)).build();
    Execution execution = TestsUtils.mockExecution(flow, ImmutableMap.of());
    ResolvedTask resolvedTask = ResolvedTask.of(bash);
    return WorkerTask.builder().runContext(runContextFactory.of(ImmutableMap.of("key", "value"))).task(bash).taskRun(TaskRun.of(execution, resolvedTask)).build();
}
Also used : Execution(io.kestra.core.models.executions.Execution) ResolvedTask(io.kestra.core.models.tasks.ResolvedTask) Bash(io.kestra.core.tasks.scripts.Bash) Flow(io.kestra.core.models.flows.Flow)

Example 8 with Bash

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

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

Example 10 with Bash

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

the class AbstractBashTest method longBashCreateTempFiles.

@Test
void longBashCreateTempFiles() throws Exception {
    List<String> commands = new ArrayList<>();
    for (int i = 0; i < 15000; i++) {
        commands.add("if [ \"" + i + "\" -eq 0 ] || [ \"" + i + "\" -eq 14999  ]; then echo " + i + ";fi;");
    }
    Bash bash = configure(Bash.builder().commands(commands.toArray(String[]::new))).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() > 0, is(false));
}
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)

Aggregations

Bash (io.kestra.core.tasks.scripts.Bash)10 RunContext (io.kestra.core.runners.RunContext)9 MicronautTest (io.micronaut.test.extensions.junit5.annotation.MicronautTest)9 Test (org.junit.jupiter.api.Test)9 ScriptOutput (io.kestra.core.tasks.scripts.ScriptOutput)7 FileInputStream (java.io.FileInputStream)3 DisabledIfEnvironmentVariable (org.junit.jupiter.api.condition.DisabledIfEnvironmentVariable)3 InputStream (java.io.InputStream)2 InputStreamReader (java.io.InputStreamReader)2 URI (java.net.URI)2 URL (java.net.URL)2 Execution (io.kestra.core.models.executions.Execution)1 Flow (io.kestra.core.models.flows.Flow)1 ResolvedTask (io.kestra.core.models.tasks.ResolvedTask)1 File (java.io.File)1