blob: 9ee1eaf53e6504ab30c63034b7081ee95d5eaa47 [file] [log] [blame]
Petr Lomakin47fee0a2017-08-01 10:46:05 -07001package com.mirantis.mcp
2
3/**
4 *
5 * Tests providing functions
6 *
7 */
8
9/**
Oleksii Zhurba7b44ef12017-11-13 17:50:16 -060010 * Run docker container with basic (keystone) parameters
Petr Lomakin47fee0a2017-08-01 10:46:05 -070011 *
Oleksii Zhurba7b44ef12017-11-13 17:50:16 -060012 * @param target Host to run container
13 * @param dockerImageLink Docker image link. May be custom or default rally image
Petr Lomakin47fee0a2017-08-01 10:46:05 -070014 */
Oleksii Zhurba7b44ef12017-11-13 17:50:16 -060015def runBasicContainer(master, target, dockerImageLink="rallyforge/rally"){
Petr Lomakin47fee0a2017-08-01 10:46:05 -070016 def salt = new com.mirantis.mk.Salt()
17 def common = new com.mirantis.mk.Common()
Sam Stoelinga28bdb722017-09-25 18:29:59 -070018 def _pillar = salt.getPillar(master, 'I@keystone:server', 'keystone:server')
19 def keystone = _pillar['return'][0].values()[0]
Oleksii Zhurba7b44ef12017-11-13 17:50:16 -060020 if ( salt.cmdRun(master, target, "docker ps -f name=cvp -q", false, null, false)['return'][0].values()[0] ) {
21 salt.cmdRun(master, target, "docker rm -f cvp")
22 }
23 salt.cmdRun(master, target, "docker run -tid --net=host --name=cvp " +
24 "-u root -e OS_USERNAME=${keystone.admin_name} " +
Petr Lomakin47fee0a2017-08-01 10:46:05 -070025 "-e OS_PASSWORD=${keystone.admin_password} -e OS_TENANT_NAME=${keystone.admin_tenant} " +
26 "-e OS_AUTH_URL=http://${keystone.bind.private_address}:${keystone.bind.private_port}/v2.0 " +
Oleksii Zhurba7b44ef12017-11-13 17:50:16 -060027 "-e OS_REGION_NAME=${keystone.region} -e OS_ENDPOINT_TYPE=admin ${dockerImageLink} /bin/bash")
Petr Lomakin47fee0a2017-08-01 10:46:05 -070028}
29
30/**
Dmitrii Kabanov23901c22017-10-20 10:25:36 -070031 * Get file content (encoded). The content encoded by Base64.
Dmitrii Kabanovd5f1c5f2017-08-30 14:51:41 -070032 *
33 * @param target Compound target (should target only one host)
34 * @param file File path to read
Dmitrii Kabanov23901c22017-10-20 10:25:36 -070035 * @return The encoded content of the file
Dmitrii Kabanovd5f1c5f2017-08-30 14:51:41 -070036 */
Dmitrii Kabanov23901c22017-10-20 10:25:36 -070037def getFileContentEncoded(master, target, file) {
Dmitrii Kabanovd5f1c5f2017-08-30 14:51:41 -070038 def salt = new com.mirantis.mk.Salt()
Dmitrii Kabanov23901c22017-10-20 10:25:36 -070039 def file_content = ''
40 def cmd = "base64 -w0 ${file} > ${file}_encoded; " +
41 "split -b 1MB -d ${file}_encoded ${file}__; " +
42 "rm ${file}_encoded"
43 salt.cmdRun(master, target, cmd, false, null, false)
44 def filename = file.tokenize('/').last()
45 def folder = file - filename
46 def parts = salt.runSaltProcessStep(master, target, 'file.find', ["${folder}", "type=f", "name=${filename}__*"])
47 for ( part in parts['return'][0].values()[0]) {
48 def _result = salt.cmdRun(master, target, "cat ${part}", false, null, false)
49 file_content = file_content + _result['return'][0].values()[0].replaceAll('Salt command execution success','')
50 }
51 salt.runSaltProcessStep(master, target, 'file.find', ["${folder}", "type=f", "name=${filename}__*", "delete"])
52 return file_content
53}
54
55/**
56 * Copy files from remote to local directory. The content of files will be
57 * decoded by Base64.
58 *
59 * @param target Compound target (should target only one host)
60 * @param folder The path to remote folder.
61 * @param output_dir The path to local folder.
62 */
63def addFiles(master, target, folder, output_dir) {
64 def salt = new com.mirantis.mk.Salt()
65 def _result = salt.runSaltProcessStep(master, target, 'file.find', ["${folder}", "type=f"])
66 def files = _result['return'][0].values()[0]
67 for (file in files) {
68 def file_content = getFileContentEncoded(master, target, "${file}")
69 def fileName = file.tokenize('/').last()
70 writeFile file: "${output_dir}${fileName}_encoded", text: file_content
71 def cmd = "base64 -d ${output_dir}${fileName}_encoded > ${output_dir}${fileName}; " +
72 "rm ${output_dir}${fileName}_encoded"
73 sh(script: cmd)
Dmitrii Kabanovd5f1c5f2017-08-30 14:51:41 -070074 }
75}
76
77/**
78 * Get reclass value
79 *
80 * @param target The host for which the values will be provided
81 * @param filter Parameters divided by dots
82 * @return The pillar data
83 */
84def getReclassValue(master, target, filter) {
85 def common = new com.mirantis.mk.Common()
86 def salt = new com.mirantis.mk.Salt()
87 def items = filter.tokenize('.')
Dmitrii Kabanov23901c22017-10-20 10:25:36 -070088 def _result = salt.cmdRun(master, 'I@salt:master', "reclass-salt -o json -p ${target}", false, null, false)
Dmitrii Kabanovd5f1c5f2017-08-30 14:51:41 -070089 _result = common.parseJSON(_result['return'][0].values()[0])
Dmitrii Kabanov23901c22017-10-20 10:25:36 -070090 for (int k = 0; k < items.size(); k++) {
Dmitrii Kabanovd5f1c5f2017-08-30 14:51:41 -070091 if ( _result ) {
Dmitrii Kabanov23901c22017-10-20 10:25:36 -070092 _result = _result["${items[k]}"]
Dmitrii Kabanovd5f1c5f2017-08-30 14:51:41 -070093 }
94 }
95 return _result
96}
97
98/**
99 * Create list of nodes in JSON format.
100 *
101 * @param filter The Salt's matcher
102 * @return JSON list of nodes
103 */
104def getNodeList(master, filter = null) {
105 def salt = new com.mirantis.mk.Salt()
106 def common = new com.mirantis.mk.Common()
Dmitrii Kabanovd5f1c5f2017-08-30 14:51:41 -0700107 def nodes = []
Dmitrii Kabanovd5f1c5f2017-08-30 14:51:41 -0700108 def filtered_list = null
109 def controllers = salt.getMinions(master, 'I@nova:controller')
110 def hw_nodes = salt.getMinions(master, 'G@virtual:physical')
Dmitrii Kabanovd5f1c5f2017-08-30 14:51:41 -0700111 if ( filter ) {
112 filtered_list = salt.getMinions(master, filter)
Dmitrii Kabanovd5f1c5f2017-08-30 14:51:41 -0700113 }
114 def _result = salt.cmdRun(master, 'I@salt:master', "reclass-salt -o json -t", false, null, false)
115 def reclass_top = common.parseJSON(_result['return'][0].values()[0])
Dmitrii Kabanov23901c22017-10-20 10:25:36 -0700116 def nodesList = reclass_top['base'].keySet()
117 for (int i = 0; i < nodesList.size(); i++) {
Dmitrii Kabanovd5f1c5f2017-08-30 14:51:41 -0700118 if ( filtered_list ) {
Dmitrii Kabanov23901c22017-10-20 10:25:36 -0700119 if ( ! filtered_list.contains(nodesList[i]) ) {
Dmitrii Kabanovd5f1c5f2017-08-30 14:51:41 -0700120 continue
121 }
122 }
Dmitrii Kabanov23901c22017-10-20 10:25:36 -0700123 def ip = getReclassValue(master, nodesList[i], '_param.linux_single_interface.address')
124 def network_data = [ip: ip, name: 'management']
125 def roles = [nodesList[i].tokenize('.')[0]]
126 if ( controllers.contains(nodesList[i]) ) {
127 roles.add('controller')
Dmitrii Kabanovd5f1c5f2017-08-30 14:51:41 -0700128 }
Dmitrii Kabanov23901c22017-10-20 10:25:36 -0700129 if ( hw_nodes.contains(nodesList[i]) ) {
130 roles.add('hw_node')
Dmitrii Kabanovd5f1c5f2017-08-30 14:51:41 -0700131 }
Dmitrii Kabanov23901c22017-10-20 10:25:36 -0700132 nodes.add([id: i+1, ip: ip, roles: roles, network_data: [network_data]])
Dmitrii Kabanovd5f1c5f2017-08-30 14:51:41 -0700133 }
Dmitrii Kabanov23901c22017-10-20 10:25:36 -0700134 return common.prettify(nodes)
Dmitrii Kabanovd5f1c5f2017-08-30 14:51:41 -0700135}
136
Oleksii Zhurbabcb97e22017-10-05 14:10:39 -0500137/**
138 * Execute mcp sanity tests
139 *
140 * @param salt_url Salt master url
141 * @param salt_credentials Salt credentials
142 * @param test_set Test set for mcp sanity framework
143 * @param output_dir Directory for results
144 */
145def runSanityTests(salt_url, salt_credentials, test_set, output_dir) {
146 def common = new com.mirantis.mk.Common()
147 creds = common.getCredentials(salt_credentials)
148 username = creds.username
149 password = creds.password
150 def script = ". ${env.WORKSPACE}/venv/bin/activate; pytest --junitxml ${output_dir}cvp_sanity.xml -sv ${env.WORKSPACE}/cvp-sanity-checks/cvp_checks/tests/${test_set}"
151 withEnv(["SALT_USERNAME=${username}", "SALT_PASSWORD=${password}", "SALT_URL=${salt_url}"]) {
152 def statusCode = sh script:script, returnStatus:true
153 }
154}
Dmitrii Kabanovd5f1c5f2017-08-30 14:51:41 -0700155
156/**
Petr Lomakin47fee0a2017-08-01 10:46:05 -0700157 * Execute tempest tests
158 *
159 * @param target Host to run tests
Dmitrii Kabanov23901c22017-10-20 10:25:36 -0700160 * @param dockerImageLink Docker image link
Petr Lomakin47fee0a2017-08-01 10:46:05 -0700161 * @param pattern If not false, will run only tests matched the pattern
162 * @param output_dir Directory for results
Dmitrii Kabanov999fda92017-11-10 00:18:30 -0800163 * @param confRepository Git repository with configuration files for Tempest
164 * @param confBranch Git branch which will be used during the checkout
165 * @param repository Git repository with Tempest
166 * @param version Version of Tempest (tag, branch or commit)
Petr Lomakin47fee0a2017-08-01 10:46:05 -0700167 */
Dmitrii Kabanov999fda92017-11-10 00:18:30 -0800168def runTempestTests(master, target, dockerImageLink, output_dir, confRepository, confBranch, repository, version, pattern = "false") {
Petr Lomakin47fee0a2017-08-01 10:46:05 -0700169 def salt = new com.mirantis.mk.Salt()
170 def output_file = 'docker-tempest.log'
Dmitrii Kabanov23901c22017-10-20 10:25:36 -0700171 def results = '/root/qa_results'
172 def dest_folder = '/home/rally/qa_results'
Dmitrii Kabanov999fda92017-11-10 00:18:30 -0800173 def skip_list = '--skip-list /opt/devops-qa-tools/deployment/skip_contrail.list'
Dmitrii Kabanov23901c22017-10-20 10:25:36 -0700174 salt.runSaltProcessStep(master, target, 'file.remove', ["${results}"])
175 salt.runSaltProcessStep(master, target, 'file.mkdir', ["${results}", "mode=777"])
176 def _pillar = salt.getPillar(master, 'I@keystone:server', 'keystone:server')
177 def keystone = _pillar['return'][0].values()[0]
178 def env_vars = ['tempest_version=15.0.0',
179 "OS_USERNAME=${keystone.admin_name}",
180 "OS_PASSWORD=${keystone.admin_password}",
181 "OS_TENANT_NAME=${keystone.admin_tenant}",
182 "OS_AUTH_URL=http://${keystone.bind.private_address}:${keystone.bind.private_port}/v2.0",
183 "OS_REGION_NAME=${keystone.region}",
184 'OS_ENDPOINT_TYPE=admin'].join(' -e ')
185 def cmd = '/opt/devops-qa-tools/deployment/configure.sh; '
Dmitrii Kabanov999fda92017-11-10 00:18:30 -0800186 if (confRepository != '' ) {
187 cmd = "git clone -b ${confBranch ?: 'master'} ${confRepository} test_config; " +
188 'rally deployment create --fromenv --name=tempest; rally deployment config; ' +
189 'rally verify create-verifier --name tempest_verifier --type tempest ' +
190 "--source ${repository ?: '/tmp/tempest/'} --version ${version: '15.0.0'}; " +
191 'rally verify configure-verifier --extend test_config/tempest/tempest.conf --show; '
192 skip_list = '--skip-list test_config/tempest/skip-list.yaml'
193 }
Dmitrii Kabanov23901c22017-10-20 10:25:36 -0700194 if (pattern == 'false') {
Dmitrii Kabanov999fda92017-11-10 00:18:30 -0800195 cmd += "rally verify start --pattern set=full ${skip_list} --detailed; "
Petr Lomakin47fee0a2017-08-01 10:46:05 -0700196 }
197 else {
Dmitrii Kabanov999fda92017-11-10 00:18:30 -0800198 cmd += "rally verify start --pattern set=${pattern} ${skip_list} --detailed; "
Petr Lomakin47fee0a2017-08-01 10:46:05 -0700199 }
Dmitrii Kabanov23901c22017-10-20 10:25:36 -0700200 cmd += "rally verify report --type json --to ${dest_folder}/report-tempest.json; " +
201 "rally verify report --type html --to ${dest_folder}/report-tempest.html"
202 salt.cmdRun(master, target, "docker run -i --rm --net=host -e ${env_vars} " +
203 "-v ${results}:${dest_folder} ${dockerImageLink} " +
204 "/bin/bash -c \"${cmd}\" > ${results}/${output_file}")
205 addFiles(master, target, results, output_dir)
Petr Lomakin47fee0a2017-08-01 10:46:05 -0700206}
207
208/**
209 * Execute rally tests
210 *
211 * @param target Host to run tests
Dmitrii Kabanov23901c22017-10-20 10:25:36 -0700212 * @param dockerImageLink Docker image link
Petr Lomakin47fee0a2017-08-01 10:46:05 -0700213 * @param output_dir Directory for results
Dmitrii Kabanov999fda92017-11-10 00:18:30 -0800214 * @param repository Git repository with files for Rally
215 * @param branch Git branch which will be used during the checkout
Dmitrii Kabanov23901c22017-10-20 10:25:36 -0700216 * @param ext_variables The list of external variables
Petr Lomakin47fee0a2017-08-01 10:46:05 -0700217 */
Dmitrii Kabanov999fda92017-11-10 00:18:30 -0800218def runRallyTests(master, target, dockerImageLink, output_dir, repository, branch, ext_variables = []) {
Petr Lomakin47fee0a2017-08-01 10:46:05 -0700219 def salt = new com.mirantis.mk.Salt()
220 def output_file = 'docker-rally.log'
Dmitrii Kabanov23901c22017-10-20 10:25:36 -0700221 def results = '/root/qa_results'
222 def dest_folder = '/home/rally/qa_results'
223 salt.runSaltProcessStep(master, target, 'file.remove', ["${results}"])
224 salt.runSaltProcessStep(master, target, 'file.mkdir', ["${results}", "mode=777"])
225 def _pillar = salt.getPillar(master, 'I@keystone:server', 'keystone:server')
226 def keystone = _pillar['return'][0].values()[0]
227 def env_vars = ( ['tempest_version=15.0.0',
228 "OS_USERNAME=${keystone.admin_name}",
229 "OS_PASSWORD=${keystone.admin_password}",
230 "OS_TENANT_NAME=${keystone.admin_tenant}",
231 "OS_AUTH_URL=http://${keystone.bind.private_address}:${keystone.bind.private_port}/v2.0",
232 "OS_REGION_NAME=${keystone.region}",
233 'OS_ENDPOINT_TYPE=admin'] + ext_variables ).join(' -e ')
234 def cmd = '/opt/devops-qa-tools/deployment/configure.sh; ' +
235 'rally task start combined_scenario.yaml ' +
Dmitrii Kabanov999fda92017-11-10 00:18:30 -0800236 '--task-args-file /opt/devops-qa-tools/rally-scenarios/task_arguments.yaml; '
237 if (repository != '' ) {
238 cmd = "git clone -b ${branch ?: 'master'} ${repository} test_config; " +
239 'rally deployment create --file=test_config/rally/existing.json --name=existing; ' +
240 'rally deployment config; ' +
241 'rally task start test_config/rally/scenario.yaml ' +
242 '--task-args-file test_config/rally/task_arguments.yaml; '
243 }
244 cmd += "rally task export --type junit-xml --to ${dest_folder}/report-rally.xml; " +
Dmitrii Kabanov23901c22017-10-20 10:25:36 -0700245 "rally task report --out ${dest_folder}/report-rally.html"
246 salt.cmdRun(master, target, "docker run -i --rm --net=host -e ${env_vars} " +
247 "-v ${results}:${dest_folder} ${dockerImageLink} " +
248 "/bin/bash -c \"${cmd}\" > ${results}/${output_file}")
249 addFiles(master, target, results, output_dir)
Petr Lomakin47fee0a2017-08-01 10:46:05 -0700250}
251
252/**
Tetiana Korchak3383cc92017-08-25 09:36:19 -0700253 * Generate test report
254 *
255 * @param target Host to run script from
Dmitrii Kabanov23901c22017-10-20 10:25:36 -0700256 * @param dockerImageLink Docker image link
Tetiana Korchak3383cc92017-08-25 09:36:19 -0700257 * @param output_dir Directory for results
258 */
Dmitrii Kabanov23901c22017-10-20 10:25:36 -0700259def generateTestReport(master, target, dockerImageLink, output_dir) {
Tetiana Korchak3383cc92017-08-25 09:36:19 -0700260 def report_file = 'jenkins_test_report.html'
Tetiana Korchak3383cc92017-08-25 09:36:19 -0700261 def salt = new com.mirantis.mk.Salt()
262 def common = new com.mirantis.mk.Common()
Dmitrii Kabanov23901c22017-10-20 10:25:36 -0700263 def results = '/root/qa_results'
264 def dest_folder = '/opt/devops-qa-tools/generate_test_report/test_results'
265 salt.runSaltProcessStep(master, target, 'file.remove', ["${results}"])
266 salt.runSaltProcessStep(master, target, 'file.mkdir', ["${results}", "mode=777"])
267 def reports = ['report-tempest.json',
268 'report-rally.xml',
269 'report-k8s-e2e-tests.txt',
270 'report-ha.json',
271 'report-spt.txt']
272 for ( report in reports ) {
Tetiana Korchak3383cc92017-08-25 09:36:19 -0700273 if ( fileExists("${output_dir}${report}") ) {
274 common.infoMsg("Copying ${report} to docker container")
Dmitrii Kabanov23901c22017-10-20 10:25:36 -0700275 def items = sh(script: "base64 -w0 ${output_dir}${report} > ${output_dir}${report}_encoded; " +
276 "split -b 100KB -d -a 4 ${output_dir}${report}_encoded ${output_dir}${report}__; " +
277 "rm ${output_dir}${report}_encoded; " +
278 "find ${output_dir} -type f -name ${report}__* -printf \'%f\\n\' | sort", returnStdout: true)
279 for ( item in items.tokenize() ) {
280 def content = sh(script: "cat ${output_dir}${item}", returnStdout: true)
281 salt.cmdRun(master, target, "echo \"${content}\" >> ${results}/${report}_encoded", false, null, false)
282 sh(script: "rm ${output_dir}${item}")
Tetiana Korchak3383cc92017-08-25 09:36:19 -0700283 }
Dmitrii Kabanov23901c22017-10-20 10:25:36 -0700284 salt.cmdRun(master, target, "base64 -d ${results}/${report}_encoded > ${results}/${report}; " +
285 "rm ${results}/${report}_encoded", false, null, false)
Tetiana Korchak3383cc92017-08-25 09:36:19 -0700286 }
287 }
Tetiana Korchak3383cc92017-08-25 09:36:19 -0700288
Dmitrii Kabanov23901c22017-10-20 10:25:36 -0700289 def cmd = "jenkins_report.py --path /opt/devops-qa-tools/generate_test_report/; " +
290 "cp ${report_file} ${dest_folder}/${report_file}"
291 salt.cmdRun(master, target, "docker run -i --rm --net=host " +
292 "-v ${results}:${dest_folder} ${dockerImageLink} " +
293 "/bin/bash -c \"${cmd}\"")
294 def report_content = salt.getFileContent(master, target, "${results}/${report_file}")
Tetiana Korchak3383cc92017-08-25 09:36:19 -0700295 writeFile file: "${output_dir}${report_file}", text: report_content
296}
297
298/**
Dmitrii Kabanovd5f1c5f2017-08-30 14:51:41 -0700299 * Execute SPT tests
300 *
301 * @param target Host to run tests
Dmitrii Kabanov23901c22017-10-20 10:25:36 -0700302 * @param dockerImageLink Docker image link
Dmitrii Kabanovd5f1c5f2017-08-30 14:51:41 -0700303 * @param output_dir Directory for results
Dmitrii Kabanov23901c22017-10-20 10:25:36 -0700304 * @param ext_variables The list of external variables
Dmitrii Kabanovd5f1c5f2017-08-30 14:51:41 -0700305 */
Dmitrii Kabanov23901c22017-10-20 10:25:36 -0700306def runSptTests(master, target, dockerImageLink, output_dir, ext_variables = []) {
Dmitrii Kabanovd5f1c5f2017-08-30 14:51:41 -0700307 def salt = new com.mirantis.mk.Salt()
Dmitrii Kabanov23901c22017-10-20 10:25:36 -0700308 def results = '/root/qa_results'
309 def dest_folder = '/home/rally/qa_results'
310 salt.runSaltProcessStep(master, target, 'file.remove', ["${results}"])
311 salt.runSaltProcessStep(master, target, 'file.mkdir', ["${results}", "mode=777"])
312 def nodes = getNodeList(master)
313 def nodes_hw = getNodeList(master, 'G@virtual:physical')
314 def _pillar = salt.getPillar(master, 'I@keystone:server', 'keystone:server')
315 def keystone = _pillar['return'][0].values()[0]
316 def ssh_key = salt.getFileContent(master, 'I@salt:master', '/root/.ssh/id_rsa')
317 def env_vars = ( ['tempest_version=15.0.0',
318 "OS_USERNAME=${keystone.admin_name}",
319 "OS_PASSWORD=${keystone.admin_password}",
320 "OS_TENANT_NAME=${keystone.admin_tenant}",
321 "OS_AUTH_URL=http://${keystone.bind.private_address}:${keystone.bind.private_port}/v2.0",
322 "OS_REGION_NAME=${keystone.region}",
323 'OS_ENDPOINT_TYPE=admin'] + ext_variables ).join(' -e ')
324 salt.runSaltProcessStep(master, target, 'file.write', ["${results}/nodes.json", nodes])
325 salt.runSaltProcessStep(master, target, 'file.write', ["${results}/nodes_hw.json", nodes_hw])
326 def cmd = '/opt/devops-qa-tools/deployment/configure.sh; ' +
327 'sudo mkdir -p /root/.ssh; sudo chmod 700 /root/.ssh; ' +
328 "echo \\\"${ssh_key}\\\" | sudo tee /root/.ssh/id_rsa > /dev/null; " +
329 'sudo chmod 600 /root/.ssh/id_rsa; ' +
330 "sudo timmy -c simplified-performance-testing/config.yaml " +
331 "--nodes-json ${dest_folder}/nodes.json --log-file ${dest_folder}/docker-spt2.log; " +
332 "./simplified-performance-testing/SPT_parser.sh > ${dest_folder}/report-spt.txt; " +
333 "custom_spt_parser.sh ${dest_folder}/nodes_hw.json > ${dest_folder}/report-spt-hw.txt; " +
334 "cp /tmp/timmy/archives/general.tar.gz ${dest_folder}/results-spt.tar.gz"
335 salt.cmdRun(master, target, "docker run -i --rm --net=host -e ${env_vars} " +
336 "-v ${results}:${dest_folder} ${dockerImageLink} /bin/bash -c " +
337 "\"${cmd}\" > ${results}/docker-spt.log")
338 addFiles(master, target, results, output_dir)
Dmitrii Kabanovd5f1c5f2017-08-30 14:51:41 -0700339}
340
Dmitrii Kabanovd5f1c5f2017-08-30 14:51:41 -0700341/**
Oleksii Zhurba7b44ef12017-11-13 17:50:16 -0600342 * Configure docker container
343 *
344 * @param target Host to run container
345 * @param proxy Proxy for accessing github and pip
346 * @param testing_tools_repo Repo with testing tools: configuration script, skip-list, etc.
347 * @param tempest_repo Url to tempest for cloning. Can be github or internal gerrit. If not specified, tempest will not be configured.
348 * @param tempest_endpoint_type internalURL or adminURL or publicURL
349 * @param tempest_version Version of tempest to use
350 */
351def configureContainer(master, target, proxy, testing_tools_repo, tempest_repo,
352 tempest_endpoint_type="internalURL", tempest_version="15.0.0",
353 configure_script="/home/rally/testing-stuff/configure.sh", ext_variables = []) {
354 def salt = new com.mirantis.mk.Salt()
355 if (testing_tools_repo != "" ) {
356 salt.cmdRun(master, target, "docker exec cvp git clone ${testing_tools_repo}")
357 }
358 salt.cmdRun(master, target, "docker exec -e tempest_version=${tempest_version} -e PROXY=${proxy} " +
359 " -e TEMPEST_ENDPOINT=${tempest_repo} -e TEMPEST_ENDPOINT_TYPE=${tempest_endpoint_type} " +
360 ext_variables.join(' -e ') +
361 " cvp bash -c ${configure_script}")
362}
363
364/**
365 * Run Tempest
366 *
367 * @param target Host to run container
368 * @param test_pattern Test pattern to run
369 * @param skip_list Path to skip-list
370 * @param output_dir Directory on target host for storing results (containers is not a good place)
371 */
372def runCVPtempest(master, target, test_pattern="set=smoke", skip_list="", output_dir, output_filename="docker-tempest") {
373 def salt = new com.mirantis.mk.Salt()
374 def xml_file = "${output_filename}.xml"
375 def log_file = "${output_filename}.log"
376 skip_list_cmd = ''
377 if (skip_list != '') {
378 skip_list_cmd = "--skip-list ${skip_list}"
379 }
380 salt.cmdRun(master, target, "docker exec cvp rally verify start --pattern ${test_pattern} ${skip_list_cmd} " +
381 "--detailed > ${log_file}", false)
382 salt.cmdRun(master, target, "cat ${log_file}")
383 salt.cmdRun(master, target, "docker exec cvp rally verify report --type junit-xml --to /home/rally/${xml_file}")
384 salt.cmdRun(master, target, "docker cp cvp:/home/rally/${xml_file} ${output_dir}")
385 return salt.cmdRun(master, target, "docker exec cvp rally verify list | tail -n 2 | grep -v '+' | awk '{print \$16}'")['return'][0].values()[0].split()[0]
386}
387
388/**
389 * Run Rally
390 *
391 * @param target Host to run container
392 * @param test_pattern Test pattern to run
393 * @param scenarios_path Path to Rally scenarios
394 * @param output_dir Directory on target host for storing results (containers is not a good place)
395 */
396def runCVPrally(master, target, scenarios_path, output_dir, output_filename="docker-rally") {
397 def salt = new com.mirantis.mk.Salt()
398 def xml_file = "${output_filename}.xml"
399 def log_file = "${output_filename}.log"
400 def html_file = "${output_filename}.html"
401 salt.cmdRun(master, target, "docker exec cvp rally task start ${scenarios_path} > ${log_file}", false)
402 salt.cmdRun(master, target, "cat ${log_file}")
403 salt.cmdRun(master, target, "docker exec cvp rally task report --out ${html_file}")
404 salt.cmdRun(master, target, "docker exec cvp rally task export --type junit-xml --to ${xml_file}")
405 salt.cmdRun(master, target, "docker cp cvp:/home/rally/${xml_file} ${output_dir}")
406 salt.cmdRun(master, target, "docker cp cvp:/home/rally/${html_file} ${output_dir}")
407}
408
409
410/**
411 * Shutdown node
412 *
413 * @param target Host to run command
414 * @param mode How to shutdown node
415 * @param retries # of retries to make to check node status
416 */
417def shutdown_vm_node(master, target, mode, retries=200) {
418 def salt = new com.mirantis.mk.Salt()
419 def common = new com.mirantis.mk.Common()
420 if (mode == 'reboot') {
421 try {
422 def out = salt.runSaltCommand(master, 'local', ['expression': target, 'type': 'compound'], 'cmd.run', null, ['reboot'], null, 3, 3)
423 } catch (Exception e) {
424 common.warningMsg('Timeout from minion: node must be rebooting now')
425 }
426 common.warningMsg("Checking that minion is down")
427 status = "True"
428 for (i = 0; i < retries; i++) {
429 status = salt.minionsReachable(master, 'I@salt:master', target, null, 5, 1)
430 if (status != "True") {
431 break
432 }
433 }
434 if (status == "True") {
435 throw new Exception("Tired to wait for minion ${target} to stop responding")
436 }
437 }
438 if (mode == 'hard_shutdown' || mode == 'soft_shutdown') {
439 kvm = locate_node_on_kvm(master, target)
440 if (mode == 'soft_shutdown') {
441 salt.cmdRun(master, target, "shutdown -h 0")
442 }
443 if (mode == 'hard_shutdown') {
444 salt.cmdRun(master, kvm, "virsh destroy ${target}")
445 }
446 common.warningMsg("Checking that vm on kvm is in power off state")
447 status = 'running'
448 for (i = 0; i < retries; i++) {
449 status = check_vm_status(master, target, kvm)
450 echo "Current status - ${status}"
451 if (status != 'running') {
452 break
453 }
454 sleep (1)
455 }
456 if (status == 'running') {
457 throw new Exception("Tired to wait for node ${target} to shutdown")
458 }
459 }
460}
461
462
463/**
464 * Locate kvm where target host is located
465 *
466 * @param target Host to check
467 */
468def locate_node_on_kvm(master, target) {
469 def salt = new com.mirantis.mk.Salt()
470 def list = salt.runSaltProcessStep(master, "I@salt:control", 'cmd.run', ["virsh list --all | grep ' ${target}'"])['return'][0]
471 for (item in list.keySet()) {
472 if (list[item]) {
473 return item
474 }
475 }
476}
477
478/**
479 * Check target host status
480 *
481 * @param target Host to check
482 * @param kvm KVM node where target host is located
483 */
484def check_vm_status(master, target, kvm) {
485 def salt = new com.mirantis.mk.Salt()
486 def list = salt.runSaltProcessStep(master, "${kvm}", 'cmd.run', ["virsh list --all | grep ' ${target}'"])['return'][0]
487 for (item in list.keySet()) {
488 if (list[item]) {
489 return list[item].split()[2]
490 }
491 }
492}
493
494/**
495 * Find vip on nodes
496 *
497 * @param target Pattern, e.g. ctl*
498 */
499def get_vip_node(master, target) {
500 def salt = new com.mirantis.mk.Salt()
501 def list = salt.runSaltProcessStep(master, "${target}", 'cmd.run', ["ip a | grep global | grep -v brd"])['return'][0]
502 for (item in list.keySet()) {
503 if (list[item]) {
504 return item
505 }
506 }
507}
508
509/**
510 * Find vip on nodes
511 *
512 * @param target Host with cvp container
513 */
514def openstack_cleanup(master, target, script_path="/home/rally/testing-stuff/clean.sh") {
515 def salt = new com.mirantis.mk.Salt()
516 salt.runSaltProcessStep(master, "${target}", 'cmd.run', ["docker exec cvp bash -c ${script_path}"])
517}
518
519
520/**
Petr Lomakin47fee0a2017-08-01 10:46:05 -0700521 * Cleanup
522 *
523 * @param target Host to run commands
Petr Lomakin47fee0a2017-08-01 10:46:05 -0700524 */
Dmitrii Kabanov23901c22017-10-20 10:25:36 -0700525def runCleanup(master, target) {
Petr Lomakin47fee0a2017-08-01 10:46:05 -0700526 def salt = new com.mirantis.mk.Salt()
Dmitrii Kabanov321405a2017-08-16 16:38:51 -0700527 if ( salt.cmdRun(master, target, "docker ps -f name=qa_tools -q", false, null, false)['return'][0].values()[0] ) {
528 salt.cmdRun(master, target, "docker rm -f qa_tools")
529 }
Oleksii Zhurba7b44ef12017-11-13 17:50:16 -0600530 if ( salt.cmdRun(master, target, "docker ps -f name=cvp -q", false, null, false)['return'][0].values()[0] ) {
531 salt.cmdRun(master, target, "docker rm -f cvp")
532 }
Petr Lomakin47fee0a2017-08-01 10:46:05 -0700533}
Oleksii Zhurbabcb97e22017-10-05 14:10:39 -0500534/**
535 * Prepare venv for any python project
536 * Note: <repo_name>\/requirements.txt content will be used
537 * for this venv
538 *
539 * @param repo_url Repository url to clone
540 * @param proxy Proxy address to use
541 */
542def prepareVenv(repo_url, proxy) {
543 def python = new com.mirantis.mk.Python()
544 repo_name = "${repo_url}".tokenize("/").last()
545 sh "rm -rf ${repo_name}"
546 withEnv(["HTTPS_PROXY=${proxy}", "HTTP_PROXY=${proxy}", "https_proxy=${proxy}", "http_proxy=${proxy}"]) {
547 sh "git clone ${repo_url}"
548 python.setupVirtualenv("${env.WORKSPACE}/venv", "python2", [], "${env.WORKSPACE}/${repo_name}/requirements.txt", true)
549 }
550}
551
Petr Lomakin47fee0a2017-08-01 10:46:05 -0700552/** Install docker if needed
553 *
554 * @param target Target node to install docker pkg
555 */
556def installDocker(master, target) {
557 def salt = new com.mirantis.mk.Salt()
558 if ( ! salt.runSaltProcessStep(master, target, 'pkg.version', ["docker-engine"]) ) {
559 salt.runSaltProcessStep(master, target, 'pkg.install', ["docker.io"])
560 }
561}