Search in sources :

Example 1 with RunContext

use of io.kestra.core.runners.RunContext in project kestra by kestra-io.

the class ConditionServiceTest method valid.

@Test
void valid() {
    Flow flow = TestsUtils.mockFlow();
    Execution execution = TestsUtils.mockExecution(flow, ImmutableMap.of());
    RunContext runContext = runContextFactory.of(flow, execution);
    ConditionContext conditionContext = conditionService.conditionContext(runContext, flow, execution);
    List<Condition> conditions = Arrays.asList(ExecutionFlowCondition.builder().namespace(flow.getNamespace()).flowId(flow.getId()).build(), ExecutionNamespaceCondition.builder().namespace(flow.getNamespace()).build());
    boolean valid = conditionService.valid(flow, conditions, conditionContext);
    assertThat(valid, is(true));
}
Also used : ExecutionFlowCondition(io.kestra.core.models.conditions.types.ExecutionFlowCondition) ExecutionNamespaceCondition(io.kestra.core.models.conditions.types.ExecutionNamespaceCondition) Condition(io.kestra.core.models.conditions.Condition) ConditionContext(io.kestra.core.models.conditions.ConditionContext) Execution(io.kestra.core.models.executions.Execution) RunContext(io.kestra.core.runners.RunContext) Flow(io.kestra.core.models.flows.Flow) MicronautTest(io.micronaut.test.extensions.junit5.annotation.MicronautTest) Test(org.junit.jupiter.api.Test)

Example 2 with RunContext

use of io.kestra.core.runners.RunContext in project kestra by kestra-io.

the class ConditionServiceTest method exception.

@Test
void exception() throws InterruptedException {
    List<LogEntry> logs = new ArrayList<>();
    logQueue.receive(logs::add);
    Flow flow = TestsUtils.mockFlow();
    Schedule schedule = Schedule.builder().id("unit").type(Schedule.class.getName()).cron("0 0 1 * *").build();
    RunContext runContext = runContextFactory.of(flow, schedule);
    ConditionContext conditionContext = conditionService.conditionContext(runContext, flow, null);
    List<Condition> conditions = Collections.singletonList(ExecutionFlowCondition.builder().namespace(flow.getNamespace()).flowId(flow.getId()).build());
    conditionService.valid(flow, conditions, conditionContext);
    Thread.sleep(250);
    assertThat(logs.stream().filter(logEntry -> logEntry.getNamespace().equals("io.kestra.core.services.ConditionServiceTest")).count(), greaterThan(0L));
    assertThat(logs.stream().filter(logEntry -> logEntry.getFlowId().equals("exception")).count(), greaterThan(0L));
}
Also used : ExecutionFlowCondition(io.kestra.core.models.conditions.types.ExecutionFlowCondition) ExecutionNamespaceCondition(io.kestra.core.models.conditions.types.ExecutionNamespaceCondition) Condition(io.kestra.core.models.conditions.Condition) ConditionContext(io.kestra.core.models.conditions.ConditionContext) Schedule(io.kestra.core.models.triggers.types.Schedule) ArrayList(java.util.ArrayList) RunContext(io.kestra.core.runners.RunContext) LogEntry(io.kestra.core.models.executions.LogEntry) Flow(io.kestra.core.models.flows.Flow) MicronautTest(io.micronaut.test.extensions.junit5.annotation.MicronautTest) Test(org.junit.jupiter.api.Test)

Example 3 with RunContext

use of io.kestra.core.runners.RunContext in project kestra by kestra-io.

the class AbstractBashTest method preventRelativeFile.

@Test
void preventRelativeFile() 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()));
    assertThrows(IllegalArgumentException.class, () -> {
        Bash bash = configure(Bash.builder().commands(new String[] { "echo 1" }).inputFiles(Map.of("{{ inputs.vars }}", put.toString()))).build();
        RunContext runContext = TestsUtils.mockRunContext(runContextFactory, bash, ImmutableMap.of("vars", "../../test.txt"));
        bash.run(runContext);
    });
    assertThrows(IllegalArgumentException.class, () -> {
        Bash bash = configure(Bash.builder().commands(new String[] { "echo 1" }).inputFiles(Map.of("{{ inputs.vars }}", put.toString()))).build();
        RunContext runContext = TestsUtils.mockRunContext(runContextFactory, bash, ImmutableMap.of("vars", "../../test.txt"));
        bash.run(runContext);
    });
    // we allow dot file starting with a .
    Bash bash = configure(Bash.builder().commands(new String[] { "echo 1" }).inputFiles(Map.of("{{ inputs.vars }}", put.toString()))).build();
    RunContext runContext = TestsUtils.mockRunContext(runContextFactory, bash, ImmutableMap.of("vars", ".test.txt"));
    ScriptOutput run = bash.run(runContext);
    assertThat(run.getExitCode(), is(0));
}
Also used : ScriptOutput(io.kestra.core.tasks.scripts.ScriptOutput) RunContext(io.kestra.core.runners.RunContext) Bash(io.kestra.core.tasks.scripts.Bash) URI(java.net.URI) URL(java.net.URL) FileInputStream(java.io.FileInputStream) MicronautTest(io.micronaut.test.extensions.junit5.annotation.MicronautTest) Test(org.junit.jupiter.api.Test)

