Merge "Remote AWX ansible playbook executor"
[ccsdk/cds.git] / ms / blueprintsprocessor / functions / ansible-awx-executor / src / test / kotlin / org / onap / ccsdk / cds / blueprintsprocessor / functions / ansible / executor / ComponentRemoteAnsibleExecutorTest.kt
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.ansible.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.Ignore
25 import org.junit.Test
26 import org.junit.runner.RunWith
27 import org.onap.ccsdk.cds.blueprintsprocessor.core.BluePrintProperties
28 import org.onap.ccsdk.cds.blueprintsprocessor.core.BlueprintPropertyConfiguration
29 import org.onap.ccsdk.cds.blueprintsprocessor.core.api.data.ExecutionServiceInput
30 import org.onap.ccsdk.cds.blueprintsprocessor.core.api.data.StepData
31 import org.onap.ccsdk.cds.blueprintsprocessor.rest.BluePrintRestLibConfiguration
32 import org.onap.ccsdk.cds.blueprintsprocessor.rest.service.BluePrintRestLibPropertyService
33 import org.onap.ccsdk.cds.controllerblueprints.core.BluePrintConstants
34 import org.onap.ccsdk.cds.controllerblueprints.core.asJsonPrimitive
35 import org.onap.ccsdk.cds.controllerblueprints.core.putJsonElement
36 import org.onap.ccsdk.cds.controllerblueprints.core.service.DefaultBluePrintRuntimeService
37 import org.onap.ccsdk.cds.controllerblueprints.core.utils.BluePrintMetadataUtils
38 import org.onap.ccsdk.cds.controllerblueprints.core.utils.JacksonUtils
39 import org.slf4j.LoggerFactory
40 import org.springframework.beans.factory.annotation.Autowired
41 import org.springframework.boot.autoconfigure.EnableAutoConfiguration
42 import org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration
43 import org.springframework.boot.test.context.SpringBootTest
44 import org.springframework.test.context.ContextConfiguration
45 import org.springframework.test.context.TestPropertySource
46 import org.springframework.test.context.junit4.SpringRunner
47
48 @RunWith(SpringRunner::class)
49 @EnableAutoConfiguration(exclude = [DataSourceAutoConfiguration::class])
50 @SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.DEFINED_PORT)
51 @ContextConfiguration(classes = [BluePrintRestLibConfiguration::class,
52     BlueprintPropertyConfiguration::class,
53     BluePrintProperties::class,
54     BluePrintProperties::class])
55 @TestPropertySource(properties =
56 [
57     "server.port=8443",
58     "server.ssl.enabled=true",
59     "server.ssl.key-store=classpath:keystore.p12",
60     "server.ssl.key-store-password=changeit",
61     "server.ssl.keyStoreType=PKCS12",
62     "server.ssl.keyAlias=tomcat",
63     "blueprintsprocessor.restclient.awx.type=token-auth",
64     "blueprintsprocessor.restclient.awx.url=http://142.44.184.236",
65     "blueprintsprocessor.restclient.awx.token=Bearer J9gEtMDqf7P4YsJ74fioY9VAhLDIs1",
66     "blueprintsprocessor.restclient.future.keyStoreInstance=PKCS12",
67     "blueprintsprocessor.restclient.future.sslTrust=src/test/resources/keystore.p12",
68     "blueprintsprocessor.restclient.future.sslTrustPassword=changeit"
69 ])
70 class ComponentRemoteAnsibleExecutorTest {
71
72     @Autowired
73     lateinit var bluePrintRestLibPropertyService: BluePrintRestLibPropertyService
74
75     @Transient
76     private val log = LoggerFactory.getLogger(ComponentRemoteAnsibleExecutorTest::class.java)
77
78     @Test
79     @Ignore
80     fun testComponentRemoteAnsibleExecutor() {
81         runBlocking {
82
83             val awxRemoteExecutor = ComponentRemoteAnsibleExecutor(bluePrintRestLibPropertyService)
84
85             val executionServiceInput = JacksonUtils.readValueFromClassPathFile(
86                     "payload/requests/sample-remote-ansible-request.json",
87                     ExecutionServiceInput::class.java)!!
88
89             log.info("Request Inputs : " + executionServiceInput.payload)
90
91             val bluePrintRuntimeService = BluePrintMetadataUtils.getBluePrintRuntime("123456-1000",
92                     "./../../../../components/model-catalog/blueprint-model/test-blueprint/remote_ansible")
93             awxRemoteExecutor.bluePrintRuntimeService = bluePrintRuntimeService
94
95             val workflowName = executionServiceInput.actionIdentifiers.actionName
96
97             // Assign Workflow inputs
98             val input = executionServiceInput.payload.get("$workflowName-request")
99             bluePrintRuntimeService.assignWorkflowInputs(workflowName, input)
100
101             val stepMetaData: MutableMap<String, JsonNode> = hashMapOf()
102             stepMetaData.putJsonElement(BluePrintConstants.PROPERTY_CURRENT_NODE_TEMPLATE, "execute-remote-ansible")
103             stepMetaData.putJsonElement(BluePrintConstants.PROPERTY_CURRENT_INTERFACE, "ComponentRemoteAnsibleExecutor")
104             stepMetaData.putJsonElement(BluePrintConstants.PROPERTY_CURRENT_OPERATION, "process")
105
106             val stepInputData = StepData().apply {
107                 name = "execute-remote-ansible"
108                 properties = stepMetaData
109             }
110             executionServiceInput.stepData = stepInputData
111
112             awxRemoteExecutor.applyNB(executionServiceInput)
113         }
114     }
115
116     /**
117      * Test cases for Ansible executor to work with the process NB of remote
118      * executor.
119      */
120     @Test
121     @Ignore
122     fun testComponentRemoteAnsibleExecutorProcessNB() {
123         runBlocking {
124             //            val remoteScriptExecutionService = MockRemoteScriptExecutionService(bluePrintRestLibPropertyService)
125             val componentRemoteAnsibleExecutor = ComponentRemoteAnsibleExecutor(bluePrintRestLibPropertyService)
126             val bluePrintRuntime = mockk<DefaultBluePrintRuntimeService>("123456-1000")
127             val input = getMockedOutput(bluePrintRuntime)
128             componentRemoteAnsibleExecutor.bluePrintRuntimeService = bluePrintRuntime
129             componentRemoteAnsibleExecutor.applyNB(input)
130         }
131     }
132
133     /**
134      * Mocked input information for remote Ansible executor.
135      */
136     fun getMockedOutput(svc: DefaultBluePrintRuntimeService):
137             ExecutionServiceInput {
138         val stepMetaData: MutableMap<String, JsonNode> = hashMapOf()
139
140         stepMetaData.putJsonElement(BluePrintConstants.PROPERTY_CURRENT_NODE_TEMPLATE, "execute-remote-ansible")
141         stepMetaData.putJsonElement(BluePrintConstants.PROPERTY_CURRENT_INTERFACE, "ComponentRemoteAnsibleExecutor")
142         stepMetaData.putJsonElement(BluePrintConstants.PROPERTY_CURRENT_OPERATION, "process")
143
144         val mapper = ObjectMapper()
145         val rootNode = mapper.createObjectNode()
146         rootNode.put("ip-address", "0.0.0.0")
147         rootNode.put("type", "rest")
148
149         val operationalInputs: MutableMap<String, JsonNode> = hashMapOf()
150         operationalInputs.putJsonElement(BluePrintConstants.PROPERTY_CURRENT_NODE_TEMPLATE, "execute-remote-ansible")
151         operationalInputs.putJsonElement(BluePrintConstants.PROPERTY_CURRENT_INTERFACE, "ComponentRemoteAnsibleExecutor")
152         operationalInputs.putJsonElement(BluePrintConstants.PROPERTY_CURRENT_OPERATION, "process")
153         operationalInputs.putJsonElement("endpoint-selector", "aai")
154 //        operationalInputs.putJsonElement("dynamic-properties", rootNode)
155 //        operationalInputs.putJsonElement("command", "./run.sh")
156         operationalInputs.putJsonElement("job-template-name", "CDS_job_template2")
157
158         every {
159             svc.resolveNodeTemplateInterfaceOperationInputs(
160                     "execute-remote-ansible",
161                     "ComponentRemoteAnsibleExecutor", "process")
162         } returns operationalInputs
163
164         val stepInputData = StepData().apply {
165             name = "execute-remote-ansible"
166             properties = stepMetaData
167         }
168
169         val executionServiceInput = JacksonUtils
170                 .readValueFromClassPathFile(
171                         "payload/requests/sample-remote-ansible-request.json",
172                         ExecutionServiceInput::class.java)!!
173         executionServiceInput.stepData = stepInputData
174
175         val operationOutputs = hashMapOf<String, JsonNode>()
176         every {
177             svc.resolveNodeTemplateInterfaceOperationOutputs(
178                     "execute-remote-ansible",
179                     "ComponentRemoteAnsibleExecutor", "process")
180         } returns operationOutputs
181         val bluePrintRuntimeService = BluePrintMetadataUtils
182                 .getBluePrintRuntime("123456-1000",
183                         "./../../../../components/model-" +
184                                 "catalog/blueprint-model/test-blueprint/" +
185                                 "remote_ansible")
186 //        every {
187 //            svc.resolveNodeTemplateArtifactDefinition("execute-remote-ansible", "component-script")
188 //        } returns bluePrintRuntimeService.resolveNodeTemplateArtifactDefinition("execute-remote-ansible",
189 //                                                                                "component-script")
190         every {
191             svc.setNodeTemplateAttributeValue(
192                     "execute-remote-ansible",
193                     "execute-command-status",
194                     "successful".asJsonPrimitive())
195         } returns Unit
196
197         every {
198             svc.setNodeTemplateAttributeValue(
199                     "execute-remote-ansible",
200                     "execute-command-logs", "N/A".asJsonPrimitive())
201         } returns Unit
202
203         every {
204             svc.setNodeTemplateAttributeValue(
205                     "execute-remote-ansible",
206                     "execute-command-logs",
207                     "processed successfully".asJsonPrimitive())
208         } returns Unit
209
210         every {
211             svc.bluePrintContext()
212         } returns bluePrintRuntimeService.bluePrintContext()
213         return executionServiceInput
214     }
215 }
216
217 //class MockRemoteScriptExecutionService : RemoteScriptExecutionService {
218 //    override suspend fun init(selector: String) {
219 //    }
220 //
221 //    override suspend fun prepareEnv(prepareEnvInput: PrepareRemoteEnvInput): RemoteScriptExecutionOutput {
222 //        assertEquals(prepareEnvInput.requestId, "123456-1000", "failed to match request id")
223 //        assertNotNull(prepareEnvInput.packages, "failed to get packages")
224 //
225 //        val remoteScriptExecutionOutput = mockk<RemoteScriptExecutionOutput>()
226 //        every { remoteScriptExecutionOutput.response } returns "prepared successfully"
227 //        every { remoteScriptExecutionOutput.status } returns StatusType.SUCCESS
228 //        return remoteScriptExecutionOutput
229 //    }
230 //
231 //    override suspend fun executeCommand(remoteExecutionInput: RemoteScriptExecutionInput): RemoteScriptExecutionOutput {
232 //        assertEquals(remoteExecutionInput.requestId, "123456-1000", "failed to match request id")
233 //
234 //        val remoteScriptExecutionOutput = mockk<RemoteScriptExecutionOutput>()
235 //        every { remoteScriptExecutionOutput.response } returns "processed successfully"
236 //        every { remoteScriptExecutionOutput.status } returns StatusType.SUCCESS
237 //        return remoteScriptExecutionOutput
238 //    }
239 //
240 //    override suspend fun close() {
241 //
242 //    }
243 //}