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.ansible.executor
19 import com.fasterxml.jackson.databind.JsonNode
20 import com.fasterxml.jackson.databind.ObjectMapper
23 import kotlinx.coroutines.runBlocking
24 import org.junit.Ignore
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
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 =
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"
70 class ComponentRemoteAnsibleExecutorTest {
73 lateinit var bluePrintRestLibPropertyService: BluePrintRestLibPropertyService
76 private val log = LoggerFactory.getLogger(ComponentRemoteAnsibleExecutorTest::class.java)
80 fun testComponentRemoteAnsibleExecutor() {
83 val awxRemoteExecutor = ComponentRemoteAnsibleExecutor(bluePrintRestLibPropertyService)
85 val executionServiceInput = JacksonUtils.readValueFromClassPathFile(
86 "payload/requests/sample-remote-ansible-request.json",
87 ExecutionServiceInput::class.java)!!
89 log.info("Request Inputs : " + executionServiceInput.payload)
91 val bluePrintRuntimeService = BluePrintMetadataUtils.getBluePrintRuntime("123456-1000",
92 "./../../../../components/model-catalog/blueprint-model/test-blueprint/remote_ansible")
93 awxRemoteExecutor.bluePrintRuntimeService = bluePrintRuntimeService
95 val workflowName = executionServiceInput.actionIdentifiers.actionName
97 // Assign Workflow inputs
98 val input = executionServiceInput.payload.get("$workflowName-request")
99 bluePrintRuntimeService.assignWorkflowInputs(workflowName, input)
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")
106 val stepInputData = StepData().apply {
107 name = "execute-remote-ansible"
108 properties = stepMetaData
110 executionServiceInput.stepData = stepInputData
112 awxRemoteExecutor.applyNB(executionServiceInput)
117 * Test cases for Ansible executor to work with the process NB of remote
122 fun testComponentRemoteAnsibleExecutorProcessNB() {
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)
134 * Mocked input information for remote Ansible executor.
136 fun getMockedOutput(svc: DefaultBluePrintRuntimeService):
137 ExecutionServiceInput {
138 val stepMetaData: MutableMap<String, JsonNode> = hashMapOf()
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")
144 val mapper = ObjectMapper()
145 val rootNode = mapper.createObjectNode()
146 rootNode.put("ip-address", "0.0.0.0")
147 rootNode.put("type", "rest")
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")
159 svc.resolveNodeTemplateInterfaceOperationInputs(
160 "execute-remote-ansible",
161 "ComponentRemoteAnsibleExecutor", "process")
162 } returns operationalInputs
164 val stepInputData = StepData().apply {
165 name = "execute-remote-ansible"
166 properties = stepMetaData
169 val executionServiceInput = JacksonUtils
170 .readValueFromClassPathFile(
171 "payload/requests/sample-remote-ansible-request.json",
172 ExecutionServiceInput::class.java)!!
173 executionServiceInput.stepData = stepInputData
175 val operationOutputs = hashMapOf<String, JsonNode>()
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/" +
187 // svc.resolveNodeTemplateArtifactDefinition("execute-remote-ansible", "component-script")
188 // } returns bluePrintRuntimeService.resolveNodeTemplateArtifactDefinition("execute-remote-ansible",
189 // "component-script")
191 svc.setNodeTemplateAttributeValue(
192 "execute-remote-ansible",
193 "execute-command-status",
194 "successful".asJsonPrimitive())
198 svc.setNodeTemplateAttributeValue(
199 "execute-remote-ansible",
200 "execute-command-logs", "N/A".asJsonPrimitive())
204 svc.setNodeTemplateAttributeValue(
205 "execute-remote-ansible",
206 "execute-command-logs",
207 "processed successfully".asJsonPrimitive())
211 svc.bluePrintContext()
212 } returns bluePrintRuntimeService.bluePrintContext()
213 return executionServiceInput
217 //class MockRemoteScriptExecutionService : RemoteScriptExecutionService {
218 // override suspend fun init(selector: String) {
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")
225 // val remoteScriptExecutionOutput = mockk<RemoteScriptExecutionOutput>()
226 // every { remoteScriptExecutionOutput.response } returns "prepared successfully"
227 // every { remoteScriptExecutionOutput.status } returns StatusType.SUCCESS
228 // return remoteScriptExecutionOutput
231 // override suspend fun executeCommand(remoteExecutionInput: RemoteScriptExecutionInput): RemoteScriptExecutionOutput {
232 // assertEquals(remoteExecutionInput.requestId, "123456-1000", "failed to match request id")
234 // val remoteScriptExecutionOutput = mockk<RemoteScriptExecutionOutput>()
235 // every { remoteScriptExecutionOutput.response } returns "processed successfully"
236 // every { remoteScriptExecutionOutput.status } returns StatusType.SUCCESS
237 // return remoteScriptExecutionOutput
240 // override suspend fun close() {