Example 4 with RunContext

use of io.kestra.core.runners.RunContext in project kestra by kestra-io.

the class AbstractBashTest method stopOnFirstFailed.

@Test
@DisabledIfEnvironmentVariable(named = "GITHUB_WORKFLOW", matches = ".*")
void stopOnFirstFailed() {
    Bash bash = configure(Bash.builder().commands(new String[] { "unknown", "echo 1" })).build();
    RunContext runContext = TestsUtils.mockRunContext(runContextFactory, bash, ImmutableMap.of());
    Bash.BashException bashException = assertThrows(Bash.BashException.class, () -> {
        bash.run(runContext);
    });
    assertThat(bashException.getExitCode(), is(127));
    assertThat(bashException.getStdOutSize(), is(0));
    assertThat(bashException.getStdErrSize(), is(1));
}
Also used : 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 5 with RunContext

use of io.kestra.core.runners.RunContext in project kestra by kestra-io.

the class AbstractBashTest method files.

@Test
void files() throws Exception {
    Bash bash = configure(Bash.builder().outputFiles(Arrays.asList("xml", "csv")).inputFiles(ImmutableMap.of("files/in/in.txt", "I'm here")).commands(new String[] { "echo '::{\"outputs\": {\"extract\":\"'$(cat files/in/in.txt)'\"}}::'", "echo 1 >> {{ outputFiles.xml }}", "echo 2 >> {{ outputFiles.csv }}", "echo 3 >> {{ outputFiles.xml }}" })).build();
    RunContext runContext = TestsUtils.mockRunContext(runContextFactory, bash, ImmutableMap.of());
    ScriptOutput run = bash.run(runContext);
    assertThat(run.getExitCode(), is(0));
    assertThat(run.getStdErrLineCount(), is(0));
    assertThat(run.getStdOutLineCount(), is(1));
    assertThat(run.getVars().get("extract"), is("I'm here"));
    InputStream get = storageInterface.get(run.getOutputFiles().get("xml"));
    assertThat(CharStreams.toString(new InputStreamReader(get)), is("1\n3\n"));
    get = storageInterface.get(run.getOutputFiles().get("csv"));
    assertThat(CharStreams.toString(new InputStreamReader(get)), is("2\n"));
}
Also used : ScriptOutput(io.kestra.core.tasks.scripts.ScriptOutput) InputStreamReader(java.io.InputStreamReader) FileInputStream(java.io.FileInputStream) InputStream(java.io.InputStream) 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

RunContext (io.kestra.core.runners.RunContext)37 MicronautTest (io.micronaut.test.extensions.junit5.annotation.MicronautTest)31 Test (org.junit.jupiter.api.Test)31 ScriptOutput (io.kestra.core.tasks.scripts.ScriptOutput)21 HashMap (java.util.HashMap)18 Bash (io.kestra.core.tasks.scripts.Bash)11 Python (io.kestra.core.tasks.scripts.Python)9 Node (io.kestra.core.tasks.scripts.Node)7 FileInputStream (java.io.FileInputStream)6 URI (java.net.URI)6 URL (java.net.URL)5 Logger (org.slf4j.Logger)5 Collectors (java.util.stream.Collectors)4 ImmutableMap (com.google.common.collect.ImmutableMap)3 ConditionContext (io.kestra.core.models.conditions.ConditionContext)3 Execution (io.kestra.core.models.executions.Execution)3 Flow (io.kestra.core.models.flows.Flow)3 List (java.util.List)3 Map (java.util.Map)3 PluginProperty (io.kestra.core.models.annotations.PluginProperty)2