blob: a7bacb15279693086f5a26b85517ecb7ce0314c8 [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
vrovachevc3b47f42018-01-25 16:08:50 +040033
34/**
35 * Upload conformance results to cfg node
36 *
37 * @param target Kubernetes node for copy test results
38 * @param artifacts_dir Path with test results
39 */
40def CopyConformanceResults(master, target, artifacts_dir, output_file) {
41 def salt = new com.mirantis.mk.Salt()
42 def containerName = 'conformance_tests'
43 def test_node = target.replace("*", "")
44
45 out = salt.runSaltProcessStep(master, target, 'cmd.run', ["docker cp ${containerName}:/report /tmp"])
46 if (! out['return'][0].values()[0].contains('Error')) {
47 print("Copy XML test results for junit artifacts...")
48 salt.runSaltProcessStep(master, target, 'cmd.run', ["tar -cf /tmp/${output_file} -C /tmp/report ."])
49
50 writeFile file: "${artifacts_dir}${output_file}", text: salt.getFileContent(master,
51 target, "/tmp/${output_file}")
52
53 sh "mkdir -p ${artifacts_dir}/conformance_tests"
54 sh "tar -xf ${artifacts_dir}${output_file} -C ${artifacts_dir}/conformance_tests"
55
56 // collect artifacts
57 archiveArtifacts artifacts: "${artifacts_dir}${output_file}"
58
59 junit(keepLongStdio: true, testResults: "${artifacts_dir}conformance_tests/**.xml")
60 }
61}
62
Tatyana Leontovichc73d63c2017-02-28 14:41:38 +020063/**
64 * Copy test output to cfg node
65 *
66 * @param image Docker image with tests
67 */
68def copyTestsOutput(master, image) {
69 def salt = new com.mirantis.mk.Salt()
70 salt.runSaltProcessStep(master, 'cfg01*', 'cmd.run', ["scp ctl01:/root/${image}.output /home/ubuntu/"])
71}
72
Victor Ryzhenkinc5b30292017-02-21 19:26:24 +040073/**
74 * Execute tempest tests
75 *
Tatyana Leontovich060e1152017-07-10 17:25:37 +030076 * @param dockerImageLink Docker image link with rally and tempest
77 * @param target Host to run tests
Mykyta Karpin80f527e2017-08-14 15:18:03 +030078 * @param pattern If not false, will run only tests matched the pattern
79 * @param logDir Directory to store tempest/rally reports
Oleksandr Kosse74bbab72017-10-11 13:48:18 +030080 * @param sourceFile Path to the keystonerc file in the container
81 * @param set Predefined set for tempest tests
82 * @param concurrency How many processes to use to run Tempest tests
83 * @param tempestConf A tempest.conf's file name
84 * @param skipList A skip.list's file name
85 * @param localKeystone Path to the keystonerc file in the local host
86 * @param localLogDir Path to local destination folder for logs
Victor Ryzhenkinc5b30292017-02-21 19:26:24 +040087 */
ibumarskov374188e2017-12-14 10:54:09 +040088def runTempestTests(master, dockerImageLink, target, pattern = "", logDir = "/home/rally/rally_reports/",
Oleksandr Kosse74bbab72017-10-11 13:48:18 +030089 sourceFile="/home/rally/keystonercv3", set="full", concurrency="0", tempestConf="mcp.conf",
90 skipList="mcp_skip.list", localKeystone="/root/keystonercv3" , localLogDir="/root/rally_reports",
Consatntine Kalinovskiy84634752017-08-30 15:31:26 +030091 doCleanupResources = "false") {
Victor Ryzhenkinc5b30292017-02-21 19:26:24 +040092 def salt = new com.mirantis.mk.Salt()
Oleksandr Kosse74bbab72017-10-11 13:48:18 +030093 salt.runSaltProcessStep(master, target, 'file.mkdir', ["${localLogDir}"])
94 def custom = ''
ibumarskov374188e2017-12-14 10:54:09 +040095 if (pattern) {
Oleksandr Kosse74bbab72017-10-11 13:48:18 +030096 custom = "--pattern " + pattern
Mykyta Karpin07ba87f2017-07-27 13:56:33 +030097 }
Oleksandr Kosse74bbab72017-10-11 13:48:18 +030098 salt.cmdRun(master, "${target}", "docker run --rm --net=host " +
99 "-e SOURCE_FILE=${sourceFile} " +
100 "-e LOG_DIR=${logDir} " +
101 "-e SET=${set} " +
Mykyta Karpin292c0662017-11-13 12:07:52 +0200102 "-e CUSTOM='${custom}' " +
Oleksandr Kosse74bbab72017-10-11 13:48:18 +0300103 "-e CONCURRENCY=${concurrency} " +
104 "-e TEMPEST_CONF=${tempestConf} " +
105 "-e SKIP_LIST=${skipList} " +
106 "-e DO_CLEANUP_RESOURCES=${doCleanupResources} " +
107 "-v ${localKeystone}:${sourceFile} " +
108 "-v ${localLogDir}:/home/rally/rally_reports " +
109 "-v /etc/ssl/certs/:/etc/ssl/certs/ " +
110 "${dockerImageLink} >> docker-tempest.log")
Victor Ryzhenkinc5b30292017-02-21 19:26:24 +0400111}
112
Consatntine Kalinovskiy84634752017-08-30 15:31:26 +0300113
114/**
115 * Execute Rally scenarios
116 *
117 * @param dockerImageLink Docker image link with rally and tempest
118 * @param target Host to run scenarios
119 * @param scenario Specify the scenario as a string
120 * @param containerName Docker container name
121 * @param doCleanupResources Do run clean-up script after tests? Cleans up OpenStack test resources
122 */
123def runRallyScenarios(master, dockerImageLink, target, scenario, logDir = "/home/rally/rally_reports/",
124 doCleanupResources = "false", containerName = "rally_ci") {
125 def salt = new com.mirantis.mk.Salt()
Ievgeniia Zadorozhna5b970232017-09-13 13:31:43 +0300126 salt.runSaltProcessStep(master, target, 'file.mkdir', ["/root/rally_reports"])
Consatntine Kalinovskiy84634752017-08-30 15:31:26 +0300127 salt.cmdRun(master, target, "docker run --net=host -dit " +
128 "--name ${containerName} " +
129 "-e SOURCE_FILE=keystonercv3 " +
130 "-e SCENARIO=${scenario} " +
131 "-e DO_CLEANUP_RESOURCES=${doCleanupResources} " +
132 "-e LOG_DIR=${logDir} " +
133 "--entrypoint /bin/bash -v /root/:/home/rally ${dockerImageLink}")
134 salt.cmdRun(master, target, "docker exec ${containerName} " +
135 "bash -c /usr/bin/run-rally | tee -a docker-rally.log")
136}
137
138
Victor Ryzhenkinc5b30292017-02-21 19:26:24 +0400139/**
Vasyl Saienkod1dd1332017-08-03 15:22:42 +0300140 * Upload results to cfg01 node
Victor Ryzhenkinc5b30292017-02-21 19:26:24 +0400141 *
142 */
Tatyana Leontovich060e1152017-07-10 17:25:37 +0300143def copyTempestResults(master, target) {
Victor Ryzhenkinc5b30292017-02-21 19:26:24 +0400144 def salt = new com.mirantis.mk.Salt()
Vasyl Saienkod1dd1332017-08-03 15:22:42 +0300145 if (! target.contains('cfg')) {
ibumarskov5c31e282018-02-14 15:05:20 +0400146 salt.cmdRun(master, target, "mkdir -p /root/rally_reports/ && rsync -av /root/rally_reports/ cfg01:/root/rally_reports/")
Vasyl Saienkod1dd1332017-08-03 15:22:42 +0300147 }
Victor Ryzhenkinc5b30292017-02-21 19:26:24 +0400148}
149
150
Tatyana Leontovich01704b32017-03-06 12:26:33 +0200151/** Store tests results on host
Victor Ryzhenkinc5b30292017-02-21 19:26:24 +0400152 *
Tatyana Leontovich01704b32017-03-06 12:26:33 +0200153 * @param image Docker image name
154 */
155def catTestsOutput(master, image) {
156 def salt = new com.mirantis.mk.Salt()
Jakub Josef432e9d92018-02-06 18:28:37 +0100157 salt.cmdRun(master, 'cfg01*', "cat /home/ubuntu/${image}.output")
Tatyana Leontovich01704b32017-03-06 12:26:33 +0200158}
Tatyana Leontovich060e1152017-07-10 17:25:37 +0300159
160
161/** Install docker if needed
162 *
163 * @param target Target node to install docker pkg
164 */
165def install_docker(master, target) {
166 def salt = new com.mirantis.mk.Salt()
Jakub Josef432e9d92018-02-06 18:28:37 +0100167 salt.runSaltProcessStep(master, target, 'pkg.install', ["docker.io"])
Tatyana Leontovich060e1152017-07-10 17:25:37 +0300168}
Vasyl Saienkodf02e9d2017-08-04 09:55:13 +0300169
Consatntine Kalinovskiy84634752017-08-30 15:31:26 +0300170
Mykyta Karpin94d82a82017-08-08 19:03:36 +0300171/** Upload Tempest test results to Testrail
172 *
173 * @param report Source report to upload
174 * @param image Testrail reporter image
175 * @param testGroup Testrail test group
176 * @param credentialsId Testrail credentials id
177 * @param plan Testrail test plan
178 * @param milestone Testrail test milestone
179 * @param suite Testrail test suite
180 * @param type Use local shell or remote salt connection
181 * @param master Salt connection.
182 * @param target Target node to install docker pkg
183 */
184
185def uploadResultsTestrail(report, image, testGroup, credentialsId, plan, milestone, suite, master = null, target = 'cfg01*') {
186 def salt = new com.mirantis.mk.Salt()
187 def common = new com.mirantis.mk.Common()
188 creds = common.getPasswordCredentials(credentialsId)
189 command = "docker run --rm --net=host " +
190 "-v ${report}:/srv/report.xml " +
191 "-e TESTRAIL_USER=${creds.username} " +
192 "-e PASS=${creds.password.toString()} " +
193 "-e TESTRAIL_PLAN_NAME=${plan} " +
194 "-e TESTRAIL_MILESTONE=${milestone} " +
195 "-e TESTRAIL_SUITE=${suite} " +
Mykyta Karpina5761a22017-08-16 16:09:56 +0300196 "-e TEST_GROUP=${testGroup} " +
Mykyta Karpin94d82a82017-08-08 19:03:36 +0300197 "${image}"
198 if (master == null) {
Jakub Josef432e9d92018-02-06 18:28:37 +0100199 sh(command)
Mykyta Karpin94d82a82017-08-08 19:03:36 +0300200 } else {
Jakub Josef432e9d92018-02-06 18:28:37 +0100201 salt.cmdRun(master, target, command)
Mykyta Karpin94d82a82017-08-08 19:03:36 +0300202 }
203}
204
Vasyl Saienkodf02e9d2017-08-04 09:55:13 +0300205/** Archive Rally results in Artifacts
206 *
207 * @param master Salt connection.
208 * @param target Target node to install docker pkg
209 * @param reports_dir Source directory to archive
210 */
211
212def archiveRallyArtifacts(master, target, reports_dir='/root/rally_reports') {
213 def salt = new com.mirantis.mk.Salt()
214
215 def artifacts_dir = '_artifacts/'
216 def output_file = 'rally_reports.tar'
217
Jakub Josef432e9d92018-02-06 18:28:37 +0100218 salt.cmdRun(master, target, "tar -cf /root/${output_file} -C ${reports_dir} .")
Vasyl Saienkodf02e9d2017-08-04 09:55:13 +0300219 sh "mkdir -p ${artifacts_dir}"
220
Mykyta Karpinf4be9e22017-08-09 18:59:57 +0300221 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 +0300222
223 writeFile file: "${artifacts_dir}${output_file}", text: encoded
224
225 // collect artifacts
226 archiveArtifacts artifacts: "${artifacts_dir}${output_file}"
227}
Jakub Josef1462c4b2017-08-18 11:15:03 +0200228/**
229 * Helper function for collecting junit tests results
230 * @param testResultAction - test result from build - use: currentBuild.rawBuild.getAction(AbstractTestResultAction.class)
231 * @return resultMap with structure ["total": total, "passed": passed, "skipped": skipped, "failed": failed]
232 */
233@NonCPS
234def collectJUnitResults(testResultAction) {
235 if (testResultAction != null) {
236 def total = testResultAction.totalCount
237 def failed = testResultAction.failCount
238 def skipped = testResultAction.skipCount
239 def passed = total - failed - skipped
240 return ["total": total, "passed": passed, "skipped": skipped, "failed": failed]
241 }else{
242 def common = new com.mirantis.mk.Common()
243 common.errorMsg("Cannot collect jUnit tests results, given result is null")
244 }
245 return [:]
246}
Consatntine Kalinovskiy84634752017-08-30 15:31:26 +0300247
248
249/** Cleanup: Remove reports directory
250 *
251 * @param target Target node to remove repo
252 * @param reports_dir_name Reports directory name to be removed (that is in /root/ on target node)
253 * @param archive_artifacts_name Archive of the artifacts
254 */
255def removeReports(master, target, reports_dir_name = 'rally_reports', archive_artifacts_name = 'rally_reports.tar') {
256 def salt = new com.mirantis.mk.Salt()
257 salt.runSaltProcessStep(master, target, 'file.find', ["/root/${reports_dir_name}", '\\*', 'delete'])
258 salt.runSaltProcessStep(master, target, 'file.remove', ["/root/${archive_artifacts_name}"])
259}
260
261
262/** Cleanup: Remove Docker container
263 *
264 * @param target Target node to remove Docker container
265 * @param image_link The link of the Docker image that was used for the container
266 */
Ievgeniia Zadorozhnaffea7ef2017-10-31 16:18:46 +0300267def removeDockerContainer(master, target, containerName) {
Consatntine Kalinovskiy84634752017-08-30 15:31:26 +0300268 def salt = new com.mirantis.mk.Salt()
Ievgeniia Zadorozhnaffea7ef2017-10-31 16:18:46 +0300269 salt.cmdRun(master, target, "docker rm -f ${containerName}")
Oleh Hryhorov83658332017-10-10 10:45:12 +0300270}