Search in sources :

Example 31 with SimpleScript

use of org.ow2.proactive.scripting.SimpleScript in project scheduling by ow2-proactive.

the class ForkedTaskVariablesManagerTest method testScriptParametersAreReplaced.

@Test
public void testScriptParametersAreReplaced() throws InvalidScriptException {
    ForkedTaskVariablesManager forkedTaskVariablesManager = new ForkedTaskVariablesManager();
    // Create a variable $[something] inside a python script
    Serializable[] parameters = new Serializable[] { "$" + testVariable1Key };
    Script script = new SimpleScript("print 'hello'", "python", parameters);
    // Create a hash map with key as varialbe name and value as variable value.
    Map<String, Serializable> variables = new HashMap<>();
    variables.put(testVariable1Key, testVariable1Value);
    VariablesMap variablesMap = new VariablesMap();
    variablesMap.setInheritedMap(variables);
    // Replace that variable inside the script parameters with its value in the hash map
    forkedTaskVariablesManager.replaceScriptParameters(script, new HashMap<String, String>(), variablesMap, System.out);
    assertThat((String) parameters[0], is(testVariable1Value));
}
Also used : TaskScript(org.ow2.proactive.scripting.TaskScript) Script(org.ow2.proactive.scripting.Script) SimpleScript(org.ow2.proactive.scripting.SimpleScript) Serializable(java.io.Serializable) HashMap(java.util.HashMap) SimpleScript(org.ow2.proactive.scripting.SimpleScript) VariablesMap(org.ow2.proactive.scheduler.task.utils.VariablesMap) Test(org.junit.Test)

Example 32 with SimpleScript

use of org.ow2.proactive.scripting.SimpleScript in project scheduling by ow2-proactive.

the class ForkedTaskVariablesManagerTest method testScriptCredentialsAreReplaced.

@Test
public void testScriptCredentialsAreReplaced() throws InvalidScriptException {
    ForkedTaskVariablesManager forkedTaskVariablesManager = new ForkedTaskVariablesManager();
    // Add $credential_[something] variable to new python script
    Serializable[] parameters = new Serializable[] { "$" + ForkedTaskVariablesManager.CREDENTIALS_KEY_PREFIX + testVariable1Key };
    Script script = new SimpleScript("print 'hello'", "python", parameters);
    // Create credentials
    Map<String, String> credentials = new HashMap<>();
    credentials.put(testVariable1Key, testVariable1Value);
    // Replace the credentials inside the script parameters
    forkedTaskVariablesManager.replaceScriptParameters(script, credentials, new VariablesMap(), System.out);
    assertThat((String) parameters[0], is(testVariable1Value));
}
Also used : TaskScript(org.ow2.proactive.scripting.TaskScript) Script(org.ow2.proactive.scripting.Script) SimpleScript(org.ow2.proactive.scripting.SimpleScript) Serializable(java.io.Serializable) HashMap(java.util.HashMap) SimpleScript(org.ow2.proactive.scripting.SimpleScript) VariablesMap(org.ow2.proactive.scheduler.task.utils.VariablesMap) Test(org.junit.Test)

Example 33 with SimpleScript

use of org.ow2.proactive.scripting.SimpleScript in project scheduling by ow2-proactive.

the class ForkedTaskExecutorRunAsMeTest method runAsMe_PasswordMethod.

@Test
public void runAsMe_PasswordMethod() throws Throwable {
    TestTaskOutput taskOutput = new TestTaskOutput();
    Decrypter decrypter = createCredentials(USERNAME, PASSWORD);
    ForkedTaskExecutor taskExecutor = new ForkedTaskExecutor(tmpFolder.newFolder());
    TaskLauncherInitializer initializer = new TaskLauncherInitializer();
    initializer.setTaskId((TaskIdImpl.createTaskId(JobIdImpl.makeJobId("1000"), "job", 1000L)));
    ScriptExecutableContainer container = new ScriptExecutableContainer(new TaskScript(new SimpleScript("whoami", "native")));
    container.setRunAsUser(true);
    TaskContext taskContext = new TaskContext(container, initializer, null, new NodeDataSpacesURIs("", "", "", "", "", ""), "", "", decrypter);
    TaskResultImpl result = taskExecutor.execute(taskContext, taskOutput.outputStream, taskOutput.error);
    assertTaskResultOk(result);
    assertEquals("admin\n", taskOutput.output());
}
Also used : TaskScript(org.ow2.proactive.scripting.TaskScript) TaskContext(org.ow2.proactive.scheduler.task.context.TaskContext) TaskResultImpl(org.ow2.proactive.scheduler.task.TaskResultImpl) ForkedTaskExecutor(org.ow2.proactive.scheduler.task.executors.ForkedTaskExecutor) SimpleScript(org.ow2.proactive.scripting.SimpleScript) Decrypter(org.ow2.proactive.scheduler.task.utils.Decrypter) ScriptExecutableContainer(org.ow2.proactive.scheduler.task.containers.ScriptExecutableContainer) NodeDataSpacesURIs(org.ow2.proactive.scheduler.task.context.NodeDataSpacesURIs) TestTaskOutput(org.ow2.proactive.scheduler.task.TestTaskOutput) TaskLauncherInitializer(org.ow2.proactive.scheduler.task.TaskLauncherInitializer) Test(org.junit.Test)

Example 34 with SimpleScript

use of org.ow2.proactive.scripting.SimpleScript in project scheduling by ow2-proactive.

the class ForkedTaskExecutorTest method nonDaemonThreadsForkedJVMExit.

