5e57b9eb705b6bc90eb7c4de1218462235ac8271
[ccsdk/cds.git] /
1 /*
2  *  Copyright © 2019 IBM.
3  *
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
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
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.
15  */
16
17 package org.onap.ccsdk.cds.blueprintsprocessor.functions.python.executor
18
19 import com.fasterxml.jackson.databind.JsonNode
20 import com.fasterxml.jackson.databind.ObjectMapper
21 import io.mockk.every
22 import io.mockk.mockk
23 import kotlinx.coroutines.runBlocking
24 import org.junit.Test
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
42
43 class ComponentRemotePythonExecutorTest {
44
45     @Test
46     fun testComponentRemotePythonExecutor() {
47         runBlocking {
48             val remoteScriptExecutionService = MockRemoteScriptExecutionService()
49
50             val componentRemotePythonExecutor = ComponentRemotePythonExecutor(remoteScriptExecutionService)
51
52             val executionServiceInput =
53                 JacksonUtils.readValueFromClassPathFile(
54                     "payload/requests/sample-remote-python-request.json",
55                     ExecutionServiceInput::class.java
56                 )!!
57
58             val bluePrintRuntimeService = BluePrintMetadataUtils.getBluePrintRuntime(
59                 "123456-1000",
60                 "./../../../../components/model-catalog/blueprint-model/test-blueprint/remote_scripts"
61             )
62
63             /** Load Workflow Inputs */
64             bluePrintRuntimeService.assignWorkflowInputs(
65                 "execute-remote-python",
66                 executionServiceInput.payload.get("execute-remote-python-request")
67             )
68
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
77             }
78             executionServiceInput.stepData = stepInputData
79             componentRemotePythonExecutor.applyNB(executionServiceInput)
80         }
81     }
82
83     /**
84      * Test cases for python executor to work with the process NB of remote
85      * executor.
86      */
87     @Test
88     fun testComponentRemotePythonExecutorProcessNB() {
89         runBlocking {
90             val remoteScriptExecutionService = MockRemoteScriptExecutionService()
91             val componentRemotePythonExecutor = ComponentRemotePythonExecutor(remoteScriptExecutionService)
92             val bluePrintRuntime = mockk<DefaultBluePrintRuntimeService>("123456-1000")
93
94             every { bluePrintRuntime.getBluePrintError() } answers { BluePrintError() } // successful case.
95             every { bluePrintRuntime.setNodeTemplateAttributeValue(any(), any(), any()) } answers {}
96
97             val input = getMockedOutput(bluePrintRuntime)
98             componentRemotePythonExecutor.bluePrintRuntimeService = bluePrintRuntime
99             componentRemotePythonExecutor.applyNB(input)
100         }
101     }
102
103     /**
104      * Mocked input information for remote python executor.
105      */
106     fun getMockedOutput(svc: DefaultBluePrintRuntimeService):
107         ExecutionServiceInput {
108         val stepMetaData: MutableMap<String, JsonNode> = hashMapOf()
109
110         stepMetaData.putJsonElement(
111             BluePrintConstants.PROPERTY_CURRENT_NODE_TEMPLATE,
112             "execute-remote-python"
113         )
114         stepMetaData.putJsonElement(
115             BluePrintConstants.PROPERTY_CURRENT_INTERFACE,
116             "ComponentRemotePythonExecutor"
117         )
118         stepMetaData.putJsonElement(
119             BluePrintConstants.PROPERTY_CURRENT_OPERATION, "process"
120         )
121
122         val mapper = ObjectMapper()
123         val rootNode = mapper.createObjectNode()
124         rootNode.put("ip-address", "0.0.0.0")
125         rootNode.put("type", "rest")
126
127         val operationalInputs: MutableMap<String, JsonNode> = hashMapOf()
128         operationalInputs.putJsonElement(
129             BluePrintConstants.PROPERTY_CURRENT_NODE_TEMPLATE,
130             "execute-remote-python"
131         )
132         operationalInputs.putJsonElement(
133             BluePrintConstants.PROPERTY_CURRENT_INTERFACE,
134             "ComponentRemotePythonExecutor"
135         )
136         operationalInputs.putJsonElement(
137             BluePrintConstants.PROPERTY_CURRENT_OPERATION, "process"
138         )
139         operationalInputs.putJsonElement("endpoint-selector", "aai")
140         operationalInputs.putJsonElement("dynamic-properties", rootNode)
141         operationalInputs.putJsonElement("command", "./run.sh")
142         operationalInputs.putJsonElement("packages", "py")
143
144         every {
145             svc.resolveNodeTemplateInterfaceOperationInputs(
146                 "execute-remote-python",
147                 "ComponentRemotePythonExecutor", "process"
148             )
149         } returns operationalInputs
150
151         val stepInputData = StepData().apply {
152             name = "execute-remote-python"
153             properties = stepMetaData
154         }
155
156         val executionServiceInput = JacksonUtils
157             .readValueFromClassPathFile(
158                 "payload/requests/sample-remote-python-request.json",
159                 ExecutionServiceInput::class.java
160             )!!
161         executionServiceInput.stepData = stepInputData
162
163         val operationOutputs = hashMapOf<String, JsonNode>()
164         every {
165             svc.resolveNodeTemplateInterfaceOperationOutputs(
166                 "execute-remote-python",
167                 "ComponentRemotePythonExecutor", "process"
168             )
169         } returns operationOutputs
170         val bluePrintRuntimeService = BluePrintMetadataUtils.bluePrintRuntime(
171             "123456-1000",
172             "./../../../../components/model-" +
173                 "catalog/blueprint-model/test-blueprint/" +
174                 "remote_scripts"
175         )
176         every {
177             svc.resolveNodeTemplateArtifactDefinition(
178                 "execute-remote-python", "component-script"
179             )
180         } returns bluePrintRuntimeService.resolveNodeTemplateArtifactDefinition(
181             "execute-remote-python", "component-script"
182         )
183         every {
184             svc.setNodeTemplateAttributeValue(
185                 "execute-remote-python", "prepare-environment-logs",
186                 "prepared successfully".asJsonPrimitive()
187             )
188         } returns Unit
189         every {
190             svc.setNodeTemplateAttributeValue(
191                 "execute-remote-python",
192                 "execute-command-logs", "N/A".asJsonPrimitive()
193             )
194         } returns Unit
195         every {
196             svc.setNodeTemplateAttributeValue(
197                 "execute-remote-python",
198                 "execute-command-logs",
199                 "processed successfully".asJsonPrimitive()
200             )
201         } returns Unit
202
203         every {
204             svc.resolveDSLExpression("aai")
205         } returns """{"url" : "http://xxx.com"}""".asJsonType()
206
207         every {
208             svc.bluePrintContext()
209         } returns bluePrintRuntimeService.bluePrintContext()
210         return executionServiceInput
211     }
212 }
213
214 class MockRemoteScriptExecutionService : RemoteScriptExecutionService {
215     override suspend fun init(selector: Any) {
216     }
217
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")
221
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
227     }
228
229     override suspend fun executeCommand(remoteExecutionInput: RemoteScriptExecutionInput): RemoteScriptExecutionOutput {
230         assertEquals(remoteExecutionInput.requestId, "123456-1000", "failed to match request id")
231
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
237     }
238
239     override suspend fun close() {
240     }
241 }