blob: 9ac618d4c87c4cd798a88cbf7d186c1deec3e2fe [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 *
Oleksii Zhurbad74bf1d2019-03-26 16:15:56 -050036 * @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 * @param mounts Map with mounts for container
Oleksii Zhurba1f4a6ff2018-06-27 16:45:17 -050042**/
43
Oleksii Zhurbad74bf1d2019-03-26 16:15:56 -050044def runContainer(Map params){
Oleksii Zhurba1f4a6ff2018-06-27 16:45:17 -050045 def common = new com.mirantis.mk.Common()
Oleksii Zhurbad74bf1d2019-03-26 16:15:56 -050046 defaults = ["name": "cvp", "env_var": [], "entrypoint": true]
47 params = defaults + params
48 def salt = new com.mirantis.mk.Salt()
Oleksii Zhurba1f4a6ff2018-06-27 16:45:17 -050049 def variables = ''
50 def entry_point = ''
Oleksii Zhurbad74bf1d2019-03-26 16:15:56 -050051 def tempest_conf_mount = ''
52 def mounts = ''
53 def cluster_name = salt.getPillar(params.master, 'I@salt:master', '_param:cluster_name')['return'][0].values()[0]
54 default_mounts = ["/etc/ssl/certs/": "/etc/ssl/certs/",
55 "/srv/salt/pki/${cluster_name}/": "/etc/certs",
56 "/root/test/": "/root/tempest/",
57 "/tmp/": "/tmp/",
58 "/etc/hosts": "/etc/hosts"]
59 params.mounts = default_mounts + params.mounts
60 if ( salt.cmdRun(params.master, params.target, "docker ps -f name=${params.name} -q", false, null, false)['return'][0].values()[0] ) {
61 salt.cmdRun(params.master, params.target, "docker rm -f ${params.name}")
Oleksii Zhurba1f4a6ff2018-06-27 16:45:17 -050062 }
Oleksii Zhurbad74bf1d2019-03-26 16:15:56 -050063 if (params.env_var.size() > 0) {
64 variables = ' -e ' + params.env_var.join(' -e ')
Oleksii Zhurba1f4a6ff2018-06-27 16:45:17 -050065 }
Oleksii Zhurbad74bf1d2019-03-26 16:15:56 -050066 if (params.entrypoint) {
Oleksii Zhurba1f4a6ff2018-06-27 16:45:17 -050067 entry_point = '--entrypoint /bin/bash'
68 }
Oleksii Zhurbad74bf1d2019-03-26 16:15:56 -050069 params.mounts.each { local, container ->
70 mounts = mounts + " -v ${local}:${container}"
71 }
72 salt.cmdRun(params.master, params.target, "docker run -tid --net=host --name=${params.name}" +
73 "${mounts} -u root ${entry_point} ${variables} ${params.dockerImageLink}")
Oleksii Zhurba1f4a6ff2018-06-27 16:45:17 -050074}
75
Oleksii Zhurbad74bf1d2019-03-26 16:15:56 -050076def runContainer(master, target, dockerImageLink, name='cvp', env_var=[], entrypoint=true, mounts=[:]){
77 def common = new com.mirantis.mk.Common()
78 common.infoMsg("This method will be deprecated. Convert you method call to use Map as input parameter")
79 // Convert to Map
80 params = ['master': master, 'target': target, 'dockerImageLink': dockerImageLink, 'name': name, 'env_var': env_var,
81 'entrypoint': entrypoint, 'mounts': mounts]
82 // Call new method with Map as parameter
83 return runContainer(params)
84}
Oleksii Zhurba1f4a6ff2018-06-27 16:45:17 -050085
Petr Lomakin47fee0a2017-08-01 10:46:05 -070086/**
Oleksii Zhurba4bcf07f2018-09-11 15:23:18 -050087 * Get v2 Keystone credentials from pillars
88 *
89 */
90def _get_keystone_creds_v2(master){
91 def salt = new com.mirantis.mk.Salt()
92 def common = new com.mirantis.mk.Common()
93 def keystone = []
Oleksii Zhurba5140ad32019-04-29 15:25:42 -050094 _pillar = false
Oleksii Zhurba4bcf07f2018-09-11 15:23:18 -050095 common.infoMsg("Fetching Keystone v2 credentials")
Oleksii Zhurba5140ad32019-04-29 15:25:42 -050096 _response = salt.runSaltProcessStep(master, 'I@keystone:server', 'pillar.get', 'keystone:server', null, false, 1)['return'][0]
97 for (i = 0; i < _response.keySet().size(); i++) {
98 if ( _response.values()[i] ) {
99 _pillar = _response.values()[i]
100 }
101 }
102 if (_pillar) {
103 keystone.add("OS_USERNAME=${_pillar.admin_name}")
104 keystone.add("OS_PASSWORD=${_pillar.admin_password}")
105 keystone.add("OS_TENANT_NAME=${_pillar.admin_tenant}")
106 keystone.add("OS_AUTH_URL=http://${_pillar.bind.private_address}:${_pillar.bind.private_port}/v2.0")
107 keystone.add("OS_REGION_NAME=${_pillar.region}")
108 keystone.add("OS_ENDPOINT_TYPE=admin")
109 return keystone
110 }
111 else {
112 throw new Exception("Cannot fetch Keystone v2 credentials. Response: ${_response}")
113 }
Oleksii Zhurba4bcf07f2018-09-11 15:23:18 -0500114}
115
116/**
117 * Get v3 Keystone credentials from pillars
118 *
119 */
120def _get_keystone_creds_v3(master){
121 def salt = new com.mirantis.mk.Salt()
122 def common = new com.mirantis.mk.Common()
Oleksii Zhurba5140ad32019-04-29 15:25:42 -0500123 _pillar = false
Oleksii Zhurba4bcf07f2018-09-11 15:23:18 -0500124 pillar_name = 'keystone:client:os_client_config:cfgs:root:content:clouds:admin_identity'
125 common.infoMsg("Fetching Keystone v3 credentials")
Oleksii Zhurba5140ad32019-04-29 15:25:42 -0500126 _response = salt.runSaltProcessStep(master, 'I@keystone:server', 'pillar.get', pillar_name, null, false, 1)['return'][0]
127 for (i = 0; i < _response.keySet().size(); i++) {
128 if ( _response.values()[i] ) {
129 _pillar = _response.values()[i]
130 }
131 }
Oleksii Zhurba4bcf07f2018-09-11 15:23:18 -0500132 def keystone = []
133 if (_pillar) {
134 keystone.add("OS_USERNAME=${_pillar.auth.username}")
135 keystone.add("OS_PASSWORD=${_pillar.auth.password}")
136 keystone.add("OS_TENANT_NAME=${_pillar.auth.project_name}")
137 keystone.add("OS_PROJECT_NAME=${_pillar.auth.project_name}")
138 keystone.add("OS_AUTH_URL=${_pillar.auth.auth_url}/v3")
139 keystone.add("OS_REGION_NAME=${_pillar.region_name}")
140 keystone.add("OS_IDENTITY_API_VERSION=${_pillar.identity_api_version}")
Oleksii Zhurba115474a2019-02-13 18:27:31 -0600141 keystone.add("OS_ENDPOINT_TYPE=internal")
Oleksii Zhurba4bcf07f2018-09-11 15:23:18 -0500142 keystone.add("OS_PROJECT_DOMAIN_NAME=${_pillar.auth.project_domain_name}")
143 keystone.add("OS_USER_DOMAIN_NAME=${_pillar.auth.user_domain_name}")
Oleksii Zhurba115474a2019-02-13 18:27:31 -0600144 // we mount /srv/salt/pki/${cluster_name}/:/etc/certs with certs for cvp container
145 keystone.add("OS_CACERT='/etc/certs/proxy-with-chain.crt'")
Oleksii Zhurba4bcf07f2018-09-11 15:23:18 -0500146 return keystone
147 }
148 else {
149 common.warningMsg("Failed to fetch Keystone v3 credentials")
150 return false
151 }
152}
153
154/**
Dmitrii Kabanov23901c22017-10-20 10:25:36 -0700155 * Get file content (encoded). The content encoded by Base64.
Dmitrii Kabanovd5f1c5f2017-08-30 14:51:41 -0700156 *
157 * @param target Compound target (should target only one host)
158 * @param file File path to read
Dmitrii Kabanov23901c22017-10-20 10:25:36 -0700159 * @return The encoded content of the file
Dmitrii Kabanovd5f1c5f2017-08-30 14:51:41 -0700160 */
Dmitrii Kabanov23901c22017-10-20 10:25:36 -0700161def getFileContentEncoded(master, target, file) {
Dmitrii Kabanovd5f1c5f2017-08-30 14:51:41 -0700162 def salt = new com.mirantis.mk.Salt()
Dmitrii Kabanov23901c22017-10-20 10:25:36 -0700163 def file_content = ''
164 def cmd = "base64 -w0 ${file} > ${file}_encoded; " +
165 "split -b 1MB -d ${file}_encoded ${file}__; " +
166 "rm ${file}_encoded"
167 salt.cmdRun(master, target, cmd, false, null, false)
168 def filename = file.tokenize('/').last()
169 def folder = file - filename
170 def parts = salt.runSaltProcessStep(master, target, 'file.find', ["${folder}", "type=f", "name=${filename}__*"])
171 for ( part in parts['return'][0].values()[0]) {
172 def _result = salt.cmdRun(master, target, "cat ${part}", false, null, false)
173 file_content = file_content + _result['return'][0].values()[0].replaceAll('Salt command execution success','')
174 }
175 salt.runSaltProcessStep(master, target, 'file.find', ["${folder}", "type=f", "name=${filename}__*", "delete"])
176 return file_content
177}
178
179/**
180 * Copy files from remote to local directory. The content of files will be
181 * decoded by Base64.
182 *
183 * @param target Compound target (should target only one host)
184 * @param folder The path to remote folder.
185 * @param output_dir The path to local folder.
186 */
187def addFiles(master, target, folder, output_dir) {
188 def salt = new com.mirantis.mk.Salt()
189 def _result = salt.runSaltProcessStep(master, target, 'file.find', ["${folder}", "type=f"])
190 def files = _result['return'][0].values()[0]
191 for (file in files) {
192 def file_content = getFileContentEncoded(master, target, "${file}")
193 def fileName = file.tokenize('/').last()
194 writeFile file: "${output_dir}${fileName}_encoded", text: file_content
195 def cmd = "base64 -d ${output_dir}${fileName}_encoded > ${output_dir}${fileName}; " +
196 "rm ${output_dir}${fileName}_encoded"
197 sh(script: cmd)
Dmitrii Kabanovd5f1c5f2017-08-30 14:51:41 -0700198 }
199}
200
201/**
202 * Get reclass value
203 *
204 * @param target The host for which the values will be provided
205 * @param filter Parameters divided by dots
206 * @return The pillar data
207 */
208def getReclassValue(master, target, filter) {
209 def common = new com.mirantis.mk.Common()
210 def salt = new com.mirantis.mk.Salt()
211 def items = filter.tokenize('.')
Dmitrii Kabanov23901c22017-10-20 10:25:36 -0700212 def _result = salt.cmdRun(master, 'I@salt:master', "reclass-salt -o json -p ${target}", false, null, false)
Dmitrii Kabanovd5f1c5f2017-08-30 14:51:41 -0700213 _result = common.parseJSON(_result['return'][0].values()[0])
Dmitrii Kabanov23901c22017-10-20 10:25:36 -0700214 for (int k = 0; k < items.size(); k++) {
Dmitrii Kabanovd5f1c5f2017-08-30 14:51:41 -0700215 if ( _result ) {
Dmitrii Kabanov23901c22017-10-20 10:25:36 -0700216 _result = _result["${items[k]}"]
Dmitrii Kabanovd5f1c5f2017-08-30 14:51:41 -0700217 }
218 }
219 return _result
220}
221
222/**
223 * Create list of nodes in JSON format.
224 *
225 * @param filter The Salt's matcher
226 * @return JSON list of nodes
227 */
228def getNodeList(master, filter = null) {
229 def salt = new com.mirantis.mk.Salt()
230 def common = new com.mirantis.mk.Common()
Dmitrii Kabanovd5f1c5f2017-08-30 14:51:41 -0700231 def nodes = []
Dmitrii Kabanovd5f1c5f2017-08-30 14:51:41 -0700232 def filtered_list = null
233 def controllers = salt.getMinions(master, 'I@nova:controller')
234 def hw_nodes = salt.getMinions(master, 'G@virtual:physical')
Dmitrii Kabanovd5f1c5f2017-08-30 14:51:41 -0700235 if ( filter ) {
236 filtered_list = salt.getMinions(master, filter)
Dmitrii Kabanovd5f1c5f2017-08-30 14:51:41 -0700237 }
238 def _result = salt.cmdRun(master, 'I@salt:master', "reclass-salt -o json -t", false, null, false)
239 def reclass_top = common.parseJSON(_result['return'][0].values()[0])
Dmitrii Kabanov23901c22017-10-20 10:25:36 -0700240 def nodesList = reclass_top['base'].keySet()
241 for (int i = 0; i < nodesList.size(); i++) {
Dmitrii Kabanovd5f1c5f2017-08-30 14:51:41 -0700242 if ( filtered_list ) {
Dmitrii Kabanov23901c22017-10-20 10:25:36 -0700243 if ( ! filtered_list.contains(nodesList[i]) ) {
Dmitrii Kabanovd5f1c5f2017-08-30 14:51:41 -0700244 continue
245 }
246 }
Dmitrii Kabanov23901c22017-10-20 10:25:36 -0700247 def ip = getReclassValue(master, nodesList[i], '_param.linux_single_interface.address')
248 def network_data = [ip: ip, name: 'management']
249 def roles = [nodesList[i].tokenize('.')[0]]
250 if ( controllers.contains(nodesList[i]) ) {
251 roles.add('controller')
Dmitrii Kabanovd5f1c5f2017-08-30 14:51:41 -0700252 }
Dmitrii Kabanov23901c22017-10-20 10:25:36 -0700253 if ( hw_nodes.contains(nodesList[i]) ) {
254 roles.add('hw_node')
Dmitrii Kabanovd5f1c5f2017-08-30 14:51:41 -0700255 }
Dmitrii Kabanov23901c22017-10-20 10:25:36 -0700256 nodes.add([id: i+1, ip: ip, roles: roles, network_data: [network_data]])
Dmitrii Kabanovd5f1c5f2017-08-30 14:51:41 -0700257 }
Dmitrii Kabanov23901c22017-10-20 10:25:36 -0700258 return common.prettify(nodes)
Dmitrii Kabanovd5f1c5f2017-08-30 14:51:41 -0700259}
260
Oleksii Zhurbabcb97e22017-10-05 14:10:39 -0500261/**
262 * Execute mcp sanity tests
Oleksii Zhurba1f4a6ff2018-06-27 16:45:17 -0500263 * Deprecated. Will be removed soon
Oleksii Zhurbabcb97e22017-10-05 14:10:39 -0500264 *
265 * @param salt_url Salt master url
266 * @param salt_credentials Salt credentials
267 * @param test_set Test set for mcp sanity framework
Oleksii Zhurba0a7b0702017-11-10 16:02:16 -0600268 * @param env_vars Additional environment variables for cvp-sanity-checks
Oleksii Zhurbabcb97e22017-10-05 14:10:39 -0500269 * @param output_dir Directory for results
270 */
Oleksii Zhurba0a7b0702017-11-10 16:02:16 -0600271def runSanityTests(salt_url, salt_credentials, test_set="", output_dir="validation_artifacts/", env_vars="") {
Oleksii Zhurbabcb97e22017-10-05 14:10:39 -0500272 def common = new com.mirantis.mk.Common()
Oleksii Zhurba0a7b0702017-11-10 16:02:16 -0600273 def creds = common.getCredentials(salt_credentials)
274 def username = creds.username
275 def password = creds.password
276 def settings = ""
277 if ( env_vars != "" ) {
278 for (var in env_vars.tokenize(";")) {
279 settings += "export ${var}; "
280 }
281 }
282 def script = ". ${env.WORKSPACE}/venv/bin/activate; ${settings}" +
Ievgeniia Zadorozhnac796f4f2019-10-11 13:35:38 +0300283 "pytest --junitxml ${output_dir}cvp_sanity.xml --tb=short -rs -sv ${env.WORKSPACE}/cvp-sanity-checks/cvp_checks/tests/${test_set}"
Oleksii Zhurbabcb97e22017-10-05 14:10:39 -0500284 withEnv(["SALT_USERNAME=${username}", "SALT_PASSWORD=${password}", "SALT_URL=${salt_url}"]) {
285 def statusCode = sh script:script, returnStatus:true
286 }
287}
Dmitrii Kabanovd5f1c5f2017-08-30 14:51:41 -0700288
289/**
Oleksii Zhurba4e366ff2018-02-16 20:06:52 -0600290 * Execute pytest framework tests
291 *
292 * @param salt_url Salt master url
293 * @param salt_credentials Salt credentials
294 * @param test_set Test set to run
295 * @param env_vars Additional environment variables for cvp-sanity-checks
296 * @param output_dir Directory for results
297 */
Oleksii Zhurba1f4a6ff2018-06-27 16:45:17 -0500298def runPyTests(salt_url, salt_credentials, test_set="", env_vars="", name='cvp', container_node="", remote_dir='/root/qa_results/', artifacts_dir='validation_artifacts/') {
299 def xml_file = "${name}_report.xml"
300 def common = new com.mirantis.mk.Common()
301 def salt = new com.mirantis.mk.Salt()
302 def creds = common.getCredentials(salt_credentials)
303 def username = creds.username
304 def password = creds.password
305 if (container_node != "") {
306 def saltMaster
307 saltMaster = salt.connection(salt_url, salt_credentials)
Ievgeniia Zadorozhnac796f4f2019-10-11 13:35:38 +0300308 def script = "pytest --junitxml ${xml_file} --tb=short -rs -sv ${test_set}"
Oleksii Zhurba1f4a6ff2018-06-27 16:45:17 -0500309 env_vars.addAll("SALT_USERNAME=${username}", "SALT_PASSWORD=${password}",
310 "SALT_URL=${salt_url}")
311 variables = ' -e ' + env_vars.join(' -e ')
312 salt.cmdRun(saltMaster, container_node, "docker exec ${variables} ${name} bash -c '${script}'", false)
313 salt.cmdRun(saltMaster, container_node, "docker cp ${name}:/var/lib/${xml_file} ${remote_dir}${xml_file}")
314 addFiles(saltMaster, container_node, remote_dir+xml_file, artifacts_dir)
315 }
316 else {
317 if (env_vars.size() > 0) {
318 variables = 'export ' + env_vars.join(';export ')
319 }
320 def script = ". ${env.WORKSPACE}/venv/bin/activate; ${variables}; " +
Ievgeniia Zadorozhnac796f4f2019-10-11 13:35:38 +0300321 "pytest --junitxml ${artifacts_dir}${xml_file} --tb=short -rs -sv ${env.WORKSPACE}/${test_set}"
Oleksii Zhurba1f4a6ff2018-06-27 16:45:17 -0500322 withEnv(["SALT_USERNAME=${username}", "SALT_PASSWORD=${password}", "SALT_URL=${salt_url}"]) {
323 def statusCode = sh script:script, returnStatus:true
324 }
325 }
326}
327
328/**
329 * Execute pytest framework tests
330 * For backward compatibility
331 * Will be removed soon
332 *
333 * @param salt_url Salt master url
334 * @param salt_credentials Salt credentials
335 * @param test_set Test set to run
336 * @param env_vars Additional environment variables for cvp-sanity-checks
337 * @param output_dir Directory for results
338 */
Oleksii Zhurba4e366ff2018-02-16 20:06:52 -0600339def runTests(salt_url, salt_credentials, test_set="", output_dir="validation_artifacts/", env_vars="") {
340 def common = new com.mirantis.mk.Common()
341 def creds = common.getCredentials(salt_credentials)
342 def username = creds.username
343 def password = creds.password
344 def settings = ""
345 if ( env_vars != "" ) {
346 for (var in env_vars.tokenize(";")) {
347 settings += "export ${var}; "
348 }
349 }
350 def script = ". ${env.WORKSPACE}/venv/bin/activate; ${settings}" +
Ievgeniia Zadorozhnac796f4f2019-10-11 13:35:38 +0300351 "pytest --junitxml ${output_dir}report.xml --tb=short -rs -sv ${env.WORKSPACE}/${test_set}"
Oleksii Zhurba4e366ff2018-02-16 20:06:52 -0600352 withEnv(["SALT_USERNAME=${username}", "SALT_PASSWORD=${password}", "SALT_URL=${salt_url}"]) {
353 def statusCode = sh script:script, returnStatus:true
354 }
355}
356
357/**
Petr Lomakin47fee0a2017-08-01 10:46:05 -0700358 * Execute tempest tests
359 *
360 * @param target Host to run tests
Dmitrii Kabanov23901c22017-10-20 10:25:36 -0700361 * @param dockerImageLink Docker image link
Petr Lomakin47fee0a2017-08-01 10:46:05 -0700362 * @param pattern If not false, will run only tests matched the pattern
363 * @param output_dir Directory for results
Dmitrii Kabanov999fda92017-11-10 00:18:30 -0800364 * @param confRepository Git repository with configuration files for Tempest
365 * @param confBranch Git branch which will be used during the checkout
366 * @param repository Git repository with Tempest
367 * @param version Version of Tempest (tag, branch or commit)
Sergey Galkind1068e22018-02-13 13:59:32 +0400368 * @param results The reports directory
Petr Lomakin47fee0a2017-08-01 10:46:05 -0700369 */
Sergey Galkind1068e22018-02-13 13:59:32 +0400370def runTempestTests(master, target, dockerImageLink, output_dir, confRepository, confBranch, repository, version, pattern = "false", results = '/root/qa_results') {
Petr Lomakin47fee0a2017-08-01 10:46:05 -0700371 def salt = new com.mirantis.mk.Salt()
372 def output_file = 'docker-tempest.log'
Dmitrii Kabanov23901c22017-10-20 10:25:36 -0700373 def dest_folder = '/home/rally/qa_results'
Dmitrii Kabanov999fda92017-11-10 00:18:30 -0800374 def skip_list = '--skip-list /opt/devops-qa-tools/deployment/skip_contrail.list'
Dmitrii Kabanov23901c22017-10-20 10:25:36 -0700375 salt.runSaltProcessStep(master, target, 'file.remove', ["${results}"])
376 salt.runSaltProcessStep(master, target, 'file.mkdir', ["${results}", "mode=777"])
377 def _pillar = salt.getPillar(master, 'I@keystone:server', 'keystone:server')
378 def keystone = _pillar['return'][0].values()[0]
Dmitry Tsapikovb6911922018-07-24 15:21:23 +0000379 def env_vars = ['tempest_version=15.0.0',
Dmitrii Kabanov23901c22017-10-20 10:25:36 -0700380 "OS_USERNAME=${keystone.admin_name}",
381 "OS_PASSWORD=${keystone.admin_password}",
382 "OS_TENANT_NAME=${keystone.admin_tenant}",
383 "OS_AUTH_URL=http://${keystone.bind.private_address}:${keystone.bind.private_port}/v2.0",
384 "OS_REGION_NAME=${keystone.region}",
385 'OS_ENDPOINT_TYPE=admin'].join(' -e ')
386 def cmd = '/opt/devops-qa-tools/deployment/configure.sh; '
Dmitrii Kabanov999fda92017-11-10 00:18:30 -0800387 if (confRepository != '' ) {
388 cmd = "git clone -b ${confBranch ?: 'master'} ${confRepository} test_config; " +
389 'rally deployment create --fromenv --name=tempest; rally deployment config; ' +
390 'rally verify create-verifier --name tempest_verifier --type tempest ' +
Dmitry Tsapikovb6911922018-07-24 15:21:23 +0000391 "--source ${repository ?: '/tmp/tempest/'} --version ${version: '15.0.0'}; " +
Dmitrii Kabanov999fda92017-11-10 00:18:30 -0800392 'rally verify configure-verifier --extend test_config/tempest/tempest.conf --show; '
393 skip_list = '--skip-list test_config/tempest/skip-list.yaml'
394 }
Dmitrii Kabanov23901c22017-10-20 10:25:36 -0700395 if (pattern == 'false') {
Dmitrii Kabanov999fda92017-11-10 00:18:30 -0800396 cmd += "rally verify start --pattern set=full ${skip_list} --detailed; "
Petr Lomakin47fee0a2017-08-01 10:46:05 -0700397 }
398 else {
Dmitrii Kabanov999fda92017-11-10 00:18:30 -0800399 cmd += "rally verify start --pattern set=${pattern} ${skip_list} --detailed; "
Petr Lomakin47fee0a2017-08-01 10:46:05 -0700400 }
Dmitrii Kabanov23901c22017-10-20 10:25:36 -0700401 cmd += "rally verify report --type json --to ${dest_folder}/report-tempest.json; " +
402 "rally verify report --type html --to ${dest_folder}/report-tempest.html"
mkraynovd49daf52018-07-12 16:11:14 +0400403 salt.cmdRun(master, target, "docker run -w /home/rally -i --rm --net=host -e ${env_vars} " +
Sergey Galkin193ef872017-11-29 14:20:35 +0400404 "-v ${results}:${dest_folder} --entrypoint /bin/bash ${dockerImageLink} " +
405 "-c \"${cmd}\" > ${results}/${output_file}")
Dmitrii Kabanov23901c22017-10-20 10:25:36 -0700406 addFiles(master, target, results, output_dir)
Petr Lomakin47fee0a2017-08-01 10:46:05 -0700407}
408
409/**
Oleg Basov40e502c2018-09-04 20:42:21 +0200410 * Make all-in-one scenario cmd for rally tests
411 *
412 * @param scenarios_path Path to scenarios folder/file
413 * @param skip_scenarios Comma-delimited list of scenarios names to skip
414 * @param bundle_file Bundle name to create
415*/
Oleg Basov06fce2a2018-11-09 21:39:03 +0100416def bundle_up_scenarios(scenarios_path, skip_scenarios, bundle_file = '' ) {
Oleg Basov40e502c2018-09-04 20:42:21 +0200417 def skip_names = ''
418 def skip_dirs = ''
419 def result = ''
420 if (skip_scenarios != ''){
421 for ( scen in skip_scenarios.split(',') ) {
422 if ( scen.contains('yaml')) {
423 skip_names += "! -name ${scen} "
424 }
425 else {
426 skip_dirs += "-path ${scenarios_path}/${scen} -prune -o "
427 }
428 }
429 }
Oleg Basov06fce2a2018-11-09 21:39:03 +0100430 if (bundle_file != '') {
431 result = "if [ -f ${scenarios_path} ]; then cp ${scenarios_path} ${bundle_file}; " +
Oleg Basov40e502c2018-09-04 20:42:21 +0200432 "else " +
433 "find -L ${scenarios_path} " + skip_dirs +
434 " -name '*.yaml' " + skip_names +
435 "-exec cat {} >> ${bundle_file} \\; ; " +
436 "sed -i '/---/d' ${bundle_file}; fi; "
Oleg Basov06fce2a2018-11-09 21:39:03 +0100437 } else {
438 result = "find -L ${scenarios_path} " + skip_dirs +
439 " -name '*.yaml' " + skip_names
440 }
Oleg Basov40e502c2018-09-04 20:42:21 +0200441
442 return result
443}
444
445/**
Petr Lomakin47fee0a2017-08-01 10:46:05 -0700446 * Execute rally tests
447 *
448 * @param target Host to run tests
Dmitrii Kabanov23901c22017-10-20 10:25:36 -0700449 * @param dockerImageLink Docker image link
Oleg Basov20afb152018-06-10 03:09:25 +0200450 * @param platform What do we have underneath (openstack/k8s)
Petr Lomakin47fee0a2017-08-01 10:46:05 -0700451 * @param output_dir Directory for results
Oleg Basov40e502c2018-09-04 20:42:21 +0200452 * @param config_repo Git repository with with files for Rally
453 * @param config_branch Git config repo branch which will be used during the checkout
454 * @param plugins_repo Git repository with Rally plugins
455 * @param plugins_branch Git plugins repo branch which will be used during the checkout
Oleg Basov20afb152018-06-10 03:09:25 +0200456 * @param scenarios Directory inside repo with specific scenarios
Oleg Basov40e502c2018-09-04 20:42:21 +0200457 * @param sl_scenarios Directory inside repo with specific scenarios for stacklight
Oleg Basov20afb152018-06-10 03:09:25 +0200458 * @param tasks_args_file Argument file that is used for throttling settings
Dmitrii Kabanov23901c22017-10-20 10:25:36 -0700459 * @param ext_variables The list of external variables
Sergey Galkind1068e22018-02-13 13:59:32 +0400460 * @param results The reports directory
Petr Lomakin47fee0a2017-08-01 10:46:05 -0700461 */
Oleg Basov40e502c2018-09-04 20:42:21 +0200462def runRallyTests(master, target, dockerImageLink, platform, output_dir, config_repo, config_branch, plugins_repo, plugins_branch, scenarios, sl_scenarios = '', tasks_args_file = '', ext_variables = [], results = '/root/qa_results', skip_list = '') {
Petr Lomakin47fee0a2017-08-01 10:46:05 -0700463 def salt = new com.mirantis.mk.Salt()
464 def output_file = 'docker-rally.log'
Dmitrii Kabanov23901c22017-10-20 10:25:36 -0700465 def dest_folder = '/home/rally/qa_results'
Oleg Basov20afb152018-06-10 03:09:25 +0200466 def env_vars = []
467 def rally_extra_args = ''
Oleg Basov40e502c2018-09-04 20:42:21 +0200468 def cmd_rally_plugins =
469 "git clone -b ${plugins_branch ?: 'master'} ${plugins_repo} /tmp/plugins; " +
470 "sudo pip install --upgrade /tmp/plugins; "
Oleg Basov20afb152018-06-10 03:09:25 +0200471 def cmd_rally_init = ''
Oleg Basov40e502c2018-09-04 20:42:21 +0200472 def cmd_rally_checkout = "git clone -b ${config_branch ?: 'master'} ${config_repo} test_config; "
Oleg Basov20afb152018-06-10 03:09:25 +0200473 def cmd_rally_start = ''
474 def cmd_rally_task_args = ''
Oleg Basov40e502c2018-09-04 20:42:21 +0200475 def cmd_rally_stacklight = ''
476 def cmd_rally_report = ''
Dmitrii Kabanov23901c22017-10-20 10:25:36 -0700477 salt.runSaltProcessStep(master, target, 'file.remove', ["${results}"])
478 salt.runSaltProcessStep(master, target, 'file.mkdir', ["${results}", "mode=777"])
Oleg Basov40e502c2018-09-04 20:42:21 +0200479 if (platform['type'] == 'openstack') {
Oleg Basov4a697fc2018-11-06 18:34:08 +0100480 def _pillar = salt.getPillar(master, 'I@keystone:server', 'keystone')
Oleg Basov20afb152018-06-10 03:09:25 +0200481 def keystone = _pillar['return'][0].values()[0]
482 env_vars = ( ['tempest_version=15.0.0',
Oleg Basov4a697fc2018-11-06 18:34:08 +0100483 "OS_USERNAME=${keystone.server.admin_name}",
484 "OS_PASSWORD=${keystone.server.admin_password}",
485 "OS_TENANT_NAME=${keystone.server.admin_tenant}",
486 "OS_AUTH_URL=http://${keystone.server.bind.private_address}:${keystone.server.bind.private_port}" +
487 "/v${keystone.client.os_client_config.cfgs.root.content.clouds.admin_identity.identity_api_version}",
488 "OS_REGION_NAME=${keystone.server.region}",
Oleg Basov20afb152018-06-10 03:09:25 +0200489 'OS_ENDPOINT_TYPE=admin'] + ext_variables ).join(' -e ')
Oleg Basov40e502c2018-09-04 20:42:21 +0200490 cmd_rally_init = 'rally db create; ' +
491 'rally deployment create --fromenv --name=existing; ' +
492 'rally deployment config; '
493 if (platform['stacklight_enabled'] == true) {
Oleg Basov418bfc42018-10-28 15:01:22 +0100494 def _pillar_grafana = salt.getPillar(master, 'I@grafana:client', 'grafana:client:server')
495 def grafana = _pillar_grafana['return'][0].values()[0]
Oleg Basov40e502c2018-09-04 20:42:21 +0200496 cmd_rally_stacklight = bundle_up_scenarios(sl_scenarios, skip_list, "scenarios_${platform.type}_stacklight.yaml")
Oleg Basov418bfc42018-10-28 15:01:22 +0100497 cmd_rally_stacklight += "sed -i 's/grafana_password: .*/grafana_password: ${grafana.password}/' " +
498 "test_config/job-params-stacklight.yaml; " +
499 "rally $rally_extra_args task start scenarios_${platform.type}_stacklight.yaml " +
Oleg Basov40e502c2018-09-04 20:42:21 +0200500 "--task-args-file test_config/job-params-stacklight.yaml; "
Oleg Basov20afb152018-06-10 03:09:25 +0200501 }
Oleg Basov40e502c2018-09-04 20:42:21 +0200502 } else if (platform['type'] == 'k8s') {
Oleg Basov20afb152018-06-10 03:09:25 +0200503 rally_extra_args = "--debug --log-file ${dest_folder}/task.log"
Oleg Basov3e050d32018-07-03 22:10:56 +0200504 def _pillar = salt.getPillar(master, 'I@kubernetes:master and *01*', 'kubernetes:master')
505 def kubernetes = _pillar['return'][0].values()[0]
506 env_vars = [
Oleg Basov491942d2018-10-17 16:11:25 +0200507 "KUBERNETES_HOST=http://${kubernetes.apiserver.vip_address}" +
Oleg Basov3e050d32018-07-03 22:10:56 +0200508 ":${kubernetes.apiserver.insecure_port}",
509 "KUBERNETES_CERT_AUTH=${dest_folder}/k8s-ca.crt",
510 "KUBERNETES_CLIENT_KEY=${dest_folder}/k8s-client.key",
511 "KUBERNETES_CLIENT_CERT=${dest_folder}/k8s-client.crt"].join(' -e ')
512 def k8s_ca = salt.getReturnValues(salt.runSaltProcessStep(master,
Oleg Basov20afb152018-06-10 03:09:25 +0200513 'I@kubernetes:master and *01*', 'cmd.run',
Oleg Basov3e050d32018-07-03 22:10:56 +0200514 ["cat /etc/kubernetes/ssl/ca-kubernetes.crt"]))
515 def k8s_client_key = salt.getReturnValues(salt.runSaltProcessStep(master,
516 'I@kubernetes:master and *01*', 'cmd.run',
517 ["cat /etc/kubernetes/ssl/kubelet-client.key"]))
518 def k8s_client_crt = salt.getReturnValues(salt.runSaltProcessStep(master,
519 'I@kubernetes:master and *01*', 'cmd.run',
520 ["cat /etc/kubernetes/ssl/kubelet-client.crt"]))
Oleg Basov20afb152018-06-10 03:09:25 +0200521 def tmp_dir = '/tmp/kube'
522 salt.runSaltProcessStep(master, target, 'file.mkdir', ["${tmp_dir}", "mode=777"])
Oleg Basov3e050d32018-07-03 22:10:56 +0200523 writeFile file: "${tmp_dir}/k8s-ca.crt", text: k8s_ca
524 writeFile file: "${tmp_dir}/k8s-client.key", text: k8s_client_key
525 writeFile file: "${tmp_dir}/k8s-client.crt", text: k8s_client_crt
Oleg Basov20afb152018-06-10 03:09:25 +0200526 salt.cmdRun(master, target, "mv ${tmp_dir}/* ${results}/")
527 salt.runSaltProcessStep(master, target, 'file.rmdir', ["${tmp_dir}"])
Oleg Basov40e502c2018-09-04 20:42:21 +0200528 cmd_rally_init = "rally db recreate; " +
Oleg Basov3e050d32018-07-03 22:10:56 +0200529 "rally env create --name k8s --from-sysenv; " +
Oleg Basov20afb152018-06-10 03:09:25 +0200530 "rally env check k8s; "
Oleg Basov20afb152018-06-10 03:09:25 +0200531 } else {
532 throw new Exception("Platform ${platform} is not supported yet")
Dmitrii Kabanov999fda92017-11-10 00:18:30 -0800533 }
Oleg Basov06fce2a2018-11-09 21:39:03 +0100534 switch(tasks_args_file) {
535 case 'none':
536 cmd_rally_task_args = '; '
Oleg Basov20afb152018-06-10 03:09:25 +0200537 break
Oleg Basov06fce2a2018-11-09 21:39:03 +0100538 case '':
539 cmd_rally_task_args = '--task-args-file test_config/job-params-light.yaml; '
540 break
541 default:
542 cmd_rally_task_args = "--task-args-file ${tasks_args_file}; "
543 break
544 }
545 if (platform['type'] == 'k8s') {
546 cmd_rally_start = "for task in \\\$(" + bundle_up_scenarios(scenarios, skip_list) +
547 "); do " +
548 "rally $rally_extra_args task start \\\$task ${cmd_rally_task_args}" +
549 "done; "
550 } else {
551 cmd_rally_checkout += bundle_up_scenarios(scenarios, skip_list, "scenarios_${platform.type}.yaml")
552 cmd_rally_start = "rally $rally_extra_args task start " +
553 "scenarios_${platform.type}.yaml ${cmd_rally_task_args}"
Oleg Basov20afb152018-06-10 03:09:25 +0200554 }
Oleg Basovaaeb51f2018-10-17 01:07:10 +0200555 cmd_rally_report= "rally task export --uuid \\\$(rally task list --uuids-only --status finished) " +
556 "--type junit-xml --to ${dest_folder}/report-rally.xml; " +
557 "rally task report --uuid \\\$(rally task list --uuids-only --status finished) " +
558 "--out ${dest_folder}/report-rally.html"
559
Oleg Basov40e502c2018-09-04 20:42:21 +0200560 full_cmd = 'set -xe; ' + cmd_rally_plugins +
561 cmd_rally_init + cmd_rally_checkout +
562 'set +e; ' + cmd_rally_start +
Oleg Basov06fce2a2018-11-09 21:39:03 +0100563 cmd_rally_stacklight +
Oleg Basov40e502c2018-09-04 20:42:21 +0200564 cmd_rally_report
Sergey Galkinf89509d2018-03-19 15:24:17 +0400565 salt.runSaltProcessStep(master, target, 'file.touch', ["${results}/rally.db"])
566 salt.cmdRun(master, target, "chmod 666 ${results}/rally.db")
mkraynovd49daf52018-07-12 16:11:14 +0400567 salt.cmdRun(master, target, "docker run -w /home/rally -i --rm --net=host -e ${env_vars} " +
Sergey Galkin3c1e9e22018-01-12 16:31:53 +0400568 "-v ${results}:${dest_folder} " +
mkraynovd49daf52018-07-12 16:11:14 +0400569 "-v ${results}/rally.db:/home/rally/data/rally.db " +
Sergey Galkin3c1e9e22018-01-12 16:31:53 +0400570 "--entrypoint /bin/bash ${dockerImageLink} " +
571 "-c \"${full_cmd}\" > ${results}/${output_file}")
Dmitrii Kabanov23901c22017-10-20 10:25:36 -0700572 addFiles(master, target, results, output_dir)
Petr Lomakin47fee0a2017-08-01 10:46:05 -0700573}
574
575/**
Tetiana Korchak3383cc92017-08-25 09:36:19 -0700576 * Generate test report
577 *
578 * @param target Host to run script from
Dmitrii Kabanov23901c22017-10-20 10:25:36 -0700579 * @param dockerImageLink Docker image link
Tetiana Korchak3383cc92017-08-25 09:36:19 -0700580 * @param output_dir Directory for results
Sergey Galkind1068e22018-02-13 13:59:32 +0400581 * @param results The reports directory
Tetiana Korchak3383cc92017-08-25 09:36:19 -0700582 */
Sergey Galkind1068e22018-02-13 13:59:32 +0400583def generateTestReport(master, target, dockerImageLink, output_dir, results = '/root/qa_results') {
Tetiana Korchak3383cc92017-08-25 09:36:19 -0700584 def report_file = 'jenkins_test_report.html'
Tetiana Korchak3383cc92017-08-25 09:36:19 -0700585 def salt = new com.mirantis.mk.Salt()
586 def common = new com.mirantis.mk.Common()
Dmitrii Kabanov23901c22017-10-20 10:25:36 -0700587 def dest_folder = '/opt/devops-qa-tools/generate_test_report/test_results'
588 salt.runSaltProcessStep(master, target, 'file.remove', ["${results}"])
589 salt.runSaltProcessStep(master, target, 'file.mkdir', ["${results}", "mode=777"])
590 def reports = ['report-tempest.json',
591 'report-rally.xml',
592 'report-k8s-e2e-tests.txt',
593 'report-ha.json',
594 'report-spt.txt']
595 for ( report in reports ) {
Tetiana Korchak3383cc92017-08-25 09:36:19 -0700596 if ( fileExists("${output_dir}${report}") ) {
597 common.infoMsg("Copying ${report} to docker container")
Dmitrii Kabanov23901c22017-10-20 10:25:36 -0700598 def items = sh(script: "base64 -w0 ${output_dir}${report} > ${output_dir}${report}_encoded; " +
599 "split -b 100KB -d -a 4 ${output_dir}${report}_encoded ${output_dir}${report}__; " +
600 "rm ${output_dir}${report}_encoded; " +
601 "find ${output_dir} -type f -name ${report}__* -printf \'%f\\n\' | sort", returnStdout: true)
602 for ( item in items.tokenize() ) {
603 def content = sh(script: "cat ${output_dir}${item}", returnStdout: true)
604 salt.cmdRun(master, target, "echo \"${content}\" >> ${results}/${report}_encoded", false, null, false)
605 sh(script: "rm ${output_dir}${item}")
Tetiana Korchak3383cc92017-08-25 09:36:19 -0700606 }
Dmitrii Kabanov23901c22017-10-20 10:25:36 -0700607 salt.cmdRun(master, target, "base64 -d ${results}/${report}_encoded > ${results}/${report}; " +
608 "rm ${results}/${report}_encoded", false, null, false)
Tetiana Korchak3383cc92017-08-25 09:36:19 -0700609 }
610 }
Tetiana Korchak3383cc92017-08-25 09:36:19 -0700611
Dmitrii Kabanov23901c22017-10-20 10:25:36 -0700612 def cmd = "jenkins_report.py --path /opt/devops-qa-tools/generate_test_report/; " +
613 "cp ${report_file} ${dest_folder}/${report_file}"
614 salt.cmdRun(master, target, "docker run -i --rm --net=host " +
615 "-v ${results}:${dest_folder} ${dockerImageLink} " +
616 "/bin/bash -c \"${cmd}\"")
617 def report_content = salt.getFileContent(master, target, "${results}/${report_file}")
Tetiana Korchak3383cc92017-08-25 09:36:19 -0700618 writeFile file: "${output_dir}${report_file}", text: report_content
619}
620
621/**
Dmitrii Kabanovd5f1c5f2017-08-30 14:51:41 -0700622 * Execute SPT tests
623 *
624 * @param target Host to run tests
Dmitrii Kabanov23901c22017-10-20 10:25:36 -0700625 * @param dockerImageLink Docker image link
Dmitrii Kabanovd5f1c5f2017-08-30 14:51:41 -0700626 * @param output_dir Directory for results
Dmitrii Kabanov23901c22017-10-20 10:25:36 -0700627 * @param ext_variables The list of external variables
Sergey Galkind1068e22018-02-13 13:59:32 +0400628 * @param results The reports directory
Dmitrii Kabanovd5f1c5f2017-08-30 14:51:41 -0700629 */
Sergey Galkind1068e22018-02-13 13:59:32 +0400630def runSptTests(master, target, dockerImageLink, output_dir, ext_variables = [], results = '/root/qa_results') {
Dmitrii Kabanovd5f1c5f2017-08-30 14:51:41 -0700631 def salt = new com.mirantis.mk.Salt()
Dmitrii Kabanov23901c22017-10-20 10:25:36 -0700632 def dest_folder = '/home/rally/qa_results'
633 salt.runSaltProcessStep(master, target, 'file.remove', ["${results}"])
634 salt.runSaltProcessStep(master, target, 'file.mkdir', ["${results}", "mode=777"])
635 def nodes = getNodeList(master)
636 def nodes_hw = getNodeList(master, 'G@virtual:physical')
637 def _pillar = salt.getPillar(master, 'I@keystone:server', 'keystone:server')
638 def keystone = _pillar['return'][0].values()[0]
639 def ssh_key = salt.getFileContent(master, 'I@salt:master', '/root/.ssh/id_rsa')
640 def env_vars = ( ['tempest_version=15.0.0',
641 "OS_USERNAME=${keystone.admin_name}",
642 "OS_PASSWORD=${keystone.admin_password}",
643 "OS_TENANT_NAME=${keystone.admin_tenant}",
644 "OS_AUTH_URL=http://${keystone.bind.private_address}:${keystone.bind.private_port}/v2.0",
645 "OS_REGION_NAME=${keystone.region}",
646 'OS_ENDPOINT_TYPE=admin'] + ext_variables ).join(' -e ')
647 salt.runSaltProcessStep(master, target, 'file.write', ["${results}/nodes.json", nodes])
648 salt.runSaltProcessStep(master, target, 'file.write', ["${results}/nodes_hw.json", nodes_hw])
649 def cmd = '/opt/devops-qa-tools/deployment/configure.sh; ' +
650 'sudo mkdir -p /root/.ssh; sudo chmod 700 /root/.ssh; ' +
651 "echo \\\"${ssh_key}\\\" | sudo tee /root/.ssh/id_rsa > /dev/null; " +
652 'sudo chmod 600 /root/.ssh/id_rsa; ' +
653 "sudo timmy -c simplified-performance-testing/config.yaml " +
654 "--nodes-json ${dest_folder}/nodes.json --log-file ${dest_folder}/docker-spt2.log; " +
655 "./simplified-performance-testing/SPT_parser.sh > ${dest_folder}/report-spt.txt; " +
656 "custom_spt_parser.sh ${dest_folder}/nodes_hw.json > ${dest_folder}/report-spt-hw.txt; " +
657 "cp /tmp/timmy/archives/general.tar.gz ${dest_folder}/results-spt.tar.gz"
658 salt.cmdRun(master, target, "docker run -i --rm --net=host -e ${env_vars} " +
659 "-v ${results}:${dest_folder} ${dockerImageLink} /bin/bash -c " +
660 "\"${cmd}\" > ${results}/docker-spt.log")
661 addFiles(master, target, results, output_dir)
Dmitrii Kabanovd5f1c5f2017-08-30 14:51:41 -0700662}
663
Dmitrii Kabanovd5f1c5f2017-08-30 14:51:41 -0700664/**
Oleksii Zhurba7b44ef12017-11-13 17:50:16 -0600665 * Configure docker container
666 *
667 * @param target Host to run container
668 * @param proxy Proxy for accessing github and pip
669 * @param testing_tools_repo Repo with testing tools: configuration script, skip-list, etc.
Oleksii Zhurba1579b972017-12-14 15:21:56 -0600670 * @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.
671 * @param tempest_endpoint_type internalURL or adminURL or publicURL to use in tests
Oleksii Zhurba198dd682018-09-07 18:16:59 -0500672 * @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 -0600673 * @param conf_script_path Path to configuration script.
674 * @param ext_variables Some custom extra variables to add into container
Oleksii Zhurbad2bb3f22019-05-15 12:20:28 -0500675 * @param container_name Name of container to use
Oleksii Zhurba7b44ef12017-11-13 17:50:16 -0600676 */
677def configureContainer(master, target, proxy, testing_tools_repo, tempest_repo,
Oleksii Zhurba198dd682018-09-07 18:16:59 -0500678 tempest_endpoint_type="internalURL", tempest_version="",
Oleksii Zhurbad2bb3f22019-05-15 12:20:28 -0500679 conf_script_path="", ext_variables = [], container_name="cvp") {
Oleksii Zhurba7b44ef12017-11-13 17:50:16 -0600680 def salt = new com.mirantis.mk.Salt()
Oleksii Zhurbaf1390b62019-04-04 12:44:09 -0500681 def common = new com.mirantis.mk.Common()
Oleksii Zhurba7b44ef12017-11-13 17:50:16 -0600682 if (testing_tools_repo != "" ) {
Oleksii Zhurbae0326f62019-04-04 15:58:21 -0500683 workdir = ''
Oleksii Zhurba0cda6e02018-06-20 14:53:19 -0500684 if (testing_tools_repo.contains('http://') || testing_tools_repo.contains('https://')) {
Oleksii Zhurbad2bb3f22019-05-15 12:20:28 -0500685 salt.cmdRun(master, target, "docker exec ${container_name} git clone ${testing_tools_repo} cvp-configuration")
Oleksii Zhurba0cda6e02018-06-20 14:53:19 -0500686 configure_script = conf_script_path != "" ? conf_script_path : "cvp-configuration/configure.sh"
687 }
688 else {
689 configure_script = testing_tools_repo
Oleksii Zhurbae0326f62019-04-04 15:58:21 -0500690 workdir = ' -w /var/lib/'
Oleksii Zhurba0cda6e02018-06-20 14:53:19 -0500691 }
692 ext_variables.addAll("PROXY=${proxy}", "TEMPEST_REPO=${tempest_repo}",
693 "TEMPEST_ENDPOINT_TYPE=${tempest_endpoint_type}",
694 "tempest_version=${tempest_version}")
Oleksii Zhurbae0326f62019-04-04 15:58:21 -0500695 salt.cmdRun(master, target, "docker exec -e " + ext_variables.join(' -e ') + " ${workdir} ${container_name} bash -c ${configure_script}")
Oleksii Zhurba7b44ef12017-11-13 17:50:16 -0600696 }
Oleksii Zhurba0cda6e02018-06-20 14:53:19 -0500697 else {
Oleksii Zhurbaf1390b62019-04-04 12:44:09 -0500698 common.infoMsg("TOOLS_REPO is empty, no configuration is needed for this container")
Oleksii Zhurba0cda6e02018-06-20 14:53:19 -0500699 }
Oleksii Zhurba7b44ef12017-11-13 17:50:16 -0600700}
701
702/**
703 * Run Tempest
704 *
705 * @param target Host to run container
706 * @param test_pattern Test pattern to run
707 * @param skip_list Path to skip-list
708 * @param output_dir Directory on target host for storing results (containers is not a good place)
709 */
710def runCVPtempest(master, target, test_pattern="set=smoke", skip_list="", output_dir, output_filename="docker-tempest") {
711 def salt = new com.mirantis.mk.Salt()
712 def xml_file = "${output_filename}.xml"
Oleksii Zhurba44045312017-12-12 15:38:26 -0600713 def html_file = "${output_filename}.html"
Oleksii Zhurba7b44ef12017-11-13 17:50:16 -0600714 skip_list_cmd = ''
715 if (skip_list != '') {
716 skip_list_cmd = "--skip-list ${skip_list}"
717 }
Oleksii Zhurba77896d42018-05-25 18:11:30 -0500718 salt.cmdRun(master, target, "docker exec cvp rally verify start --pattern ${test_pattern} ${skip_list_cmd} --detailed")
Oleksii Zhurba7b44ef12017-11-13 17:50:16 -0600719 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 -0600720 salt.cmdRun(master, target, "docker exec cvp rally verify report --type html --to /home/rally/${html_file}")
Oleksii Zhurba7b44ef12017-11-13 17:50:16 -0600721 salt.cmdRun(master, target, "docker cp cvp:/home/rally/${xml_file} ${output_dir}")
Oleksii Zhurba44045312017-12-12 15:38:26 -0600722 salt.cmdRun(master, target, "docker cp cvp:/home/rally/${html_file} ${output_dir}")
Oleksii Zhurba77896d42018-05-25 18:11:30 -0500723 return salt.cmdRun(master, target, "docker exec cvp rally verify show | head -5 | tail -1 | " +
724 "awk '{print \$4}'")['return'][0].values()[0].split()[0]
Oleksii Zhurba7b44ef12017-11-13 17:50:16 -0600725}
726
727/**
728 * Run Rally
729 *
730 * @param target Host to run container
731 * @param test_pattern Test pattern to run
732 * @param scenarios_path Path to Rally scenarios
733 * @param output_dir Directory on target host for storing results (containers is not a good place)
Oleksii Zhurbad2bb3f22019-05-15 12:20:28 -0500734 * @param container_name Name of container to use
Oleksii Zhurba7b44ef12017-11-13 17:50:16 -0600735 */
Oleksii Zhurbad2bb3f22019-05-15 12:20:28 -0500736def runCVPrally(master, target, scenarios_path, output_dir, output_filename="docker-rally", container_name="cvp") {
Oleksii Zhurba7b44ef12017-11-13 17:50:16 -0600737 def salt = new com.mirantis.mk.Salt()
738 def xml_file = "${output_filename}.xml"
Oleksii Zhurba7b44ef12017-11-13 17:50:16 -0600739 def html_file = "${output_filename}.html"
Oleksii Zhurba365eba62019-05-20 14:25:49 -0500740 salt.cmdRun(master, target, "docker exec ${container_name} rally task start ${scenarios_path}", false)
741 salt.cmdRun(master, target, "docker exec ${container_name} rally task report --out /home/rally/${html_file}", false)
742 salt.cmdRun(master, target, "docker exec ${container_name} rally task report --junit --out /home/rally/${xml_file}", false)
Oleksii Zhurbad2bb3f22019-05-15 12:20:28 -0500743 salt.cmdRun(master, target, "docker cp ${container_name}:/home/rally/${xml_file} ${output_dir}")
744 salt.cmdRun(master, target, "docker cp ${container_name}:/home/rally/${html_file} ${output_dir}")
Oleksii Zhurba7b44ef12017-11-13 17:50:16 -0600745}
746
747
748/**
749 * Shutdown node
750 *
751 * @param target Host to run command
752 * @param mode How to shutdown node
753 * @param retries # of retries to make to check node status
754 */
755def shutdown_vm_node(master, target, mode, retries=200) {
756 def salt = new com.mirantis.mk.Salt()
757 def common = new com.mirantis.mk.Common()
758 if (mode == 'reboot') {
759 try {
760 def out = salt.runSaltCommand(master, 'local', ['expression': target, 'type': 'compound'], 'cmd.run', null, ['reboot'], null, 3, 3)
761 } catch (Exception e) {
762 common.warningMsg('Timeout from minion: node must be rebooting now')
763 }
764 common.warningMsg("Checking that minion is down")
765 status = "True"
766 for (i = 0; i < retries; i++) {
767 status = salt.minionsReachable(master, 'I@salt:master', target, null, 5, 1)
768 if (status != "True") {
769 break
770 }
771 }
772 if (status == "True") {
773 throw new Exception("Tired to wait for minion ${target} to stop responding")
774 }
775 }
776 if (mode == 'hard_shutdown' || mode == 'soft_shutdown') {
777 kvm = locate_node_on_kvm(master, target)
778 if (mode == 'soft_shutdown') {
779 salt.cmdRun(master, target, "shutdown -h 0")
780 }
781 if (mode == 'hard_shutdown') {
782 salt.cmdRun(master, kvm, "virsh destroy ${target}")
783 }
784 common.warningMsg("Checking that vm on kvm is in power off state")
785 status = 'running'
786 for (i = 0; i < retries; i++) {
787 status = check_vm_status(master, target, kvm)
788 echo "Current status - ${status}"
789 if (status != 'running') {
790 break
791 }
792 sleep (1)
793 }
794 if (status == 'running') {
795 throw new Exception("Tired to wait for node ${target} to shutdown")
796 }
797 }
798}
799
800
801/**
802 * Locate kvm where target host is located
803 *
804 * @param target Host to check
805 */
806def locate_node_on_kvm(master, target) {
807 def salt = new com.mirantis.mk.Salt()
808 def list = salt.runSaltProcessStep(master, "I@salt:control", 'cmd.run', ["virsh list --all | grep ' ${target}'"])['return'][0]
809 for (item in list.keySet()) {
810 if (list[item]) {
811 return item
812 }
813 }
814}
815
816/**
817 * Check target host status
818 *
819 * @param target Host to check
820 * @param kvm KVM node where target host is located
821 */
822def check_vm_status(master, target, kvm) {
823 def salt = new com.mirantis.mk.Salt()
824 def list = salt.runSaltProcessStep(master, "${kvm}", 'cmd.run', ["virsh list --all | grep ' ${target}'"])['return'][0]
825 for (item in list.keySet()) {
826 if (list[item]) {
827 return list[item].split()[2]
828 }
829 }
830}
831
832/**
833 * Find vip on nodes
834 *
835 * @param target Pattern, e.g. ctl*
836 */
837def get_vip_node(master, target) {
838 def salt = new com.mirantis.mk.Salt()
Oleksii Zhurba5f73cf62018-08-03 16:11:10 -0500839 def list = salt.runSaltProcessStep(master, "${target}", 'cmd.run', ["ip a | grep '/32'"])['return'][0]
Oleksii Zhurba7b44ef12017-11-13 17:50:16 -0600840 for (item in list.keySet()) {
841 if (list[item]) {
842 return item
843 }
844 }
845}
846
847/**
848 * Find vip on nodes
849 *
850 * @param target Host with cvp container
Oleksii Zhurba93e31982019-05-20 21:40:25 -0500851 * @param container_name Name of container
852 * @param script_path Path to cleanup script (inside container)
Oleksii Zhurba7b44ef12017-11-13 17:50:16 -0600853 */
Oleksii Zhurba93e31982019-05-20 21:40:25 -0500854def openstack_cleanup(master, target, container_name="cvp", script_path="/home/rally/cleanup.sh") {
Oleksii Zhurba7b44ef12017-11-13 17:50:16 -0600855 def salt = new com.mirantis.mk.Salt()
Oleksii Zhurba93e31982019-05-20 21:40:25 -0500856 salt.runSaltProcessStep(master, "${target}", 'cmd.run', ["docker exec ${container_name} bash -c ${script_path}"])
Oleksii Zhurba7b44ef12017-11-13 17:50:16 -0600857}
858
859
860/**
Petr Lomakin47fee0a2017-08-01 10:46:05 -0700861 * Cleanup
862 *
863 * @param target Host to run commands
Oleksii Zhurba1f4a6ff2018-06-27 16:45:17 -0500864 * @param name Name of container to remove
Petr Lomakin47fee0a2017-08-01 10:46:05 -0700865 */
Oleksii Zhurba1f4a6ff2018-06-27 16:45:17 -0500866def runCleanup(master, target, name='cvp') {
Petr Lomakin47fee0a2017-08-01 10:46:05 -0700867 def salt = new com.mirantis.mk.Salt()
Oleksii Zhurba1f4a6ff2018-06-27 16:45:17 -0500868 if ( salt.cmdRun(master, target, "docker ps -f name=${name} -q", false, null, false)['return'][0].values()[0] ) {
869 salt.cmdRun(master, target, "docker rm -f ${name}")
Oleksii Zhurba7b44ef12017-11-13 17:50:16 -0600870 }
Petr Lomakin47fee0a2017-08-01 10:46:05 -0700871}
Oleksii Zhurbabcb97e22017-10-05 14:10:39 -0500872/**
873 * Prepare venv for any python project
874 * Note: <repo_name>\/requirements.txt content will be used
875 * for this venv
876 *
877 * @param repo_url Repository url to clone
878 * @param proxy Proxy address to use
879 */
dtsapikovf2e1bb12018-11-29 18:49:48 +0400880def prepareVenv(repo_url, proxy, useSystemPackages=false) {
Oleksii Zhurbabcb97e22017-10-05 14:10:39 -0500881 def python = new com.mirantis.mk.Python()
882 repo_name = "${repo_url}".tokenize("/").last()
Oleksii Zhurbae711ebb2018-06-15 16:36:38 -0500883 if (repo_url.tokenize().size() > 1){
884 if (repo_url.tokenize()[1] == '-b'){
885 repo_name = repo_url.tokenize()[0].tokenize("/").last()
886 }
887 }
Oleksii Zhurba83e3e5c2018-06-27 16:59:29 -0500888 path_venv = "${env.WORKSPACE}/venv"
889 path_req = "${env.WORKSPACE}/${repo_name}/requirements.txt"
Oleksii Zhurbabcb97e22017-10-05 14:10:39 -0500890 sh "rm -rf ${repo_name}"
Oleksii Zhurba83e3e5c2018-06-27 16:59:29 -0500891 // this is temporary W/A for offline deployments
892 // Jenkins slave image has /opt/pip-mirror/ folder
893 // where pip wheels for cvp projects are located
894 if (proxy != 'offline') {
895 withEnv(["HTTPS_PROXY=${proxy}", "HTTP_PROXY=${proxy}", "https_proxy=${proxy}", "http_proxy=${proxy}"]) {
896 sh "git clone ${repo_url}"
dtsapikovf2e1bb12018-11-29 18:49:48 +0400897 python.setupVirtualenv(path_venv, "python2", [], path_req, true, useSystemPackages)
Oleksii Zhurba83e3e5c2018-06-27 16:59:29 -0500898 }
899 }
900 else {
Oleksii Zhurbabcb97e22017-10-05 14:10:39 -0500901 sh "git clone ${repo_url}"
Oleksii Zhurba83e3e5c2018-06-27 16:59:29 -0500902 sh "virtualenv ${path_venv} --python python2"
903 python.runVirtualenvCommand(path_venv, "pip install --no-index --find-links=/opt/pip-mirror/ -r ${path_req}", true)
Oleksii Zhurbabcb97e22017-10-05 14:10:39 -0500904 }
905}
906
Petr Lomakin47fee0a2017-08-01 10:46:05 -0700907/** Install docker if needed
908 *
909 * @param target Target node to install docker pkg
910 */
911def installDocker(master, target) {
912 def salt = new com.mirantis.mk.Salt()
913 if ( ! salt.runSaltProcessStep(master, target, 'pkg.version', ["docker-engine"]) ) {
914 salt.runSaltProcessStep(master, target, 'pkg.install', ["docker.io"])
915 }
916}