blob: f2fcc7e3c9f8c197410640d34e851e26de2da0f9 [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 *
12 * @param k8s_api Kubernetes api address
13 * @param image Docker image with tests
Matthew Mosesohne5c07e82017-06-14 11:55:01 +030014 * @param timeout Timeout waiting for e2e conformance tests
Jakub Josef79ecec32017-02-17 14:36:28 +010015 */
Matthew Mosesohne5c07e82017-06-14 11:55:01 +030016def runConformanceTests(master, k8s_api, image, timeout=2400) {
Jakub Josef79ecec32017-02-17 14:36:28 +010017 def salt = new com.mirantis.mk.Salt()
Matthew Mosesohne5c07e82017-06-14 11:55:01 +030018 def containerName = 'conformance_tests'
Tomáš Kukrála7318f52017-04-21 16:15:29 +020019 def outfile = "/tmp/" + image.replaceAll('/', '-') + '.output'
Matthew Mosesohne5c07e82017-06-14 11:55:01 +030020 salt.cmdRun(master, 'ctl01*', "docker rm -f ${containerName}", false)
21 salt.cmdRun(master, 'ctl01*', "docker run -d --name ${containerName} --net=host -e API_SERVER=${k8s_api} ${image}")
22 sleep(10)
23
24 print("Waiting for tests to run...")
25 salt.runSaltProcessStep(master, 'ctl01*', 'cmd.run', ["docker wait ${containerName}"], null, false, timeout)
26
27 print("Writing test results to output file...")
28 salt.runSaltProcessStep(master, 'ctl01*', 'cmd.run', ["docker logs -t ${containerName} &> ${outfile}"])
29
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()
Tatyana Leontovich060e1152017-07-10 17:25:37 +030054 if (pattern == "false") {
Mykyta Karpin07ba87f2017-07-27 13:56:33 +030055 salt.cmdRun(master, "${target}", "docker run --rm --net=host " +
56 "-e TEMPEST_CONF=mcp.conf " +
57 "-e SKIP_LIST=mcp_skip.list " +
58 "-e SOURCE_FILE=keystonercv3 " +
Jakub Pavlik0bfc2632017-08-18 10:07:09 +020059 "-e LOG_DIR=${logDir} " +
Consatntine Kalinovskiy84634752017-08-30 15:31:26 +030060 "-e DO_CLEANUP_RESOURCES=${doCleanupResources} " +
Mykyta Karpin07ba87f2017-07-27 13:56:33 +030061 "-v /root/:/home/rally ${dockerImageLink} >> docker-tempest.log")
62 }
Tatyana Leontovich060e1152017-07-10 17:25:37 +030063 else {
Mykyta Karpin07ba87f2017-07-27 13:56:33 +030064 salt.cmdRun(master, "${target}", "docker run --rm --net=host " +
65 "-e TEMPEST_CONF=mcp.conf " +
66 "-e SKIP_LIST=mcp_skip.list " +
67 "-e SOURCE_FILE=keystonercv3 " +
Mykyta Karpin80f527e2017-08-14 15:18:03 +030068 "-e LOG_DIR=${logDir} " +
Consatntine Kalinovskiy84634752017-08-30 15:31:26 +030069 "-e DO_CLEANUP_RESOURCES=${doCleanupResources} " +
Mykyta Karpin07ba87f2017-07-27 13:56:33 +030070 "-e CUSTOM='--pattern ${pattern}' " +
71 "-v /root/:/home/rally ${dockerImageLink} >> docker-tempest.log")
72 }
Victor Ryzhenkinc5b30292017-02-21 19:26:24 +040073}
74
Consatntine Kalinovskiy84634752017-08-30 15:31:26 +030075
76/**
77 * Execute Rally scenarios
78 *
79 * @param dockerImageLink Docker image link with rally and tempest
80 * @param target Host to run scenarios
81 * @param scenario Specify the scenario as a string
82 * @param containerName Docker container name
83 * @param doCleanupResources Do run clean-up script after tests? Cleans up OpenStack test resources
84 */
85def runRallyScenarios(master, dockerImageLink, target, scenario, logDir = "/home/rally/rally_reports/",
86 doCleanupResources = "false", containerName = "rally_ci") {
87 def salt = new com.mirantis.mk.Salt()
88 salt.cmdRun(master, target, "docker run --net=host -dit " +
89 "--name ${containerName} " +
90 "-e SOURCE_FILE=keystonercv3 " +
91 "-e SCENARIO=${scenario} " +
92 "-e DO_CLEANUP_RESOURCES=${doCleanupResources} " +
93 "-e LOG_DIR=${logDir} " +
94 "--entrypoint /bin/bash -v /root/:/home/rally ${dockerImageLink}")
95 salt.cmdRun(master, target, "docker exec ${containerName} " +
96 "bash -c /usr/bin/run-rally | tee -a docker-rally.log")
97}
98
99
Victor Ryzhenkinc5b30292017-02-21 19:26:24 +0400100/**
Vasyl Saienkod1dd1332017-08-03 15:22:42 +0300101 * Upload results to cfg01 node
Victor Ryzhenkinc5b30292017-02-21 19:26:24 +0400102 *
103 */
Tatyana Leontovich060e1152017-07-10 17:25:37 +0300104def copyTempestResults(master, target) {
Victor Ryzhenkinc5b30292017-02-21 19:26:24 +0400105 def salt = new com.mirantis.mk.Salt()
Vasyl Saienkod1dd1332017-08-03 15:22:42 +0300106 if (! target.contains('cfg')) {
107 salt.runSaltProcessStep(master, "${target}", 'cmd.run', ["mkdir /root/rally_reports/ && " +
108 "rsync -av /root/rally_reports/ cfg01:/root/rally_reports/"])
109 }
Victor Ryzhenkinc5b30292017-02-21 19:26:24 +0400110}
111
112
Tatyana Leontovich01704b32017-03-06 12:26:33 +0200113/** Store tests results on host
Victor Ryzhenkinc5b30292017-02-21 19:26:24 +0400114 *
Tatyana Leontovich01704b32017-03-06 12:26:33 +0200115 * @param image Docker image name
116 */
117def catTestsOutput(master, image) {
118 def salt = new com.mirantis.mk.Salt()
119 salt.runSaltProcessStep(master, 'cfg01*', 'cmd.run', ["cat /home/ubuntu/${image}.output"])
120}
Tatyana Leontovich060e1152017-07-10 17:25:37 +0300121
122
123/** Install docker if needed
124 *
125 * @param target Target node to install docker pkg
126 */
127def install_docker(master, target) {
128 def salt = new com.mirantis.mk.Salt()
129 salt.runSaltProcessStep(master, "${target}", 'pkg.install', ["docker.io"])
130}
Vasyl Saienkodf02e9d2017-08-04 09:55:13 +0300131
Consatntine Kalinovskiy84634752017-08-30 15:31:26 +0300132
Mykyta Karpin94d82a82017-08-08 19:03:36 +0300133/** Upload Tempest test results to Testrail
134 *
135 * @param report Source report to upload
136 * @param image Testrail reporter image
137 * @param testGroup Testrail test group
138 * @param credentialsId Testrail credentials id
139 * @param plan Testrail test plan
140 * @param milestone Testrail test milestone
141 * @param suite Testrail test suite
142 * @param type Use local shell or remote salt connection
143 * @param master Salt connection.
144 * @param target Target node to install docker pkg
145 */
146
147def uploadResultsTestrail(report, image, testGroup, credentialsId, plan, milestone, suite, master = null, target = 'cfg01*') {
148 def salt = new com.mirantis.mk.Salt()
149 def common = new com.mirantis.mk.Common()
150 creds = common.getPasswordCredentials(credentialsId)
151 command = "docker run --rm --net=host " +
152 "-v ${report}:/srv/report.xml " +
153 "-e TESTRAIL_USER=${creds.username} " +
154 "-e PASS=${creds.password.toString()} " +
155 "-e TESTRAIL_PLAN_NAME=${plan} " +
156 "-e TESTRAIL_MILESTONE=${milestone} " +
157 "-e TESTRAIL_SUITE=${suite} " +
Mykyta Karpina5761a22017-08-16 16:09:56 +0300158 "-e TEST_GROUP=${testGroup} " +
Mykyta Karpin94d82a82017-08-08 19:03:36 +0300159 "${image}"
160 if (master == null) {
161 sh("${command}")
162 } else {
163 salt.cmdRun(master, "${target}", "${command}")
164 }
165}
166
Vasyl Saienkodf02e9d2017-08-04 09:55:13 +0300167/** Archive Rally results in Artifacts
168 *
169 * @param master Salt connection.
170 * @param target Target node to install docker pkg
171 * @param reports_dir Source directory to archive
172 */
173
174def archiveRallyArtifacts(master, target, reports_dir='/root/rally_reports') {
175 def salt = new com.mirantis.mk.Salt()
176
177 def artifacts_dir = '_artifacts/'
178 def output_file = 'rally_reports.tar'
179
Mykyta Karpinf4be9e22017-08-09 18:59:57 +0300180 salt.runSaltProcessStep(master, "${target}", 'cmd.run', ["tar -cf /root/${output_file} -C ${reports_dir} ."])
Vasyl Saienkodf02e9d2017-08-04 09:55:13 +0300181 sh "mkdir -p ${artifacts_dir}"
182
Mykyta Karpinf4be9e22017-08-09 18:59:57 +0300183 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 +0300184
185 writeFile file: "${artifacts_dir}${output_file}", text: encoded
186
187 // collect artifacts
188 archiveArtifacts artifacts: "${artifacts_dir}${output_file}"
189}
Jakub Josef1462c4b2017-08-18 11:15:03 +0200190/**
191 * Helper function for collecting junit tests results
192 * @param testResultAction - test result from build - use: currentBuild.rawBuild.getAction(AbstractTestResultAction.class)
193 * @return resultMap with structure ["total": total, "passed": passed, "skipped": skipped, "failed": failed]
194 */
195@NonCPS
196def collectJUnitResults(testResultAction) {
197 if (testResultAction != null) {
198 def total = testResultAction.totalCount
199 def failed = testResultAction.failCount
200 def skipped = testResultAction.skipCount
201 def passed = total - failed - skipped
202 return ["total": total, "passed": passed, "skipped": skipped, "failed": failed]
203 }else{
204 def common = new com.mirantis.mk.Common()
205 common.errorMsg("Cannot collect jUnit tests results, given result is null")
206 }
207 return [:]
208}
Consatntine Kalinovskiy84634752017-08-30 15:31:26 +0300209
210
211/** Cleanup: Remove reports directory
212 *
213 * @param target Target node to remove repo
214 * @param reports_dir_name Reports directory name to be removed (that is in /root/ on target node)
215 * @param archive_artifacts_name Archive of the artifacts
216 */
217def removeReports(master, target, reports_dir_name = 'rally_reports', archive_artifacts_name = 'rally_reports.tar') {
218 def salt = new com.mirantis.mk.Salt()
219 salt.runSaltProcessStep(master, target, 'file.find', ["/root/${reports_dir_name}", '\\*', 'delete'])
220 salt.runSaltProcessStep(master, target, 'file.remove', ["/root/${archive_artifacts_name}"])
221}
222
223
224/** Cleanup: Remove Docker container
225 *
226 * @param target Target node to remove Docker container
227 * @param image_link The link of the Docker image that was used for the container
228 */
229def removeDockerContainer(master, target, image_link) {
230 def salt = new com.mirantis.mk.Salt()
231 salt.cmdRun(master, target, "docker stop \$(docker ps -a | grep ${image_link} | awk '{print \$1}')")
232 salt.cmdRun(master, target, "docker rm \$(docker ps -a | grep ${image_link} | awk '{print \$1}')")
233}