blob: 1c596a2759dc655116bdf8e968a6c78d7b572eb9 [file] [log] [blame]
Jakub Josef79ecec32017-02-17 14:36:28 +01001package com.mirantis.mk
2
3/**
4 *
5 * Tests providing functions
6 *
7 */
8
9/**
10 * Run e2e conformance tests
11 *
Tetiana Korchakf500ab92017-09-27 14:53:51 -070012 * @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 Josef79ecec32017-02-17 14:36:28 +010016 */
Tetiana Korchakf500ab92017-09-27 14:53:51 -070017def runConformanceTests(master, target, k8s_api, image, timeout=2400) {
Jakub Josef79ecec32017-02-17 14:36:28 +010018 def salt = new com.mirantis.mk.Salt()
Matthew Mosesohne5c07e82017-06-14 11:55:01 +030019 def containerName = 'conformance_tests'
Tomáš Kukrála7318f52017-04-21 16:15:29 +020020 def outfile = "/tmp/" + image.replaceAll('/', '-') + '.output'
Tetiana Korchakf500ab92017-09-27 14:53:51 -070021 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 Mosesohne5c07e82017-06-14 11:55:01 +030023 sleep(10)
24
25 print("Waiting for tests to run...")
Tetiana Korchakf500ab92017-09-27 14:53:51 -070026 salt.runSaltProcessStep(master, target, 'cmd.run', ["docker wait ${containerName}"], null, false, timeout)
Matthew Mosesohne5c07e82017-06-14 11:55:01 +030027
28 print("Writing test results to output file...")
Tetiana Korchakf500ab92017-09-27 14:53:51 -070029 salt.runSaltProcessStep(master, target, 'cmd.run', ["docker logs -t ${containerName} > ${outfile}"])
Tomáš Kukrál798b3f52017-04-28 13:19:32 +020030 print("Conformance test output saved in " + outfile)
Tatyana Leontovichc73d63c2017-02-28 14:41:38 +020031}
32
33/**
34 * Copy test output to cfg node
35 *
36 * @param image Docker image with tests
37 */
38def 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 Ryzhenkinc5b30292017-02-21 19:26:24 +040043/**
44 * Execute tempest tests
45 *
Tatyana Leontovich060e1152017-07-10 17:25:37 +030046 * @param dockerImageLink Docker image link with rally and tempest
47 * @param target Host to run tests
Mykyta Karpin80f527e2017-08-14 15:18:03 +030048 * @param pattern If not false, will run only tests matched the pattern
49 * @param logDir Directory to store tempest/rally reports
Victor Ryzhenkinc5b30292017-02-21 19:26:24 +040050 */
Consatntine Kalinovskiy84634752017-08-30 15:31:26 +030051def runTempestTests(master, dockerImageLink, target, pattern = "false", logDir = "/home/rally/rally_reports/",
52 doCleanupResources = "false") {
Victor Ryzhenkinc5b30292017-02-21 19:26:24 +040053 def salt = new com.mirantis.mk.Salt()
Ievgeniia Zadorozhna5b970232017-09-13 13:31:43 +030054 salt.runSaltProcessStep(master, target, 'file.mkdir', ["/root/rally_reports"])
Tatyana Leontovich060e1152017-07-10 17:25:37 +030055 if (pattern == "false") {
Mykyta Karpin07ba87f2017-07-27 13:56:33 +030056 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 Pavlik0bfc2632017-08-18 10:07:09 +020060 "-e LOG_DIR=${logDir} " +
Consatntine Kalinovskiy84634752017-08-30 15:31:26 +030061 "-e DO_CLEANUP_RESOURCES=${doCleanupResources} " +
Mykyta Karpin07ba87f2017-07-27 13:56:33 +030062 "-v /root/:/home/rally ${dockerImageLink} >> docker-tempest.log")
63 }
Tatyana Leontovich060e1152017-07-10 17:25:37 +030064 else {
Mykyta Karpin07ba87f2017-07-27 13:56:33 +030065 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 Karpin80f527e2017-08-14 15:18:03 +030069 "-e LOG_DIR=${logDir} " +
Consatntine Kalinovskiy84634752017-08-30 15:31:26 +030070 "-e DO_CLEANUP_RESOURCES=${doCleanupResources} " +
Mykyta Karpin07ba87f2017-07-27 13:56:33 +030071 "-e CUSTOM='--pattern ${pattern}' " +
72 "-v /root/:/home/rally ${dockerImageLink} >> docker-tempest.log")
73 }
Victor Ryzhenkinc5b30292017-02-21 19:26:24 +040074}
75
Consatntine Kalinovskiy84634752017-08-30 15:31:26 +030076
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 */
86def 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 Zadorozhna5b970232017-09-13 13:31:43 +030089 salt.runSaltProcessStep(master, target, 'file.mkdir', ["/root/rally_reports"])
Consatntine Kalinovskiy84634752017-08-30 15:31:26 +030090 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 Ryzhenkinc5b30292017-02-21 19:26:24 +0400102/**
Vasyl Saienkod1dd1332017-08-03 15:22:42 +0300103 * Upload results to cfg01 node
Victor Ryzhenkinc5b30292017-02-21 19:26:24 +0400104 *
105 */
Tatyana Leontovich060e1152017-07-10 17:25:37 +0300106def copyTempestResults(master, target) {
Victor Ryzhenkinc5b30292017-02-21 19:26:24 +0400107 def salt = new com.mirantis.mk.Salt()
Vasyl Saienkod1dd1332017-08-03 15:22:42 +0300108 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 Ryzhenkinc5b30292017-02-21 19:26:24 +0400112}
113
114
Tatyana Leontovich01704b32017-03-06 12:26:33 +0200115/** Store tests results on host
Victor Ryzhenkinc5b30292017-02-21 19:26:24 +0400116 *
Tatyana Leontovich01704b32017-03-06 12:26:33 +0200117 * @param image Docker image name
118 */
119def 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 Leontovich060e1152017-07-10 17:25:37 +0300123
124
125/** Install docker if needed
126 *
127 * @param target Target node to install docker pkg
128 */
129def install_docker(master, target) {
130 def salt = new com.mirantis.mk.Salt()
131 salt.runSaltProcessStep(master, "${target}", 'pkg.install', ["docker.io"])
132}
Vasyl Saienkodf02e9d2017-08-04 09:55:13 +0300133
Consatntine Kalinovskiy84634752017-08-30 15:31:26 +0300134
Mykyta Karpin94d82a82017-08-08 19:03:36 +0300135/** 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
149def 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 Karpina5761a22017-08-16 16:09:56 +0300160 "-e TEST_GROUP=${testGroup} " +
Mykyta Karpin94d82a82017-08-08 19:03:36 +0300161 "${image}"
162 if (master == null) {
163 sh("${command}")
164 } else {
165 salt.cmdRun(master, "${target}", "${command}")
166 }
167}
168
Vasyl Saienkodf02e9d2017-08-04 09:55:13 +0300169/** 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
176def 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 Karpinf4be9e22017-08-09 18:59:57 +0300182 salt.runSaltProcessStep(master, "${target}", 'cmd.run', ["tar -cf /root/${output_file} -C ${reports_dir} ."])
Vasyl Saienkodf02e9d2017-08-04 09:55:13 +0300183 sh "mkdir -p ${artifacts_dir}"
184
Mykyta Karpinf4be9e22017-08-09 18:59:57 +0300185 encoded = salt.cmdRun(master, target, "cat /root/${output_file}", true, null, false)['return'][0].values()[0].replaceAll('Salt command execution success','')
Vasyl Saienkodf02e9d2017-08-04 09:55:13 +0300186
187 writeFile file: "${artifacts_dir}${output_file}", text: encoded
188
189 // collect artifacts
190 archiveArtifacts artifacts: "${artifacts_dir}${output_file}"
191}
Jakub Josef1462c4b2017-08-18 11:15:03 +0200192/**
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
198def 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 Kalinovskiy84634752017-08-30 15:31:26 +0300211
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 */
219def 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 */
231def 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}