blob: 9b5621d8bd73d466f71f1403dcd8da74536535e5 [file] [log] [blame]
Dennis Dmitrievb3b37492018-07-08 21:23:00 +03001package com.mirantis.system_qa
2
Dennis Dmitriev27a96792018-07-30 07:52:03 +03003import groovy.xml.XmlUtil
Dennis Dmitrievb3b37492018-07-08 21:23:00 +03004
Dennis Dmitrieveb50ce12018-09-27 13:34:32 +03005def run_sh(String cmd) {
6 // run shell script without catching any output
7 def common = new com.mirantis.mk.Common()
8 common.printMsg("Run shell command:\n" + cmd, "blue")
9 def VENV_PATH='/home/jenkins/fuel-devops30'
10 script = """\
11 set -ex;
12 . ${VENV_PATH}/bin/activate;
13 bash -c '${cmd.stripIndent()}'
14 """
15 return sh(script: script)
16}
17
Dennis Dmitriev8df35442018-07-31 08:40:20 +030018def run_cmd(String cmd, Boolean returnStdout=false) {
Dennis Dmitrievb3b37492018-07-08 21:23:00 +030019 def common = new com.mirantis.mk.Common()
20 common.printMsg("Run shell command:\n" + cmd, "blue")
21 def VENV_PATH='/home/jenkins/fuel-devops30'
Dennis Dmitriev27a96792018-07-30 07:52:03 +030022 def stderr_path = "/tmp/${JOB_NAME}_${BUILD_NUMBER}_stderr.log"
Dennis Dmitrievb3b37492018-07-08 21:23:00 +030023 script = """\
24 set +x;
25 echo 'activate python virtualenv ${VENV_PATH}';
26 . ${VENV_PATH}/bin/activate;
Dennis Dmitriev8df35442018-07-31 08:40:20 +030027 bash -c 'set -ex; set -ex; ${cmd.stripIndent()}' 2>${stderr_path}
Dennis Dmitrievb3b37492018-07-08 21:23:00 +030028 """
Dennis Dmitriev27a96792018-07-30 07:52:03 +030029 def result
30 try {
Dennis Dmitriev8df35442018-07-31 08:40:20 +030031 return sh(script: script, returnStdout: returnStdout)
Dennis Dmitriev27a96792018-07-30 07:52:03 +030032 } catch (e) {
Dennis Dmitriev8df35442018-07-31 08:40:20 +030033 def stderr = readFile("${stderr_path}")
34 def error_message = e.message + "\n<<<<<< STDERR: >>>>>>\n" + stderr
35 throw new Exception(error_message)
Dennis Dmitriev27a96792018-07-30 07:52:03 +030036 } finally {
Dennis Dmitriev8df35442018-07-31 08:40:20 +030037 sh(script: "rm ${stderr_path} || true")
Dennis Dmitriev27a96792018-07-30 07:52:03 +030038 }
Dennis Dmitrievb3b37492018-07-08 21:23:00 +030039}
40
41def run_cmd_stdout(cmd) {
Dennis Dmitriev8df35442018-07-31 08:40:20 +030042 return run_cmd(cmd, true)
Dennis Dmitrievb3b37492018-07-08 21:23:00 +030043}
44
Dennis Dmitriev27a96792018-07-30 07:52:03 +030045def build_pipeline_job(job_name, parameters) {
46 //Build a job, grab the results if failed and use the results in exception
47 def common = new com.mirantis.mk.Common()
48 common.printMsg("Start building job '${job_name}' with parameters:", "purple")
49 common.prettyPrint(parameters)
50
51 def job_info = build job: "${job_name}",
52 parameters: parameters,
53 propagate: false
54
55 if (job_info.getResult() != "SUCCESS") {
56 currentBuild.result = job_info.getResult()
57 def build_number = job_info.getNumber()
Dennis Dmitrievb08c0772018-10-17 15:10:26 +030058 common.printMsg("Job '${job_name}' failed, getting details", "purple")
Dennis Dmitriev27a96792018-07-30 07:52:03 +030059 def workflow_details=run_cmd_stdout("""\
60 export JOB_NAME=${job_name}
61 export BUILD_NUMBER=${build_number}
62 python ./tcp_tests/utils/get_jenkins_job_stages.py
63 """)
64 throw new Exception(workflow_details)
65 }
66}
67
68def build_shell_job(job_name, parameters, junit_report_filename=null, junit_report_source_dir='**/') {
69 //Build a job, grab the results if failed and use the results in exception
70 //junit_report_filename: if not null, try to copy this JUnit report first from remote job
71 def common = new com.mirantis.mk.Common()
72 common.printMsg("Start building job '${job_name}' with parameters:", "purple")
73 common.prettyPrint(parameters)
74
75 def job_info = build job: "${job_name}",
76 parameters: parameters,
77 propagate: false
78
79 if (job_info.getResult() != "SUCCESS") {
80 def build_status = job_info.getResult()
81 def build_number = job_info.getNumber()
82 def build_url = job_info.getAbsoluteUrl()
83 def job_url = "${build_url}"
84 currentBuild.result = build_status
85 if (junit_report_filename) {
Dennis Dmitrievb08c0772018-10-17 15:10:26 +030086 common.printMsg("Job '${job_url}' failed with status ${build_status}, getting details", "purple")
Dennis Dmitriev27a96792018-07-30 07:52:03 +030087 step($class: 'hudson.plugins.copyartifact.CopyArtifact',
88 projectName: job_name,
89 selector: specific("${build_number}"),
90 filter: "${junit_report_source_dir}/${junit_report_filename}",
91 target: '.',
92 flatten: true,
93 fingerprintArtifacts: true)
94
95 def String junit_report_xml = readFile("${junit_report_filename}")
96 def String junit_report_xml_pretty = new XmlUtil().serialize(junit_report_xml)
97 def String msg = "Job '${job_url}' failed with status ${build_status}, JUnit report:\n"
Dennis Dmitrievb08c0772018-10-17 15:10:26 +030098 throw new Exception(msg + junit_report_xml_pretty)
Dennis Dmitriev27a96792018-07-30 07:52:03 +030099 } else {
100 throw new Exception("Job '${job_url}' failed with status ${build_status}, please check the console output.")
101 }
102 }
103}
Dennis Dmitrievb3b37492018-07-08 21:23:00 +0300104
105def prepare_working_dir() {
106 println "Clean the working directory ${env.WORKSPACE}"
107 deleteDir()
108
Dennis Dmitriev27a96792018-07-30 07:52:03 +0300109 // do not fail if environment doesn't exists
110 println "Remove environment ${ENV_NAME}"
111 run_cmd("""\
112 dos.py erase ${ENV_NAME} || true
113 """)
114 println "Remove config drive ISO"
115 run_cmd("""\
116 rm /home/jenkins/images/${CFG01_CONFIG_IMAGE_NAME} || true
117 """)
Dennis Dmitrievb3b37492018-07-08 21:23:00 +0300118
119 run_cmd("""\
Dennis Dmitriev27a96792018-07-30 07:52:03 +0300120 git clone https://github.com/Mirantis/tcp-qa.git ${env.WORKSPACE}
121 if [ -n "$TCP_QA_REFS" ]; then
122 set -e
123 git fetch https://review.gerrithub.io/Mirantis/tcp-qa $TCP_QA_REFS && git checkout FETCH_HEAD || exit \$?
124 fi
125 pip install --upgrade --upgrade-strategy=only-if-needed -r tcp_tests/requirements.txt
Dennis Dmitriev8df35442018-07-31 08:40:20 +0300126 """)
Dennis Dmitrievb3b37492018-07-08 21:23:00 +0300127}
128
Dennis Dmitrieveb50ce12018-09-27 13:34:32 +0300129def update_working_dir() {
130 // Use to fetch a patchset from gerrit to the working dir
131 run_cmd("""\
132 if [ -n "$TCP_QA_REFS" ]; then
133 set -e
134 git fetch https://review.gerrithub.io/Mirantis/tcp-qa $TCP_QA_REFS && git checkout FETCH_HEAD || exit \$?
135 fi
136 pip install -r tcp_tests/requirements.txt
137 """)
138}
139
Dennis Dmitrievb3b37492018-07-08 21:23:00 +0300140def swarm_bootstrap_salt_cluster_devops() {
141 def common = new com.mirantis.mk.Common()
Dennis Dmitrievee5ef232018-08-31 13:53:18 +0300142 def cookiecutter_template_commit = env.COOKIECUTTER_TEMPLATE_COMMIT ?: env.MCP_VERSION
143 def salt_models_system_commit = env.SALT_MODELS_SYSTEM_COMMIT ?: env.MCP_VERSION
144 def tcp_qa_refs = env.TCP_QA_REFS ?: ''
145 def mk_pipelines_ref = env.MK_PIPELINES_REF ?: ''
146 def pipeline_library_ref = env.PIPELINE_LIBRARY_REF ?: ''
147 def cookiecutter_ref_change = env.COOKIECUTTER_REF_CHANGE ?: ''
148 def environment_template_ref_change = env.ENVIRONMENT_TEMPLATE_REF_CHANGE ?: ''
Dennis Dmitrievb3b37492018-07-08 21:23:00 +0300149 def parameters = [
150 string(name: 'PARENT_NODE_NAME', value: "${NODE_NAME}"),
151 string(name: 'PARENT_WORKSPACE', value: pwd()),
152 string(name: 'LAB_CONFIG_NAME', value: "${LAB_CONFIG_NAME}"),
153 string(name: 'ENV_NAME', value: "${ENV_NAME}"),
154 string(name: 'MCP_VERSION', value: "${MCP_VERSION}"),
155 string(name: 'MCP_IMAGE_PATH1604', value: "${MCP_IMAGE_PATH1604}"),
156 string(name: 'IMAGE_PATH_CFG01_DAY01', value: "${IMAGE_PATH_CFG01_DAY01}"),
157 string(name: 'CFG01_CONFIG_IMAGE_NAME', value: "${CFG01_CONFIG_IMAGE_NAME}"),
Dennis Dmitrievee5ef232018-08-31 13:53:18 +0300158 string(name: 'TCP_QA_REFS', value: "${tcp_qa_refs}"),
159 string(name: 'PIPELINE_LIBRARY_REF', value: "${pipeline_library_ref}"),
160 string(name: 'MK_PIPELINES_REF', value: "${mk_pipelines_ref}"),
161 string(name: 'COOKIECUTTER_TEMPLATE_COMMIT', value: "${cookiecutter_template_commit}"),
162 string(name: 'SALT_MODELS_SYSTEM_COMMIT', value: "${salt_models_system_commit}"),
163 string(name: 'COOKIECUTTER_REF_CHANGE', value: "${cookiecutter_ref_change}"),
164 string(name: 'ENVIRONMENT_TEMPLATE_REF_CHANGE', value: "${environment_template_ref_change}"),
Dennis Dmitrievb3b37492018-07-08 21:23:00 +0300165 booleanParam(name: 'SHUTDOWN_ENV_ON_TEARDOWN', value: false),
166 ]
Dennis Dmitriev27a96792018-07-30 07:52:03 +0300167
168 build_pipeline_job('swarm-bootstrap-salt-cluster-devops', parameters)
Dennis Dmitrievb3b37492018-07-08 21:23:00 +0300169}
170
171def swarm_deploy_cicd(String stack_to_install='core,cicd') {
172 // Run openstack_deploy job on cfg01 Jenkins for specified stacks
173 def common = new com.mirantis.mk.Common()
Dennis Dmitrievee5ef232018-08-31 13:53:18 +0300174 def tcp_qa_refs = env.TCP_QA_REFS ?: ''
Dennis Dmitrievb3b37492018-07-08 21:23:00 +0300175 def parameters = [
176 string(name: 'PARENT_NODE_NAME', value: "${NODE_NAME}"),
177 string(name: 'PARENT_WORKSPACE', value: pwd()),
178 string(name: 'ENV_NAME', value: "${ENV_NAME}"),
179 string(name: 'STACK_INSTALL', value: stack_to_install),
Dennis Dmitrievee5ef232018-08-31 13:53:18 +0300180 string(name: 'TCP_QA_REFS', value: "${tcp_qa_refs}"),
Dennis Dmitrievb3b37492018-07-08 21:23:00 +0300181 booleanParam(name: 'SHUTDOWN_ENV_ON_TEARDOWN', value: false),
182 ]
Dennis Dmitriev27a96792018-07-30 07:52:03 +0300183 build_pipeline_job('swarm-deploy-cicd', parameters)
Dennis Dmitrievb3b37492018-07-08 21:23:00 +0300184}
185
186def swarm_deploy_platform(String stack_to_install) {
187 // Run openstack_deploy job on CICD Jenkins for specified stacks
188 def common = new com.mirantis.mk.Common()
Dennis Dmitrievee5ef232018-08-31 13:53:18 +0300189 def tcp_qa_refs = env.TCP_QA_REFS ?: ''
Dennis Dmitrievb3b37492018-07-08 21:23:00 +0300190 def parameters = [
191 string(name: 'PARENT_NODE_NAME', value: "${NODE_NAME}"),
192 string(name: 'PARENT_WORKSPACE', value: pwd()),
193 string(name: 'ENV_NAME', value: "${ENV_NAME}"),
194 string(name: 'STACK_INSTALL', value: stack_to_install),
Dennis Dmitrievee5ef232018-08-31 13:53:18 +0300195 string(name: 'TCP_QA_REFS', value: "${tcp_qa_refs}"),
Dennis Dmitrievb3b37492018-07-08 21:23:00 +0300196 booleanParam(name: 'SHUTDOWN_ENV_ON_TEARDOWN', value: false),
197 ]
Dennis Dmitriev27a96792018-07-30 07:52:03 +0300198 build_pipeline_job('swarm-deploy-platform', parameters)
Dennis Dmitrievb3b37492018-07-08 21:23:00 +0300199}
200
Dennis Dmitrievfde667f2018-07-23 16:26:50 +0300201def swarm_run_pytest(String passed_steps) {
202 // Run pytest tests
203 def common = new com.mirantis.mk.Common()
Dennis Dmitrievee5ef232018-08-31 13:53:18 +0300204 def tcp_qa_refs = env.TCP_QA_REFS ?: ''
Dennis Dmitrievfde667f2018-07-23 16:26:50 +0300205 def parameters = [
206 string(name: 'ENV_NAME', value: "${ENV_NAME}"),
207 string(name: 'PASSED_STEPS', value: passed_steps),
208 string(name: 'RUN_TEST_OPTS', value: "${RUN_TEST_OPTS}"),
209 string(name: 'PARENT_NODE_NAME', value: "${NODE_NAME}"),
210 string(name: 'PARENT_WORKSPACE', value: pwd()),
Dennis Dmitrievee5ef232018-08-31 13:53:18 +0300211 string(name: 'TCP_QA_REFS', value: "${tcp_qa_refs}"),
Dennis Dmitrievfde667f2018-07-23 16:26:50 +0300212 booleanParam(name: 'SHUTDOWN_ENV_ON_TEARDOWN', value: false),
213 string(name: 'LAB_CONFIG_NAME', value: "${LAB_CONFIG_NAME}"),
214 string(name: 'REPOSITORY_SUITE', value: "${MCP_VERSION}"),
215 string(name: 'MCP_IMAGE_PATH1604', value: "${MCP_IMAGE_PATH1604}"),
216 string(name: 'IMAGE_PATH_CFG01_DAY01', value: "${IMAGE_PATH_CFG01_DAY01}"),
217 ]
218 common.printMsg("Start building job 'swarm-run-pytest' with parameters:", "purple")
219 common.prettyPrint(parameters)
220 build job: 'swarm-run-pytest',
221 parameters: parameters
222}
223
Dennis Dmitrieve4b831b2018-08-15 17:16:10 +0300224def swarm_testrail_report(String passed_steps) {
225 // Run pytest tests
226 def common = new com.mirantis.mk.Common()
Dennis Dmitrievee5ef232018-08-31 13:53:18 +0300227 def tcp_qa_refs = env.TCP_QA_REFS ?: ''
Dennis Dmitrieve4b831b2018-08-15 17:16:10 +0300228 def parameters = [
229 string(name: 'ENV_NAME', value: "${ENV_NAME}"),
230 string(name: 'MCP_VERSION', value: "${MCP_VERSION}"),
231 string(name: 'PASSED_STEPS', value: passed_steps),
232 string(name: 'PARENT_NODE_NAME', value: "${NODE_NAME}"),
233 string(name: 'PARENT_WORKSPACE', value: pwd()),
Dennis Dmitrievee5ef232018-08-31 13:53:18 +0300234 string(name: 'TCP_QA_REFS', value: "${tcp_qa_refs}"),
Dennis Dmitrieve4b831b2018-08-15 17:16:10 +0300235 ]
236 common.printMsg("Start building job 'swarm-testrail-report' with parameters:", "purple")
237 common.prettyPrint(parameters)
238 build job: 'swarm-testrail-report',
239 parameters: parameters
240}
241
Dennis Dmitrievb3b37492018-07-08 21:23:00 +0300242def generate_cookied_model() {
243 def common = new com.mirantis.mk.Common()
244 // do not fail if environment doesn't exists
245 def IPV4_NET_ADMIN=run_cmd_stdout("dos.py net-list ${ENV_NAME} | grep admin-pool01").trim().split().last()
246 def IPV4_NET_CONTROL=run_cmd_stdout("dos.py net-list ${ENV_NAME} | grep private-pool01").trim().split().last()
247 def IPV4_NET_TENANT=run_cmd_stdout("dos.py net-list ${ENV_NAME} | grep tenant-pool01").trim().split().last()
248 def IPV4_NET_EXTERNAL=run_cmd_stdout("dos.py net-list ${ENV_NAME} | grep external-pool01").trim().split().last()
249 println("IPV4_NET_ADMIN=" + IPV4_NET_ADMIN)
250 println("IPV4_NET_CONTROL=" + IPV4_NET_CONTROL)
251 println("IPV4_NET_TENANT=" + IPV4_NET_TENANT)
252 println("IPV4_NET_EXTERNAL=" + IPV4_NET_EXTERNAL)
253
254 def cookiecuttertemplate_commit = env.COOKIECUTTER_TEMPLATE_COMMIT ?: env.MCP_VERSION
255 def saltmodels_system_commit = env.SALT_MODELS_SYSTEM_COMMIT ?: env.MCP_VERSION
Dennis Dmitrievee5ef232018-08-31 13:53:18 +0300256 def tcp_qa_refs = env.TCP_QA_REFS ?: ''
257 def environment_template_ref_change = env.ENVIRONMENT_TEMPLATE_REF_CHANGE ?: ''
258 def cookiecutter_ref_change = env.COOKIECUTTER_REF_CHANGE ?: ''
Dennis Dmitrievb3b37492018-07-08 21:23:00 +0300259
260 def parameters = [
261 string(name: 'LAB_CONTEXT_NAME', value: "${LAB_CONFIG_NAME}"),
262 string(name: 'CLUSTER_NAME', value: "${LAB_CONFIG_NAME}"),
263 string(name: 'DOMAIN_NAME', value: "${LAB_CONFIG_NAME}.local"),
264 string(name: 'REPOSITORY_SUITE', value: "${env.MCP_VERSION}"),
265 string(name: 'SALT_MODELS_SYSTEM_COMMIT', value: "${saltmodels_system_commit}"),
266 string(name: 'COOKIECUTTER_TEMPLATE_COMMIT', value: "${cookiecuttertemplate_commit}"),
Dennis Dmitrievee5ef232018-08-31 13:53:18 +0300267 string(name: 'COOKIECUTTER_REF_CHANGE', value: "${cookiecutter_ref_change}"),
268 string(name: 'ENVIRONMENT_TEMPLATE_REF_CHANGE', value: "${environment_template_ref_change}"),
269 string(name: 'TCP_QA_REVIEW', value: "${tcp_qa_refs}"),
Dennis Dmitrievb3b37492018-07-08 21:23:00 +0300270 string(name: 'IPV4_NET_ADMIN', value: IPV4_NET_ADMIN),
271 string(name: 'IPV4_NET_CONTROL', value: IPV4_NET_CONTROL),
272 string(name: 'IPV4_NET_TENANT', value: IPV4_NET_TENANT),
273 string(name: 'IPV4_NET_EXTERNAL', value: IPV4_NET_EXTERNAL),
274 ]
Dennis Dmitriev27a96792018-07-30 07:52:03 +0300275
276 build_shell_job('swarm-cookied-model-generator', parameters, "deploy_generate_model.xml")
Dennis Dmitrievb3b37492018-07-08 21:23:00 +0300277}
278
279def generate_configdrive_iso() {
280 def common = new com.mirantis.mk.Common()
281 def SALT_MASTER_IP=run_cmd_stdout("""\
282 export ENV_NAME=${ENV_NAME}
283 . ./tcp_tests/utils/env_salt
284 echo \$SALT_MASTER_IP
285 """).trim().split().last()
286 println("SALT_MASTER_IP=" + SALT_MASTER_IP)
Dennis Dmitrievee5ef232018-08-31 13:53:18 +0300287
Dennis Dmitrievf220d972018-10-10 15:19:14 +0300288 def dhcp_ranges_json=run_cmd_stdout("""\
289 fgrep dhcp_ranges ${ENV_NAME}_hardware.ini |
290 fgrep "admin-pool01"|
291 cut -d"=" -f2
292 """).trim().split("\n").last()
293 def dhcp_ranges = new groovy.json.JsonSlurperClassic().parseText(dhcp_ranges_json)
294 def ADMIN_NETWORK_GW = dhcp_ranges['admin-pool01']['gateway']
295 println("ADMIN_NETWORK_GW=" + ADMIN_NETWORK_GW)
296
Dennis Dmitrievee5ef232018-08-31 13:53:18 +0300297 def mk_pipelines_ref = env.MK_PIPELINES_REF ?: ''
298 def pipeline_library_ref = env.PIPELINE_LIBRARY_REF ?: ''
Dennis Dmitrievf220d972018-10-10 15:19:14 +0300299 def tcp_qa_refs = env.TCP_QA_REFS ?: ''
Dennis Dmitrievee5ef232018-08-31 13:53:18 +0300300
Dennis Dmitrievb3b37492018-07-08 21:23:00 +0300301 def parameters = [
302 string(name: 'CLUSTER_NAME', value: "${LAB_CONFIG_NAME}"),
303 string(name: 'MODEL_URL', value: "http://cz8133.bud.mirantis.net:8098/${LAB_CONFIG_NAME}.git"),
304 string(name: 'MODEL_URL_OBJECT_TYPE', value: "git"),
305 booleanParam(name: 'DOWNLOAD_CONFIG_DRIVE', value: true),
306 string(name: 'MCP_VERSION', value: "${MCP_VERSION}"),
307 string(name: 'COMMON_SCRIPTS_COMMIT', value: "${MCP_VERSION}"),
308 string(name: 'NODE_NAME', value: "${NODE_NAME}"),
309 string(name: 'CONFIG_DRIVE_ISO_NAME', value: "${CFG01_CONFIG_IMAGE_NAME}"),
310 string(name: 'SALT_MASTER_DEPLOY_IP', value: SALT_MASTER_IP),
Dennis Dmitrievf220d972018-10-10 15:19:14 +0300311 string(name: 'DEPLOY_NETWORK_GW', value: "${ADMIN_NETWORK_GW}"),
Dennis Dmitrievb3b37492018-07-08 21:23:00 +0300312 string(name: 'PIPELINE_REPO_URL', value: "https://github.com/Mirantis"),
313 booleanParam(name: 'PIPELINES_FROM_ISO', value: true),
314 string(name: 'MCP_SALT_REPO_URL', value: "http://apt.mirantis.com/xenial"),
315 string(name: 'MCP_SALT_REPO_KEY', value: "http://apt.mirantis.com/public.gpg"),
Dennis Dmitrievee5ef232018-08-31 13:53:18 +0300316 string(name: 'PIPELINE_LIBRARY_REF', value: "${pipeline_library_ref}"),
317 string(name: 'MK_PIPELINES_REF', value: "${mk_pipelines_ref}"),
Dennis Dmitrievf220d972018-10-10 15:19:14 +0300318 string(name: 'TCP_QA_REFS', value: "${tcp_qa_refs}"),
Dennis Dmitrievb3b37492018-07-08 21:23:00 +0300319 ]
Dennis Dmitrievf220d972018-10-10 15:19:14 +0300320 build_pipeline_job('swarm-create-cfg-config-drive', parameters)
Dennis Dmitrievb3b37492018-07-08 21:23:00 +0300321}
322
Dennis Dmitriev13e804b2018-10-09 19:25:14 +0300323def run_job_on_day01_node(stack_to_install, timeout=2400) {
Dennis Dmitrievb3b37492018-07-08 21:23:00 +0300324 // stack_to_install="core,cicd"
325 def stack = "${stack_to_install}"
Dennis Dmitriev27a96792018-07-30 07:52:03 +0300326 try {
327 run_cmd("""\
328 export ENV_NAME=${ENV_NAME}
329 . ./tcp_tests/utils/env_salt
330 . ./tcp_tests/utils/env_jenkins_day01
331 export JENKINS_BUILD_TIMEOUT=${timeout}
332 JOB_PARAMETERS=\"{
333 \\\"SALT_MASTER_URL\\\": \\\"\${SALTAPI_URL}\\\",
334 \\\"STACK_INSTALL\\\": \\\"${stack}\\\"
335 }\"
336 JOB_PREFIX="[ ${ENV_NAME}/{build_number}:${stack} {time} ] "
337 python ./tcp_tests/utils/run_jenkins_job.py --verbose --job-name=deploy_openstack --job-parameters="\$JOB_PARAMETERS" --job-output-prefix="\$JOB_PREFIX"
338 """)
339 } catch (e) {
Dennis Dmitriev8df35442018-07-31 08:40:20 +0300340 def common = new com.mirantis.mk.Common()
Dennis Dmitrievb08c0772018-10-17 15:10:26 +0300341 common.printMsg("Product job 'deploy_openstack' failed, getting details", "purple")
Dennis Dmitriev27a96792018-07-30 07:52:03 +0300342 def workflow_details=run_cmd_stdout("""\
343 . ./tcp_tests/utils/env_salt
344 . ./tcp_tests/utils/env_jenkins_day01
345 export JOB_NAME=deploy_openstack
346 export BUILD_NUMBER=lastBuild
347 python ./tcp_tests/utils/get_jenkins_job_stages.py
348 """)
349 throw new Exception(workflow_details)
350 }
Dennis Dmitrievb3b37492018-07-08 21:23:00 +0300351}
352
Dennis Dmitriev13e804b2018-10-09 19:25:14 +0300353def run_job_on_cicd_nodes(stack_to_install, timeout=2400) {
Dennis Dmitrievb3b37492018-07-08 21:23:00 +0300354 // stack_to_install="k8s,calico,stacklight"
355 def stack = "${stack_to_install}"
Dennis Dmitriev27a96792018-07-30 07:52:03 +0300356 try {
357 run_cmd("""\
358 export ENV_NAME=${ENV_NAME}
359 . ./tcp_tests/utils/env_salt
360 . ./tcp_tests/utils/env_jenkins_cicd
361 export JENKINS_BUILD_TIMEOUT=${timeout}
362 JOB_PARAMETERS=\"{
363 \\\"SALT_MASTER_URL\\\": \\\"\${SALTAPI_URL}\\\",
364 \\\"STACK_INSTALL\\\": \\\"${stack}\\\"
365 }\"
366 JOB_PREFIX="[ ${ENV_NAME}/{build_number}:${stack} {time} ] "
367 python ./tcp_tests/utils/run_jenkins_job.py --verbose --job-name=deploy_openstack --job-parameters="\$JOB_PARAMETERS" --job-output-prefix="\$JOB_PREFIX"
368 sleep 60 # Wait for IO calm down on cluster nodes
369 """)
370 } catch (e) {
Dennis Dmitriev8df35442018-07-31 08:40:20 +0300371 def common = new com.mirantis.mk.Common()
Dennis Dmitrievb08c0772018-10-17 15:10:26 +0300372 common.printMsg("Product job 'deploy_openstack' failed, getting details", "purple")
Dennis Dmitriev27a96792018-07-30 07:52:03 +0300373 def workflow_details=run_cmd_stdout("""\
374 . ./tcp_tests/utils/env_salt
375 . ./tcp_tests/utils/env_jenkins_cicd
376 export JOB_NAME=deploy_openstack
377 export BUILD_NUMBER=lastBuild
378 python ./tcp_tests/utils/get_jenkins_job_stages.py
379 """)
380 throw new Exception(workflow_details)
381 }
Dennis Dmitrievb3b37492018-07-08 21:23:00 +0300382}
383
384def sanity_check_component(stack) {
385 // Run sanity check for the component ${stack}.
386 // Result will be stored in JUnit XML file deploy_${stack}.xml
Dennis Dmitriev27a96792018-07-30 07:52:03 +0300387 try {
388 run_cmd("""\
389 py.test --junit-xml=deploy_${stack}.xml -m check_${stack}
390 """)
391 } catch (e) {
392 def String junit_report_xml = readFile("deploy_${stack}.xml")
393 def String junit_report_xml_pretty = new XmlUtil().serialize(junit_report_xml)
394 def String msg = "Sanity check for '${stack}' failed, JUnit report:\n"
395 throw new Exception(msg + junit_report_xml_pretty)
396 }
Dennis Dmitrievb3b37492018-07-08 21:23:00 +0300397}
398
Dennis Dmitrievefe5c0b2018-10-24 20:35:26 +0300399def download_logs(archive_name_prefix) {
400 // Archive and download logs and debug info from salt nodes in the lab
401 // Do not fail in case of error to not lose the original error from the parent exception.
402 def common = new com.mirantis.mk.Common()
403 common.printMsg("Downloading nodes logs by ${archive_name_prefix}", "blue")
404 run_cmd("""\
405 export TESTS_CONFIGS=\$(pwd)/${ENV_NAME}_salt_deployed.ini
406 ./tcp_tests/utils/get_logs.py --archive-name-prefix ${archive_name_prefix} || true
407 """)
408}
409
Dennis Dmitrievb08c0772018-10-17 15:10:26 +0300410def devops_snapshot_info(snapshot_name) {
411 // Print helper message after snapshot
412 def common = new com.mirantis.mk.Common()
413
414 def SALT_MASTER_IP=run_cmd_stdout("""\
415 . ./tcp_tests/utils/env_salt
416 echo \$SALT_MASTER_IP
417 """).trim().split().last()
418 def login = "root" // set fixed 'root' login for now
419 def password = "r00tme" // set fixed 'root' login for now
420 def key_file = "${env.WORKSPACE}/id_rsa" // set fixed path in the WORKSPACE
421 def VENV_PATH='/home/jenkins/fuel-devops30'
422
423 common.printMsg("""\
424#########################
425# To revert the snapshot:
426#########################
427. ${VENV_PATH}/bin/activate;
428dos.py revert ${ENV_NAME} ${snapshot_name};
429dos.py resume ${ENV_NAME};
430# dos.py time-sync ${ENV_NAME}; # Optional\n
431ssh -i ${key_file} ${login}@${SALT_MASTER_IP} # Optional password: ${password}
432""", "cyan")
433}
434
Dennis Dmitrievb3b37492018-07-08 21:23:00 +0300435def devops_snapshot(stack) {
436 // Make the snapshot with name "${stack}_deployed"
437 // for all VMs in the environment.
438 // If oslo_config INI file ${ENV_NAME}_salt_deployed.ini exists,
439 // then make a copy for the created snapshot to allow the system
440 // tests to revert this snapshot along with the metadata from the INI file.
441 run_cmd("""\
Dennis Dmitrievee5ef232018-08-31 13:53:18 +0300442 set -ex
Dennis Dmitrievb3b37492018-07-08 21:23:00 +0300443 dos.py suspend ${ENV_NAME}
444 dos.py snapshot ${ENV_NAME} ${stack}_deployed
445 dos.py resume ${ENV_NAME}
Dennis Dmitriev1fed6662018-07-27 18:22:13 +0300446 sleep 20 # Wait for I/O on the host calms down
Dennis Dmitrievee5ef232018-08-31 13:53:18 +0300447
448 CFG01_NAME=\$(dos.py show-resources ${ENV_NAME} | grep ^cfg01 | cut -d" " -f1)
449 dos.py time-sync ${ENV_NAME} --skip-sync \${CFG01_NAME}
450
Dennis Dmitrievb3b37492018-07-08 21:23:00 +0300451 if [ -f \$(pwd)/${ENV_NAME}_salt_deployed.ini ]; then
452 cp \$(pwd)/${ENV_NAME}_salt_deployed.ini \$(pwd)/${ENV_NAME}_${stack}_deployed.ini
453 fi
Dennis Dmitriev8df35442018-07-31 08:40:20 +0300454 """)
Dennis Dmitrievb08c0772018-10-17 15:10:26 +0300455 devops_snapshot_info("${stack}_deployed")
Dennis Dmitrievb3b37492018-07-08 21:23:00 +0300456}
457
Dennis Dmitrievfde667f2018-07-23 16:26:50 +0300458def get_steps_list(steps) {
459 // Make a list from comma separated string
460 return steps.split(',').collect { it.split(':')[0] }
461}
462
Dennis Dmitrieve4b831b2018-08-15 17:16:10 +0300463def create_xml_report(String filename, String classname, String name, String status='success', String status_message='', String text='', String stdout='', String stderr='') {
464 // <filename> is name of the XML report file that will be created
465 // <status> is one of the 'success', 'skipped', 'failure' or 'error'
466 // 'error' status is assumed as 'Blocker' in TestRail reporter
Dennis Dmitrievb08c0772018-10-17 15:10:26 +0300467
468 // Replace '<' and '>' to '&lt;' and '&gt;' to avoid conflicts between xml tags in the message and JUnit report
469 def String text_filtered = text.replaceAll("<","&lt;").replaceAll(">", "&gt;")
470
Dennis Dmitriev39666082018-08-29 15:30:45 +0300471 def script = """\
Dennis Dmitrieve4b831b2018-08-15 17:16:10 +0300472<?xml version=\"1.0\" encoding=\"utf-8\"?>
473 <testsuite>
474 <testcase classname=\"${classname}\" name=\"${name}\" time=\"0\">
Dennis Dmitrievb08c0772018-10-17 15:10:26 +0300475 <${status} message=\"${status_message}\">${text_filtered}</${status}>
Dennis Dmitrieve4b831b2018-08-15 17:16:10 +0300476 <system-out>${stdout}</system-out>
477 <system-err>${stderr}</system-err>
478 </testcase>
479 </testsuite>
Dennis Dmitriev39666082018-08-29 15:30:45 +0300480"""
481 writeFile(file: filename, text: script, encoding: "UTF-8")
Dennis Dmitrievb3b37492018-07-08 21:23:00 +0300482}
483
Dennis Dmitrieve4b831b2018-08-15 17:16:10 +0300484def upload_results_to_testrail(report_name, testSuiteName, methodname, testrail_name_template, reporter_extra_options=[]) {
485 def venvPath = '/home/jenkins/venv_testrail_reporter'
486 def testPlanDesc = env.ENV_NAME
487 def testrailURL = "https://mirantis.testrail.com"
488 def testrailProject = "Mirantis Cloud Platform"
489 def testPlanName = "[MCP-Q2]System-${MCP_VERSION}-${new Date().format('yyyy-MM-dd')}"
490 def testrailMilestone = "MCP1.1"
Dennis Dmitriev707b2152018-10-23 19:12:48 +0300491 def testrailCaseMaxNameLenght = 250
Dennis Dmitrieve4b831b2018-08-15 17:16:10 +0300492 def jobURL = env.BUILD_URL
493
494 def reporterOptions = [
495 "--verbose",
496 "--env-description \"${testPlanDesc}\"",
497 "--testrail-run-update",
498 "--testrail-url \"${testrailURL}\"",
499 "--testrail-user \"\${TESTRAIL_USER}\"",
500 "--testrail-password \"\${TESTRAIL_PASSWORD}\"",
501 "--testrail-project \"${testrailProject}\"",
502 "--testrail-plan-name \"${testPlanName}\"",
503 "--testrail-milestone \"${testrailMilestone}\"",
504 "--testrail-suite \"${testSuiteName}\"",
505 "--xunit-name-template \"${methodname}\"",
506 "--testrail-name-template \"${testrail_name_template}\"",
507 "--test-results-link \"${jobURL}\"",
Dennis Dmitriev707b2152018-10-23 19:12:48 +0300508 "--testrail-case-max-name-lenght ${testrailCaseMaxNameLenght}",
Dennis Dmitrieve4b831b2018-08-15 17:16:10 +0300509 ] + reporter_extra_options
510
511 def script = """
512 . ${venvPath}/bin/activate
513 set -ex
Dennis Dmitrievee5ef232018-08-31 13:53:18 +0300514 report ${reporterOptions.join(' ')} ${report_name}
Dennis Dmitrieve4b831b2018-08-15 17:16:10 +0300515 """
516
517 def testrail_cred_id = params.TESTRAIL_CRED ?: 'testrail_system_tests'
518
519 withCredentials([
520 [$class : 'UsernamePasswordMultiBinding',
521 credentialsId : testrail_cred_id,
522 passwordVariable: 'TESTRAIL_PASSWORD',
523 usernameVariable: 'TESTRAIL_USER']
524 ]) {
Dennis Dmitrievfbf42272018-10-23 00:19:50 +0300525 return run_cmd_stdout(script)
Dennis Dmitrieve4b831b2018-08-15 17:16:10 +0300526 }
527}
528
529
530def create_deploy_result_report(deploy_expected_stacks, result, text) {
531 def STATUS_MAP = ['SUCCESS': 'success', 'FAILURE': 'failure', 'UNSTABLE': 'failure', 'ABORTED': 'error']
532 def classname = "Deploy"
533 def name = "deployment_${ENV_NAME}"
Dennis Dmitriev39666082018-08-29 15:30:45 +0300534 def filename = "${name}.xml"
Dennis Dmitrieve4b831b2018-08-15 17:16:10 +0300535 def status = STATUS_MAP[result ?: 'FAILURE'] // currentBuild.result *must* be set at the finish of the try/catch
536 create_xml_report(filename, classname, name, status, "Deploy components: ${deploy_expected_stacks}", text, '', '')
Dennis Dmitriev27a96792018-07-30 07:52:03 +0300537}