diff options
author | Dan Timoney <dtimoney@att.com> | 2019-05-30 18:32:14 +0000 |
---|---|---|
committer | Gerrit Code Review <gerrit@onap.org> | 2019-05-30 18:32:14 +0000 |
commit | 32d132a78288521798d302cafbd9a7eac9121cae (patch) | |
tree | 838588d4a6cb8441cc9e20c92b93ddc2ab5ac059 /ms/blueprintsprocessor/functions/ansible-awx-executor/src/test | |
parent | b86fe0e27bc66ecae386007c1d751f82cbf920cd (diff) | |
parent | a3d9ac859fa47f2aa2849160e08c7a3c1048589e (diff) |
Merge "Remote AWX ansible playbook executor"
Diffstat (limited to 'ms/blueprintsprocessor/functions/ansible-awx-executor/src/test')
4 files changed, 307 insertions, 0 deletions
diff --git a/ms/blueprintsprocessor/functions/ansible-awx-executor/src/test/kotlin/org/onap/ccsdk/cds/blueprintsprocessor/functions/ansible/executor/ComponentRemoteAnsibleExecutorTest.kt b/ms/blueprintsprocessor/functions/ansible-awx-executor/src/test/kotlin/org/onap/ccsdk/cds/blueprintsprocessor/functions/ansible/executor/ComponentRemoteAnsibleExecutorTest.kt new file mode 100644 index 000000000..b7f1ed5f9 --- /dev/null +++ b/ms/blueprintsprocessor/functions/ansible-awx-executor/src/test/kotlin/org/onap/ccsdk/cds/blueprintsprocessor/functions/ansible/executor/ComponentRemoteAnsibleExecutorTest.kt @@ -0,0 +1,243 @@ +/* + * Copyright © 2019 IBM. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.onap.ccsdk.cds.blueprintsprocessor.functions.ansible.executor + +import com.fasterxml.jackson.databind.JsonNode +import com.fasterxml.jackson.databind.ObjectMapper +import io.mockk.every +import io.mockk.mockk +import kotlinx.coroutines.runBlocking +import org.junit.Ignore +import org.junit.Test +import org.junit.runner.RunWith +import org.onap.ccsdk.cds.blueprintsprocessor.core.BluePrintProperties +import org.onap.ccsdk.cds.blueprintsprocessor.core.BlueprintPropertyConfiguration +import org.onap.ccsdk.cds.blueprintsprocessor.core.api.data.ExecutionServiceInput +import org.onap.ccsdk.cds.blueprintsprocessor.core.api.data.StepData +import org.onap.ccsdk.cds.blueprintsprocessor.rest.BluePrintRestLibConfiguration +import org.onap.ccsdk.cds.blueprintsprocessor.rest.service.BluePrintRestLibPropertyService +import org.onap.ccsdk.cds.controllerblueprints.core.BluePrintConstants +import org.onap.ccsdk.cds.controllerblueprints.core.asJsonPrimitive +import org.onap.ccsdk.cds.controllerblueprints.core.putJsonElement +import org.onap.ccsdk.cds.controllerblueprints.core.service.DefaultBluePrintRuntimeService +import org.onap.ccsdk.cds.controllerblueprints.core.utils.BluePrintMetadataUtils +import org.onap.ccsdk.cds.controllerblueprints.core.utils.JacksonUtils +import org.slf4j.LoggerFactory +import org.springframework.beans.factory.annotation.Autowired +import org.springframework.boot.autoconfigure.EnableAutoConfiguration +import org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration +import org.springframework.boot.test.context.SpringBootTest +import org.springframework.test.context.ContextConfiguration +import org.springframework.test.context.TestPropertySource +import org.springframework.test.context.junit4.SpringRunner + +@RunWith(SpringRunner::class) +@EnableAutoConfiguration(exclude = [DataSourceAutoConfiguration::class]) +@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.DEFINED_PORT) +@ContextConfiguration(classes = [BluePrintRestLibConfiguration::class, + BlueprintPropertyConfiguration::class, + BluePrintProperties::class, + BluePrintProperties::class]) +@TestPropertySource(properties = +[ + "server.port=8443", + "server.ssl.enabled=true", + "server.ssl.key-store=classpath:keystore.p12", + "server.ssl.key-store-password=changeit", + "server.ssl.keyStoreType=PKCS12", + "server.ssl.keyAlias=tomcat", + "blueprintsprocessor.restclient.awx.type=token-auth", + "blueprintsprocessor.restclient.awx.url=http://142.44.184.236", + "blueprintsprocessor.restclient.awx.token=Bearer J9gEtMDqf7P4YsJ74fioY9VAhLDIs1", + "blueprintsprocessor.restclient.future.keyStoreInstance=PKCS12", + "blueprintsprocessor.restclient.future.sslTrust=src/test/resources/keystore.p12", + "blueprintsprocessor.restclient.future.sslTrustPassword=changeit" +]) +class ComponentRemoteAnsibleExecutorTest { + + @Autowired + lateinit var bluePrintRestLibPropertyService: BluePrintRestLibPropertyService + + @Transient + private val log = LoggerFactory.getLogger(ComponentRemoteAnsibleExecutorTest::class.java) + + @Test + @Ignore + fun testComponentRemoteAnsibleExecutor() { + runBlocking { + + val awxRemoteExecutor = ComponentRemoteAnsibleExecutor(bluePrintRestLibPropertyService) + + val executionServiceInput = JacksonUtils.readValueFromClassPathFile( + "payload/requests/sample-remote-ansible-request.json", + ExecutionServiceInput::class.java)!! + + log.info("Request Inputs : " + executionServiceInput.payload) + + val bluePrintRuntimeService = BluePrintMetadataUtils.getBluePrintRuntime("123456-1000", + "./../../../../components/model-catalog/blueprint-model/test-blueprint/remote_ansible") + awxRemoteExecutor.bluePrintRuntimeService = bluePrintRuntimeService + + val workflowName = executionServiceInput.actionIdentifiers.actionName + + // Assign Workflow inputs + val input = executionServiceInput.payload.get("$workflowName-request") + bluePrintRuntimeService.assignWorkflowInputs(workflowName, input) + + val stepMetaData: MutableMap<String, JsonNode> = hashMapOf() + stepMetaData.putJsonElement(BluePrintConstants.PROPERTY_CURRENT_NODE_TEMPLATE, "execute-remote-ansible") + stepMetaData.putJsonElement(BluePrintConstants.PROPERTY_CURRENT_INTERFACE, "ComponentRemoteAnsibleExecutor") + stepMetaData.putJsonElement(BluePrintConstants.PROPERTY_CURRENT_OPERATION, "process") + + val stepInputData = StepData().apply { + name = "execute-remote-ansible" + properties = stepMetaData + } + executionServiceInput.stepData = stepInputData + + awxRemoteExecutor.applyNB(executionServiceInput) + } + } + + /** + * Test cases for Ansible executor to work with the process NB of remote + * executor. + */ + @Test + @Ignore + fun testComponentRemoteAnsibleExecutorProcessNB() { + runBlocking { + // val remoteScriptExecutionService = MockRemoteScriptExecutionService(bluePrintRestLibPropertyService) + val componentRemoteAnsibleExecutor = ComponentRemoteAnsibleExecutor(bluePrintRestLibPropertyService) + val bluePrintRuntime = mockk<DefaultBluePrintRuntimeService>("123456-1000") + val input = getMockedOutput(bluePrintRuntime) + componentRemoteAnsibleExecutor.bluePrintRuntimeService = bluePrintRuntime + componentRemoteAnsibleExecutor.applyNB(input) + } + } + + /** + * Mocked input information for remote Ansible executor. + */ + fun getMockedOutput(svc: DefaultBluePrintRuntimeService): + ExecutionServiceInput { + val stepMetaData: MutableMap<String, JsonNode> = hashMapOf() + + stepMetaData.putJsonElement(BluePrintConstants.PROPERTY_CURRENT_NODE_TEMPLATE, "execute-remote-ansible") + stepMetaData.putJsonElement(BluePrintConstants.PROPERTY_CURRENT_INTERFACE, "ComponentRemoteAnsibleExecutor") + stepMetaData.putJsonElement(BluePrintConstants.PROPERTY_CURRENT_OPERATION, "process") + + val mapper = ObjectMapper() + val rootNode = mapper.createObjectNode() + rootNode.put("ip-address", "0.0.0.0") + rootNode.put("type", "rest") + + val operationalInputs: MutableMap<String, JsonNode> = hashMapOf() + operationalInputs.putJsonElement(BluePrintConstants.PROPERTY_CURRENT_NODE_TEMPLATE, "execute-remote-ansible") + operationalInputs.putJsonElement(BluePrintConstants.PROPERTY_CURRENT_INTERFACE, "ComponentRemoteAnsibleExecutor") + operationalInputs.putJsonElement(BluePrintConstants.PROPERTY_CURRENT_OPERATION, "process") + operationalInputs.putJsonElement("endpoint-selector", "aai") +// operationalInputs.putJsonElement("dynamic-properties", rootNode) +// operationalInputs.putJsonElement("command", "./run.sh") + operationalInputs.putJsonElement("job-template-name", "CDS_job_template2") + + every { + svc.resolveNodeTemplateInterfaceOperationInputs( + "execute-remote-ansible", + "ComponentRemoteAnsibleExecutor", "process") + } returns operationalInputs + + val stepInputData = StepData().apply { + name = "execute-remote-ansible" + properties = stepMetaData + } + + val executionServiceInput = JacksonUtils + .readValueFromClassPathFile( + "payload/requests/sample-remote-ansible-request.json", + ExecutionServiceInput::class.java)!! + executionServiceInput.stepData = stepInputData + + val operationOutputs = hashMapOf<String, JsonNode>() + every { + svc.resolveNodeTemplateInterfaceOperationOutputs( + "execute-remote-ansible", + "ComponentRemoteAnsibleExecutor", "process") + } returns operationOutputs + val bluePrintRuntimeService = BluePrintMetadataUtils + .getBluePrintRuntime("123456-1000", + "./../../../../components/model-" + + "catalog/blueprint-model/test-blueprint/" + + "remote_ansible") +// every { +// svc.resolveNodeTemplateArtifactDefinition("execute-remote-ansible", "component-script") +// } returns bluePrintRuntimeService.resolveNodeTemplateArtifactDefinition("execute-remote-ansible", +// "component-script") + every { + svc.setNodeTemplateAttributeValue( + "execute-remote-ansible", + "execute-command-status", + "successful".asJsonPrimitive()) + } returns Unit + + every { + svc.setNodeTemplateAttributeValue( + "execute-remote-ansible", + "execute-command-logs", "N/A".asJsonPrimitive()) + } returns Unit + + every { + svc.setNodeTemplateAttributeValue( + "execute-remote-ansible", + "execute-command-logs", + "processed successfully".asJsonPrimitive()) + } returns Unit + + every { + svc.bluePrintContext() + } returns bluePrintRuntimeService.bluePrintContext() + return executionServiceInput + } +} + +//class MockRemoteScriptExecutionService : RemoteScriptExecutionService { +// override suspend fun init(selector: String) { +// } +// +// override suspend fun prepareEnv(prepareEnvInput: PrepareRemoteEnvInput): RemoteScriptExecutionOutput { +// assertEquals(prepareEnvInput.requestId, "123456-1000", "failed to match request id") +// assertNotNull(prepareEnvInput.packages, "failed to get packages") +// +// val remoteScriptExecutionOutput = mockk<RemoteScriptExecutionOutput>() +// every { remoteScriptExecutionOutput.response } returns "prepared successfully" +// every { remoteScriptExecutionOutput.status } returns StatusType.SUCCESS +// return remoteScriptExecutionOutput +// } +// +// override suspend fun executeCommand(remoteExecutionInput: RemoteScriptExecutionInput): RemoteScriptExecutionOutput { +// assertEquals(remoteExecutionInput.requestId, "123456-1000", "failed to match request id") +// +// val remoteScriptExecutionOutput = mockk<RemoteScriptExecutionOutput>() +// every { remoteScriptExecutionOutput.response } returns "processed successfully" +// every { remoteScriptExecutionOutput.status } returns StatusType.SUCCESS +// return remoteScriptExecutionOutput +// } +// +// override suspend fun close() { +// +// } +//} diff --git a/ms/blueprintsprocessor/functions/ansible-awx-executor/src/test/resources/keystore.p12 b/ms/blueprintsprocessor/functions/ansible-awx-executor/src/test/resources/keystore.p12 Binary files differnew file mode 100644 index 000000000..96b0d3ac3 --- /dev/null +++ b/ms/blueprintsprocessor/functions/ansible-awx-executor/src/test/resources/keystore.p12 diff --git a/ms/blueprintsprocessor/functions/ansible-awx-executor/src/test/resources/logback-test.xml b/ms/blueprintsprocessor/functions/ansible-awx-executor/src/test/resources/logback-test.xml new file mode 100644 index 000000000..5b36ac046 --- /dev/null +++ b/ms/blueprintsprocessor/functions/ansible-awx-executor/src/test/resources/logback-test.xml @@ -0,0 +1,35 @@ +<!--
+ ~ Copyright © 2017-2018 AT&T Intellectual Property.
+ ~
+ ~ Licensed under the Apache License, Version 2.0 (the "License");
+ ~ you may not use this file except in compliance with the License.
+ ~ You may obtain a copy of the License at
+ ~
+ ~ http://www.apache.org/licenses/LICENSE-2.0
+ ~
+ ~ Unless required by applicable law or agreed to in writing, software
+ ~ distributed under the License is distributed on an "AS IS" BASIS,
+ ~ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ ~ See the License for the specific language governing permissions and
+ ~ limitations under the License.
+ -->
+
+<configuration>
+ <appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
+ <!-- encoders are assigned the type
+ ch.qos.logback.classic.encoder.PatternLayoutEncoder by default -->
+ <encoder>
+ <pattern>%d{HH:mm:ss.SSS} %-5level %logger{55} - %msg%n</pattern>
+ </encoder>
+ </appender>
+
+
+ <logger name="org.springframework" level="warn"/>
+ <logger name="org.hibernate" level="info"/>
+ <logger name="org.onap.ccsdk.cds.blueprintsprocessor" level="info"/>
+
+ <root level="warn">
+ <appender-ref ref="STDOUT"/>
+ </root>
+
+</configuration>
diff --git a/ms/blueprintsprocessor/functions/ansible-awx-executor/src/test/resources/payload/requests/sample-remote-ansible-request.json b/ms/blueprintsprocessor/functions/ansible-awx-executor/src/test/resources/payload/requests/sample-remote-ansible-request.json new file mode 100644 index 000000000..6169da9c7 --- /dev/null +++ b/ms/blueprintsprocessor/functions/ansible-awx-executor/src/test/resources/payload/requests/sample-remote-ansible-request.json @@ -0,0 +1,29 @@ +{ + "actionIdentifiers": { + "blueprintName": "remote_ansible", + "blueprintVersion": "1.0.0", + "actionName": "execute-remote-ansible", + "mode": "sync" + }, + "commonHeader": { + "flags": { + "force": true, + "ttl": 3600 + }, + "originatorId": "mock", + "requestId": "123456-2000", + "subRequestId": "sub-123456-1000", + "timestamp": "2012-04-23T18:25:43.511Z" + }, + "payload": { + "execute-remote-ansible-request": { + "endpoint-selector": "awx", + "job-template-name": "hello_world_job_template", + "inventory": "Demo Inventory", + "extra-vars": { + "site_id": "3 - Belmont", + "tor_group": "vEPC" + } + } + } +} |