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"));
}
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"));
}
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"));
}
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"));
}
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);
}
Aggregations