Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add pythonServer case for eval #1399

Merged
merged 3 commits into from
Jan 12, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 = "externalPythonServerService")
private PythonRuntimeService pythonServerService;

@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 doEvaluateExpressionServerPython(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 doEvaluateExpressionServerPython(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 doEvaluateExpressionServerPython(String expr,
opreateodora marked this conversation as resolved.
Show resolved Hide resolved
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 = pythonServerService.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 @@ -29,7 +29,6 @@
import io.cloudslang.runtime.impl.python.PythonRuntimeServiceImpl;
import io.cloudslang.runtime.impl.python.external.ExternalPythonExecutionEngine;
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;
Expand Down Expand Up @@ -461,8 +460,9 @@ public PythonExecutionEngine pythonExecutionEngine() {
}

@Bean(name = "externalPythonServerService")
public ExternalPythonServerService externalPythonServerService() {
return new ExternalPythonServerServiceImpl(mock(StatefulRestEasyClientsHolder.class));
public PythonRuntimeService externalPythonServerService() {
return new ExternalPythonServerServiceImpl(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 @@ -27,7 +27,6 @@
import io.cloudslang.runtime.impl.python.PythonRuntimeServiceImpl;
import io.cloudslang.runtime.impl.python.external.ExternalPythonExecutionEngine;
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;
Expand Down Expand Up @@ -560,8 +559,9 @@ public PythonExecutionEngine pythonExecutionEngine() {
}

@Bean(name = "externalPythonServerService")
public ExternalPythonServerService externalPythonServerService() {
return new ExternalPythonServerServiceImpl(mock(StatefulRestEasyClientsHolder.class));
public PythonRuntimeService externalPythonServerService() {
return new ExternalPythonServerServiceImpl(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,7 +26,6 @@
import io.cloudslang.runtime.impl.python.PythonRuntimeServiceImpl;
import io.cloudslang.runtime.impl.python.external.ExternalPythonExecutionEngine;
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;
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 @@ -268,8 +267,9 @@ public PythonExecutionEngine pythonExecutionEngine() {
}

@Bean(name = "externalPythonServerService")
public ExternalPythonServerService externalPythonServerService() {
return new ExternalPythonServerServiceImpl(mock(StatefulRestEasyClientsHolder.class));
public PythonRuntimeService externalPythonServerService() {
return new ExternalPythonServerServiceImpl(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 @@ -27,7 +27,6 @@
import io.cloudslang.runtime.impl.python.PythonRuntimeServiceImpl;
import io.cloudslang.runtime.impl.python.external.ExternalPythonExecutionEngine;
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;
Expand Down Expand Up @@ -245,8 +244,9 @@ public PythonExecutionEngine pythonExecutionEngine() {
}

@Bean(name = "externalPythonServerService")
public ExternalPythonServerService externalPythonServerService() {
return new ExternalPythonServerServiceImpl(mock(StatefulRestEasyClientsHolder.class));
public PythonRuntimeService externalPythonServerService() {
return new ExternalPythonServerServiceImpl(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,7 +25,6 @@
import io.cloudslang.runtime.impl.python.PythonExecutor;
import io.cloudslang.runtime.impl.python.external.ExternalPythonExecutionEngine;
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;
Expand Down Expand Up @@ -211,8 +210,9 @@ protected PythonInterpreter initInterpreter(Set<String> dependencies) {
}

@Bean(name = "externalPythonServerService")
public ExternalPythonServerService externalPythonServerService() {
return new ExternalPythonServerServiceImpl(mock(StatefulRestEasyClientsHolder.class));
public PythonRuntimeService externalPythonServerService() {
return new ExternalPythonServerServiceImpl(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,7 +24,6 @@
import io.cloudslang.runtime.impl.python.PythonRuntimeServiceImpl;
import io.cloudslang.runtime.impl.python.external.ExternalPythonExecutionEngine;
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;
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 @@ -200,8 +200,9 @@ protected PythonInterpreter initInterpreter(Set<String> dependencies) {
}

@Bean(name = "externalPythonServerService")
public ExternalPythonServerService externalPythonServerService() {
return new ExternalPythonServerServiceImpl(mock(StatefulRestEasyClientsHolder.class));
public PythonRuntimeService externalPythonServerService() {
return new ExternalPythonServerServiceImpl(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 @@ -36,7 +36,6 @@
import io.cloudslang.runtime.impl.python.PythonRuntimeServiceImpl;
import io.cloudslang.runtime.impl.python.external.ExternalPythonExecutionEngine;
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;
Expand Down Expand Up @@ -1208,8 +1207,9 @@ public PythonExecutionEngine pythonExecutionEngine() {
}

@Bean(name = "externalPythonServerService")
public ExternalPythonServerService externalPythonServerService() {
return new ExternalPythonServerServiceImpl(mock(StatefulRestEasyClientsHolder.class));
public PythonRuntimeService externalPythonServerService() {
return new ExternalPythonServerServiceImpl(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 @@ -46,7 +46,6 @@
import io.cloudslang.runtime.impl.python.PythonRuntimeServiceImpl;
import io.cloudslang.runtime.impl.python.external.ExternalPythonExecutionEngine;
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;
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 @@ -512,8 +511,9 @@ public PythonExecutionEngine pythonExecutionEngine() {
}

@Bean(name = "externalPythonServerService")
public ExternalPythonServerService externalPythonServerService() {
return new ExternalPythonServerServiceImpl(mock(StatefulRestEasyClientsHolder.class));
public PythonRuntimeService externalPythonServerService() {
return new ExternalPythonServerServiceImpl(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.ExternalPythonServerServiceImpl;
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 = "externalPythonServerService")
public PythonRuntimeService externalPythonServerService() {
return new ExternalPythonServerServiceImpl(mock(StatefulRestEasyClientsHolder.class),
new Semaphore(100), new Semaphore(50));
}


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

@Bean
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,6 @@
import io.cloudslang.runtime.impl.python.PythonRuntimeServiceImpl;
import io.cloudslang.runtime.impl.python.external.ExternalPythonExecutionEngine;
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;
Expand Down Expand Up @@ -644,8 +643,9 @@ public PythonExecutionEngine pythonExecutionEngine() {
}

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

@Bean(name = "externalPythonRuntimeService")
Expand Down