Skip to content

Commit

Permalink
Add pythonServer case for eval (#1399)
Browse files Browse the repository at this point in the history
* add pythonServer case for eval
fix tests

* change name pythonServer in pythonExecutor

* rename function

Co-authored-by: TVerdesOprea <[email protected]>
  • Loading branch information
opreateodora and TVerdesOprea authored Jan 12, 2023
1 parent 9b10816 commit cd647e9
Show file tree
Hide file tree
Showing 11 changed files with 96 additions and 55 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -59,18 +59,24 @@ public class ScriptEvaluator extends ScriptProcessor {
@Resource(name = "jythonRuntimeService")
private PythonRuntimeService legacyJythonRuntimeService;

@Resource(name = "externalPythonExecutorService")
private PythonRuntimeService pythonExecutorService;

@Autowired
private ScriptsService scriptsService;

public Value evalExpr(String expr, Map<String, Value> context, Set<SystemProperty> systemProperties,
Set<ScriptFunction> functionDependencies) {
try {
switch (PYTHON_EVALUATOR) {
case PYTHON_EXECUTOR:
return doEvaluateExpressionPythonExecutor(expr, context, systemProperties, functionDependencies);
case PYTHON:
return doEvaluateExpressionExternalPython(expr, context, systemProperties, functionDependencies);
case JYTHON:
return doEvaluateExpressionJython(expr, context, systemProperties, functionDependencies);
//case PYTHON_EXECUTOR or PYTHON
default:
return doEvaluateExpressionExternalPython(expr, context, systemProperties, functionDependencies);
return doEvaluateExpressionPythonExecutor(expr, context, systemProperties, functionDependencies);
}
} catch (Exception exception) {
throw new RuntimeException("Error in evaluating expression: '" +
Expand Down Expand Up @@ -121,6 +127,29 @@ private Value doEvaluateExpressionExternalPython(String expr,
return ValueFactory.create(result.getEvalResult(), getSensitive(pythonContext, accessedResources));
}

private Value doEvaluateExpressionPythonExecutor(String expr,
Map<String, Value> context,
Set<SystemProperty> systemProperties,
Set<ScriptFunction> functionDependencies) {
Map<String, Serializable> pythonContext = createExternalPythonContext(context);
boolean systemPropertiesDefined = false;
if (functionDependencies.contains(ScriptFunction.GET_SYSTEM_PROPERTY) ||
functionDependencies.contains(ScriptFunction.GET_SP_VAR)) {
systemPropertiesDefined = true;
}
if (systemPropertiesDefined) {
pythonContext.put(SYSTEM_PROPERTIES_MAP,
(Serializable) prepareSystemPropertiesForExternalPython(systemProperties));
}

PythonEvaluationResult result = pythonExecutorService.eval(
buildAddFunctionsScriptForExternalPython(functionDependencies), expr, pythonContext);

//noinspection unchecked
Set<String> accessedResources = (Set<String>) result.getResultContext().get(ACCESSED_RESOURCES_SET);
return ValueFactory.create(result.getEvalResult(), getSensitive(pythonContext, accessedResources));
}

public Value testExpr(String expr, Map<String, Value> context, Set<SystemProperty> systemProperties,
Set<ScriptFunction> functionDependencies, long timeoutPeriod) {
try {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,9 +28,8 @@
import io.cloudslang.runtime.impl.python.PythonExecutionNotCachedEngine;
import io.cloudslang.runtime.impl.python.PythonRuntimeServiceImpl;
import io.cloudslang.runtime.impl.python.external.ExternalPythonExecutionEngine;
import io.cloudslang.runtime.impl.python.external.ExternalPythonExecutorServiceImpl;
import io.cloudslang.runtime.impl.python.external.ExternalPythonRuntimeServiceImpl;
import io.cloudslang.runtime.impl.python.external.ExternalPythonServerService;
import io.cloudslang.runtime.impl.python.external.ExternalPythonServerServiceImpl;
import io.cloudslang.runtime.impl.python.external.StatefulRestEasyClientsHolder;
import io.cloudslang.score.events.EventBus;
import io.cloudslang.score.events.EventBusImpl;
Expand Down Expand Up @@ -460,9 +459,10 @@ public PythonExecutionEngine pythonExecutionEngine() {
return new PythonExecutionCachedEngine();
}

@Bean(name = "externalPythonServerService")
public ExternalPythonServerService externalPythonServerService() {
return new ExternalPythonServerServiceImpl(mock(StatefulRestEasyClientsHolder.class));
@Bean(name = "externalPythonExecutorService")
public PythonRuntimeService externalPythonExecutorService() {
return new ExternalPythonExecutorServiceImpl(mock(StatefulRestEasyClientsHolder.class),
new Semaphore(100), new Semaphore(50));
}

@Bean(name = "externalPythonRuntimeService")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,8 @@
import io.cloudslang.runtime.impl.python.PythonExecutionEngine;
import io.cloudslang.runtime.impl.python.PythonRuntimeServiceImpl;
import io.cloudslang.runtime.impl.python.external.ExternalPythonExecutionEngine;
import io.cloudslang.runtime.impl.python.external.ExternalPythonExecutorServiceImpl;
import io.cloudslang.runtime.impl.python.external.ExternalPythonRuntimeServiceImpl;
import io.cloudslang.runtime.impl.python.external.ExternalPythonServerService;
import io.cloudslang.runtime.impl.python.external.ExternalPythonServerServiceImpl;
import io.cloudslang.runtime.impl.python.external.StatefulRestEasyClientsHolder;
import io.cloudslang.score.events.EventBus;
import io.cloudslang.score.events.EventBusImpl;
Expand Down Expand Up @@ -559,9 +558,10 @@ public PythonExecutionEngine pythonExecutionEngine() {
return new PythonExecutionCachedEngine();
}

@Bean(name = "externalPythonServerService")
public ExternalPythonServerService externalPythonServerService() {
return new ExternalPythonServerServiceImpl(mock(StatefulRestEasyClientsHolder.class));
@Bean(name = "externalPythonExecutorService")
public PythonRuntimeService externalPythonExecutorService() {
return new ExternalPythonExecutorServiceImpl(mock(StatefulRestEasyClientsHolder.class),
new Semaphore(100), new Semaphore(50));
}

@Bean(name = "externalPythonRuntimeService")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,8 @@
import io.cloudslang.runtime.impl.python.PythonExecutionEngine;
import io.cloudslang.runtime.impl.python.PythonRuntimeServiceImpl;
import io.cloudslang.runtime.impl.python.external.ExternalPythonExecutionEngine;
import io.cloudslang.runtime.impl.python.external.ExternalPythonExecutorServiceImpl;
import io.cloudslang.runtime.impl.python.external.ExternalPythonRuntimeServiceImpl;
import io.cloudslang.runtime.impl.python.external.ExternalPythonServerService;
import io.cloudslang.runtime.impl.python.external.ExternalPythonServerServiceImpl;
import io.cloudslang.runtime.impl.python.external.StatefulRestEasyClientsHolder;
import io.cloudslang.score.events.EventBus;
import io.cloudslang.score.events.EventBusImpl;
Expand Down Expand Up @@ -143,7 +142,7 @@ public void testOperationOutputsExpression() {
Map<String, Value> operationContext = prepareOperationContext();
Map<String, Value> actionReturnValues = prepareActionReturnValues();
List<Output> outputs = singletonList(
createExpressionOutput("hostFromExpression", "${ 'http://' + hostExpr + ':' + str(port) }"));
createExpressionOutput("hostFromExpression", "${ 'http://' + hostExpr + ':' + str(port) }"));

Map<String, Value> result = outputsBinding
.bindOutputs(new ReadOnlyContextAccessor(operationContext, actionReturnValues), EMPTY_SET, outputs);
Expand All @@ -159,9 +158,9 @@ public void testOutputsRetainOrder() {
Map<String, Value> operationContext = prepareOperationContext();
Map<String, Value> actionReturnValues = prepareActionReturnValues();
List<Output> outputs = Lists.newArrayList(
createExpressionOutput("output1", "1"),
createExpressionOutput("output2", "2"),
createExpressionOutput("output3", "3")
createExpressionOutput("output1", "1"),
createExpressionOutput("output2", "2"),
createExpressionOutput("output3", "3")
);

Map<String, Value> result = outputsBinding
Expand Down Expand Up @@ -190,8 +189,8 @@ public void testOperationOutputsMixed() {
Map<String, Value> operationContext = prepareOperationContext();
Map<String, Value> actionReturnValues = prepareActionReturnValues();
List<Output> outputs = Arrays.asList(
createNoExpressionOutput("host1"),
createExpressionOutput("hostFromExpression", "${ 'http://' + hostExpr + ':' + str(port) }"));
createNoExpressionOutput("host1"),
createExpressionOutput("hostFromExpression", "${ 'http://' + hostExpr + ':' + str(port) }"));

Map<String, Value> result = outputsBinding
.bindOutputs(new ReadOnlyContextAccessor(operationContext, actionReturnValues), EMPTY_SET, outputs);
Expand Down Expand Up @@ -267,9 +266,10 @@ public PythonExecutionEngine pythonExecutionEngine() {
return new PythonExecutionCachedEngine();
}

@Bean(name = "externalPythonServerService")
public ExternalPythonServerService externalPythonServerService() {
return new ExternalPythonServerServiceImpl(mock(StatefulRestEasyClientsHolder.class));
@Bean(name = "externalPythonExecutorService")
public PythonRuntimeService externalPythonExecutorService() {
return new ExternalPythonExecutorServiceImpl(mock(StatefulRestEasyClientsHolder.class),
new Semaphore(100), new Semaphore(50));
}

@Bean(name = "externalPythonRuntimeService")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,8 @@
import io.cloudslang.runtime.impl.python.PythonExecutionEngine;
import io.cloudslang.runtime.impl.python.PythonRuntimeServiceImpl;
import io.cloudslang.runtime.impl.python.external.ExternalPythonExecutionEngine;
import io.cloudslang.runtime.impl.python.external.ExternalPythonExecutorServiceImpl;
import io.cloudslang.runtime.impl.python.external.ExternalPythonRuntimeServiceImpl;
import io.cloudslang.runtime.impl.python.external.ExternalPythonServerService;
import io.cloudslang.runtime.impl.python.external.ExternalPythonServerServiceImpl;
import io.cloudslang.runtime.impl.python.external.StatefulRestEasyClientsHolder;
import io.cloudslang.score.events.EventBus;
import io.cloudslang.score.events.EventBusImpl;
Expand Down Expand Up @@ -244,9 +243,10 @@ public PythonExecutionEngine pythonExecutionEngine() {
return new PythonExecutionCachedEngine();
}

@Bean(name = "externalPythonServerService")
public ExternalPythonServerService externalPythonServerService() {
return new ExternalPythonServerServiceImpl(mock(StatefulRestEasyClientsHolder.class));
@Bean(name = "externalPythonExecutorService")
public PythonRuntimeService externalPythonExecutorService() {
return new ExternalPythonExecutorServiceImpl(mock(StatefulRestEasyClientsHolder.class),
new Semaphore(100), new Semaphore(50));
}

@Bean(name = "externalPythonRuntimeService")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,8 @@
import io.cloudslang.runtime.impl.python.PythonExecutionEngine;
import io.cloudslang.runtime.impl.python.PythonExecutor;
import io.cloudslang.runtime.impl.python.external.ExternalPythonExecutionEngine;
import io.cloudslang.runtime.impl.python.external.ExternalPythonExecutorServiceImpl;
import io.cloudslang.runtime.impl.python.external.ExternalPythonRuntimeServiceImpl;
import io.cloudslang.runtime.impl.python.external.ExternalPythonServerService;
import io.cloudslang.runtime.impl.python.external.ExternalPythonServerServiceImpl;
import io.cloudslang.runtime.impl.python.external.StatefulRestEasyClientsHolder;
import io.cloudslang.score.events.EventBus;
import io.cloudslang.score.events.EventBusImpl;
Expand Down Expand Up @@ -210,9 +209,10 @@ protected PythonInterpreter initInterpreter(Set<String> dependencies) {
};
}

@Bean(name = "externalPythonServerService")
public ExternalPythonServerService externalPythonServerService() {
return new ExternalPythonServerServiceImpl(mock(StatefulRestEasyClientsHolder.class));
@Bean(name = "externalPythonExecutorService")
public PythonRuntimeService externalPythonExecutorService() {
return new ExternalPythonExecutorServiceImpl(mock(StatefulRestEasyClientsHolder.class),
new Semaphore(100), new Semaphore(50));
}

@Bean(name = "externalPythonRuntimeService")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,8 @@
import io.cloudslang.runtime.impl.python.PythonExecutor;
import io.cloudslang.runtime.impl.python.PythonRuntimeServiceImpl;
import io.cloudslang.runtime.impl.python.external.ExternalPythonExecutionEngine;
import io.cloudslang.runtime.impl.python.external.ExternalPythonExecutorServiceImpl;
import io.cloudslang.runtime.impl.python.external.ExternalPythonRuntimeServiceImpl;
import io.cloudslang.runtime.impl.python.external.ExternalPythonServerService;
import io.cloudslang.runtime.impl.python.external.ExternalPythonServerServiceImpl;
import io.cloudslang.runtime.impl.python.external.StatefulRestEasyClientsHolder;
import io.cloudslang.score.events.EventBus;
import io.cloudslang.score.events.EventBusImpl;
Expand All @@ -50,6 +49,7 @@
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.concurrent.Semaphore;

import static org.junit.Assert.assertThrows;
import static org.mockito.Matchers.any;
Expand Down Expand Up @@ -199,9 +199,10 @@ protected PythonInterpreter initInterpreter(Set<String> dependencies) {
};
}

@Bean(name = "externalPythonServerService")
public ExternalPythonServerService externalPythonServerService() {
return new ExternalPythonServerServiceImpl(mock(StatefulRestEasyClientsHolder.class));
@Bean(name = "externalPythonExecutorService")
public PythonRuntimeService externalPythonExecutorService() {
return new ExternalPythonExecutorServiceImpl(mock(StatefulRestEasyClientsHolder.class),
new Semaphore(100), new Semaphore(50));
}

@Bean(name = "externalPythonRuntimeService")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,9 +35,8 @@
import io.cloudslang.runtime.impl.python.PythonExecutionEngine;
import io.cloudslang.runtime.impl.python.PythonRuntimeServiceImpl;
import io.cloudslang.runtime.impl.python.external.ExternalPythonExecutionEngine;
import io.cloudslang.runtime.impl.python.external.ExternalPythonExecutorServiceImpl;
import io.cloudslang.runtime.impl.python.external.ExternalPythonRuntimeServiceImpl;
import io.cloudslang.runtime.impl.python.external.ExternalPythonServerService;
import io.cloudslang.runtime.impl.python.external.ExternalPythonServerServiceImpl;
import io.cloudslang.runtime.impl.python.external.StatefulRestEasyClientsHolder;
import io.cloudslang.score.api.execution.ExecutionParametersConsts;
import io.cloudslang.score.events.EventBus;
Expand Down Expand Up @@ -1207,9 +1206,10 @@ public PythonExecutionEngine pythonExecutionEngine() {
return new PythonExecutionCachedEngine();
}

@Bean(name = "externalPythonServerService")
public ExternalPythonServerService externalPythonServerService() {
return new ExternalPythonServerServiceImpl(mock(StatefulRestEasyClientsHolder.class));
@Bean(name = "externalPythonExecutorService")
public PythonRuntimeService externalPythonExecutorService() {
return new ExternalPythonExecutorServiceImpl(mock(StatefulRestEasyClientsHolder.class),
new Semaphore(100), new Semaphore(50));
}

@Bean(name = "externalPythonRuntimeService")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,9 +45,8 @@
import io.cloudslang.runtime.impl.python.PythonExecutionEngine;
import io.cloudslang.runtime.impl.python.PythonRuntimeServiceImpl;
import io.cloudslang.runtime.impl.python.external.ExternalPythonExecutionEngine;
import io.cloudslang.runtime.impl.python.external.ExternalPythonExecutorServiceImpl;
import io.cloudslang.runtime.impl.python.external.ExternalPythonRuntimeServiceImpl;
import io.cloudslang.runtime.impl.python.external.ExternalPythonServerService;
import io.cloudslang.runtime.impl.python.external.ExternalPythonServerServiceImpl;
import io.cloudslang.runtime.impl.python.external.StatefulRestEasyClientsHolder;
import io.cloudslang.score.api.execution.precondition.ExecutionPreconditionService;
import io.cloudslang.score.events.EventBus;
Expand Down Expand Up @@ -272,7 +271,7 @@ public void testStartExecutableRebindArguments() {
public void testStartExecutableRebindWithNoArguments() {
List<Argument> modifiedArguments = new ArrayList<>();
final RunEnvironment runEnv = new RunEnvironment();
runEnv.getStack().pushContext(new Context(new HashMap<>(),new HashMap<>()));
runEnv.getStack().pushContext(new Context(new HashMap<>(), new HashMap<>()));
runEnv.setContextModified(true);
runEnv.setModifiedArguments(modifiedArguments);
ExecutionRuntimeServices runtimeServices = new ExecutionRuntimeServices();
Expand Down Expand Up @@ -511,9 +510,10 @@ public PythonExecutionEngine pythonExecutionEngine() {
return new PythonExecutionCachedEngine();
}

@Bean(name = "externalPythonServerService")
public ExternalPythonServerService externalPythonServerService() {
return new ExternalPythonServerServiceImpl(mock(StatefulRestEasyClientsHolder.class));
@Bean(name = "externalPythonExecutorService")
public PythonRuntimeService externalPythonExecutorService() {
return new ExternalPythonExecutorServiceImpl(mock(StatefulRestEasyClientsHolder.class),
new Semaphore(100), new Semaphore(50));
}

@Bean(name = "externalPythonRuntimeService")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,9 @@
import io.cloudslang.runtime.impl.python.PythonExecutionCachedEngine;
import io.cloudslang.runtime.impl.python.PythonExecutionEngine;
import io.cloudslang.runtime.impl.python.PythonRuntimeServiceImpl;
import io.cloudslang.runtime.impl.python.external.ExternalPythonExecutionEngine;
import io.cloudslang.runtime.impl.python.external.ExternalPythonExecutorServiceImpl;
import io.cloudslang.runtime.impl.python.external.StatefulRestEasyClientsHolder;
import io.cloudslang.score.api.EndBranchDataContainer;
import io.cloudslang.score.events.EventBus;
import io.cloudslang.score.events.EventBusImpl;
Expand All @@ -57,6 +60,7 @@
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.concurrent.Semaphore;

import static org.junit.Assert.assertThrows;
import static org.junit.Assert.assertTrue;
Expand Down Expand Up @@ -607,9 +611,16 @@ public PythonRuntimeService externalPythonRuntimeService() {
return new PythonRuntimeServiceImpl();
}

@Bean(name = "externalPythonExecutorService")
public PythonRuntimeService externalPythonexecutorService() {
return new ExternalPythonExecutorServiceImpl(mock(StatefulRestEasyClientsHolder.class),
new Semaphore(100), new Semaphore(50));
}


@Bean(name = "externalPythonExecutionEngine")
public PythonExecutionEngine externalPythonExecutionEngine() {
return new PythonExecutionCachedEngine();
return new ExternalPythonExecutionEngine();
}

@Bean
Expand Down
Loading

0 comments on commit cd647e9

Please sign in to comment.