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.putJsonElement
35 import org.onap.ccsdk.cds.controllerblueprints.core.service.DefaultBluePrintRuntimeService
36 import org.onap.ccsdk.cds.controllerblueprints.core.utils.BluePrintMetadataUtils
37 import org.onap.ccsdk.cds.controllerblueprints.core.utils.JacksonUtils
38 import kotlin.test.assertEquals
39 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("payload/requests/sample-activate-request.json",
53 ExecutionServiceInput::class.java)!!
55 val bluePrintRuntimeService = BluePrintMetadataUtils.getBluePrintRuntime("123456-1000",
56 "./../../../../components/model-catalog/blueprint-model/test-blueprint/remote_scripts")
58 val stepMetaData: MutableMap<String, JsonNode> = hashMapOf()
59 stepMetaData.putJsonElement(BluePrintConstants.PROPERTY_CURRENT_NODE_TEMPLATE, "execute-remote-python")
60 stepMetaData.putJsonElement(BluePrintConstants.PROPERTY_CURRENT_INTERFACE, "ComponentRemotePythonExecutor")
61 stepMetaData.putJsonElement(BluePrintConstants.PROPERTY_CURRENT_OPERATION, "process")
62 componentRemotePythonExecutor.bluePrintRuntimeService = bluePrintRuntimeService
63 val stepInputData = StepData().apply {
64 name = "execute-remote-python"
65 properties = stepMetaData
67 executionServiceInput.stepData = stepInputData
68 componentRemotePythonExecutor.applyNB(executionServiceInput)
73 * Test cases for python executor to work with the process NB of remote
77 fun testComponentRemotePythonExecutorProcessNB() {
79 val remoteScriptExecutionService =
80 MockRemoteScriptExecutionService()
81 val componentRemotePythonExecutor = ComponentRemotePythonExecutor(
82 remoteScriptExecutionService)
83 val bluePrintRuntime = mockk<DefaultBluePrintRuntimeService>(
86 every { bluePrintRuntime.setNodeTemplateAttributeValue(any(), any(), any()) } answers {}
88 val input = getMockedOutput(bluePrintRuntime)
89 componentRemotePythonExecutor.bluePrintRuntimeService =
91 componentRemotePythonExecutor.applyNB(input)
96 * Mocked input information for remote python executor.
98 fun getMockedOutput(svc: DefaultBluePrintRuntimeService):
99 ExecutionServiceInput {
100 val stepMetaData: MutableMap<String, JsonNode> = hashMapOf()
102 stepMetaData.putJsonElement(
103 BluePrintConstants.PROPERTY_CURRENT_NODE_TEMPLATE,
104 "execute-remote-python")
105 stepMetaData.putJsonElement(
106 BluePrintConstants.PROPERTY_CURRENT_INTERFACE,
107 "ComponentRemotePythonExecutor")
108 stepMetaData.putJsonElement(
109 BluePrintConstants.PROPERTY_CURRENT_OPERATION, "process")
111 val mapper = ObjectMapper()
112 val rootNode = mapper.createObjectNode()
113 rootNode.put("ip-address", "0.0.0.0")
114 rootNode.put("type", "rest")
116 val operationalInputs: MutableMap<String, JsonNode> = hashMapOf()
117 operationalInputs.putJsonElement(
118 BluePrintConstants.PROPERTY_CURRENT_NODE_TEMPLATE,
119 "execute-remote-python")
120 operationalInputs.putJsonElement(
121 BluePrintConstants.PROPERTY_CURRENT_INTERFACE,
122 "ComponentRemotePythonExecutor")
123 operationalInputs.putJsonElement(
124 BluePrintConstants.PROPERTY_CURRENT_OPERATION, "process")
125 operationalInputs.putJsonElement("endpoint-selector", "aai")
126 operationalInputs.putJsonElement("dynamic-properties", rootNode)
127 operationalInputs.putJsonElement("command", "./run.sh")
128 operationalInputs.putJsonElement("packages", "py")
131 svc.resolveNodeTemplateInterfaceOperationInputs(
132 "execute-remote-python",
133 "ComponentRemotePythonExecutor", "process")
134 } returns operationalInputs
136 val stepInputData = StepData().apply {
137 name = "execute-remote-python"
138 properties = stepMetaData
141 val executionServiceInput = JacksonUtils
142 .readValueFromClassPathFile(
143 "payload/requests/sample-remote-python-request.json",
144 ExecutionServiceInput::class.java)!!
145 executionServiceInput.stepData = stepInputData
147 val operationOutputs = hashMapOf<String, JsonNode>()
149 svc.resolveNodeTemplateInterfaceOperationOutputs(
150 "execute-remote-python",
151 "ComponentRemotePythonExecutor", "process")
152 } returns operationOutputs
153 val bluePrintRuntimeService = BluePrintMetadataUtils
154 .getBluePrintRuntime("123456-1000",
155 "./../../../../components/model-" +
156 "catalog/blueprint-model/test-blueprint/" +
159 svc.resolveNodeTemplateArtifactDefinition(
160 "execute-remote-python", "component-script")
161 } returns bluePrintRuntimeService.resolveNodeTemplateArtifactDefinition(
162 "execute-remote-python", "component-script")
164 svc.setNodeTemplateAttributeValue(
165 "execute-remote-python", "prepare-environment-logs",
166 "prepared successfully".asJsonPrimitive())
169 svc.setNodeTemplateAttributeValue(
170 "execute-remote-python",
171 "execute-command-logs", "N/A".asJsonPrimitive())
174 svc.setNodeTemplateAttributeValue(
175 "execute-remote-python",
176 "execute-command-logs",
177 "processed successfully".asJsonPrimitive())
181 svc.bluePrintContext()
182 } returns bluePrintRuntimeService.bluePrintContext()
183 return executionServiceInput
187 class MockRemoteScriptExecutionService : RemoteScriptExecutionService {
188 override suspend fun init(selector: String) {
191 override suspend fun prepareEnv(prepareEnvInput: PrepareRemoteEnvInput): RemoteScriptExecutionOutput {
192 assertEquals(prepareEnvInput.requestId, "123456-1000", "failed to match request id")
193 assertNotNull(prepareEnvInput.packages, "failed to get packages")
195 val remoteScriptExecutionOutput = mockk<RemoteScriptExecutionOutput>()
196 every { remoteScriptExecutionOutput.response } returns listOf("prepared successfully")
197 every { remoteScriptExecutionOutput.status } returns StatusType.SUCCESS
198 return remoteScriptExecutionOutput
201 override suspend fun executeCommand(remoteExecutionInput: RemoteScriptExecutionInput): RemoteScriptExecutionOutput {
202 assertEquals(remoteExecutionInput.requestId, "123456-1000", "failed to match request id")
204 val remoteScriptExecutionOutput = mockk<RemoteScriptExecutionOutput>()
205 every { remoteScriptExecutionOutput.response } returns listOf("processed successfully")
206 every { remoteScriptExecutionOutput.status } returns StatusType.SUCCESS
207 return remoteScriptExecutionOutput
210 override suspend fun close() {