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.BlueprintPropertiesService
26 import org.onap.ccsdk.cds.blueprintsprocessor.core.api.data.ExecutionServiceInput
27 import org.onap.ccsdk.cds.blueprintsprocessor.core.api.data.PrepareRemoteEnvInput
28 import org.onap.ccsdk.cds.blueprintsprocessor.core.api.data.RemoteScriptExecutionInput
29 import org.onap.ccsdk.cds.blueprintsprocessor.core.api.data.RemoteScriptExecutionOutput
30 import org.onap.ccsdk.cds.blueprintsprocessor.core.api.data.StatusType
31 import org.onap.ccsdk.cds.blueprintsprocessor.core.api.data.StepData
32 import org.onap.ccsdk.cds.blueprintsprocessor.services.execution.RemoteScriptExecutionService
33 import org.onap.ccsdk.cds.controllerblueprints.core.BlueprintConstants
34 import org.onap.ccsdk.cds.controllerblueprints.core.BlueprintError
35 import org.onap.ccsdk.cds.controllerblueprints.core.asJsonPrimitive
36 import org.onap.ccsdk.cds.controllerblueprints.core.asJsonType
37 import org.onap.ccsdk.cds.controllerblueprints.core.putJsonElement
38 import org.onap.ccsdk.cds.controllerblueprints.core.service.DefaultBlueprintRuntimeService
39 import org.onap.ccsdk.cds.controllerblueprints.core.utils.BlueprintMetadataUtils
40 import org.onap.ccsdk.cds.controllerblueprints.core.utils.JacksonUtils
41 import kotlin.test.assertEquals
42 import kotlin.test.assertNotNull
44 class ComponentRemotePythonExecutorTest {
47 fun testComponentRemotePythonExecutor() {
49 val remoteScriptExecutionService = MockRemoteScriptExecutionService()
51 val componentRemotePythonExecutor = ComponentRemotePythonExecutor(
52 remoteScriptExecutionService,
53 mockk<BlueprintPropertiesService>()
56 val executionServiceInput =
57 JacksonUtils.readValueFromClassPathFile(
58 "payload/requests/sample-remote-python-request.json",
59 ExecutionServiceInput::class.java
62 val bluePrintRuntimeService = BlueprintMetadataUtils.getBlueprintRuntime(
64 "./../../../../components/model-catalog/blueprint-model/test-blueprint/remote_scripts"
67 /** Load Workflow Inputs */
68 bluePrintRuntimeService.assignWorkflowInputs(
69 "execute-remote-python",
70 executionServiceInput.payload.get("execute-remote-python-request")
73 val stepMetaData: MutableMap<String, JsonNode> = hashMapOf()
74 stepMetaData.putJsonElement(BlueprintConstants.PROPERTY_CURRENT_NODE_TEMPLATE, "execute-remote-python")
75 stepMetaData.putJsonElement(BlueprintConstants.PROPERTY_CURRENT_INTERFACE, "ComponentRemotePythonExecutor")
76 stepMetaData.putJsonElement(BlueprintConstants.PROPERTY_CURRENT_OPERATION, "process")
77 componentRemotePythonExecutor.bluePrintRuntimeService = bluePrintRuntimeService
78 val stepInputData = StepData().apply {
79 name = "execute-remote-python"
80 properties = stepMetaData
82 executionServiceInput.stepData = stepInputData
83 componentRemotePythonExecutor.applyNB(executionServiceInput)
88 * Test cases for python executor to work with the process NB of remote
92 fun testComponentRemotePythonExecutorProcessNB() {
94 val remoteScriptExecutionService = MockRemoteScriptExecutionService()
95 val componentRemotePythonExecutor = ComponentRemotePythonExecutor(
96 remoteScriptExecutionService,
97 mockk<BlueprintPropertiesService>()
99 val bluePrintRuntime = mockk<DefaultBlueprintRuntimeService>("123456-1000")
101 every { bluePrintRuntime.getBlueprintError() } answers { BlueprintError() } // successful case.
102 every { bluePrintRuntime.setNodeTemplateAttributeValue(any(), any(), any()) } answers {}
104 val input = getMockedOutput(bluePrintRuntime)
105 componentRemotePythonExecutor.bluePrintRuntimeService = bluePrintRuntime
106 componentRemotePythonExecutor.applyNB(input)
111 * Mocked input information for remote python executor.
113 fun getMockedOutput(svc: DefaultBlueprintRuntimeService):
114 ExecutionServiceInput {
115 val stepMetaData: MutableMap<String, JsonNode> = hashMapOf()
117 stepMetaData.putJsonElement(
118 BlueprintConstants.PROPERTY_CURRENT_NODE_TEMPLATE,
119 "execute-remote-python"
121 stepMetaData.putJsonElement(
122 BlueprintConstants.PROPERTY_CURRENT_INTERFACE,
123 "ComponentRemotePythonExecutor"
125 stepMetaData.putJsonElement(
126 BlueprintConstants.PROPERTY_CURRENT_OPERATION, "process"
129 val mapper = ObjectMapper()
130 val rootNode = mapper.createObjectNode()
131 rootNode.put("ip-address", "0.0.0.0")
132 rootNode.put("type", "rest")
134 val operationalInputs: MutableMap<String, JsonNode> = hashMapOf()
135 operationalInputs.putJsonElement(
136 BlueprintConstants.PROPERTY_CURRENT_NODE_TEMPLATE,
137 "execute-remote-python"
139 operationalInputs.putJsonElement(
140 BlueprintConstants.PROPERTY_CURRENT_INTERFACE,
141 "ComponentRemotePythonExecutor"
143 operationalInputs.putJsonElement(
144 BlueprintConstants.PROPERTY_CURRENT_OPERATION, "process"
146 operationalInputs.putJsonElement("endpoint-selector", "aai")
147 operationalInputs.putJsonElement("dynamic-properties", rootNode)
148 operationalInputs.putJsonElement("command", "./run.sh")
149 operationalInputs.putJsonElement("packages", "py")
152 svc.resolveNodeTemplateInterfaceOperationInputs(
153 "execute-remote-python",
154 "ComponentRemotePythonExecutor", "process"
156 } returns operationalInputs
158 val stepInputData = StepData().apply {
159 name = "execute-remote-python"
160 properties = stepMetaData
163 val executionServiceInput = JacksonUtils
164 .readValueFromClassPathFile(
165 "payload/requests/sample-remote-python-request.json",
166 ExecutionServiceInput::class.java
168 executionServiceInput.stepData = stepInputData
170 val operationOutputs = hashMapOf<String, JsonNode>()
172 svc.resolveNodeTemplateInterfaceOperationOutputs(
173 "execute-remote-python",
174 "ComponentRemotePythonExecutor", "process"
176 } returns operationOutputs
177 val bluePrintRuntimeService = BlueprintMetadataUtils.bluePrintRuntime(
179 "./../../../../components/model-" +
180 "catalog/blueprint-model/test-blueprint/" +
184 svc.resolveNodeTemplateArtifactDefinition(
185 "execute-remote-python", "component-script"
187 } returns bluePrintRuntimeService.resolveNodeTemplateArtifactDefinition(
188 "execute-remote-python", "component-script"
191 svc.setNodeTemplateAttributeValue(
192 "execute-remote-python", "prepare-environment-logs",
193 "prepared successfully".asJsonPrimitive()
197 svc.setNodeTemplateAttributeValue(
198 "execute-remote-python",
199 "execute-command-logs", "N/A".asJsonPrimitive()
203 svc.setNodeTemplateAttributeValue(
204 "execute-remote-python",
205 "execute-command-logs",
206 "processed successfully".asJsonPrimitive()
211 svc.resolveDSLExpression("aai")
212 } returns """{"url" : "http://xxx.com"}""".asJsonType()
215 svc.bluePrintContext()
216 } returns bluePrintRuntimeService.bluePrintContext()
217 return executionServiceInput
221 class MockRemoteScriptExecutionService : RemoteScriptExecutionService {
223 override suspend fun init(selector: Any) {
226 override suspend fun prepareEnv(prepareEnvInput: PrepareRemoteEnvInput): RemoteScriptExecutionOutput {
227 assertEquals(prepareEnvInput.requestId, "123456-1000", "failed to match request id")
228 assertNotNull(prepareEnvInput.packages, "failed to get packages")
230 val remoteScriptExecutionOutput = mockk<RemoteScriptExecutionOutput>()
231 every { remoteScriptExecutionOutput.payload } returns "payload".asJsonPrimitive()
232 every { remoteScriptExecutionOutput.response } returns listOf("prepared successfully")
233 every { remoteScriptExecutionOutput.status } returns StatusType.SUCCESS
234 return remoteScriptExecutionOutput
237 override suspend fun executeCommand(remoteExecutionInput: RemoteScriptExecutionInput): RemoteScriptExecutionOutput {
238 assertEquals(remoteExecutionInput.requestId, "123456-1000", "failed to match request id")
240 val remoteScriptExecutionOutput = mockk<RemoteScriptExecutionOutput>()
241 every { remoteScriptExecutionOutput.payload } returns "payload".asJsonPrimitive()
242 every { remoteScriptExecutionOutput.response } returns listOf("processed successfully")
243 every { remoteScriptExecutionOutput.status } returns StatusType.SUCCESS
244 return remoteScriptExecutionOutput
247 override suspend fun close() {