blob: 0355544bcbac4a374506fa746a247e8f1a16e70a [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
Oleksandr Kosse74bbab72017-10-11 13:48:18 +030050 * @param sourceFile Path to the keystonerc file in the container
51 * @param set Predefined set for tempest tests
52 * @param concurrency How many processes to use to run Tempest tests
53 * @param tempestConf A tempest.conf's file name
54 * @param skipList A skip.list's file name
55 * @param localKeystone Path to the keystonerc file in the local host
56 * @param localLogDir Path to local destination folder for logs
Victor Ryzhenkinc5b30292017-02-21 19:26:24 +040057 */
Consatntine Kalinovskiy84634752017-08-30 15:31:26 +030058def runTempestTests(master, dockerImageLink, target, pattern = "false", logDir = "/home/rally/rally_reports/",
Oleksandr Kosse74bbab72017-10-11 13:48:18 +030059 sourceFile="/home/rally/keystonercv3", set="full", concurrency="0", tempestConf="mcp.conf",
60 skipList="mcp_skip.list", localKeystone="/root/keystonercv3" , localLogDir="/root/rally_reports",
Consatntine Kalinovskiy84634752017-08-30 15:31:26 +030061 doCleanupResources = "false") {
Victor Ryzhenkinc5b30292017-02-21 19:26:24 +040062 def salt = new com.mirantis.mk.Salt()
Oleksandr Kosse74bbab72017-10-11 13:48:18 +030063 salt.runSaltProcessStep(master, target, 'file.mkdir', ["${localLogDir}"])
64 def custom = ''
65 if (pattern != "false") {
66 custom = "--pattern " + pattern
Mykyta Karpin07ba87f2017-07-27 13:56:33 +030067 }
Oleksandr Kosse74bbab72017-10-11 13:48:18 +030068 salt.cmdRun(master, "${target}", "docker run --rm --net=host " +
69 "-e SOURCE_FILE=${sourceFile} " +
70 "-e LOG_DIR=${logDir} " +
71 "-e SET=${set} " +
72 "-e CUSTOM=\"${custom}\" " +
73 "-e CONCURRENCY=${concurrency} " +
74 "-e TEMPEST_CONF=${tempestConf} " +
75 "-e SKIP_LIST=${skipList} " +
76 "-e DO_CLEANUP_RESOURCES=${doCleanupResources} " +
77 "-v ${localKeystone}:${sourceFile} " +
78 "-v ${localLogDir}:/home/rally/rally_reports " +
79 "-v /etc/ssl/certs/:/etc/ssl/certs/ " +
80 "${dockerImageLink} >> docker-tempest.log")
Victor Ryzhenkinc5b30292017-02-21 19:26:24 +040081}
82
Consatntine Kalinovskiy84634752017-08-30 15:31:26 +030083
84/**
85 * Execute Rally scenarios
86 *
87 * @param dockerImageLink Docker image link with rally and tempest
88 * @param target Host to run scenarios
89 * @param scenario Specify the scenario as a string
90 * @param containerName Docker container name
91 * @param doCleanupResources Do run clean-up script after tests? Cleans up OpenStack test resources
92 */
93def runRallyScenarios(master, dockerImageLink, target, scenario, logDir = "/home/rally/rally_reports/",
94 doCleanupResources = "false", containerName = "rally_ci") {
95 def salt = new com.mirantis.mk.Salt()
Ievgeniia Zadorozhna5b970232017-09-13 13:31:43 +030096 salt.runSaltProcessStep(master, target, 'file.mkdir', ["/root/rally_reports"])
Consatntine Kalinovskiy84634752017-08-30 15:31:26 +030097 salt.cmdRun(master, target, "docker run --net=host -dit " +
98 "--name ${containerName} " +
99 "-e SOURCE_FILE=keystonercv3 " +
100 "-e SCENARIO=${scenario} " +
101 "-e DO_CLEANUP_RESOURCES=${doCleanupResources} " +
102 "-e LOG_DIR=${logDir} " +
103 "--entrypoint /bin/bash -v /root/:/home/rally ${dockerImageLink}")
104 salt.cmdRun(master, target, "docker exec ${containerName} " +
105 "bash -c /usr/bin/run-rally | tee -a docker-rally.log")
106}
107
108
Victor Ryzhenkinc5b30292017-02-21 19:26:24 +0400109/**
Vasyl Saienkod1dd1332017-08-03 15:22:42 +0300110 * Upload results to cfg01 node
Victor Ryzhenkinc5b30292017-02-21 19:26:24 +0400111 *
112 */
Tatyana Leontovich060e1152017-07-10 17:25:37 +0300113def copyTempestResults(master, target) {
Victor Ryzhenkinc5b30292017-02-21 19:26:24 +0400114 def salt = new com.mirantis.mk.Salt()
Vasyl Saienkod1dd1332017-08-03 15:22:42 +0300115 if (! target.contains('cfg')) {
116 salt.runSaltProcessStep(master, "${target}", 'cmd.run', ["mkdir /root/rally_reports/ && " +
117 "rsync -av /root/rally_reports/ cfg01:/root/rally_reports/"])
118 }
Victor Ryzhenkinc5b30292017-02-21 19:26:24 +0400119}
120
121
Tatyana Leontovich01704b32017-03-06 12:26:33 +0200122/** Store tests results on host
Victor Ryzhenkinc5b30292017-02-21 19:26:24 +0400123 *
Tatyana Leontovich01704b32017-03-06 12:26:33 +0200124 * @param image Docker image name
125 */
126def catTestsOutput(master, image) {
127 def salt = new com.mirantis.mk.Salt()
128 salt.runSaltProcessStep(master, 'cfg01*', 'cmd.run', ["cat /home/ubuntu/${image}.output"])
129}
Tatyana Leontovich060e1152017-07-10 17:25:37 +0300130
131
132/** Install docker if needed
133 *
134 * @param target Target node to install docker pkg
135 */
136def install_docker(master, target) {
137 def salt = new com.mirantis.mk.Salt()
138 salt.runSaltProcessStep(master, "${target}", 'pkg.install', ["docker.io"])
139}
Vasyl Saienkodf02e9d2017-08-04 09:55:13 +0300140
Consatntine Kalinovskiy84634752017-08-30 15:31:26 +0300141
Mykyta Karpin94d82a82017-08-08 19:03:36 +0300142/** Upload Tempest test results to Testrail
143 *
144 * @param report Source report to upload
145 * @param image Testrail reporter image
146 * @param testGroup Testrail test group
147 * @param credentialsId Testrail credentials id
148 * @param plan Testrail test plan
149 * @param milestone Testrail test milestone
150 * @param suite Testrail test suite
151 * @param type Use local shell or remote salt connection
152 * @param master Salt connection.
153 * @param target Target node to install docker pkg
154 */
155
156def uploadResultsTestrail(report, image, testGroup, credentialsId, plan, milestone, suite, master = null, target = 'cfg01*') {
157 def salt = new com.mirantis.mk.Salt()
158 def common = new com.mirantis.mk.Common()
159 creds = common.getPasswordCredentials(credentialsId)
160 command = "docker run --rm --net=host " +
161 "-v ${report}:/srv/report.xml " +
162 "-e TESTRAIL_USER=${creds.username} " +
163 "-e PASS=${creds.password.toString()} " +
164 "-e TESTRAIL_PLAN_NAME=${plan} " +
165 "-e TESTRAIL_MILESTONE=${milestone} " +
166 "-e TESTRAIL_SUITE=${suite} " +
Mykyta Karpina5761a22017-08-16 16:09:56 +0300167 "-e TEST_GROUP=${testGroup} " +
Mykyta Karpin94d82a82017-08-08 19:03:36 +0300168 "${image}"
169 if (master == null) {
170 sh("${command}")
171 } else {
172 salt.cmdRun(master, "${target}", "${command}")
173 }
174}
175
Vasyl Saienkodf02e9d2017-08-04 09:55:13 +0300176/** Archive Rally results in Artifacts
177 *
178 * @param master Salt connection.
179 * @param target Target node to install docker pkg
180 * @param reports_dir Source directory to archive
181 */
182
183def archiveRallyArtifacts(master, target, reports_dir='/root/rally_reports') {
184 def salt = new com.mirantis.mk.Salt()
185
186 def artifacts_dir = '_artifacts/'
187 def output_file = 'rally_reports.tar'
188
Mykyta Karpinf4be9e22017-08-09 18:59:57 +0300189 salt.runSaltProcessStep(master, "${target}", 'cmd.run', ["tar -cf /root/${output_file} -C ${reports_dir} ."])
Vasyl Saienkodf02e9d2017-08-04 09:55:13 +0300190 sh "mkdir -p ${artifacts_dir}"
191
Mykyta Karpinf4be9e22017-08-09 18:59:57 +0300192 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 +0300193
194 writeFile file: "${artifacts_dir}${output_file}", text: encoded
195
196 // collect artifacts
197 archiveArtifacts artifacts: "${artifacts_dir}${output_file}"
198}
Jakub Josef1462c4b2017-08-18 11:15:03 +0200199/**
200 * Helper function for collecting junit tests results
201 * @param testResultAction - test result from build - use: currentBuild.rawBuild.getAction(AbstractTestResultAction.class)
202 * @return resultMap with structure ["total": total, "passed": passed, "skipped": skipped, "failed": failed]
203 */
204@NonCPS
205def collectJUnitResults(testResultAction) {
206 if (testResultAction != null) {
207 def total = testResultAction.totalCount
208 def failed = testResultAction.failCount
209 def skipped = testResultAction.skipCount
210 def passed = total - failed - skipped
211 return ["total": total, "passed": passed, "skipped": skipped, "failed": failed]
212 }else{
213 def common = new com.mirantis.mk.Common()
214 common.errorMsg("Cannot collect jUnit tests results, given result is null")
215 }
216 return [:]
217}
Consatntine Kalinovskiy84634752017-08-30 15:31:26 +0300218
219
220/** Cleanup: Remove reports directory
221 *
222 * @param target Target node to remove repo
223 * @param reports_dir_name Reports directory name to be removed (that is in /root/ on target node)
224 * @param archive_artifacts_name Archive of the artifacts
225 */
226def removeReports(master, target, reports_dir_name = 'rally_reports', archive_artifacts_name = 'rally_reports.tar') {
227 def salt = new com.mirantis.mk.Salt()
228 salt.runSaltProcessStep(master, target, 'file.find', ["/root/${reports_dir_name}", '\\*', 'delete'])
229 salt.runSaltProcessStep(master, target, 'file.remove', ["/root/${archive_artifacts_name}"])
230}
231
232
233/** Cleanup: Remove Docker container
234 *
235 * @param target Target node to remove Docker container
236 * @param image_link The link of the Docker image that was used for the container
237 */
238def removeDockerContainer(master, target, image_link) {
239 def salt = new com.mirantis.mk.Salt()
240 salt.cmdRun(master, target, "docker stop \$(docker ps -a | grep ${image_link} | awk '{print \$1}')")
241 salt.cmdRun(master, target, "docker rm \$(docker ps -a | grep ${image_link} | awk '{print \$1}')")
Oleh Hryhorov83658332017-10-10 10:45:12 +0300242}