/**
 * This test ensures that the forked JVM exited properly when non-daemon threads are created inside the task
 *
 * @throws Exception
 */
@Test(timeout = 30000)
public void nonDaemonThreadsForkedJVMExit() throws Exception {
    String taskScript = CharStreams.toString(new InputStreamReader(getClass().getResourceAsStream("/task-nondaemon-thread.groovy"), Charsets.UTF_8));
    TestTaskOutput taskOutput = new TestTaskOutput();
    File workingDir = tmpFolder.newFolder();
    ForkedTaskExecutor taskExecutor = new ForkedTaskExecutor(workingDir);
    TaskLauncherInitializer initializer = new TaskLauncherInitializer();
    initializer.setTaskId((TaskIdImpl.createTaskId(JobIdImpl.makeJobId("1000"), "job", 1000L)));
    TaskResultImpl result = taskExecutor.execute(new TaskContext(new ScriptExecutableContainer(new TaskScript(new SimpleScript(taskScript, "groovy"))), initializer, null, new NodeDataSpacesURIs("", "", "", "", "", ""), "", ""), taskOutput.outputStream, taskOutput.error);
    Assert.assertFalse(result.hadException());
}
Also used : TaskContext(org.ow2.proactive.scheduler.task.context.TaskContext) TaskScript(org.ow2.proactive.scripting.TaskScript) InputStreamReader(java.io.InputStreamReader) TaskResultImpl(org.ow2.proactive.scheduler.task.TaskResultImpl) ForkedTaskExecutor(org.ow2.proactive.scheduler.task.executors.ForkedTaskExecutor) SimpleScript(org.ow2.proactive.scripting.SimpleScript) ScriptExecutableContainer(org.ow2.proactive.scheduler.task.containers.ScriptExecutableContainer) NodeDataSpacesURIs(org.ow2.proactive.scheduler.task.context.NodeDataSpacesURIs) TestTaskOutput(org.ow2.proactive.scheduler.task.TestTaskOutput) File(java.io.File) TaskLauncherInitializer(org.ow2.proactive.scheduler.task.TaskLauncherInitializer) Test(org.junit.Test)

Example 35 with SimpleScript

use of org.ow2.proactive.scripting.SimpleScript in project scheduling by ow2-proactive.

the class InProcessTaskExecutorTest method simpleScriptTask.

@Test
public void simpleScriptTask() throws Throwable {
    TestTaskOutput taskOutput = new TestTaskOutput();
    TaskLauncherInitializer initializer = new TaskLauncherInitializer();
    initializer.setPreScript(new SimpleScript("println('pre')", "groovy"));
    initializer.setPostScript(new SimpleScript("println('post')", "groovy"));
    initializer.setTaskId(TaskIdImpl.createTaskId(JobIdImpl.makeJobId("1000"), "job", 1000L));
    TaskResultImpl result = new InProcessTaskExecutor().execute(new TaskContext(new ScriptExecutableContainer(new TaskScript(new SimpleScript("println('hello'); java.lang.Thread.sleep(5); result='hello'", "groovy"))), initializer, null, new NodeDataSpacesURIs("", "", "", "", "", ""), "", ""), taskOutput.outputStream, taskOutput.error);
    assertEquals(String.format("pre%nhello%npost%n"), taskOutput.output());
    assertEquals("hello", result.value());
    assertTrue("Task duration should be at least 5", result.getTaskDuration() >= 5);
}
Also used : TaskContext(org.ow2.proactive.scheduler.task.context.TaskContext) TaskScript(org.ow2.proactive.scripting.TaskScript) InProcessTaskExecutor(org.ow2.proactive.scheduler.task.executors.InProcessTaskExecutor) SimpleScript(org.ow2.proactive.scripting.SimpleScript) ScriptExecutableContainer(org.ow2.proactive.scheduler.task.containers.ScriptExecutableContainer) NodeDataSpacesURIs(org.ow2.proactive.scheduler.task.context.NodeDataSpacesURIs) Test(org.junit.Test)

Aggregations

SimpleScript (org.ow2.proactive.scripting.SimpleScript)99 TaskScript (org.ow2.proactive.scripting.TaskScript)78 Test (org.junit.Test)68 ScriptExecutableContainer (org.ow2.proactive.scheduler.task.containers.ScriptExecutableContainer)64 NodeDataSpacesURIs (org.ow2.proactive.scheduler.task.context.NodeDataSpacesURIs)31 TaskContext (org.ow2.proactive.scheduler.task.context.TaskContext)31 TaskResult (org.ow2.proactive.scheduler.common.task.TaskResult)21 TaskLauncherInitializer (org.ow2.proactive.scheduler.task.TaskLauncherInitializer)19 InProcessTaskExecutor (org.ow2.proactive.scheduler.task.executors.InProcessTaskExecutor)19 TaskFlowJob (org.ow2.proactive.scheduler.common.job.TaskFlowJob)18 File (java.io.File)16 ForkEnvironment (org.ow2.proactive.scheduler.common.task.ForkEnvironment)13 Serializable (java.io.Serializable)12 ScriptTask (org.ow2.proactive.scheduler.common.task.ScriptTask)10 TaskResultImpl (org.ow2.proactive.scheduler.task.TaskResultImpl)10 TestTaskOutput (org.ow2.proactive.scheduler.task.TestTaskOutput)9 ForkedTaskExecutor (org.ow2.proactive.scheduler.task.executors.ForkedTaskExecutor)9 JobIdImpl (org.ow2.proactive.scheduler.job.JobIdImpl)8 HashMap (java.util.HashMap)7 JobId (org.ow2.proactive.scheduler.common.job.JobId)7