blob: 79efa08db8768c065877874f01de0fe7cb0d6562 [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
Oleksii Zhurba1f4a6ff2018-06-27 16:45:17 -050011 * For backward compatibility. Deprecated.
12 * Will be removed soon.
Petr Lomakin47fee0a2017-08-01 10:46:05 -070013 *
Oleksii Zhurba7b44ef12017-11-13 17:50:16 -060014 * @param target Host to run container
15 * @param dockerImageLink Docker image link. May be custom or default rally image
Petr Lomakin47fee0a2017-08-01 10:46:05 -070016 */
Oleksii Zhurba1f4a6ff2018-06-27 16:45:17 -050017def runBasicContainer(master, target, dockerImageLink="xrally/xrally-openstack:0.10.1"){
Petr Lomakin47fee0a2017-08-01 10:46:05 -070018 def salt = new com.mirantis.mk.Salt()
19 def common = new com.mirantis.mk.Common()
Sam Stoelinga28bdb722017-09-25 18:29:59 -070020 def _pillar = salt.getPillar(master, 'I@keystone:server', 'keystone:server')
21 def keystone = _pillar['return'][0].values()[0]
Oleksii Zhurba7b44ef12017-11-13 17:50:16 -060022 if ( salt.cmdRun(master, target, "docker ps -f name=cvp -q", false, null, false)['return'][0].values()[0] ) {
23 salt.cmdRun(master, target, "docker rm -f cvp")
24 }
25 salt.cmdRun(master, target, "docker run -tid --net=host --name=cvp " +
26 "-u root -e OS_USERNAME=${keystone.admin_name} " +
Petr Lomakin47fee0a2017-08-01 10:46:05 -070027 "-e OS_PASSWORD=${keystone.admin_password} -e OS_TENANT_NAME=${keystone.admin_tenant} " +
28 "-e OS_AUTH_URL=http://${keystone.bind.private_address}:${keystone.bind.private_port}/v2.0 " +
Oleksii Zhurba1bf9be12018-01-17 15:20:00 -060029 "-e OS_REGION_NAME=${keystone.region} -e OS_ENDPOINT_TYPE=admin --entrypoint /bin/bash ${dockerImageLink}")
Petr Lomakin47fee0a2017-08-01 10:46:05 -070030}
31
Oleksii Zhurba1f4a6ff2018-06-27 16:45:17 -050032
33/**
34 * Run docker container with parameters
35 *
36 * @param target Host to run container
37 * @param dockerImageLink Docker image link. May be custom or default rally image
38 * @param name Name for container
39 * @param env_var Environment variables to set in container
40 * @param entrypoint Set entrypoint to /bin/bash or leave default
41**/
42
43
44def runContainer(master, target, dockerImageLink, name='cvp', env_var=[], entrypoint=true){
45 def salt = new com.mirantis.mk.Salt()
46 def common = new com.mirantis.mk.Common()
47 def variables = ''
48 def entry_point = ''
49 if ( salt.cmdRun(master, target, "docker ps -f name=${name} -q", false, null, false)['return'][0].values()[0] ) {
50 salt.cmdRun(master, target, "docker rm -f ${name}")
51 }
52 if (env_var.size() > 0) {
53 variables = ' -e ' + env_var.join(' -e ')
54 }
55 if (entrypoint) {
56 entry_point = '--entrypoint /bin/bash'
57 }
58 salt.cmdRun(master, target, "docker run -tid --net=host --name=${name} " +
Oleksii Zhurbafa885ed2019-02-13 18:27:31 -060059 "-u root ${entry_point} ${variables} " +
60 "-v /srv/salt/pki/${cluster_name}/:/etc/certs ${dockerImageLink}")
Oleksii Zhurba1f4a6ff2018-06-27 16:45:17 -050061}
62
63
Petr Lomakin47fee0a2017-08-01 10:46:05 -070064/**
Oleksii Zhurba4bcf07f2018-09-11 15:23:18 -050065 * Get v2 Keystone credentials from pillars
66 *
67 */
68def _get_keystone_creds_v2(master){
69 def salt = new com.mirantis.mk.Salt()
70 def common = new com.mirantis.mk.Common()
71 def keystone = []
72 common.infoMsg("Fetching Keystone v2 credentials")
73 _pillar = salt.getPillar(master, 'I@keystone:server', 'keystone:server')['return'][0].values()[0]
74 keystone.add("OS_USERNAME=${_pillar.admin_name}")
75 keystone.add("OS_PASSWORD=${_pillar.admin_password}")
76 keystone.add("OS_TENANT_NAME=${_pillar.admin_tenant}")
77 keystone.add("OS_AUTH_URL=http://${_pillar.bind.private_address}:${_pillar.bind.private_port}/v2.0")
78 keystone.add("OS_REGION_NAME=${_pillar.region}")
79 keystone.add("OS_ENDPOINT_TYPE=admin")
80 return keystone
81}
82
83/**
84 * Get v3 Keystone credentials from pillars
85 *
86 */
87def _get_keystone_creds_v3(master){
88 def salt = new com.mirantis.mk.Salt()
89 def common = new com.mirantis.mk.Common()
90 pillar_name = 'keystone:client:os_client_config:cfgs:root:content:clouds:admin_identity'
91 common.infoMsg("Fetching Keystone v3 credentials")
92 def _pillar = salt.getPillar(master, 'I@keystone:client', pillar_name)['return'][0].values()[0]
93 def keystone = []
94 if (_pillar) {
95 keystone.add("OS_USERNAME=${_pillar.auth.username}")
96 keystone.add("OS_PASSWORD=${_pillar.auth.password}")
97 keystone.add("OS_TENANT_NAME=${_pillar.auth.project_name}")
98 keystone.add("OS_PROJECT_NAME=${_pillar.auth.project_name}")
99 keystone.add("OS_AUTH_URL=${_pillar.auth.auth_url}/v3")
100 keystone.add("OS_REGION_NAME=${_pillar.region_name}")
101 keystone.add("OS_IDENTITY_API_VERSION=${_pillar.identity_api_version}")
Oleksii Zhurbafa885ed2019-02-13 18:27:31 -0600102 keystone.add("OS_ENDPOINT_TYPE=internal")
Oleksii Zhurba4bcf07f2018-09-11 15:23:18 -0500103 keystone.add("OS_PROJECT_DOMAIN_NAME=${_pillar.auth.project_domain_name}")
104 keystone.add("OS_USER_DOMAIN_NAME=${_pillar.auth.user_domain_name}")
Oleksii Zhurbafa885ed2019-02-13 18:27:31 -0600105 // we mount /srv/salt/pki/${cluster_name}/:/etc/certs with certs for cvp container
106 keystone.add("OS_CACERT='/etc/certs/proxy-with-chain.crt'")
Oleksii Zhurba4bcf07f2018-09-11 15:23:18 -0500107 return keystone
108 }
109 else {
110 common.warningMsg("Failed to fetch Keystone v3 credentials")
111 return false
112 }
113}
114
115/**
Dmitrii Kabanov23901c22017-10-20 10:25:36 -0700116 * Get file content (encoded). The content encoded by Base64.
Dmitrii Kabanovd5f1c5f2017-08-30 14:51:41 -0700117 *
118 * @param target Compound target (should target only one host)
119 * @param file File path to read
Dmitrii Kabanov23901c22017-10-20 10:25:36 -0700120 * @return The encoded content of the file
Dmitrii Kabanovd5f1c5f2017-08-30 14:51:41 -0700121 */
Dmitrii Kabanov23901c22017-10-20 10:25:36 -0700122def getFileContentEncoded(master, target, file) {
Dmitrii Kabanovd5f1c5f2017-08-30 14:51:41 -0700123 def salt = new com.mirantis.mk.Salt()
Dmitrii Kabanov23901c22017-10-20 10:25:36 -0700124 def file_content = ''
125 def cmd = "base64 -w0 ${file} > ${file}_encoded; " +
126 "split -b 1MB -d ${file}_encoded ${file}__; " +
127 "rm ${file}_encoded"
128 salt.cmdRun(master, target, cmd, false, null, false)
129 def filename = file.tokenize('/').last()
130 def folder = file - filename
131 def parts = salt.runSaltProcessStep(master, target, 'file.find', ["${folder}", "type=f", "name=${filename}__*"])
132 for ( part in parts['return'][0].values()[0]) {
133 def _result = salt.cmdRun(master, target, "cat ${part}", false, null, false)
134 file_content = file_content + _result['return'][0].values()[0].replaceAll('Salt command execution success','')
135 }
136 salt.runSaltProcessStep(master, target, 'file.find', ["${folder}", "type=f", "name=${filename}__*", "delete"])
137 return file_content
138}
139
140/**
141 * Copy files from remote to local directory. The content of files will be
142 * decoded by Base64.
143 *
144 * @param target Compound target (should target only one host)
145 * @param folder The path to remote folder.
146 * @param output_dir The path to local folder.
147 */
148def addFiles(master, target, folder, output_dir) {
149 def salt = new com.mirantis.mk.Salt()
150 def _result = salt.runSaltProcessStep(master, target, 'file.find', ["${folder}", "type=f"])
151 def files = _result['return'][0].values()[0]
152 for (file in files) {
153 def file_content = getFileContentEncoded(master, target, "${file}")
154 def fileName = file.tokenize('/').last()
155 writeFile file: "${output_dir}${fileName}_encoded", text: file_content
156 def cmd = "base64 -d ${output_dir}${fileName}_encoded > ${output_dir}${fileName}; " +
157 "rm ${output_dir}${fileName}_encoded"
158 sh(script: cmd)
Dmitrii Kabanovd5f1c5f2017-08-30 14:51:41 -0700159 }
160}
161
162/**
163 * Get reclass value
164 *
165 * @param target The host for which the values will be provided
166 * @param filter Parameters divided by dots
167 * @return The pillar data
168 */
169def getReclassValue(master, target, filter) {
170 def common = new com.mirantis.mk.Common()
171 def salt = new com.mirantis.mk.Salt()
172 def items = filter.tokenize('.')
Dmitrii Kabanov23901c22017-10-20 10:25:36 -0700173 def _result = salt.cmdRun(master, 'I@salt:master', "reclass-salt -o json -p ${target}", false, null, false)
Dmitrii Kabanovd5f1c5f2017-08-30 14:51:41 -0700174 _result = common.parseJSON(_result['return'][0].values()[0])
Dmitrii Kabanov23901c22017-10-20 10:25:36 -0700175 for (int k = 0; k < items.size(); k++) {
Dmitrii Kabanovd5f1c5f2017-08-30 14:51:41 -0700176 if ( _result ) {
Dmitrii Kabanov23901c22017-10-20 10:25:36 -0700177 _result = _result["${items[k]}"]
Dmitrii Kabanovd5f1c5f2017-08-30 14:51:41 -0700178 }
179 }
180 return _result
181}
182
183/**
184 * Create list of nodes in JSON format.
185 *
186 * @param filter The Salt's matcher
187 * @return JSON list of nodes
188 */
189def getNodeList(master, filter = null) {
190 def salt = new com.mirantis.mk.Salt()
191 def common = new com.mirantis.mk.Common()
Dmitrii Kabanovd5f1c5f2017-08-30 14:51:41 -0700192 def nodes = []
Dmitrii Kabanovd5f1c5f2017-08-30 14:51:41 -0700193 def filtered_list = null
194 def controllers = salt.getMinions(master, 'I@nova:controller')
195 def hw_nodes = salt.getMinions(master, 'G@virtual:physical')
Dmitrii Kabanovd5f1c5f2017-08-30 14:51:41 -0700196 if ( filter ) {
197 filtered_list = salt.getMinions(master, filter)
Dmitrii Kabanovd5f1c5f2017-08-30 14:51:41 -0700198 }
199 def _result = salt.cmdRun(master, 'I@salt:master', "reclass-salt -o json -t", false, null, false)
200 def reclass_top = common.parseJSON(_result['return'][0].values()[0])
Dmitrii Kabanov23901c22017-10-20 10:25:36 -0700201 def nodesList = reclass_top['base'].keySet()
202 for (int i = 0; i < nodesList.size(); i++) {
Dmitrii Kabanovd5f1c5f2017-08-30 14:51:41 -0700203 if ( filtered_list ) {
Dmitrii Kabanov23901c22017-10-20 10:25:36 -0700204 if ( ! filtered_list.contains(nodesList[i]) ) {
Dmitrii Kabanovd5f1c5f2017-08-30 14:51:41 -0700205 continue
206 }
207 }
Dmitrii Kabanov23901c22017-10-20 10:25:36 -0700208 def ip = getReclassValue(master, nodesList[i], '_param.linux_single_interface.address')
209 def network_data = [ip: ip, name: 'management']
210 def roles = [nodesList[i].tokenize('.')[0]]
211 if ( controllers.contains(nodesList[i]) ) {
212 roles.add('controller')
Dmitrii Kabanovd5f1c5f2017-08-30 14:51:41 -0700213 }
Dmitrii Kabanov23901c22017-10-20 10:25:36 -0700214 if ( hw_nodes.contains(nodesList[i]) ) {
215 roles.add('hw_node')
Dmitrii Kabanovd5f1c5f2017-08-30 14:51:41 -0700216 }
Dmitrii Kabanov23901c22017-10-20 10:25:36 -0700217 nodes.add([id: i+1, ip: ip, roles: roles, network_data: [network_data]])
Dmitrii Kabanovd5f1c5f2017-08-30 14:51:41 -0700218 }
Dmitrii Kabanov23901c22017-10-20 10:25:36 -0700219 return common.prettify(nodes)
Dmitrii Kabanovd5f1c5f2017-08-30 14:51:41 -0700220}
221
Oleksii Zhurbabcb97e22017-10-05 14:10:39 -0500222/**
223 * Execute mcp sanity tests
Oleksii Zhurba1f4a6ff2018-06-27 16:45:17 -0500224 * Deprecated. Will be removed soon
Oleksii Zhurbabcb97e22017-10-05 14:10:39 -0500225 *
226 * @param salt_url Salt master url
227 * @param salt_credentials Salt credentials
228 * @param test_set Test set for mcp sanity framework
Oleksii Zhurba0a7b0702017-11-10 16:02:16 -0600229 * @param env_vars Additional environment variables for cvp-sanity-checks
Oleksii Zhurbabcb97e22017-10-05 14:10:39 -0500230 * @param output_dir Directory for results
231 */
Oleksii Zhurba0a7b0702017-11-10 16:02:16 -0600232def runSanityTests(salt_url, salt_credentials, test_set="", output_dir="validation_artifacts/", env_vars="") {
Oleksii Zhurbabcb97e22017-10-05 14:10:39 -0500233 def common = new com.mirantis.mk.Common()
Oleksii Zhurba0a7b0702017-11-10 16:02:16 -0600234 def creds = common.getCredentials(salt_credentials)
235 def username = creds.username
236 def password = creds.password
237 def settings = ""
238 if ( env_vars != "" ) {
239 for (var in env_vars.tokenize(";")) {
240 settings += "export ${var}; "
241 }
242 }
243 def script = ". ${env.WORKSPACE}/venv/bin/activate; ${settings}" +
Oleksii Zhurba5250a9c2018-03-21 15:47:03 -0500244 "pytest --junitxml ${output_dir}cvp_sanity.xml --tb=short -sv ${env.WORKSPACE}/cvp-sanity-checks/cvp_checks/tests/${test_set}"
Oleksii Zhurbabcb97e22017-10-05 14:10:39 -0500245 withEnv(["SALT_USERNAME=${username}", "SALT_PASSWORD=${password}", "SALT_URL=${salt_url}"]) {
246 def statusCode = sh script:script, returnStatus:true
247 }
248}
Dmitrii Kabanovd5f1c5f2017-08-30 14:51:41 -0700249
250/**
Oleksii Zhurba4e366ff2018-02-16 20:06:52 -0600251 * Execute pytest framework tests
252 *
253 * @param salt_url Salt master url
254 * @param salt_credentials Salt credentials
255 * @param test_set Test set to run
256 * @param env_vars Additional environment variables for cvp-sanity-checks
257 * @param output_dir Directory for results
258 */
Oleksii Zhurba1f4a6ff2018-06-27 16:45:17 -0500259def runPyTests(salt_url, salt_credentials, test_set="", env_vars="", name='cvp', container_node="", remote_dir='/root/qa_results/', artifacts_dir='validation_artifacts/') {
260 def xml_file = "${name}_report.xml"
261 def common = new com.mirantis.mk.Common()
262 def salt = new com.mirantis.mk.Salt()
263 def creds = common.getCredentials(salt_credentials)
264 def username = creds.username
265 def password = creds.password
266 if (container_node != "") {
267 def saltMaster
268 saltMaster = salt.connection(salt_url, salt_credentials)
269 def script = "pytest --junitxml ${xml_file} --tb=short -sv ${test_set}"
270 env_vars.addAll("SALT_USERNAME=${username}", "SALT_PASSWORD=${password}",
271 "SALT_URL=${salt_url}")
272 variables = ' -e ' + env_vars.join(' -e ')
273 salt.cmdRun(saltMaster, container_node, "docker exec ${variables} ${name} bash -c '${script}'", false)
274 salt.cmdRun(saltMaster, container_node, "docker cp ${name}:/var/lib/${xml_file} ${remote_dir}${xml_file}")
275 addFiles(saltMaster, container_node, remote_dir+xml_file, artifacts_dir)
276 }
277 else {
278 if (env_vars.size() > 0) {
279 variables = 'export ' + env_vars.join(';export ')
280 }
281 def script = ". ${env.WORKSPACE}/venv/bin/activate; ${variables}; " +
282 "pytest --junitxml ${artifacts_dir}${xml_file} --tb=short -sv ${env.WORKSPACE}/${test_set}"
283 withEnv(["SALT_USERNAME=${username}", "SALT_PASSWORD=${password}", "SALT_URL=${salt_url}"]) {
284 def statusCode = sh script:script, returnStatus:true
285 }
286 }
287}
288
289/**
290 * Execute pytest framework tests
291 * For backward compatibility
292 * Will be removed soon
293 *
294 * @param salt_url Salt master url
295 * @param salt_credentials Salt credentials
296 * @param test_set Test set to run
297 * @param env_vars Additional environment variables for cvp-sanity-checks
298 * @param output_dir Directory for results
299 */
Oleksii Zhurba4e366ff2018-02-16 20:06:52 -0600300def runTests(salt_url, salt_credentials, test_set="", output_dir="validation_artifacts/", env_vars="") {
301 def common = new com.mirantis.mk.Common()
302 def creds = common.getCredentials(salt_credentials)
303 def username = creds.username
304 def password = creds.password
305 def settings = ""
306 if ( env_vars != "" ) {
307 for (var in env_vars.tokenize(";")) {
308 settings += "export ${var}; "
309 }
310 }
311 def script = ". ${env.WORKSPACE}/venv/bin/activate; ${settings}" +
312 "pytest --junitxml ${output_dir}report.xml --tb=short -sv ${env.WORKSPACE}/${test_set}"
313 withEnv(["SALT_USERNAME=${username}", "SALT_PASSWORD=${password}", "SALT_URL=${salt_url}"]) {
314 def statusCode = sh script:script, returnStatus:true
315 }
316}
317
318/**
Petr Lomakin47fee0a2017-08-01 10:46:05 -0700319 * Execute tempest tests
320 *
321 * @param target Host to run tests
Dmitrii Kabanov23901c22017-10-20 10:25:36 -0700322 * @param dockerImageLink Docker image link
Petr Lomakin47fee0a2017-08-01 10:46:05 -0700323 * @param pattern If not false, will run only tests matched the pattern
324 * @param output_dir Directory for results
Dmitrii Kabanov999fda92017-11-10 00:18:30 -0800325 * @param confRepository Git repository with configuration files for Tempest
326 * @param confBranch Git branch which will be used during the checkout
327 * @param repository Git repository with Tempest
328 * @param version Version of Tempest (tag, branch or commit)
Sergey Galkind1068e22018-02-13 13:59:32 +0400329 * @param results The reports directory
Petr Lomakin47fee0a2017-08-01 10:46:05 -0700330 */
Sergey Galkind1068e22018-02-13 13:59:32 +0400331def runTempestTests(master, target, dockerImageLink, output_dir, confRepository, confBranch, repository, version, pattern = "false", results = '/root/qa_results') {
Petr Lomakin47fee0a2017-08-01 10:46:05 -0700332 def salt = new com.mirantis.mk.Salt()
333 def output_file = 'docker-tempest.log'
Dmitrii Kabanov23901c22017-10-20 10:25:36 -0700334 def dest_folder = '/home/rally/qa_results'
Dmitrii Kabanov999fda92017-11-10 00:18:30 -0800335 def skip_list = '--skip-list /opt/devops-qa-tools/deployment/skip_contrail.list'
Dmitrii Kabanov23901c22017-10-20 10:25:36 -0700336 salt.runSaltProcessStep(master, target, 'file.remove', ["${results}"])
337 salt.runSaltProcessStep(master, target, 'file.mkdir', ["${results}", "mode=777"])
338 def _pillar = salt.getPillar(master, 'I@keystone:server', 'keystone:server')
339 def keystone = _pillar['return'][0].values()[0]
Dmitry Tsapikovb6911922018-07-24 15:21:23 +0000340 def env_vars = ['tempest_version=15.0.0',
Dmitrii Kabanov23901c22017-10-20 10:25:36 -0700341 "OS_USERNAME=${keystone.admin_name}",
342 "OS_PASSWORD=${keystone.admin_password}",
343 "OS_TENANT_NAME=${keystone.admin_tenant}",
344 "OS_AUTH_URL=http://${keystone.bind.private_address}:${keystone.bind.private_port}/v2.0",
345 "OS_REGION_NAME=${keystone.region}",
346 'OS_ENDPOINT_TYPE=admin'].join(' -e ')
347 def cmd = '/opt/devops-qa-tools/deployment/configure.sh; '
Dmitrii Kabanov999fda92017-11-10 00:18:30 -0800348 if (confRepository != '' ) {
349 cmd = "git clone -b ${confBranch ?: 'master'} ${confRepository} test_config; " +
350 'rally deployment create --fromenv --name=tempest; rally deployment config; ' +
351 'rally verify create-verifier --name tempest_verifier --type tempest ' +
Dmitry Tsapikovb6911922018-07-24 15:21:23 +0000352 "--source ${repository ?: '/tmp/tempest/'} --version ${version: '15.0.0'}; " +
Dmitrii Kabanov999fda92017-11-10 00:18:30 -0800353 'rally verify configure-verifier --extend test_config/tempest/tempest.conf --show; '
354 skip_list = '--skip-list test_config/tempest/skip-list.yaml'
355 }
Dmitrii Kabanov23901c22017-10-20 10:25:36 -0700356 if (pattern == 'false') {
Dmitrii Kabanov999fda92017-11-10 00:18:30 -0800357 cmd += "rally verify start --pattern set=full ${skip_list} --detailed; "
Petr Lomakin47fee0a2017-08-01 10:46:05 -0700358 }
359 else {
Dmitrii Kabanov999fda92017-11-10 00:18:30 -0800360 cmd += "rally verify start --pattern set=${pattern} ${skip_list} --detailed; "
Petr Lomakin47fee0a2017-08-01 10:46:05 -0700361 }
Dmitrii Kabanov23901c22017-10-20 10:25:36 -0700362 cmd += "rally verify report --type json --to ${dest_folder}/report-tempest.json; " +
363 "rally verify report --type html --to ${dest_folder}/report-tempest.html"
mkraynovd49daf52018-07-12 16:11:14 +0400364 salt.cmdRun(master, target, "docker run -w /home/rally -i --rm --net=host -e ${env_vars} " +
Sergey Galkin193ef872017-11-29 14:20:35 +0400365 "-v ${results}:${dest_folder} --entrypoint /bin/bash ${dockerImageLink} " +
366 "-c \"${cmd}\" > ${results}/${output_file}")
Dmitrii Kabanov23901c22017-10-20 10:25:36 -0700367 addFiles(master, target, results, output_dir)
Petr Lomakin47fee0a2017-08-01 10:46:05 -0700368}
369
370/**
Oleg Basov40e502c2018-09-04 20:42:21 +0200371 * Make all-in-one scenario cmd for rally tests
372 *
373 * @param scenarios_path Path to scenarios folder/file
374 * @param skip_scenarios Comma-delimited list of scenarios names to skip
375 * @param bundle_file Bundle name to create
376*/
Oleg Basov06fce2a2018-11-09 21:39:03 +0100377def bundle_up_scenarios(scenarios_path, skip_scenarios, bundle_file = '' ) {
Oleg Basov40e502c2018-09-04 20:42:21 +0200378 def skip_names = ''
379 def skip_dirs = ''
380 def result = ''
381 if (skip_scenarios != ''){
382 for ( scen in skip_scenarios.split(',') ) {
383 if ( scen.contains('yaml')) {
384 skip_names += "! -name ${scen} "
385 }
386 else {
387 skip_dirs += "-path ${scenarios_path}/${scen} -prune -o "
388 }
389 }
390 }
Oleg Basov06fce2a2018-11-09 21:39:03 +0100391 if (bundle_file != '') {
392 result = "if [ -f ${scenarios_path} ]; then cp ${scenarios_path} ${bundle_file}; " +
Oleg Basov40e502c2018-09-04 20:42:21 +0200393 "else " +
394 "find -L ${scenarios_path} " + skip_dirs +
395 " -name '*.yaml' " + skip_names +
396 "-exec cat {} >> ${bundle_file} \\; ; " +
397 "sed -i '/---/d' ${bundle_file}; fi; "
Oleg Basov06fce2a2018-11-09 21:39:03 +0100398 } else {
399 result = "find -L ${scenarios_path} " + skip_dirs +
400 " -name '*.yaml' " + skip_names
401 }
Oleg Basov40e502c2018-09-04 20:42:21 +0200402
403 return result
404}
405
406/**
Petr Lomakin47fee0a2017-08-01 10:46:05 -0700407 * Execute rally tests
408 *
409 * @param target Host to run tests
Dmitrii Kabanov23901c22017-10-20 10:25:36 -0700410 * @param dockerImageLink Docker image link
Oleg Basov20afb152018-06-10 03:09:25 +0200411 * @param platform What do we have underneath (openstack/k8s)
Petr Lomakin47fee0a2017-08-01 10:46:05 -0700412 * @param output_dir Directory for results
Oleg Basov40e502c2018-09-04 20:42:21 +0200413 * @param config_repo Git repository with with files for Rally
414 * @param config_branch Git config repo branch which will be used during the checkout
415 * @param plugins_repo Git repository with Rally plugins
416 * @param plugins_branch Git plugins repo branch which will be used during the checkout
Oleg Basov20afb152018-06-10 03:09:25 +0200417 * @param scenarios Directory inside repo with specific scenarios
Oleg Basov40e502c2018-09-04 20:42:21 +0200418 * @param sl_scenarios Directory inside repo with specific scenarios for stacklight
Oleg Basov20afb152018-06-10 03:09:25 +0200419 * @param tasks_args_file Argument file that is used for throttling settings
Oleg Basov1fa6c662019-03-05 22:04:01 +0100420 * @param db_connection_str Rally-compliant external DB connection string
421 * @param tags Additional tags used for tagging tasks or building trends
422 * @param trends Build rally trends if enabled
Dmitrii Kabanov23901c22017-10-20 10:25:36 -0700423 * @param ext_variables The list of external variables
Sergey Galkind1068e22018-02-13 13:59:32 +0400424 * @param results The reports directory
Petr Lomakin47fee0a2017-08-01 10:46:05 -0700425 */
Oleg Basov1fa6c662019-03-05 22:04:01 +0100426def runRallyTests(
427 master, target, dockerImageLink,
428 platform, output_dir, config_repo,
429 config_branch, plugins_repo, plugins_branch,
430 scenarios, sl_scenarios = '', tasks_args_file = '',
431 db_connection_str = '', tags = [],
432 trends = false, ext_variables = [],
433 results = '/root/qa_results', skip_list = ''
434 ) {
435
Petr Lomakin47fee0a2017-08-01 10:46:05 -0700436 def salt = new com.mirantis.mk.Salt()
437 def output_file = 'docker-rally.log'
Dmitrii Kabanov23901c22017-10-20 10:25:36 -0700438 def dest_folder = '/home/rally/qa_results'
Oleg Basov20afb152018-06-10 03:09:25 +0200439 def env_vars = []
Oleg Basov1fa6c662019-03-05 22:04:01 +0100440 def work_dir = 'test_config'
441
442 // compile rally deployment name from env name, platform name,
443 // date, cmp nodes count
444 def deployment_name = ''
445 def cluster_name = salt.getPillar(
446 master, 'I@salt:master', '_param:cluster_name'
447 )['return'][0].values()[0]
448 def rcs_str_node = salt.getPillar(
449 master, 'I@salt:master', 'reclass:storage:node'
450 )['return'][0].values()[0]
451 def date = new Date()
452 date = date.format("yyyyMMddHHmm")
453 def cmp_count
454
455 if (platform['type'] == 'openstack') {
456 cmp_count = rcs_str_node.openstack_compute_rack01['repeat']['count']
457 } else if (platform['type'] == 'k8s') {
458 cmp_count = rcs_str_node.kubernetes_compute_rack01['repeat']['count']
459 } else {
460 throw new Exception("Platform ${platform} is not supported yet")
461 }
462 deployment_name = "env=${cluster_name}:platform=${platform.type}:" +
463 "date=${date}:cmp=${cmp_count}"
464
465 // set up rally cmds
Oleg Basov20afb152018-06-10 03:09:25 +0200466 def rally_extra_args = ''
Oleg Basov40e502c2018-09-04 20:42:21 +0200467 def cmd_rally_plugins =
Oleg Basov1fa6c662019-03-05 22:04:01 +0100468 "git clone -b ${plugins_branch ?: 'master'} ${plugins_repo} /tmp/plugins; " +
469 "sudo pip install --upgrade /tmp/plugins; "
470 def cmd_rally_init = 'rally db ensure; '
471 if (db_connection_str) {
472 cmd_rally_init = "sudo sed -i -e " +
473 "'s#connection=.*#connection=${db_connection_str}#' " +
474 "/etc/rally/rally.conf; "
475 }
476 def cmd_rally_checkout = "git clone -b ${config_branch ?: 'master'} ${config_repo} ${work_dir}; "
Oleg Basov20afb152018-06-10 03:09:25 +0200477 def cmd_rally_start = ''
478 def cmd_rally_task_args = ''
Oleg Basov40e502c2018-09-04 20:42:21 +0200479 def cmd_rally_stacklight = ''
Oleg Basov1fa6c662019-03-05 22:04:01 +0100480 def cmd_rally_report = "rally task export " +
481 "--uuid \\\$(rally task list --uuids-only --status finished) " +
482 "--type junit-xml --to ${dest_folder}/report-rally.xml; " +
483 "rally task report --uuid \\\$(rally task list --uuids-only --status finished) " +
484 "--out ${dest_folder}/report-rally.html"
485 def cmd_filter_tags = ''
486
487 // build rally trends if required
488 if (trends && db_connection_str) {
489 if (tags) {
490 cmd_filter_tags = "--tag " + tags.join(' ')
491 }
492 cmd_rally_report += "; rally task trends --tasks " +
493 "\\\$(rally task list " + cmd_filter_tags +
494 " --all-deployments --uuids-only --status finished) " +
495 "--out ${dest_folder}/trends-rally.html"
496 }
497
498 // add default env tags for inserting into rally tasks
499 tags = tags + [
500 "env=${cluster_name}",
501 "platform=${platform.type}",
502 "cmp=${cmp_count}"
503 ]
504
505 // create results directory
Dmitrii Kabanov23901c22017-10-20 10:25:36 -0700506 salt.runSaltProcessStep(master, target, 'file.remove', ["${results}"])
507 salt.runSaltProcessStep(master, target, 'file.mkdir', ["${results}", "mode=777"])
Oleg Basov1fa6c662019-03-05 22:04:01 +0100508
509 // get required OS data
Oleg Basov40e502c2018-09-04 20:42:21 +0200510 if (platform['type'] == 'openstack') {
Oleg Basov1fa6c662019-03-05 22:04:01 +0100511 cmd_rally_init += "rally deployment create --name='${deployment_name}' --fromenv; " +
512 "rally deployment check; "
513
514 def keystone = salt.getPillar(
515 master, 'I@keystone:server', 'keystone'
516 )['return'][0].values()[0]
Oleg Basov20afb152018-06-10 03:09:25 +0200517 env_vars = ( ['tempest_version=15.0.0',
Oleg Basov4a697fc2018-11-06 18:34:08 +0100518 "OS_USERNAME=${keystone.server.admin_name}",
519 "OS_PASSWORD=${keystone.server.admin_password}",
520 "OS_TENANT_NAME=${keystone.server.admin_tenant}",
521 "OS_AUTH_URL=http://${keystone.server.bind.private_address}:${keystone.server.bind.private_port}" +
522 "/v${keystone.client.os_client_config.cfgs.root.content.clouds.admin_identity.identity_api_version}",
523 "OS_REGION_NAME=${keystone.server.region}",
Oleg Basov20afb152018-06-10 03:09:25 +0200524 'OS_ENDPOINT_TYPE=admin'] + ext_variables ).join(' -e ')
Oleg Basov1fa6c662019-03-05 22:04:01 +0100525
526 // get required SL data
Oleg Basov40e502c2018-09-04 20:42:21 +0200527 if (platform['stacklight_enabled'] == true) {
Oleg Basov1fa6c662019-03-05 22:04:01 +0100528 def grafana = salt.getPillar(
529 master, 'I@grafana:client', 'grafana:client:server'
530 )['return'][0].values()[0]
531 cmd_rally_stacklight = bundle_up_scenarios(
532 work_dir + '/' + sl_scenarios,
533 skip_list,
534 "scenarios_${platform.type}_stacklight.yaml"
535 )
536 tags.add('stacklight')
Oleg Basov418bfc42018-10-28 15:01:22 +0100537 cmd_rally_stacklight += "sed -i 's/grafana_password: .*/grafana_password: ${grafana.password}/' " +
Oleg Basov1fa6c662019-03-05 22:04:01 +0100538 "${work_dir}/job-params-stacklight.yaml; " +
539 "rally $rally_extra_args task start --tag " + tags.join(' ') +
540 " --task scenarios_${platform.type}_stacklight.yaml " +
541 "--task-args-file ${work_dir}/job-params-stacklight.yaml; "
Oleg Basov20afb152018-06-10 03:09:25 +0200542 }
Oleg Basov1fa6c662019-03-05 22:04:01 +0100543
544 // get required K8S data
Oleg Basov40e502c2018-09-04 20:42:21 +0200545 } else if (platform['type'] == 'k8s') {
Oleg Basov1fa6c662019-03-05 22:04:01 +0100546 cmd_rally_init += "rally env create --name='${deployment_name}' --from-sysenv; " +
547 "rally env check; "
Oleg Basov20afb152018-06-10 03:09:25 +0200548 rally_extra_args = "--debug --log-file ${dest_folder}/task.log"
Oleg Basov1fa6c662019-03-05 22:04:01 +0100549
550 def kubernetes = salt.getPillar(
551 master, 'I@kubernetes:master and *01*', 'kubernetes:master'
552 )['return'][0].values()[0]
Oleg Basov3e050d32018-07-03 22:10:56 +0200553 env_vars = [
Oleg Basov491942d2018-10-17 16:11:25 +0200554 "KUBERNETES_HOST=http://${kubernetes.apiserver.vip_address}" +
Oleg Basov3e050d32018-07-03 22:10:56 +0200555 ":${kubernetes.apiserver.insecure_port}",
556 "KUBERNETES_CERT_AUTH=${dest_folder}/k8s-ca.crt",
557 "KUBERNETES_CLIENT_KEY=${dest_folder}/k8s-client.key",
558 "KUBERNETES_CLIENT_CERT=${dest_folder}/k8s-client.crt"].join(' -e ')
Oleg Basov1fa6c662019-03-05 22:04:01 +0100559
560 def k8s_ca = salt.getFileContent(
561 master, 'I@kubernetes:master and *01*', '/etc/kubernetes/ssl/ca-kubernetes.crt'
562 )
563 def k8s_client_key = salt.getFileContent(
564 master, 'I@kubernetes:master and *01*', '/etc/kubernetes/ssl/kubelet-client.key'
565 )
566 def k8s_client_crt = salt.getFileContent(
567 master, 'I@kubernetes:master and *01*', '/etc/kubernetes/ssl/kubelet-client.crt'
568 )
Oleg Basov20afb152018-06-10 03:09:25 +0200569 def tmp_dir = '/tmp/kube'
Oleg Basov1fa6c662019-03-05 22:04:01 +0100570
Oleg Basov6c0d6712019-01-16 18:52:30 +0100571 salt.runSaltProcessStep(master, target, 'file.mkdir', ["${tmp_dir}"])
Oleg Basov1fa6c662019-03-05 22:04:01 +0100572 salt.runSaltProcessStep(
573 master, target, 'file.write', ["${tmp_dir}/k8s-ca.crt","${k8s_ca}"]
574 )
575 salt.runSaltProcessStep(
576 master, target, 'file.write', ["${tmp_dir}/k8s-client.key", "${k8s_client_key}"]
577 )
578 salt.runSaltProcessStep(
579 master, target, 'file.write', ["${tmp_dir}/k8s-client.crt", "${k8s_client_crt}"]
580 )
Oleg Basov20afb152018-06-10 03:09:25 +0200581 salt.cmdRun(master, target, "mv ${tmp_dir}/* ${results}/")
582 salt.runSaltProcessStep(master, target, 'file.rmdir', ["${tmp_dir}"])
Oleg Basov1fa6c662019-03-05 22:04:01 +0100583
Oleg Basov20afb152018-06-10 03:09:25 +0200584 } else {
585 throw new Exception("Platform ${platform} is not supported yet")
Dmitrii Kabanov999fda92017-11-10 00:18:30 -0800586 }
Oleg Basov1fa6c662019-03-05 22:04:01 +0100587
588 // set up rally task args file
Oleg Basov06fce2a2018-11-09 21:39:03 +0100589 switch(tasks_args_file) {
590 case 'none':
591 cmd_rally_task_args = '; '
Oleg Basov20afb152018-06-10 03:09:25 +0200592 break
Oleg Basov06fce2a2018-11-09 21:39:03 +0100593 case '':
Oleg Basov1fa6c662019-03-05 22:04:01 +0100594 cmd_rally_task_args = "--task-args-file ${work_dir}/job-params-light.yaml; "
Oleg Basov06fce2a2018-11-09 21:39:03 +0100595 break
596 default:
Oleg Basov1fa6c662019-03-05 22:04:01 +0100597 cmd_rally_task_args = "--task-args-file ${work_dir}/${tasks_args_file}; "
Oleg Basov06fce2a2018-11-09 21:39:03 +0100598 break
599 }
600 if (platform['type'] == 'k8s') {
Oleg Basov1fa6c662019-03-05 22:04:01 +0100601 cmd_rally_start = "for task in \\\$(" +
602 bundle_up_scenarios(work_dir + '/' + scenarios, skip_list) +
Oleg Basov06fce2a2018-11-09 21:39:03 +0100603 "); do " +
Oleg Basov1fa6c662019-03-05 22:04:01 +0100604 "rally $rally_extra_args task start --tag " + tags.join(' ') +
605 " --task \\\$task ${cmd_rally_task_args}" +
Oleg Basov06fce2a2018-11-09 21:39:03 +0100606 "done; "
607 } else {
Oleg Basov1fa6c662019-03-05 22:04:01 +0100608 cmd_rally_checkout += bundle_up_scenarios(
609 work_dir + '/' + scenarios,
610 skip_list,
611 "scenarios_${platform.type}.yaml"
612 )
613 cmd_rally_start = "rally $rally_extra_args task start --tag " + tags.join(' ') +
614 " --task scenarios_${platform.type}.yaml ${cmd_rally_task_args}"
Oleg Basov20afb152018-06-10 03:09:25 +0200615 }
Oleg Basovaaeb51f2018-10-17 01:07:10 +0200616
Oleg Basov1fa6c662019-03-05 22:04:01 +0100617 // compile full rally cmd
Oleg Basov40e502c2018-09-04 20:42:21 +0200618 full_cmd = 'set -xe; ' + cmd_rally_plugins +
619 cmd_rally_init + cmd_rally_checkout +
620 'set +e; ' + cmd_rally_start +
Oleg Basov1fa6c662019-03-05 22:04:01 +0100621 cmd_rally_stacklight + cmd_rally_report
622
623 // run rally
Sergey Galkinf89509d2018-03-19 15:24:17 +0400624 salt.runSaltProcessStep(master, target, 'file.touch', ["${results}/rally.db"])
625 salt.cmdRun(master, target, "chmod 666 ${results}/rally.db")
Oleg Basov1fa6c662019-03-05 22:04:01 +0100626 salt.cmdRun(
627 master, target,
628 "docker run -w /home/rally -i --rm --net=host -e ${env_vars} " +
Sergey Galkin3c1e9e22018-01-12 16:31:53 +0400629 "-v ${results}:${dest_folder} " +
mkraynovd49daf52018-07-12 16:11:14 +0400630 "-v ${results}/rally.db:/home/rally/data/rally.db " +
Sergey Galkin3c1e9e22018-01-12 16:31:53 +0400631 "--entrypoint /bin/bash ${dockerImageLink} " +
632 "-c \"${full_cmd}\" > ${results}/${output_file}")
Oleg Basov1fa6c662019-03-05 22:04:01 +0100633
634 // remove k8s secrets
635 if (platform['type'] == 'k8s') {
636 salt.cmdRun(master, target, "rm ${results}/k8s-*")
637 }
638
639 // save artifacts
Dmitrii Kabanov23901c22017-10-20 10:25:36 -0700640 addFiles(master, target, results, output_dir)
Petr Lomakin47fee0a2017-08-01 10:46:05 -0700641}
642
643/**
Tetiana Korchak3383cc92017-08-25 09:36:19 -0700644 * Generate test report
645 *
646 * @param target Host to run script from
Dmitrii Kabanov23901c22017-10-20 10:25:36 -0700647 * @param dockerImageLink Docker image link
Tetiana Korchak3383cc92017-08-25 09:36:19 -0700648 * @param output_dir Directory for results
Sergey Galkind1068e22018-02-13 13:59:32 +0400649 * @param results The reports directory
Tetiana Korchak3383cc92017-08-25 09:36:19 -0700650 */
Sergey Galkind1068e22018-02-13 13:59:32 +0400651def generateTestReport(master, target, dockerImageLink, output_dir, results = '/root/qa_results') {
Tetiana Korchak3383cc92017-08-25 09:36:19 -0700652 def report_file = 'jenkins_test_report.html'
Tetiana Korchak3383cc92017-08-25 09:36:19 -0700653 def salt = new com.mirantis.mk.Salt()
654 def common = new com.mirantis.mk.Common()
Dmitrii Kabanov23901c22017-10-20 10:25:36 -0700655 def dest_folder = '/opt/devops-qa-tools/generate_test_report/test_results'
656 salt.runSaltProcessStep(master, target, 'file.remove', ["${results}"])
657 salt.runSaltProcessStep(master, target, 'file.mkdir', ["${results}", "mode=777"])
658 def reports = ['report-tempest.json',
659 'report-rally.xml',
660 'report-k8s-e2e-tests.txt',
661 'report-ha.json',
662 'report-spt.txt']
663 for ( report in reports ) {
Tetiana Korchak3383cc92017-08-25 09:36:19 -0700664 if ( fileExists("${output_dir}${report}") ) {
665 common.infoMsg("Copying ${report} to docker container")
Dmitrii Kabanov23901c22017-10-20 10:25:36 -0700666 def items = sh(script: "base64 -w0 ${output_dir}${report} > ${output_dir}${report}_encoded; " +
667 "split -b 100KB -d -a 4 ${output_dir}${report}_encoded ${output_dir}${report}__; " +
668 "rm ${output_dir}${report}_encoded; " +
669 "find ${output_dir} -type f -name ${report}__* -printf \'%f\\n\' | sort", returnStdout: true)
670 for ( item in items.tokenize() ) {
671 def content = sh(script: "cat ${output_dir}${item}", returnStdout: true)
672 salt.cmdRun(master, target, "echo \"${content}\" >> ${results}/${report}_encoded", false, null, false)
673 sh(script: "rm ${output_dir}${item}")
Tetiana Korchak3383cc92017-08-25 09:36:19 -0700674 }
Dmitrii Kabanov23901c22017-10-20 10:25:36 -0700675 salt.cmdRun(master, target, "base64 -d ${results}/${report}_encoded > ${results}/${report}; " +
676 "rm ${results}/${report}_encoded", false, null, false)
Tetiana Korchak3383cc92017-08-25 09:36:19 -0700677 }
678 }
Tetiana Korchak3383cc92017-08-25 09:36:19 -0700679
Dmitrii Kabanov23901c22017-10-20 10:25:36 -0700680 def cmd = "jenkins_report.py --path /opt/devops-qa-tools/generate_test_report/; " +
681 "cp ${report_file} ${dest_folder}/${report_file}"
682 salt.cmdRun(master, target, "docker run -i --rm --net=host " +
683 "-v ${results}:${dest_folder} ${dockerImageLink} " +
684 "/bin/bash -c \"${cmd}\"")
685 def report_content = salt.getFileContent(master, target, "${results}/${report_file}")
Tetiana Korchak3383cc92017-08-25 09:36:19 -0700686 writeFile file: "${output_dir}${report_file}", text: report_content
687}
688
689/**
Dmitrii Kabanovd5f1c5f2017-08-30 14:51:41 -0700690 * Execute SPT tests
691 *
692 * @param target Host to run tests
Dmitrii Kabanov23901c22017-10-20 10:25:36 -0700693 * @param dockerImageLink Docker image link
Dmitrii Kabanovd5f1c5f2017-08-30 14:51:41 -0700694 * @param output_dir Directory for results
Dmitrii Kabanov23901c22017-10-20 10:25:36 -0700695 * @param ext_variables The list of external variables
Sergey Galkind1068e22018-02-13 13:59:32 +0400696 * @param results The reports directory
Dmitrii Kabanovd5f1c5f2017-08-30 14:51:41 -0700697 */
Sergey Galkind1068e22018-02-13 13:59:32 +0400698def runSptTests(master, target, dockerImageLink, output_dir, ext_variables = [], results = '/root/qa_results') {
Dmitrii Kabanovd5f1c5f2017-08-30 14:51:41 -0700699 def salt = new com.mirantis.mk.Salt()
Dmitrii Kabanov23901c22017-10-20 10:25:36 -0700700 def dest_folder = '/home/rally/qa_results'
701 salt.runSaltProcessStep(master, target, 'file.remove', ["${results}"])
702 salt.runSaltProcessStep(master, target, 'file.mkdir', ["${results}", "mode=777"])
703 def nodes = getNodeList(master)
704 def nodes_hw = getNodeList(master, 'G@virtual:physical')
705 def _pillar = salt.getPillar(master, 'I@keystone:server', 'keystone:server')
706 def keystone = _pillar['return'][0].values()[0]
707 def ssh_key = salt.getFileContent(master, 'I@salt:master', '/root/.ssh/id_rsa')
708 def env_vars = ( ['tempest_version=15.0.0',
709 "OS_USERNAME=${keystone.admin_name}",
710 "OS_PASSWORD=${keystone.admin_password}",
711 "OS_TENANT_NAME=${keystone.admin_tenant}",
712 "OS_AUTH_URL=http://${keystone.bind.private_address}:${keystone.bind.private_port}/v2.0",
713 "OS_REGION_NAME=${keystone.region}",
714 'OS_ENDPOINT_TYPE=admin'] + ext_variables ).join(' -e ')
715 salt.runSaltProcessStep(master, target, 'file.write', ["${results}/nodes.json", nodes])
716 salt.runSaltProcessStep(master, target, 'file.write', ["${results}/nodes_hw.json", nodes_hw])
717 def cmd = '/opt/devops-qa-tools/deployment/configure.sh; ' +
718 'sudo mkdir -p /root/.ssh; sudo chmod 700 /root/.ssh; ' +
719 "echo \\\"${ssh_key}\\\" | sudo tee /root/.ssh/id_rsa > /dev/null; " +
720 'sudo chmod 600 /root/.ssh/id_rsa; ' +
721 "sudo timmy -c simplified-performance-testing/config.yaml " +
722 "--nodes-json ${dest_folder}/nodes.json --log-file ${dest_folder}/docker-spt2.log; " +
723 "./simplified-performance-testing/SPT_parser.sh > ${dest_folder}/report-spt.txt; " +
724 "custom_spt_parser.sh ${dest_folder}/nodes_hw.json > ${dest_folder}/report-spt-hw.txt; " +
725 "cp /tmp/timmy/archives/general.tar.gz ${dest_folder}/results-spt.tar.gz"
726 salt.cmdRun(master, target, "docker run -i --rm --net=host -e ${env_vars} " +
727 "-v ${results}:${dest_folder} ${dockerImageLink} /bin/bash -c " +
728 "\"${cmd}\" > ${results}/docker-spt.log")
729 addFiles(master, target, results, output_dir)
Dmitrii Kabanovd5f1c5f2017-08-30 14:51:41 -0700730}
731
Dmitrii Kabanovd5f1c5f2017-08-30 14:51:41 -0700732/**
Oleksii Zhurba7b44ef12017-11-13 17:50:16 -0600733 * Configure docker container
734 *
735 * @param target Host to run container
736 * @param proxy Proxy for accessing github and pip
737 * @param testing_tools_repo Repo with testing tools: configuration script, skip-list, etc.
Oleksii Zhurba1579b972017-12-14 15:21:56 -0600738 * @param tempest_repo Tempest repo to clone. Can be upstream tempest (default, recommended), your customized tempest in local/remote repo or path inside container. If not specified, tempest will not be configured.
739 * @param tempest_endpoint_type internalURL or adminURL or publicURL to use in tests
Oleksii Zhurba198dd682018-09-07 18:16:59 -0500740 * @param tempest_version Version of tempest to use. This value will be just passed to configure.sh script (cvp-configuration repo).
Oleksii Zhurba1579b972017-12-14 15:21:56 -0600741 * @param conf_script_path Path to configuration script.
742 * @param ext_variables Some custom extra variables to add into container
Oleksii Zhurba7b44ef12017-11-13 17:50:16 -0600743 */
744def configureContainer(master, target, proxy, testing_tools_repo, tempest_repo,
Oleksii Zhurba198dd682018-09-07 18:16:59 -0500745 tempest_endpoint_type="internalURL", tempest_version="",
Oleksii Zhurba1579b972017-12-14 15:21:56 -0600746 conf_script_path="", ext_variables = []) {
Oleksii Zhurba7b44ef12017-11-13 17:50:16 -0600747 def salt = new com.mirantis.mk.Salt()
748 if (testing_tools_repo != "" ) {
Oleksii Zhurba0cda6e02018-06-20 14:53:19 -0500749 if (testing_tools_repo.contains('http://') || testing_tools_repo.contains('https://')) {
750 salt.cmdRun(master, target, "docker exec cvp git clone ${testing_tools_repo} cvp-configuration")
751 configure_script = conf_script_path != "" ? conf_script_path : "cvp-configuration/configure.sh"
752 }
753 else {
754 configure_script = testing_tools_repo
755 }
756 ext_variables.addAll("PROXY=${proxy}", "TEMPEST_REPO=${tempest_repo}",
757 "TEMPEST_ENDPOINT_TYPE=${tempest_endpoint_type}",
758 "tempest_version=${tempest_version}")
759 salt.cmdRun(master, target, "docker exec -e " + ext_variables.join(' -e ') + " cvp bash -c ${configure_script}")
Oleksii Zhurba7b44ef12017-11-13 17:50:16 -0600760 }
Oleksii Zhurba0cda6e02018-06-20 14:53:19 -0500761 else {
762 common.infoMsg("TOOLS_REPO is empty, no confguration is needed for container")
763 }
Oleksii Zhurba7b44ef12017-11-13 17:50:16 -0600764}
765
766/**
767 * Run Tempest
768 *
769 * @param target Host to run container
770 * @param test_pattern Test pattern to run
771 * @param skip_list Path to skip-list
772 * @param output_dir Directory on target host for storing results (containers is not a good place)
773 */
774def runCVPtempest(master, target, test_pattern="set=smoke", skip_list="", output_dir, output_filename="docker-tempest") {
775 def salt = new com.mirantis.mk.Salt()
776 def xml_file = "${output_filename}.xml"
Oleksii Zhurba44045312017-12-12 15:38:26 -0600777 def html_file = "${output_filename}.html"
Oleksii Zhurba7b44ef12017-11-13 17:50:16 -0600778 skip_list_cmd = ''
779 if (skip_list != '') {
780 skip_list_cmd = "--skip-list ${skip_list}"
781 }
Oleksii Zhurba77896d42018-05-25 18:11:30 -0500782 salt.cmdRun(master, target, "docker exec cvp rally verify start --pattern ${test_pattern} ${skip_list_cmd} --detailed")
Oleksii Zhurba7b44ef12017-11-13 17:50:16 -0600783 salt.cmdRun(master, target, "docker exec cvp rally verify report --type junit-xml --to /home/rally/${xml_file}")
Oleksii Zhurba44045312017-12-12 15:38:26 -0600784 salt.cmdRun(master, target, "docker exec cvp rally verify report --type html --to /home/rally/${html_file}")
Oleksii Zhurba7b44ef12017-11-13 17:50:16 -0600785 salt.cmdRun(master, target, "docker cp cvp:/home/rally/${xml_file} ${output_dir}")
Oleksii Zhurba44045312017-12-12 15:38:26 -0600786 salt.cmdRun(master, target, "docker cp cvp:/home/rally/${html_file} ${output_dir}")
Oleksii Zhurba77896d42018-05-25 18:11:30 -0500787 return salt.cmdRun(master, target, "docker exec cvp rally verify show | head -5 | tail -1 | " +
788 "awk '{print \$4}'")['return'][0].values()[0].split()[0]
Oleksii Zhurba7b44ef12017-11-13 17:50:16 -0600789}
790
791/**
792 * Run Rally
793 *
794 * @param target Host to run container
795 * @param test_pattern Test pattern to run
796 * @param scenarios_path Path to Rally scenarios
797 * @param output_dir Directory on target host for storing results (containers is not a good place)
798 */
799def runCVPrally(master, target, scenarios_path, output_dir, output_filename="docker-rally") {
800 def salt = new com.mirantis.mk.Salt()
801 def xml_file = "${output_filename}.xml"
Oleksii Zhurba7b44ef12017-11-13 17:50:16 -0600802 def html_file = "${output_filename}.html"
Oleksii Zhurba77896d42018-05-25 18:11:30 -0500803 salt.cmdRun(master, target, "docker exec cvp rally task start ${scenarios_path}")
Oleksii Zhurba7b44ef12017-11-13 17:50:16 -0600804 salt.cmdRun(master, target, "docker exec cvp rally task report --out ${html_file}")
Oleksii Zhurba1bf9be12018-01-17 15:20:00 -0600805 salt.cmdRun(master, target, "docker exec cvp rally task report --junit --out ${xml_file}")
Oleksii Zhurba7b44ef12017-11-13 17:50:16 -0600806 salt.cmdRun(master, target, "docker cp cvp:/home/rally/${xml_file} ${output_dir}")
807 salt.cmdRun(master, target, "docker cp cvp:/home/rally/${html_file} ${output_dir}")
808}
809
810
811/**
812 * Shutdown node
813 *
814 * @param target Host to run command
815 * @param mode How to shutdown node
816 * @param retries # of retries to make to check node status
817 */
818def shutdown_vm_node(master, target, mode, retries=200) {
819 def salt = new com.mirantis.mk.Salt()
820 def common = new com.mirantis.mk.Common()
821 if (mode == 'reboot') {
822 try {
823 def out = salt.runSaltCommand(master, 'local', ['expression': target, 'type': 'compound'], 'cmd.run', null, ['reboot'], null, 3, 3)
824 } catch (Exception e) {
825 common.warningMsg('Timeout from minion: node must be rebooting now')
826 }
827 common.warningMsg("Checking that minion is down")
828 status = "True"
829 for (i = 0; i < retries; i++) {
830 status = salt.minionsReachable(master, 'I@salt:master', target, null, 5, 1)
831 if (status != "True") {
832 break
833 }
834 }
835 if (status == "True") {
836 throw new Exception("Tired to wait for minion ${target} to stop responding")
837 }
838 }
839 if (mode == 'hard_shutdown' || mode == 'soft_shutdown') {
840 kvm = locate_node_on_kvm(master, target)
841 if (mode == 'soft_shutdown') {
842 salt.cmdRun(master, target, "shutdown -h 0")
843 }
844 if (mode == 'hard_shutdown') {
845 salt.cmdRun(master, kvm, "virsh destroy ${target}")
846 }
847 common.warningMsg("Checking that vm on kvm is in power off state")
848 status = 'running'
849 for (i = 0; i < retries; i++) {
850 status = check_vm_status(master, target, kvm)
851 echo "Current status - ${status}"
852 if (status != 'running') {
853 break
854 }
855 sleep (1)
856 }
857 if (status == 'running') {
858 throw new Exception("Tired to wait for node ${target} to shutdown")
859 }
860 }
861}
862
863
864/**
865 * Locate kvm where target host is located
866 *
867 * @param target Host to check
868 */
869def locate_node_on_kvm(master, target) {
870 def salt = new com.mirantis.mk.Salt()
871 def list = salt.runSaltProcessStep(master, "I@salt:control", 'cmd.run', ["virsh list --all | grep ' ${target}'"])['return'][0]
872 for (item in list.keySet()) {
873 if (list[item]) {
874 return item
875 }
876 }
877}
878
879/**
880 * Check target host status
881 *
882 * @param target Host to check
883 * @param kvm KVM node where target host is located
884 */
885def check_vm_status(master, target, kvm) {
886 def salt = new com.mirantis.mk.Salt()
887 def list = salt.runSaltProcessStep(master, "${kvm}", 'cmd.run', ["virsh list --all | grep ' ${target}'"])['return'][0]
888 for (item in list.keySet()) {
889 if (list[item]) {
890 return list[item].split()[2]
891 }
892 }
893}
894
895/**
896 * Find vip on nodes
897 *
898 * @param target Pattern, e.g. ctl*
899 */
900def get_vip_node(master, target) {
901 def salt = new com.mirantis.mk.Salt()
Oleksii Zhurba5f73cf62018-08-03 16:11:10 -0500902 def list = salt.runSaltProcessStep(master, "${target}", 'cmd.run', ["ip a | grep '/32'"])['return'][0]
Oleksii Zhurba7b44ef12017-11-13 17:50:16 -0600903 for (item in list.keySet()) {
904 if (list[item]) {
905 return item
906 }
907 }
908}
909
910/**
911 * Find vip on nodes
912 *
913 * @param target Host with cvp container
914 */
Oleksii Zhurba5250a9c2018-03-21 15:47:03 -0500915def openstack_cleanup(master, target, script_path="/home/rally/cvp-configuration/cleanup.sh") {
Oleksii Zhurba7b44ef12017-11-13 17:50:16 -0600916 def salt = new com.mirantis.mk.Salt()
917 salt.runSaltProcessStep(master, "${target}", 'cmd.run', ["docker exec cvp bash -c ${script_path}"])
918}
919
920
921/**
Petr Lomakin47fee0a2017-08-01 10:46:05 -0700922 * Cleanup
923 *
924 * @param target Host to run commands
Oleksii Zhurba1f4a6ff2018-06-27 16:45:17 -0500925 * @param name Name of container to remove
Petr Lomakin47fee0a2017-08-01 10:46:05 -0700926 */
Oleksii Zhurba1f4a6ff2018-06-27 16:45:17 -0500927def runCleanup(master, target, name='cvp') {
Petr Lomakin47fee0a2017-08-01 10:46:05 -0700928 def salt = new com.mirantis.mk.Salt()
Oleksii Zhurba1f4a6ff2018-06-27 16:45:17 -0500929 if ( salt.cmdRun(master, target, "docker ps -f name=${name} -q", false, null, false)['return'][0].values()[0] ) {
930 salt.cmdRun(master, target, "docker rm -f ${name}")
Oleksii Zhurba7b44ef12017-11-13 17:50:16 -0600931 }
Petr Lomakin47fee0a2017-08-01 10:46:05 -0700932}
Oleksii Zhurbabcb97e22017-10-05 14:10:39 -0500933/**
934 * Prepare venv for any python project
935 * Note: <repo_name>\/requirements.txt content will be used
936 * for this venv
937 *
938 * @param repo_url Repository url to clone
939 * @param proxy Proxy address to use
940 */
dtsapikovf2e1bb12018-11-29 18:49:48 +0400941def prepareVenv(repo_url, proxy, useSystemPackages=false) {
Oleksii Zhurbabcb97e22017-10-05 14:10:39 -0500942 def python = new com.mirantis.mk.Python()
943 repo_name = "${repo_url}".tokenize("/").last()
Oleksii Zhurbae711ebb2018-06-15 16:36:38 -0500944 if (repo_url.tokenize().size() > 1){
945 if (repo_url.tokenize()[1] == '-b'){
946 repo_name = repo_url.tokenize()[0].tokenize("/").last()
947 }
948 }
Oleksii Zhurba83e3e5c2018-06-27 16:59:29 -0500949 path_venv = "${env.WORKSPACE}/venv"
950 path_req = "${env.WORKSPACE}/${repo_name}/requirements.txt"
Oleksii Zhurbabcb97e22017-10-05 14:10:39 -0500951 sh "rm -rf ${repo_name}"
Oleksii Zhurba83e3e5c2018-06-27 16:59:29 -0500952 // this is temporary W/A for offline deployments
953 // Jenkins slave image has /opt/pip-mirror/ folder
954 // where pip wheels for cvp projects are located
955 if (proxy != 'offline') {
956 withEnv(["HTTPS_PROXY=${proxy}", "HTTP_PROXY=${proxy}", "https_proxy=${proxy}", "http_proxy=${proxy}"]) {
957 sh "git clone ${repo_url}"
dtsapikovf2e1bb12018-11-29 18:49:48 +0400958 python.setupVirtualenv(path_venv, "python2", [], path_req, true, useSystemPackages)
Oleksii Zhurba83e3e5c2018-06-27 16:59:29 -0500959 }
960 }
961 else {
Oleksii Zhurbabcb97e22017-10-05 14:10:39 -0500962 sh "git clone ${repo_url}"
Oleksii Zhurba83e3e5c2018-06-27 16:59:29 -0500963 sh "virtualenv ${path_venv} --python python2"
964 python.runVirtualenvCommand(path_venv, "pip install --no-index --find-links=/opt/pip-mirror/ -r ${path_req}", true)
Oleksii Zhurbabcb97e22017-10-05 14:10:39 -0500965 }
966}
967
Petr Lomakin47fee0a2017-08-01 10:46:05 -0700968/** Install docker if needed
969 *
970 * @param target Target node to install docker pkg
971 */
972def installDocker(master, target) {
973 def salt = new com.mirantis.mk.Salt()
974 if ( ! salt.runSaltProcessStep(master, target, 'pkg.version', ["docker-engine"]) ) {
975 salt.runSaltProcessStep(master, target, 'pkg.install', ["docker.io"])
976 }
977}