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.BluePrintError
34 import org.onap.ccsdk.cds.controllerblueprints.core.asJsonPrimitive
35 import org.onap.ccsdk.cds.controllerblueprints.core.asJsonType
36 import org.onap.ccsdk.cds.controllerblueprints.core.putJsonElement
37 import org.onap.ccsdk.cds.controllerblueprints.core.service.DefaultBluePrintRuntimeService
38 import org.onap.ccsdk.cds.controllerblueprints.core.utils.BluePrintMetadataUtils
39 import org.onap.ccsdk.cds.controllerblueprints.core.utils.JacksonUtils
40 import kotlin.test.assertEquals
41 import kotlin.test.assertNotNull
43 class ComponentRemotePythonExecutorTest {
46 fun testComponentRemotePythonExecutor() {
48 val remoteScriptExecutionService = MockRemoteScriptExecutionService()
50 val componentRemotePythonExecutor = ComponentRemotePythonExecutor(remoteScriptExecutionService)
52 val executionServiceInput =
53 JacksonUtils.readValueFromClassPathFile(
54 "payload/requests/sample-remote-python-request.json",
55 ExecutionServiceInput::class.java
58 val bluePrintRuntimeService = BluePrintMetadataUtils.getBluePrintRuntime(
60 "./../../../../components/model-catalog/blueprint-model/test-blueprint/remote_scripts"
63 /** Load Workflow Inputs */
64 bluePrintRuntimeService.assignWorkflowInputs(
65 "execute-remote-python",
66 executionServiceInput.payload.get("execute-remote-python-request")
69 val stepMetaData: MutableMap<String, JsonNode> = hashMapOf()
70 stepMetaData.putJsonElement(BluePrintConstants.PROPERTY_CURRENT_NODE_TEMPLATE, "execute-remote-python")
71 stepMetaData.putJsonElement(BluePrintConstants.PROPERTY_CURRENT_INTERFACE, "ComponentRemotePythonExecutor")
72 stepMetaData.putJsonElement(BluePrintConstants.PROPERTY_CURRENT_OPERATION, "process")
73 componentRemotePythonExecutor.bluePrintRuntimeService = bluePrintRuntimeService
74 val stepInputData = StepData().apply {
75 name = "execute-remote-python"
76 properties = stepMetaData
78 executionServiceInput.stepData = stepInputData
79 componentRemotePythonExecutor.applyNB(executionServiceInput)
84 * Test cases for python executor to work with the process NB of remote
88 fun testComponentRemotePythonExecutorProcessNB() {
90 val remoteScriptExecutionService = MockRemoteScriptExecutionService()
91 val componentRemotePythonExecutor = ComponentRemotePythonExecutor(remoteScriptExecutionService)
92 val bluePrintRuntime = mockk<DefaultBluePrintRuntimeService>("123456-1000")
94 every { bluePrintRuntime.getBluePrintError() } answers { BluePrintError() } // successful case.
95 every { bluePrintRuntime.setNodeTemplateAttributeValue(any(), any(), any()) } answers {}
97 val input = getMockedOutput(bluePrintRuntime)
98 componentRemotePythonExecutor.bluePrintRuntimeService = bluePrintRuntime
99 componentRemotePythonExecutor.applyNB(input)
104 * Mocked input information for remote python executor.
106 fun getMockedOutput(svc: DefaultBluePrintRuntimeService):
107 ExecutionServiceInput {
108 val stepMetaData: MutableMap<String, JsonNode> = hashMapOf()
110 stepMetaData.putJsonElement(
111 BluePrintConstants.PROPERTY_CURRENT_NODE_TEMPLATE,
112 "execute-remote-python"
114 stepMetaData.putJsonElement(
115 BluePrintConstants.PROPERTY_CURRENT_INTERFACE,
116 "ComponentRemotePythonExecutor"
118 stepMetaData.putJsonElement(
119 BluePrintConstants.PROPERTY_CURRENT_OPERATION, "process"
122 val mapper = ObjectMapper()
123 val rootNode = mapper.createObjectNode()
124 rootNode.put("ip-address", "0.0.0.0")
125 rootNode.put("type", "rest")
127 val operationalInputs: MutableMap<String, JsonNode> = hashMapOf()
128 operationalInputs.putJsonElement(
129 BluePrintConstants.PROPERTY_CURRENT_NODE_TEMPLATE,
130 "execute-remote-python"
132 operationalInputs.putJsonElement(
133 BluePrintConstants.PROPERTY_CURRENT_INTERFACE,
134 "ComponentRemotePythonExecutor"
136 operationalInputs.putJsonElement(
137 BluePrintConstants.PROPERTY_CURRENT_OPERATION, "process"
139 operationalInputs.putJsonElement("endpoint-selector", "aai")
140 operationalInputs.putJsonElement("dynamic-properties", rootNode)
141 operationalInputs.putJsonElement("command", "./run.sh")
142 operationalInputs.putJsonElement("packages", "py")
145 svc.resolveNodeTemplateInterfaceOperationInputs(
146 "execute-remote-python",
147 "ComponentRemotePythonExecutor", "process"
149 } returns operationalInputs
151 val stepInputData = StepData().apply {
152 name = "execute-remote-python"
153 properties = stepMetaData
156 val executionServiceInput = JacksonUtils
157 .readValueFromClassPathFile(
158 "payload/requests/sample-remote-python-request.json",
159 ExecutionServiceInput::class.java
161 executionServiceInput.stepData = stepInputData
163 val operationOutputs = hashMapOf<String, JsonNode>()
165 svc.resolveNodeTemplateInterfaceOperationOutputs(
166 "execute-remote-python",
167 "ComponentRemotePythonExecutor", "process"
169 } returns operationOutputs
170 val bluePrintRuntimeService = BluePrintMetadataUtils.bluePrintRuntime(
172 "./../../../../components/model-" +
173 "catalog/blueprint-model/test-blueprint/" +
177 svc.resolveNodeTemplateArtifactDefinition(
178 "execute-remote-python", "component-script"
180 } returns bluePrintRuntimeService.resolveNodeTemplateArtifactDefinition(
181 "execute-remote-python", "component-script"
184 svc.setNodeTemplateAttributeValue(
185 "execute-remote-python", "prepare-environment-logs",
186 "prepared successfully".asJsonPrimitive()
190 svc.setNodeTemplateAttributeValue(
191 "execute-remote-python",
192 "execute-command-logs", "N/A".asJsonPrimitive()
196 svc.setNodeTemplateAttributeValue(
197 "execute-remote-python",
198 "execute-command-logs",
199 "processed successfully".asJsonPrimitive()
204 svc.resolveDSLExpression("aai")
205 } returns """{"url" : "http://xxx.com"}""".asJsonType()
208 svc.bluePrintContext()
209 } returns bluePrintRuntimeService.bluePrintContext()
210 return executionServiceInput
214 class MockRemoteScriptExecutionService : RemoteScriptExecutionService {
215 override suspend fun init(selector: Any) {
218 override suspend fun prepareEnv(prepareEnvInput: PrepareRemoteEnvInput): RemoteScriptExecutionOutput {
219 assertEquals(prepareEnvInput.requestId, "123456-1000", "failed to match request id")
220 assertNotNull(prepareEnvInput.packages, "failed to get packages")
222 val remoteScriptExecutionOutput = mockk<RemoteScriptExecutionOutput>()
223 every { remoteScriptExecutionOutput.payload } returns "payload".asJsonPrimitive()
224 every { remoteScriptExecutionOutput.response } returns listOf("prepared successfully")
225 every { remoteScriptExecutionOutput.status } returns StatusType.SUCCESS
226 return remoteScriptExecutionOutput
229 override suspend fun executeCommand(remoteExecutionInput: RemoteScriptExecutionInput): RemoteScriptExecutionOutput {
230 assertEquals(remoteExecutionInput.requestId, "123456-1000", "failed to match request id")
232 val remoteScriptExecutionOutput = mockk<RemoteScriptExecutionOutput>()
233 every { remoteScriptExecutionOutput.payload } returns "payload".asJsonPrimitive()
234 every { remoteScriptExecutionOutput.response } returns listOf("processed successfully")
235 every { remoteScriptExecutionOutput.status } returns StatusType.SUCCESS
236 return remoteScriptExecutionOutput
239 override suspend fun close() {