| package com.mirantis.mk |
| |
| /** |
| * |
| * Tests providing functions |
| * |
| */ |
| |
| /** |
| * Get conformance pod statuses |
| * |
| * @param target Any control node of k8s |
| */ |
| def getConformanceStatus(master, target) { |
| def salt = new com.mirantis.mk.Salt() |
| def status = salt.cmdRun(master, target, "kubectl get po conformance -n conformance | awk {'print \$3'} | tail -n +2")['return'][0].values()[0].replaceAll('Salt command execution success','').trim() |
| return status |
| } |
| |
| /** |
| * Replace conformance image not relying on deployed version. Will be useful for testing a new k8s builds from docker-dev |
| * |
| * @param target I@kubernetes:master target |
| * @param image Desired image for conformance |
| * @param autodetect Default behaviour - use version discovered on deployment. Non default - use image provided via image param |
| */ |
| def passCustomConformanceImage(LinkedHashMap config) { |
| def salt = new com.mirantis.mk.Salt() |
| |
| // Listing defaults |
| def master = config.get('master', 'pepperVenv') |
| def target = config.get('target', 'I@kubernetes:master') |
| def pod_path = config.get('pod_path', '/srv/kubernetes/conformance.yml') |
| def autodetect = config.get('autodetect', true) |
| def image = config.get('image', null) |
| // End listing defaults |
| |
| if (!(autodetect.toBoolean()) && image) { |
| print("Replacing conformance image with ${image}") |
| salt.cmdRun(master, target, "sed -i 's|image: .*|image: ${image}|' ${pod_path}") |
| } |
| } |
| |
| /** |
| * Run e2e conformance on ContainerD environments |
| * |
| * @param target Any control node of k8s |
| * @param pd_path Conformance pod path to create |
| * @param timeout Test timeout |
| */ |
| def runConformanceTestsOnContainerD(LinkedHashMap config) { |
| def salt = new com.mirantis.mk.Salt() |
| |
| // Listing defaults |
| def master = config.get('master', 'pepperVenv') |
| def target = config.get('target', 'I@kubernetes:master and ctl01*') |
| def pod_path = config.get('pod_path', '/srv/kubernetes/conformance.yml') |
| def timeout = config.get('timeout', 3600) |
| // End listing defaults |
| |
| def status = "" |
| salt.cmdRun(master, target, "kubectl delete -f ${pod_path}", false) |
| salt.cmdRun(master, target, "kubectl create -f ${pod_path}") |
| sleep(10) |
| |
| counter = timeout/60 |
| |
| print("Waiting for results") |
| for (i = 0; i < counter; i++) { |
| current = getConformanceStatus(master, target) |
| if (current == "Running" || current == "ContainerCreating") { |
| sleep(60) |
| print("Wait counter: $i . Cap is $counter") |
| } else if (current == "Completed") { |
| print("Conformance succeeded. Proceed with artifacts.") |
| status = "OK" |
| return status |
| } else if (current == "Error") { |
| status = "ERR" |
| print("Tests failed. Proceed with artifacts") |
| return status |
| } else if (current == "ContainerCannotRun") { |
| print("Container can not run. Please check executor logs") |
| status = "NOTEXECUTED" |
| salt.cmdRun(master, target, "kubectl describe po conformance -n conformance") |
| return status |
| } else if (current == "ImagePullBackOff" || current == "ErrImagePull") { |
| print("Can not pull conformance image. Image is not exists or can not be accessed") |
| status = "PULLERR" |
| salt.cmdRun(master, target, "kubectl describe po conformance -n conformance") |
| return status |
| } else { |
| print("Unexpected status: ${current}") |
| status = "UNKNOWN" |
| salt.cmdRun(master, target, "kubectl describe po conformance -n conformance") |
| salt.cmdRun(master, target, "kubectl get cs") |
| salt.cmdRun(master, target, "kubectl get po --all-namespaces -o wide") |
| return status |
| } |
| } |
| status = "TIMEDOUT" |
| salt.cmdRun(master, target, "kubectl describe po conformance -n conformance") |
| salt.cmdRun(master, target, "kubectl logs conformance -n conformance") |
| return status |
| } |
| |
| |
| /** |
| * Locate node where conformance pod runs |
| * |
| * @param target Any control node of k8s |
| */ |
| def locateConformancePod(master, target) { |
| def salt = new com.mirantis.mk.Salt() |
| def node = salt.cmdRun(master, target, "kubectl get po conformance -n conformance -o wide -o=custom-columns=NODE:.spec.nodeName | tail -n +2")['return'][0].values()[0].replaceAll('Salt command execution success','').trim() |
| return node |
| } |
| |
| /** |
| * Get conformance results and logs |
| * |
| * @param ctl_target Target maps to all k8s masters |
| * @param artifacts_dir Artifacts_dir Local directory to push artifacts to |
| * @param output_file Output tar file that will be archived and (optional) published |
| * @param status Status of conformance run to react (if NOTEXECUTED - xml will never published) |
| * @param junitResults Whether or not build test graph |
| */ |
| def uploadConformanceContainerdResults(LinkedHashMap config) { |
| def salt = new com.mirantis.mk.Salt() |
| |
| // Listing defaults |
| def master = config.get('master', 'pepperVenv') |
| def target = config.get('target', 'I@kubernetes:master and ctl01*') |
| def status = config.get('status') |
| def ctl_target = config.get('ctl_target', 'I@kubernetes:master') |
| def results_dir = config.get('results_dir', '/tmp/conformance') |
| def artifacts_dir = config.get('artifacts_dir', '_artifacts/') |
| def output_file = config.get('output_file', 'conformance.tar') |
| def junitResults = config.get('junitResults', false) |
| // End listing defaults |
| |
| def short_node = locateConformancePod(master, target) |
| print("Pod located on $short_node") |
| |
| minions = salt.getMinionsSorted(master, ctl_target) |
| conformance_target = minions.find {it =~ short_node} |
| |
| if (status == 'NOTEXECUTED') { |
| salt.cmdRun(master, conformance_target, "test -e ${results_dir}/conformance.log || kubectl logs conformance -n conformance > ${results_dir}/conformance.log") |
| } else if (status == "PULLERR") { |
| print("Conformance image failed to pull. Skipping logs publishing") |
| return conformance_target |
| } else if (status == "UNKNOWN") { |
| print("Can not recognize pod status as acceptable. Skipping logs publishing") |
| return conformance_target |
| } |
| |
| print("Copy XML test results for junit artifacts and logs") |
| salt.runSaltProcessStep(master, conformance_target, 'cmd.run', ["tar -cf /tmp/${output_file} -C ${results_dir} ."]) |
| |
| writeFile file: "${artifacts_dir}${output_file}", text: salt.getFileContent(master, conformance_target, "/tmp/${output_file}") |
| sh "mkdir -p ${artifacts_dir}/conformance_tests" |
| sh "tar -xf ${artifacts_dir}${output_file} -C ${artifacts_dir}/conformance_tests" |
| sh "cat ${artifacts_dir}/conformance_tests/conformance.log" |
| if (junitResults.toBoolean() && (status == 'OK' || status == 'ERR')) { |
| archiveArtifacts artifacts: "${artifacts_dir}${output_file}" |
| archiveArtifacts artifacts: "${artifacts_dir}conformance_tests/conformance.log" |
| junit(keepLongStdio: true, testResults: "${artifacts_dir}conformance_tests/**.xml") |
| } |
| return conformance_target |
| } |
| |
| |
| /** |
| * Clean conformance pod and tmp files |
| * |
| * @param target Node where conformance was executed\ |
| * @param results_dir Directory to clean up |
| */ |
| def cleanUpConformancePod(LinkedHashMap config) { |
| def salt = new com.mirantis.mk.Salt() |
| |
| // Listing defaults |
| def master = config.get('master', 'pepperVenv') |
| def target = config.get('target', 'I@kubernetes:master and ctl01*') |
| def pod_path = config.get('pod_path', '/srv/kubernetes/conformance.yml') |
| def results_dir = config.get('results_dir', '/tmp/conformance') |
| def output_file = config.get('output_file', ) |
| // End listing defaults |
| |
| salt.cmdRun(master, target, "kubectl delete -f ${pod_path}") |
| salt.cmdRun(master, target, "rm -rf ${results_dir}", false) |
| salt.cmdRun(master, target, "rm -f ${output_file}", false) |
| } |
| |
| /** |
| * Throw exception if any |
| * |
| * @param status Conformance tests status |
| */ |
| def conformanceStatusReact(status) { |
| if (status == "ERR" || status == "NOTEXECUTED") { |
| throw new RuntimeException("Conformance tests failed") |
| } else if (status == "TIMEDOUT") { |
| throw new RuntimeException("Conformance tests timed out") |
| } else if (status == "PULLERR") { |
| throw new RuntimeException("Image is not exists or can not reach repository") |
| } else if (status == "UNKNOWN") { |
| throw new RuntimeException("Pod status unacceptable. Please check pipeline logs for more information") |
| } |
| } |
| |
| /** |
| * Orchestrate conformance tests inside kubernetes cluster |
| * |
| * @param junitResults Whether or not build junit graph |
| * @param autodetect Default behaviour - use version discovered on deployment. Non default - use image provided via image param |
| * @param image Can be used only if autodetection disabled. Overriding pod image. |
| * @param ctl_target Target maps to all k8s masters |
| * @param pod_path Path where conformance pod located |
| * @param results_dir Directory with results after conformance run |
| * @param artifacts_dir Local artifacts dir |
| * @param output_file Conformance tar output |
| */ |
| def executeConformance(LinkedHashMap config) { |
| // Listing defaults |
| def master = config.get('master', 'pepperVenv') |
| def target = config.get('target', 'I@kubernetes:master and ctl01*') |
| def junitResults = config.get('junitResults', false) |
| def autodetect = config.get('autodetect', true) |
| def image = config.get('image', null) |
| def ctl_target = config.get('ctl_target', 'I@kubernetes:master') |
| def pod_path = config.get('pod_path', '/srv/kubernetes/conformance.yml') |
| def results_dir = config.get('results_dir', '/tmp/conformance') |
| def artifacts_dir = config.get('artifacts_dir', '_artifacts/') |
| def output_file = config.get('output_file', 'conformance.tar') |
| // End listing defaults |
| |
| // Check whether or not custom image is defined and apply it |
| passCustomConformanceImage(['master': master, 'ctl_target': ctl_target, 'pod_path': pod_path, 'autodetect': autodetect, 'image': image]) |
| |
| // Start conformance pod and get its status |
| status = runConformanceTestsOnContainerD('master': master, 'target': target, 'pod_path': pod_path) |
| |
| // Manage results |
| cleanup_target = uploadConformanceContainerdResults('master': master, 'target': target, 'status': status, 'ctl_target': ctl_target, 'results_dir': results_dir, 'artifacts_dir': artifacts_dir, 'output_file': output_file, 'junitResults': junitResults) |
| |
| // Do cleanup |
| cleanUpConformancePod('master': master, 'target': cleanup_target, 'pod_path': pod_path, 'results_dir': results_dir, 'output_file': output_file) |
| |
| // Throw exception to Jenkins if any |
| conformanceStatusReact(status) |
| } |
| |
| /** |
| * Run e2e conformance tests |
| * |
| * @param target Kubernetes node to run tests from |
| * @param k8s_api Kubernetes api address |
| * @param image Docker image with tests |
| * @param timeout Timeout waiting for e2e conformance tests |
| */ |
| def runConformanceTests(master, target, k8s_api, image, timeout=2400) { |
| def salt = new com.mirantis.mk.Salt() |
| def containerName = 'conformance_tests' |
| def outfile = "/tmp/" + image.replaceAll('/', '-') + '.output' |
| salt.cmdRun(master, target, "docker rm -f ${containerName}", false) |
| salt.cmdRun(master, target, "docker run -d --name ${containerName} --net=host -e API_SERVER=${k8s_api} ${image}") |
| sleep(10) |
| |
| print("Waiting for tests to run...") |
| salt.runSaltProcessStep(master, target, 'cmd.run', ["docker wait ${containerName}"], null, false, timeout) |
| |
| print("Writing test results to output file...") |
| salt.runSaltProcessStep(master, target, 'cmd.run', ["docker logs -t ${containerName} > ${outfile}"]) |
| print("Conformance test output saved in " + outfile) |
| } |
| |
| /** |
| * Upload conformance results to cfg node |
| * |
| * @param target Kubernetes node for copy test results |
| * @param artifacts_dir Path with test results |
| */ |
| def CopyConformanceResults(master, target, artifacts_dir, output_file) { |
| def salt = new com.mirantis.mk.Salt() |
| def containerName = 'conformance_tests' |
| def test_node = target.replace("*", "") |
| |
| out = salt.runSaltProcessStep(master, target, 'cmd.run', ["docker cp ${containerName}:/report /tmp"]) |
| if (! out['return'][0].values()[0].contains('Error')) { |
| print("Copy XML test results for junit artifacts...") |
| salt.runSaltProcessStep(master, target, 'cmd.run', ["tar -cf /tmp/${output_file} -C /tmp/report ."]) |
| |
| writeFile file: "${artifacts_dir}${output_file}", text: salt.getFileContent(master, |
| target, "/tmp/${output_file}") |
| |
| sh "mkdir -p ${artifacts_dir}/conformance_tests" |
| sh "tar -xf ${artifacts_dir}${output_file} -C ${artifacts_dir}/conformance_tests" |
| |
| // collect artifacts |
| archiveArtifacts artifacts: "${artifacts_dir}${output_file}" |
| |
| junit(keepLongStdio: true, testResults: "${artifacts_dir}conformance_tests/**.xml") |
| } |
| } |
| |
| /** |
| * Copy test output to cfg node |
| * |
| * @param image Docker image with tests |
| */ |
| def copyTestsOutput(master, image) { |
| def salt = new com.mirantis.mk.Salt() |
| salt.runSaltProcessStep(master, 'cfg01*', 'cmd.run', ["scp ctl01:/root/${image}.output /home/ubuntu/"]) |
| } |
| |
| /** |
| * Execute tempest tests |
| * |
| * @param dockerImageLink Docker image link with rally and tempest |
| * @param target Host to run tests |
| * @param pattern If not false, will run only tests matched the pattern |
| * @param logDir Directory to store tempest/rally reports |
| * @param sourceFile Path to the keystonerc file in the container |
| * @param set Predefined set for tempest tests |
| * @param concurrency How many processes to use to run Tempest tests |
| * @param tempestConf A tempest.conf's file name |
| * @param skipList A skip.list's file name |
| * @param localKeystone Path to the keystonerc file in the local host |
| * @param localLogDir Path to local destination folder for logs |
| */ |
| def runTempestTests(master, dockerImageLink, target, pattern = "", logDir = "/home/rally/rally_reports/", |
| sourceFile="/home/rally/keystonercv3", set="full", concurrency="0", tempestConf="mcp.conf", |
| skipList="mcp_skip.list", localKeystone="/root/keystonercv3" , localLogDir="/root/rally_reports", |
| doCleanupResources = "false") { |
| def salt = new com.mirantis.mk.Salt() |
| salt.runSaltProcessStep(master, target, 'file.mkdir', ["${localLogDir}"]) |
| def custom = '' |
| if (pattern) { |
| custom = "--pattern " + pattern |
| } |
| salt.cmdRun(master, "${target}", "docker run --rm --net=host " + |
| "-e SOURCE_FILE=${sourceFile} " + |
| "-e LOG_DIR=${logDir} " + |
| "-e SET=${set} " + |
| "-e CUSTOM='${custom}' " + |
| "-e CONCURRENCY=${concurrency} " + |
| "-e TEMPEST_CONF=${tempestConf} " + |
| "-e SKIP_LIST=${skipList} " + |
| "-e DO_CLEANUP_RESOURCES=${doCleanupResources} " + |
| "-v ${localKeystone}:${sourceFile} " + |
| "-v ${localLogDir}:/home/rally/rally_reports " + |
| "-v /etc/ssl/certs/:/etc/ssl/certs/ " + |
| "${dockerImageLink} >> docker-tempest.log") |
| } |
| |
| |
| /** |
| * Execute Rally scenarios |
| * |
| * @param dockerImageLink Docker image link with rally and tempest |
| * @param target Host to run scenarios |
| * @param scenario Specify the scenario as a string |
| * @param containerName Docker container name |
| * @param doCleanupResources Do run clean-up script after tests? Cleans up OpenStack test resources |
| */ |
| def runRallyScenarios(master, dockerImageLink, target, scenario, logDir = "/home/rally/rally_reports/", |
| doCleanupResources = "false", containerName = "rally_ci") { |
| def salt = new com.mirantis.mk.Salt() |
| salt.runSaltProcessStep(master, target, 'file.mkdir', ["/root/rally_reports"]) |
| salt.cmdRun(master, target, "docker run --net=host -dit " + |
| "--name ${containerName} " + |
| "-e SOURCE_FILE=keystonercv3 " + |
| "-e SCENARIO=${scenario} " + |
| "-e DO_CLEANUP_RESOURCES=${doCleanupResources} " + |
| "-e LOG_DIR=${logDir} " + |
| "--entrypoint /bin/bash -v /root/:/home/rally ${dockerImageLink}") |
| salt.cmdRun(master, target, "docker exec ${containerName} " + |
| "bash -c /usr/bin/run-rally | tee -a docker-rally.log") |
| } |
| |
| |
| /** |
| * Upload results to cfg01 node |
| * |
| */ |
| def copyTempestResults(master, target) { |
| def salt = new com.mirantis.mk.Salt() |
| if (! target.contains('cfg')) { |
| salt.cmdRun(master, target, "mkdir -p /root/rally_reports/ && rsync -av /root/rally_reports/ cfg01:/root/rally_reports/") |
| } |
| } |
| |
| |
| /** Store tests results on host |
| * |
| * @param image Docker image name |
| */ |
| def catTestsOutput(master, image) { |
| def salt = new com.mirantis.mk.Salt() |
| salt.cmdRun(master, 'cfg01*', "cat /home/ubuntu/${image}.output") |
| } |
| |
| |
| /** Install docker if needed |
| * |
| * @param target Target node to install docker pkg |
| */ |
| def install_docker(master, target) { |
| def salt = new com.mirantis.mk.Salt() |
| def dockerPackagesPillar = salt.getPillar(master, target, 'docker:host:pkgs') |
| def dockerPackages = salt.getReturnValues(dockerPackagesPillar) ?: ['docker.io'] |
| salt.runSaltProcessStep(master, target, 'pkg.install', [dockerPackages.join(',')]) |
| } |
| |
| |
| /** Upload Tempest test results to Testrail |
| * |
| * @param report Source report to upload |
| * @param image Testrail reporter image |
| * @param testGroup Testrail test group |
| * @param credentialsId Testrail credentials id |
| * @param plan Testrail test plan |
| * @param milestone Testrail test milestone |
| * @param suite Testrail test suite |
| * @param type Use local shell or remote salt connection |
| * @param master Salt connection. |
| * @param target Target node to install docker pkg |
| */ |
| |
| def uploadResultsTestrail(report, image, testGroup, credentialsId, plan, milestone, suite, master = null, target = 'cfg01*') { |
| def salt = new com.mirantis.mk.Salt() |
| def common = new com.mirantis.mk.Common() |
| creds = common.getPasswordCredentials(credentialsId) |
| command = "docker run --rm --net=host " + |
| "-v ${report}:/srv/report.xml " + |
| "-e TESTRAIL_USER=${creds.username} " + |
| "-e PASS=${creds.password.toString()} " + |
| "-e TESTRAIL_PLAN_NAME=${plan} " + |
| "-e TESTRAIL_MILESTONE=${milestone} " + |
| "-e TESTRAIL_SUITE=${suite} " + |
| "-e TEST_GROUP=${testGroup} " + |
| "${image}" |
| if (master == null) { |
| sh(command) |
| } else { |
| salt.cmdRun(master, target, command) |
| } |
| } |
| |
| /** Archive Rally results in Artifacts |
| * |
| * @param master Salt connection. |
| * @param target Target node to install docker pkg |
| * @param reports_dir Source directory to archive |
| */ |
| |
| def archiveRallyArtifacts(master, target, reports_dir='/root/rally_reports') { |
| def salt = new com.mirantis.mk.Salt() |
| |
| def artifacts_dir = '_artifacts/' |
| def output_file = 'rally_reports.tar' |
| |
| salt.cmdRun(master, target, "tar -cf /root/${output_file} -C ${reports_dir} .") |
| sh "mkdir -p ${artifacts_dir}" |
| |
| encoded = salt.cmdRun(master, target, "cat /root/${output_file}", true, null, false)['return'][0].values()[0].replaceAll('Salt command execution success','') |
| |
| writeFile file: "${artifacts_dir}${output_file}", text: encoded |
| |
| // collect artifacts |
| archiveArtifacts artifacts: "${artifacts_dir}${output_file}" |
| } |
| /** |
| * Helper function for collecting junit tests results |
| * @param testResultAction - test result from build - use: currentBuild.rawBuild.getAction(AbstractTestResultAction.class) |
| * @return resultMap with structure ["total": total, "passed": passed, "skipped": skipped, "failed": failed] |
| */ |
| @NonCPS |
| def collectJUnitResults(testResultAction) { |
| if (testResultAction != null) { |
| def total = testResultAction.totalCount |
| def failed = testResultAction.failCount |
| def skipped = testResultAction.skipCount |
| def passed = total - failed - skipped |
| return ["total": total, "passed": passed, "skipped": skipped, "failed": failed] |
| }else{ |
| def common = new com.mirantis.mk.Common() |
| common.errorMsg("Cannot collect jUnit tests results, given result is null") |
| } |
| return [:] |
| } |
| |
| |
| /** Cleanup: Remove reports directory |
| * |
| * @param target Target node to remove repo |
| * @param reports_dir_name Reports directory name to be removed (that is in /root/ on target node) |
| * @param archive_artifacts_name Archive of the artifacts |
| */ |
| def removeReports(master, target, reports_dir_name = 'rally_reports', archive_artifacts_name = 'rally_reports.tar') { |
| def salt = new com.mirantis.mk.Salt() |
| salt.runSaltProcessStep(master, target, 'file.find', ["/root/${reports_dir_name}", '\\*', 'delete']) |
| salt.runSaltProcessStep(master, target, 'file.remove', ["/root/${archive_artifacts_name}"]) |
| } |
| |
| |
| /** Cleanup: Remove Docker container |
| * |
| * @param target Target node to remove Docker container |
| * @param image_link The link of the Docker image that was used for the container |
| */ |
| def removeDockerContainer(master, target, containerName) { |
| def salt = new com.mirantis.mk.Salt() |
| salt.cmdRun(master, target, "docker rm -f ${containerName}") |
| } |