Jakub Josef | 79ecec3 | 2017-02-17 14:36:28 +0100 | [diff] [blame] | 1 | package com.mirantis.mk |
| 2 | |
| 3 | /** |
| 4 | * |
| 5 | * Tests providing functions |
| 6 | * |
| 7 | */ |
| 8 | |
| 9 | /** |
| 10 | * Run e2e conformance tests |
| 11 | * |
Tetiana Korchak | f500ab9 | 2017-09-27 14:53:51 -0700 | [diff] [blame] | 12 | * @param target Kubernetes node to run tests from |
| 13 | * @param k8s_api Kubernetes api address |
| 14 | * @param image Docker image with tests |
| 15 | * @param timeout Timeout waiting for e2e conformance tests |
Jakub Josef | 79ecec3 | 2017-02-17 14:36:28 +0100 | [diff] [blame] | 16 | */ |
Tetiana Korchak | f500ab9 | 2017-09-27 14:53:51 -0700 | [diff] [blame] | 17 | def runConformanceTests(master, target, k8s_api, image, timeout=2400) { |
Jakub Josef | 79ecec3 | 2017-02-17 14:36:28 +0100 | [diff] [blame] | 18 | def salt = new com.mirantis.mk.Salt() |
Matthew Mosesohn | e5c07e8 | 2017-06-14 11:55:01 +0300 | [diff] [blame] | 19 | def containerName = 'conformance_tests' |
Tomáš Kukrál | a7318f5 | 2017-04-21 16:15:29 +0200 | [diff] [blame] | 20 | def outfile = "/tmp/" + image.replaceAll('/', '-') + '.output' |
Tetiana Korchak | f500ab9 | 2017-09-27 14:53:51 -0700 | [diff] [blame] | 21 | salt.cmdRun(master, target, "docker rm -f ${containerName}", false) |
| 22 | salt.cmdRun(master, target, "docker run -d --name ${containerName} --net=host -e API_SERVER=${k8s_api} ${image}") |
Matthew Mosesohn | e5c07e8 | 2017-06-14 11:55:01 +0300 | [diff] [blame] | 23 | sleep(10) |
| 24 | |
| 25 | print("Waiting for tests to run...") |
Tetiana Korchak | f500ab9 | 2017-09-27 14:53:51 -0700 | [diff] [blame] | 26 | salt.runSaltProcessStep(master, target, 'cmd.run', ["docker wait ${containerName}"], null, false, timeout) |
Matthew Mosesohn | e5c07e8 | 2017-06-14 11:55:01 +0300 | [diff] [blame] | 27 | |
| 28 | print("Writing test results to output file...") |
Tetiana Korchak | f500ab9 | 2017-09-27 14:53:51 -0700 | [diff] [blame] | 29 | salt.runSaltProcessStep(master, target, 'cmd.run', ["docker logs -t ${containerName} > ${outfile}"]) |
Tomáš Kukrál | 798b3f5 | 2017-04-28 13:19:32 +0200 | [diff] [blame] | 30 | print("Conformance test output saved in " + outfile) |
Tatyana Leontovich | c73d63c | 2017-02-28 14:41:38 +0200 | [diff] [blame] | 31 | } |
| 32 | |
| 33 | /** |
| 34 | * Copy test output to cfg node |
| 35 | * |
| 36 | * @param image Docker image with tests |
| 37 | */ |
| 38 | def copyTestsOutput(master, image) { |
| 39 | def salt = new com.mirantis.mk.Salt() |
| 40 | salt.runSaltProcessStep(master, 'cfg01*', 'cmd.run', ["scp ctl01:/root/${image}.output /home/ubuntu/"]) |
| 41 | } |
| 42 | |
Victor Ryzhenkin | c5b3029 | 2017-02-21 19:26:24 +0400 | [diff] [blame] | 43 | /** |
| 44 | * Execute tempest tests |
| 45 | * |
Tatyana Leontovich | 060e115 | 2017-07-10 17:25:37 +0300 | [diff] [blame] | 46 | * @param dockerImageLink Docker image link with rally and tempest |
| 47 | * @param target Host to run tests |
Mykyta Karpin | 80f527e | 2017-08-14 15:18:03 +0300 | [diff] [blame] | 48 | * @param pattern If not false, will run only tests matched the pattern |
| 49 | * @param logDir Directory to store tempest/rally reports |
Victor Ryzhenkin | c5b3029 | 2017-02-21 19:26:24 +0400 | [diff] [blame] | 50 | */ |
Consatntine Kalinovskiy | 8463475 | 2017-08-30 15:31:26 +0300 | [diff] [blame] | 51 | def runTempestTests(master, dockerImageLink, target, pattern = "false", logDir = "/home/rally/rally_reports/", |
| 52 | doCleanupResources = "false") { |
Victor Ryzhenkin | c5b3029 | 2017-02-21 19:26:24 +0400 | [diff] [blame] | 53 | def salt = new com.mirantis.mk.Salt() |
Ievgeniia Zadorozhna | 5b97023 | 2017-09-13 13:31:43 +0300 | [diff] [blame] | 54 | salt.runSaltProcessStep(master, target, 'file.mkdir', ["/root/rally_reports"]) |
Tatyana Leontovich | 060e115 | 2017-07-10 17:25:37 +0300 | [diff] [blame] | 55 | if (pattern == "false") { |
Mykyta Karpin | 07ba87f | 2017-07-27 13:56:33 +0300 | [diff] [blame] | 56 | salt.cmdRun(master, "${target}", "docker run --rm --net=host " + |
| 57 | "-e TEMPEST_CONF=mcp.conf " + |
| 58 | "-e SKIP_LIST=mcp_skip.list " + |
| 59 | "-e SOURCE_FILE=keystonercv3 " + |
Jakub Pavlik | 0bfc263 | 2017-08-18 10:07:09 +0200 | [diff] [blame] | 60 | "-e LOG_DIR=${logDir} " + |
Consatntine Kalinovskiy | 8463475 | 2017-08-30 15:31:26 +0300 | [diff] [blame] | 61 | "-e DO_CLEANUP_RESOURCES=${doCleanupResources} " + |
Mykyta Karpin | 07ba87f | 2017-07-27 13:56:33 +0300 | [diff] [blame] | 62 | "-v /root/:/home/rally ${dockerImageLink} >> docker-tempest.log") |
| 63 | } |
Tatyana Leontovich | 060e115 | 2017-07-10 17:25:37 +0300 | [diff] [blame] | 64 | else { |
Mykyta Karpin | 07ba87f | 2017-07-27 13:56:33 +0300 | [diff] [blame] | 65 | salt.cmdRun(master, "${target}", "docker run --rm --net=host " + |
| 66 | "-e TEMPEST_CONF=mcp.conf " + |
| 67 | "-e SKIP_LIST=mcp_skip.list " + |
| 68 | "-e SOURCE_FILE=keystonercv3 " + |
Mykyta Karpin | 80f527e | 2017-08-14 15:18:03 +0300 | [diff] [blame] | 69 | "-e LOG_DIR=${logDir} " + |
Consatntine Kalinovskiy | 8463475 | 2017-08-30 15:31:26 +0300 | [diff] [blame] | 70 | "-e DO_CLEANUP_RESOURCES=${doCleanupResources} " + |
Mykyta Karpin | 07ba87f | 2017-07-27 13:56:33 +0300 | [diff] [blame] | 71 | "-e CUSTOM='--pattern ${pattern}' " + |
| 72 | "-v /root/:/home/rally ${dockerImageLink} >> docker-tempest.log") |
| 73 | } |
Victor Ryzhenkin | c5b3029 | 2017-02-21 19:26:24 +0400 | [diff] [blame] | 74 | } |
| 75 | |
Consatntine Kalinovskiy | 8463475 | 2017-08-30 15:31:26 +0300 | [diff] [blame] | 76 | |
| 77 | /** |
| 78 | * Execute Rally scenarios |
| 79 | * |
| 80 | * @param dockerImageLink Docker image link with rally and tempest |
| 81 | * @param target Host to run scenarios |
| 82 | * @param scenario Specify the scenario as a string |
| 83 | * @param containerName Docker container name |
| 84 | * @param doCleanupResources Do run clean-up script after tests? Cleans up OpenStack test resources |
| 85 | */ |
| 86 | def runRallyScenarios(master, dockerImageLink, target, scenario, logDir = "/home/rally/rally_reports/", |
| 87 | doCleanupResources = "false", containerName = "rally_ci") { |
| 88 | def salt = new com.mirantis.mk.Salt() |
Ievgeniia Zadorozhna | 5b97023 | 2017-09-13 13:31:43 +0300 | [diff] [blame] | 89 | salt.runSaltProcessStep(master, target, 'file.mkdir', ["/root/rally_reports"]) |
Consatntine Kalinovskiy | 8463475 | 2017-08-30 15:31:26 +0300 | [diff] [blame] | 90 | salt.cmdRun(master, target, "docker run --net=host -dit " + |
| 91 | "--name ${containerName} " + |
| 92 | "-e SOURCE_FILE=keystonercv3 " + |
| 93 | "-e SCENARIO=${scenario} " + |
| 94 | "-e DO_CLEANUP_RESOURCES=${doCleanupResources} " + |
| 95 | "-e LOG_DIR=${logDir} " + |
| 96 | "--entrypoint /bin/bash -v /root/:/home/rally ${dockerImageLink}") |
| 97 | salt.cmdRun(master, target, "docker exec ${containerName} " + |
| 98 | "bash -c /usr/bin/run-rally | tee -a docker-rally.log") |
| 99 | } |
| 100 | |
| 101 | |
Victor Ryzhenkin | c5b3029 | 2017-02-21 19:26:24 +0400 | [diff] [blame] | 102 | /** |
Vasyl Saienko | d1dd133 | 2017-08-03 15:22:42 +0300 | [diff] [blame] | 103 | * Upload results to cfg01 node |
Victor Ryzhenkin | c5b3029 | 2017-02-21 19:26:24 +0400 | [diff] [blame] | 104 | * |
| 105 | */ |
Tatyana Leontovich | 060e115 | 2017-07-10 17:25:37 +0300 | [diff] [blame] | 106 | def copyTempestResults(master, target) { |
Victor Ryzhenkin | c5b3029 | 2017-02-21 19:26:24 +0400 | [diff] [blame] | 107 | def salt = new com.mirantis.mk.Salt() |
Vasyl Saienko | d1dd133 | 2017-08-03 15:22:42 +0300 | [diff] [blame] | 108 | if (! target.contains('cfg')) { |
| 109 | salt.runSaltProcessStep(master, "${target}", 'cmd.run', ["mkdir /root/rally_reports/ && " + |
| 110 | "rsync -av /root/rally_reports/ cfg01:/root/rally_reports/"]) |
| 111 | } |
Victor Ryzhenkin | c5b3029 | 2017-02-21 19:26:24 +0400 | [diff] [blame] | 112 | } |
| 113 | |
| 114 | |
Tatyana Leontovich | 01704b3 | 2017-03-06 12:26:33 +0200 | [diff] [blame] | 115 | /** Store tests results on host |
Victor Ryzhenkin | c5b3029 | 2017-02-21 19:26:24 +0400 | [diff] [blame] | 116 | * |
Tatyana Leontovich | 01704b3 | 2017-03-06 12:26:33 +0200 | [diff] [blame] | 117 | * @param image Docker image name |
| 118 | */ |
| 119 | def catTestsOutput(master, image) { |
| 120 | def salt = new com.mirantis.mk.Salt() |
| 121 | salt.runSaltProcessStep(master, 'cfg01*', 'cmd.run', ["cat /home/ubuntu/${image}.output"]) |
| 122 | } |
Tatyana Leontovich | 060e115 | 2017-07-10 17:25:37 +0300 | [diff] [blame] | 123 | |
| 124 | |
| 125 | /** Install docker if needed |
| 126 | * |
| 127 | * @param target Target node to install docker pkg |
| 128 | */ |
| 129 | def install_docker(master, target) { |
| 130 | def salt = new com.mirantis.mk.Salt() |
| 131 | salt.runSaltProcessStep(master, "${target}", 'pkg.install', ["docker.io"]) |
| 132 | } |
Vasyl Saienko | df02e9d | 2017-08-04 09:55:13 +0300 | [diff] [blame] | 133 | |
Consatntine Kalinovskiy | 8463475 | 2017-08-30 15:31:26 +0300 | [diff] [blame] | 134 | |
Mykyta Karpin | 94d82a8 | 2017-08-08 19:03:36 +0300 | [diff] [blame] | 135 | /** Upload Tempest test results to Testrail |
| 136 | * |
| 137 | * @param report Source report to upload |
| 138 | * @param image Testrail reporter image |
| 139 | * @param testGroup Testrail test group |
| 140 | * @param credentialsId Testrail credentials id |
| 141 | * @param plan Testrail test plan |
| 142 | * @param milestone Testrail test milestone |
| 143 | * @param suite Testrail test suite |
| 144 | * @param type Use local shell or remote salt connection |
| 145 | * @param master Salt connection. |
| 146 | * @param target Target node to install docker pkg |
| 147 | */ |
| 148 | |
| 149 | def uploadResultsTestrail(report, image, testGroup, credentialsId, plan, milestone, suite, master = null, target = 'cfg01*') { |
| 150 | def salt = new com.mirantis.mk.Salt() |
| 151 | def common = new com.mirantis.mk.Common() |
| 152 | creds = common.getPasswordCredentials(credentialsId) |
| 153 | command = "docker run --rm --net=host " + |
| 154 | "-v ${report}:/srv/report.xml " + |
| 155 | "-e TESTRAIL_USER=${creds.username} " + |
| 156 | "-e PASS=${creds.password.toString()} " + |
| 157 | "-e TESTRAIL_PLAN_NAME=${plan} " + |
| 158 | "-e TESTRAIL_MILESTONE=${milestone} " + |
| 159 | "-e TESTRAIL_SUITE=${suite} " + |
Mykyta Karpin | a5761a2 | 2017-08-16 16:09:56 +0300 | [diff] [blame] | 160 | "-e TEST_GROUP=${testGroup} " + |
Mykyta Karpin | 94d82a8 | 2017-08-08 19:03:36 +0300 | [diff] [blame] | 161 | "${image}" |
| 162 | if (master == null) { |
| 163 | sh("${command}") |
| 164 | } else { |
| 165 | salt.cmdRun(master, "${target}", "${command}") |
| 166 | } |
| 167 | } |
| 168 | |
Vasyl Saienko | df02e9d | 2017-08-04 09:55:13 +0300 | [diff] [blame] | 169 | /** Archive Rally results in Artifacts |
| 170 | * |
| 171 | * @param master Salt connection. |
| 172 | * @param target Target node to install docker pkg |
| 173 | * @param reports_dir Source directory to archive |
| 174 | */ |
| 175 | |
| 176 | def archiveRallyArtifacts(master, target, reports_dir='/root/rally_reports') { |
| 177 | def salt = new com.mirantis.mk.Salt() |
| 178 | |
| 179 | def artifacts_dir = '_artifacts/' |
| 180 | def output_file = 'rally_reports.tar' |
| 181 | |
Mykyta Karpin | f4be9e2 | 2017-08-09 18:59:57 +0300 | [diff] [blame] | 182 | salt.runSaltProcessStep(master, "${target}", 'cmd.run', ["tar -cf /root/${output_file} -C ${reports_dir} ."]) |
Vasyl Saienko | df02e9d | 2017-08-04 09:55:13 +0300 | [diff] [blame] | 183 | sh "mkdir -p ${artifacts_dir}" |
| 184 | |
Mykyta Karpin | f4be9e2 | 2017-08-09 18:59:57 +0300 | [diff] [blame] | 185 | encoded = salt.cmdRun(master, target, "cat /root/${output_file}", true, null, false)['return'][0].values()[0].replaceAll('Salt command execution success','') |
Vasyl Saienko | df02e9d | 2017-08-04 09:55:13 +0300 | [diff] [blame] | 186 | |
| 187 | writeFile file: "${artifacts_dir}${output_file}", text: encoded |
| 188 | |
| 189 | // collect artifacts |
| 190 | archiveArtifacts artifacts: "${artifacts_dir}${output_file}" |
| 191 | } |
Jakub Josef | 1462c4b | 2017-08-18 11:15:03 +0200 | [diff] [blame] | 192 | /** |
| 193 | * Helper function for collecting junit tests results |
| 194 | * @param testResultAction - test result from build - use: currentBuild.rawBuild.getAction(AbstractTestResultAction.class) |
| 195 | * @return resultMap with structure ["total": total, "passed": passed, "skipped": skipped, "failed": failed] |
| 196 | */ |
| 197 | @NonCPS |
| 198 | def collectJUnitResults(testResultAction) { |
| 199 | if (testResultAction != null) { |
| 200 | def total = testResultAction.totalCount |
| 201 | def failed = testResultAction.failCount |
| 202 | def skipped = testResultAction.skipCount |
| 203 | def passed = total - failed - skipped |
| 204 | return ["total": total, "passed": passed, "skipped": skipped, "failed": failed] |
| 205 | }else{ |
| 206 | def common = new com.mirantis.mk.Common() |
| 207 | common.errorMsg("Cannot collect jUnit tests results, given result is null") |
| 208 | } |
| 209 | return [:] |
| 210 | } |
Consatntine Kalinovskiy | 8463475 | 2017-08-30 15:31:26 +0300 | [diff] [blame] | 211 | |
| 212 | |
| 213 | /** Cleanup: Remove reports directory |
| 214 | * |
| 215 | * @param target Target node to remove repo |
| 216 | * @param reports_dir_name Reports directory name to be removed (that is in /root/ on target node) |
| 217 | * @param archive_artifacts_name Archive of the artifacts |
| 218 | */ |
| 219 | def removeReports(master, target, reports_dir_name = 'rally_reports', archive_artifacts_name = 'rally_reports.tar') { |
| 220 | def salt = new com.mirantis.mk.Salt() |
| 221 | salt.runSaltProcessStep(master, target, 'file.find', ["/root/${reports_dir_name}", '\\*', 'delete']) |
| 222 | salt.runSaltProcessStep(master, target, 'file.remove', ["/root/${archive_artifacts_name}"]) |
| 223 | } |
| 224 | |
| 225 | |
| 226 | /** Cleanup: Remove Docker container |
| 227 | * |
| 228 | * @param target Target node to remove Docker container |
| 229 | * @param image_link The link of the Docker image that was used for the container |
| 230 | */ |
| 231 | def removeDockerContainer(master, target, image_link) { |
| 232 | def salt = new com.mirantis.mk.Salt() |
| 233 | salt.cmdRun(master, target, "docker stop \$(docker ps -a | grep ${image_link} | awk '{print \$1}')") |
| 234 | salt.cmdRun(master, target, "docker rm \$(docker ps -a | grep ${image_link} | awk '{print \$1}')") |
| 235 | } |