2 * Copyright © 2019 IBM.
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
17 package org.onap.ccsdk.cds.blueprintsprocessor.functions.python.executor
19 import com.fasterxml.jackson.databind.JsonNode
20 import com.fasterxml.jackson.databind.ObjectMapper
23 import kotlinx.coroutines.runBlocking
25 import org.onap.ccsdk.cds.blueprintsprocessor.core.api.data.ExecutionServiceInput
26 import org.onap.ccsdk.cds.blueprintsprocessor.core.api.data.PrepareRemoteEnvInput
27 import org.onap.ccsdk.cds.blueprintsprocessor.core.api.data.RemoteScriptExecutionInput
28 import org.onap.ccsdk.cds.blueprintsprocessor.core.api.data.RemoteScriptExecutionOutput
29 import org.onap.ccsdk.cds.blueprintsprocessor.core.api.data.StatusType
30 import org.onap.ccsdk.cds.blueprintsprocessor.core.api.data.StepData
31 import org.onap.ccsdk.cds.blueprintsprocessor.services.execution.RemoteScriptExecutionService
32 import org.onap.ccsdk.cds.controllerblueprints.core.BluePrintConstants
33 import org.onap.ccsdk.cds.controllerblueprints.core.asJsonPrimitive
34 import org.onap.ccsdk.cds.controllerblueprints.core.asJsonType
35 import org.onap.ccsdk.cds.controllerblueprints.core.putJsonElement
36 import org.onap.ccsdk.cds.controllerblueprints.core.service.DefaultBluePrintRuntimeService
37 import org.onap.ccsdk.cds.controllerblueprints.core.utils.BluePrintMetadataUtils
38 import org.onap.ccsdk.cds.controllerblueprints.core.utils.JacksonUtils
39 import kotlin.test.assertEquals
40 import kotlin.test.assertNotNull
42 class ComponentRemotePythonExecutorTest {
45 fun testComponentRemotePythonExecutor() {
47 val remoteScriptExecutionService = MockRemoteScriptExecutionService()
49 val componentRemotePythonExecutor = ComponentRemotePythonExecutor(remoteScriptExecutionService)
51 val executionServiceInput =
52 JacksonUtils.readValueFromClassPathFile(
53 "payload/requests/sample-remote-python-request.json",
54 ExecutionServiceInput::class.java
57 val bluePrintRuntimeService = BluePrintMetadataUtils.getBluePrintRuntime(
59 "./../../../../components/model-catalog/blueprint-model/test-blueprint/remote_scripts"
62 /** Load Workflow Inputs */
63 bluePrintRuntimeService.assignWorkflowInputs(
64 "execute-remote-python",
65 executionServiceInput.payload.get("execute-remote-python-request")
68 val stepMetaData: MutableMap<String, JsonNode> = hashMapOf()
69 stepMetaData.putJsonElement(BluePrintConstants.PROPERTY_CURRENT_NODE_TEMPLATE, "execute-remote-python")
70 stepMetaData.putJsonElement(BluePrintConstants.PROPERTY_CURRENT_INTERFACE, "ComponentRemotePythonExecutor")
71 stepMetaData.putJsonElement(BluePrintConstants.PROPERTY_CURRENT_OPERATION, "process")
72 componentRemotePythonExecutor.bluePrintRuntimeService = bluePrintRuntimeService
73 val stepInputData = StepData().apply {
74 name = "execute-remote-python"
75 properties = stepMetaData
77 executionServiceInput.stepData = stepInputData
78 componentRemotePythonExecutor.applyNB(executionServiceInput)
83 * Test cases for python executor to work with the process NB of remote
87 fun testComponentRemotePythonExecutorProcessNB() {
89 val remoteScriptExecutionService = MockRemoteScriptExecutionService()
90 val componentRemotePythonExecutor = ComponentRemotePythonExecutor(remoteScriptExecutionService)
91 val bluePrintRuntime = mockk<DefaultBluePrintRuntimeService>("123456-1000")
93 every { bluePrintRuntime.setNodeTemplateAttributeValue(any(), any(), any()) } answers {}
95 val input = getMockedOutput(bluePrintRuntime)
96 componentRemotePythonExecutor.bluePrintRuntimeService = bluePrintRuntime
97 componentRemotePythonExecutor.applyNB(input)
102 * Mocked input information for remote python executor.
104 fun getMockedOutput(svc: DefaultBluePrintRuntimeService):
105 ExecutionServiceInput {
106 val stepMetaData: MutableMap<String, JsonNode> = hashMapOf()
108 stepMetaData.putJsonElement(
109 BluePrintConstants.PROPERTY_CURRENT_NODE_TEMPLATE,
110 "execute-remote-python"
112 stepMetaData.putJsonElement(
113 BluePrintConstants.PROPERTY_CURRENT_INTERFACE,
114 "ComponentRemotePythonExecutor"
116 stepMetaData.putJsonElement(
117 BluePrintConstants.PROPERTY_CURRENT_OPERATION, "process"
120 val mapper = ObjectMapper()
121 val rootNode = mapper.createObjectNode()
122 rootNode.put("ip-address", "0.0.0.0")
123 rootNode.put("type", "rest")
125 val operationalInputs: MutableMap<String, JsonNode> = hashMapOf()
126 operationalInputs.putJsonElement(
127 BluePrintConstants.PROPERTY_CURRENT_NODE_TEMPLATE,
128 "execute-remote-python"
130 operationalInputs.putJsonElement(
131 BluePrintConstants.PROPERTY_CURRENT_INTERFACE,
132 "ComponentRemotePythonExecutor"
134 operationalInputs.putJsonElement(
135 BluePrintConstants.PROPERTY_CURRENT_OPERATION, "process"
137 operationalInputs.putJsonElement("endpoint-selector", "aai")
138 operationalInputs.putJsonElement("dynamic-properties", rootNode)
139 operationalInputs.putJsonElement("command", "./run.sh")
140 operationalInputs.putJsonElement("packages", "py")
143 svc.resolveNodeTemplateInterfaceOperationInputs(
144 "execute-remote-python",
145 "ComponentRemotePythonExecutor", "process"
147 } returns operationalInputs
149 val stepInputData = StepData().apply {
150 name = "execute-remote-python"
151 properties = stepMetaData
154 val executionServiceInput = JacksonUtils
155 .readValueFromClassPathFile(
156 "payload/requests/sample-remote-python-request.json",
157 ExecutionServiceInput::class.java
159 executionServiceInput.stepData = stepInputData
161 val operationOutputs = hashMapOf<String, JsonNode>()
163 svc.resolveNodeTemplateInterfaceOperationOutputs(
164 "execute-remote-python",
165 "ComponentRemotePythonExecutor", "process"
167 } returns operationOutputs
168 val bluePrintRuntimeService = BluePrintMetadataUtils
169 .getBluePrintRuntime(
171 "./../../../../components/model-" +
172 "catalog/blueprint-model/test-blueprint/" +
176 svc.resolveNodeTemplateArtifactDefinition(
177 "execute-remote-python", "component-script"
179 } returns bluePrintRuntimeService.resolveNodeTemplateArtifactDefinition(
180 "execute-remote-python", "component-script"
183 svc.setNodeTemplateAttributeValue(
184 "execute-remote-python", "prepare-environment-logs",
185 "prepared successfully".asJsonPrimitive()
189 svc.setNodeTemplateAttributeValue(
190 "execute-remote-python",
191 "execute-command-logs", "N/A".asJsonPrimitive()
195 svc.setNodeTemplateAttributeValue(
196 "execute-remote-python",
197 "execute-command-logs",
198 "processed successfully".asJsonPrimitive()
203 svc.resolveDSLExpression("aai")
204 } returns """{"url" : "http://xxx.com"}""".asJsonType()
207 svc.bluePrintContext()
208 } returns bluePrintRuntimeService.bluePrintContext()
209 return executionServiceInput
213 class MockRemoteScriptExecutionService : RemoteScriptExecutionService {
214 override suspend fun init(selector: Any) {
217 override suspend fun prepareEnv(prepareEnvInput: PrepareRemoteEnvInput): RemoteScriptExecutionOutput {
218 assertEquals(prepareEnvInput.requestId, "123456-1000", "failed to match request id")
219 assertNotNull(prepareEnvInput.packages, "failed to get packages")
221 val remoteScriptExecutionOutput = mockk<RemoteScriptExecutionOutput>()
222 every { remoteScriptExecutionOutput.payload } returns "payload".asJsonPrimitive()
223 every { remoteScriptExecutionOutput.response } returns listOf("prepared successfully")
224 every { remoteScriptExecutionOutput.status } returns StatusType.SUCCESS
225 return remoteScriptExecutionOutput
228 override suspend fun executeCommand(remoteExecutionInput: RemoteScriptExecutionInput): RemoteScriptExecutionOutput {
229 assertEquals(remoteExecutionInput.requestId, "123456-1000", "failed to match request id")
231 val remoteScriptExecutionOutput = mockk<RemoteScriptExecutionOutput>()
232 every { remoteScriptExecutionOutput.payload } returns "payload".asJsonPrimitive()
233 every { remoteScriptExecutionOutput.response } returns listOf("processed successfully")
234 every { remoteScriptExecutionOutput.status } returns StatusType.SUCCESS
235 return remoteScriptExecutionOutput
238 override suspend fun close() {