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.RemoteScriptUploadBlueprintInput
31 import org.onap.ccsdk.cds.blueprintsprocessor.core.api.data.RemoteScriptUploadBlueprintOutput
32 import org.onap.ccsdk.cds.blueprintsprocessor.core.api.data.StatusType
33 import org.onap.ccsdk.cds.blueprintsprocessor.core.api.data.StepData
34 import org.onap.ccsdk.cds.blueprintsprocessor.db.primary.repository.BlueprintModelRepository
35 import org.onap.ccsdk.cds.blueprintsprocessor.services.execution.RemoteScriptExecutionService
36 import org.onap.ccsdk.cds.controllerblueprints.core.BluePrintConstants
37 import org.onap.ccsdk.cds.controllerblueprints.core.BluePrintError
38 import org.onap.ccsdk.cds.controllerblueprints.core.asJsonPrimitive
39 import org.onap.ccsdk.cds.controllerblueprints.core.asJsonType
40 import org.onap.ccsdk.cds.controllerblueprints.core.putJsonElement
41 import org.onap.ccsdk.cds.controllerblueprints.core.service.DefaultBluePrintRuntimeService
42 import org.onap.ccsdk.cds.controllerblueprints.core.utils.BluePrintMetadataUtils
43 import org.onap.ccsdk.cds.controllerblueprints.core.utils.JacksonUtils
44 import kotlin.test.assertEquals
45 import kotlin.test.assertNotNull
47 class ComponentRemotePythonExecutorTest {
50 fun testComponentRemotePythonExecutor() {
52 val remoteScriptExecutionService = MockRemoteScriptExecutionService()
54 val componentRemotePythonExecutor = ComponentRemotePythonExecutor(
55 remoteScriptExecutionService,
56 mockk<BluePrintPropertiesService>(),
57 mockk<BlueprintModelRepository>()
60 val executionServiceInput =
61 JacksonUtils.readValueFromClassPathFile(
62 "payload/requests/sample-remote-python-request.json",
63 ExecutionServiceInput::class.java
66 val bluePrintRuntimeService = BluePrintMetadataUtils.getBluePrintRuntime(
68 "./../../../../components/model-catalog/blueprint-model/test-blueprint/remote_scripts"
71 /** Load Workflow Inputs */
72 bluePrintRuntimeService.assignWorkflowInputs(
73 "execute-remote-python",
74 executionServiceInput.payload.get("execute-remote-python-request")
77 val stepMetaData: MutableMap<String, JsonNode> = hashMapOf()
78 stepMetaData.putJsonElement(BluePrintConstants.PROPERTY_CURRENT_NODE_TEMPLATE, "execute-remote-python")
79 stepMetaData.putJsonElement(BluePrintConstants.PROPERTY_CURRENT_INTERFACE, "ComponentRemotePythonExecutor")
80 stepMetaData.putJsonElement(BluePrintConstants.PROPERTY_CURRENT_OPERATION, "process")
81 componentRemotePythonExecutor.bluePrintRuntimeService = bluePrintRuntimeService
82 val stepInputData = StepData().apply {
83 name = "execute-remote-python"
84 properties = stepMetaData
86 executionServiceInput.stepData = stepInputData
87 componentRemotePythonExecutor.applyNB(executionServiceInput)
92 * Test cases for python executor to work with the process NB of remote
96 fun testComponentRemotePythonExecutorProcessNB() {
98 val remoteScriptExecutionService = MockRemoteScriptExecutionService()
99 val componentRemotePythonExecutor = ComponentRemotePythonExecutor(
100 remoteScriptExecutionService,
101 mockk<BluePrintPropertiesService>(),
102 mockk<BlueprintModelRepository>()
104 val bluePrintRuntime = mockk<DefaultBluePrintRuntimeService>("123456-1000")
106 every { bluePrintRuntime.getBluePrintError() } answers { BluePrintError() } // successful case.
107 every { bluePrintRuntime.setNodeTemplateAttributeValue(any(), any(), any()) } answers {}
109 val input = getMockedOutput(bluePrintRuntime)
110 componentRemotePythonExecutor.bluePrintRuntimeService = bluePrintRuntime
111 componentRemotePythonExecutor.applyNB(input)
116 * Mocked input information for remote python executor.
118 fun getMockedOutput(svc: DefaultBluePrintRuntimeService):
119 ExecutionServiceInput {
120 val stepMetaData: MutableMap<String, JsonNode> = hashMapOf()
122 stepMetaData.putJsonElement(
123 BluePrintConstants.PROPERTY_CURRENT_NODE_TEMPLATE,
124 "execute-remote-python"
126 stepMetaData.putJsonElement(
127 BluePrintConstants.PROPERTY_CURRENT_INTERFACE,
128 "ComponentRemotePythonExecutor"
130 stepMetaData.putJsonElement(
131 BluePrintConstants.PROPERTY_CURRENT_OPERATION, "process"
134 val mapper = ObjectMapper()
135 val rootNode = mapper.createObjectNode()
136 rootNode.put("ip-address", "0.0.0.0")
137 rootNode.put("type", "rest")
139 val operationalInputs: MutableMap<String, JsonNode> = hashMapOf()
140 operationalInputs.putJsonElement(
141 BluePrintConstants.PROPERTY_CURRENT_NODE_TEMPLATE,
142 "execute-remote-python"
144 operationalInputs.putJsonElement(
145 BluePrintConstants.PROPERTY_CURRENT_INTERFACE,
146 "ComponentRemotePythonExecutor"
148 operationalInputs.putJsonElement(
149 BluePrintConstants.PROPERTY_CURRENT_OPERATION, "process"
151 operationalInputs.putJsonElement("endpoint-selector", "aai")
152 operationalInputs.putJsonElement("dynamic-properties", rootNode)
153 operationalInputs.putJsonElement("command", "./run.sh")
154 operationalInputs.putJsonElement("packages", "py")
157 svc.resolveNodeTemplateInterfaceOperationInputs(
158 "execute-remote-python",
159 "ComponentRemotePythonExecutor", "process"
161 } returns operationalInputs
163 val stepInputData = StepData().apply {
164 name = "execute-remote-python"
165 properties = stepMetaData
168 val executionServiceInput = JacksonUtils
169 .readValueFromClassPathFile(
170 "payload/requests/sample-remote-python-request.json",
171 ExecutionServiceInput::class.java
173 executionServiceInput.stepData = stepInputData
175 val operationOutputs = hashMapOf<String, JsonNode>()
177 svc.resolveNodeTemplateInterfaceOperationOutputs(
178 "execute-remote-python",
179 "ComponentRemotePythonExecutor", "process"
181 } returns operationOutputs
182 val bluePrintRuntimeService = BluePrintMetadataUtils.bluePrintRuntime(
184 "./../../../../components/model-" +
185 "catalog/blueprint-model/test-blueprint/" +
189 svc.resolveNodeTemplateArtifactDefinition(
190 "execute-remote-python", "component-script"
192 } returns bluePrintRuntimeService.resolveNodeTemplateArtifactDefinition(
193 "execute-remote-python", "component-script"
196 svc.setNodeTemplateAttributeValue(
197 "execute-remote-python", "prepare-environment-logs",
198 "prepared successfully".asJsonPrimitive()
202 svc.setNodeTemplateAttributeValue(
203 "execute-remote-python",
204 "execute-command-logs", "N/A".asJsonPrimitive()
208 svc.setNodeTemplateAttributeValue(
209 "execute-remote-python",
210 "execute-command-logs",
211 "processed successfully".asJsonPrimitive()
216 svc.resolveDSLExpression("aai")
217 } returns """{"url" : "http://xxx.com"}""".asJsonType()
220 svc.bluePrintContext()
221 } returns bluePrintRuntimeService.bluePrintContext()
222 return executionServiceInput
226 class MockRemoteScriptExecutionService : RemoteScriptExecutionService {
228 override suspend fun init(selector: Any) {
231 override suspend fun uploadBlueprint(uploadBpInput: RemoteScriptUploadBlueprintInput): RemoteScriptUploadBlueprintOutput {
232 val uploadBpOutput = mockk<RemoteScriptUploadBlueprintOutput>()
233 every { uploadBpOutput.payload } returns "[]".asJsonPrimitive()
234 every { uploadBpOutput.status } returns StatusType.SUCCESS
235 every { uploadBpOutput.requestId } returns "123456-1000"
236 every { uploadBpOutput.subRequestId } returns "1234"
237 return uploadBpOutput
240 override suspend fun prepareEnv(prepareEnvInput: PrepareRemoteEnvInput): RemoteScriptExecutionOutput {
241 assertEquals(prepareEnvInput.requestId, "123456-1000", "failed to match request id")
242 assertNotNull(prepareEnvInput.packages, "failed to get packages")
244 val remoteScriptExecutionOutput = mockk<RemoteScriptExecutionOutput>()
245 every { remoteScriptExecutionOutput.payload } returns "payload".asJsonPrimitive()
246 every { remoteScriptExecutionOutput.response } returns listOf("prepared successfully")
247 every { remoteScriptExecutionOutput.status } returns StatusType.SUCCESS
248 return remoteScriptExecutionOutput
251 override suspend fun executeCommand(remoteExecutionInput: RemoteScriptExecutionInput): RemoteScriptExecutionOutput {
252 assertEquals(remoteExecutionInput.requestId, "123456-1000", "failed to match request id")
254 val remoteScriptExecutionOutput = mockk<RemoteScriptExecutionOutput>()
255 every { remoteScriptExecutionOutput.payload } returns "payload".asJsonPrimitive()
256 every { remoteScriptExecutionOutput.response } returns listOf("processed successfully")
257 every { remoteScriptExecutionOutput.status } returns StatusType.SUCCESS
258 return remoteScriptExecutionOutput
261 override suspend fun close() {