blob: 99f5e3c7ca220bb82253c366dafe81aca528bcbd [file] [log] [blame]
Oleksii Zhurba71e97882018-02-16 20:09:43 -06001/**
2 *
3 * Launch pytest frameworks in Jenkins
4 *
5 * Expected parameters:
6 * SALT_MASTER_URL URL of Salt master
7 * SALT_MASTER_CREDENTIALS Credentials to the Salt API
Oleksii Zhurbacaa46e12019-04-16 17:38:09 -05008 * IMAGE Docker image link to use for running container with test framework.
9 * EXTRA_PARAMS Yaml context which contains additional setting for job
Oleksii Zhurba71e97882018-02-16 20:09:43 -060010 *
Oleksii Zhurba71e97882018-02-16 20:09:43 -060011 */
12
Oleksii Zhurbad9034952018-09-03 21:06:06 -050013common = new com.mirantis.mk.Common()
Oleksii Zhurba71e97882018-02-16 20:09:43 -060014validate = new com.mirantis.mcp.Validate()
Hanna Arhipova3b4b0652018-12-03 14:06:58 +020015salt_testing = new com.mirantis.mk.SaltModelTesting()
Hanna Arhipovab451e792018-12-18 15:27:35 +020016
Oleksii Zhurbacaa46e12019-04-16 17:38:09 -050017def EXTRA_PARAMS = readYaml(text: env.getProperty('EXTRA_PARAMS')) ?: [:]
18def env_vars = EXTRA_PARAMS.get("envs") ?: []
Ievgeniia Zadorozhnaf32758b2019-11-13 18:32:59 +030019def override_config = env.getProperty('EXTRA_PARAMS') ?: ""
Oleksii Zhurba71e97882018-02-16 20:09:43 -060020
Oleksii Zhurbacaa46e12019-04-16 17:38:09 -050021def IMAGE = (env.getProperty('IMAGE')) ?: 'docker-prod-local.docker.mirantis.net/mirantis/cvp/cvp-sanity-checks:stable'
22def SLAVE_NODE = (env.getProperty('SLAVE_NODE')) ?: 'docker'
Hanna Arhipova3b4b0652018-12-03 14:06:58 +020023
Oleksii Zhurbacaa46e12019-04-16 17:38:09 -050024/*
25YAML example
26=====
27# commands is a map of commands which looks like step_name: shell_command
28commands:
29 001_prepare: rm /var/lib/g.txt
30 002_prepare: git clone http://repo_with_tests.git
31 003_test: cd repo_with_tests && pytest /var/lib/ --collect-only
32 004_collect: cp cvp-spt /var/lib/validation_artifacts/
33# envs is a list of new environment variables
34envs:
35 - SALT_USERNAME=admin
36 - SALT_PASSWORD=password
37 - drivetrain_version=testing
38*/
Oleksii Zhurba71e97882018-02-16 20:09:43 -060039
Oleksii Zhurbacaa46e12019-04-16 17:38:09 -050040node (SLAVE_NODE) {
41 def artifacts_dir = 'validation_artifacts'
Oleksii Zhurba92730752019-05-30 18:03:41 -050042 def test_suite_name = "${env.JOB_NAME}"
43 def xml_file = "${test_suite_name}_report.xml"
44
Oleksii Zhurbacaa46e12019-04-16 17:38:09 -050045 def configRun = [:]
46 try {
47 withEnv(env_vars) {
48 stage('Initialization') {
49 def container_workdir = '/var/lib'
Oleksii Zhurbacaa46e12019-04-16 17:38:09 -050050 def tests_set = (env.getProperty('tests_set')) ?: ''
Dmitriy Kruglovb60e1702019-08-19 12:53:41 +020051 def script = "pytest --junitxml ${container_workdir}/${artifacts_dir}/${xml_file} -vv ${tests_set}"
Hanna Arhipova3b4b0652018-12-03 14:06:58 +020052
Oleksii Zhurbacaa46e12019-04-16 17:38:09 -050053 sh "mkdir -p ${artifacts_dir}"
Hanna Arhipova3b4b0652018-12-03 14:06:58 +020054
Oleksii Zhurbacaa46e12019-04-16 17:38:09 -050055 // Enrichment for docker commands
Hanna Arhipovaa5fff2c2020-02-11 22:27:26 +020056 def commands = EXTRA_PARAMS.get("commands") ?: ['010_start_tests': "tox -e ${test_suite_name} -- ${script}"]
Oleksii Zhurbacaa46e12019-04-16 17:38:09 -050057 def commands_list = commands.collectEntries{ [ (it.key) : { sh("${it.value}") } ] }
Hanna Arhipova3b4b0652018-12-03 14:06:58 +020058
Oleksii Zhurbacaa46e12019-04-16 17:38:09 -050059 // Enrichment for env variables
60 def creds = common.getCredentials(SALT_MASTER_CREDENTIALS)
61 def env_vars_list = [
62 "SALT_USERNAME=${creds.username}",
63 "SALT_PASSWORD=${creds.password}",
Hanna Arhipova6cda4862019-06-04 12:39:30 +030064 "SALT_URL=${SALT_MASTER_URL}",
Ievgeniia Zadorozhnaf32758b2019-11-13 18:32:59 +030065 "REQUESTS_CA_BUNDLE=/etc/ssl/certs/ca-certificates.crt",
66 "OVERRIDE_CONFIG=${override_config}"
Oleksii Zhurbacaa46e12019-04-16 17:38:09 -050067 ] + env_vars
Oleksii Zhurbad9034952018-09-03 21:06:06 -050068
Oleksii Zhurbacaa46e12019-04-16 17:38:09 -050069 // Generating final config
Oleksii Zhurba11167442019-07-22 11:15:02 -050070 def force_pull = (env.getProperty('force_pull')) ?: false
Oleksii Zhurbacaa46e12019-04-16 17:38:09 -050071 configRun = [
72 'image': IMAGE,
Oleksii Zhurba11167442019-07-22 11:15:02 -050073 'dockerPull': force_pull.toBoolean(),
Oleksii Zhurbacaa46e12019-04-16 17:38:09 -050074 'baseRepoPreConfig': false,
75 'dockerMaxCpus': 2,
76 'dockerExtraOpts' : [
77 "--network=host",
78 "-v /root/qa_results/:/root/qa_results/",
Hanna Arhipova6cda4862019-06-04 12:39:30 +030079 "-v /etc/ssl/certs/:/etc/ssl/certs/:ro",
Oleksii Zhurbacaa46e12019-04-16 17:38:09 -050080 "-v ${env.WORKSPACE}/${artifacts_dir}/:${container_workdir}/${artifacts_dir}/",
81 ],
82 'envOpts' : env_vars_list,
83 'runCommands' : commands_list
84 ]
85 }
86
87 stage('Run Tests') {
88 salt_testing.setupDockerAndTest(configRun)
89 }
90
91 stage ('Publish results') {
92 archiveArtifacts artifacts: "${artifacts_dir}/*"
93 junit "${artifacts_dir}/*.xml"
94 if (env.JOB_NAME.contains("cvp-spt")) {
95 plot csvFileName: 'plot-glance.csv',
96 group: 'SPT',
97 style: 'line',
98 title: 'SPT Glance results',
99 xmlSeries: [[
Oleksii Zhurba92730752019-05-30 18:03:41 -0500100 file: "${artifacts_dir}/${xml_file}",
Oleksii Zhurbacaa46e12019-04-16 17:38:09 -0500101 nodeType: 'NODESET',
102 url: '',
Hanna Arhipova39484d02020-03-02 15:43:31 +0200103 xpath: '/testsuites/testsuite/testcase[@classname="tests.test_glance"]/properties/property']]
Oleksii Zhurbacaa46e12019-04-16 17:38:09 -0500104 plot csvFileName: 'plot-hw2hw.csv',
105 group: 'SPT',
106 style: 'line',
107 title: 'SPT HW2HW results',
108 xmlSeries: [[
Oleksii Zhurba92730752019-05-30 18:03:41 -0500109 file: "${artifacts_dir}/${xml_file}",
Oleksii Zhurbacaa46e12019-04-16 17:38:09 -0500110 nodeType: 'NODESET',
111 url: '',
Hanna Arhipova39484d02020-03-02 15:43:31 +0200112 xpath: '/testsuites/testsuite/testcase[@classname="tests.test_hw2hw"]/properties/property']]
Oleksii Zhurbacaa46e12019-04-16 17:38:09 -0500113 plot csvFileName: 'plot-vm2vm.csv',
114 group: 'SPT',
115 style: 'line',
116 title: 'SPT VM2VM results',
117 xmlSeries: [[
Oleksii Zhurba92730752019-05-30 18:03:41 -0500118 file: "${artifacts_dir}/${xml_file}",
Oleksii Zhurbacaa46e12019-04-16 17:38:09 -0500119 nodeType: 'NODESET',
120 url: '',
Hanna Arhipova39484d02020-03-02 15:43:31 +0200121 xpath: '/testsuites/testsuite/testcase[@classname="tests.test_vm2vm"]/properties/property']]
Oleksii Zhurbacaa46e12019-04-16 17:38:09 -0500122 }
Sergey Galkinaef13c72020-01-27 21:03:25 +0400123 try {
124 sh """
125 for i in ${artifacts_dir}/*.xml; do
126 grep 'failures="0"' \$i
127 grep 'errors="0"' \$i
128 done
129 """
130 } catch(err) {
131 currentBuild.result = "FAILURE"
132 common.errorMsg("[ERROR] Failures or errors is not zero in ${artifacts_dir}/*.xml")
133 throw err
134 }
Oleksii Zhurbad9034952018-09-03 21:06:06 -0500135 }
Oleksii Zhurba71e97882018-02-16 20:09:43 -0600136 }
Oleksii Zhurbacaa46e12019-04-16 17:38:09 -0500137 }
138 catch (Throwable e) {
Oleksii Zhurba71e97882018-02-16 20:09:43 -0600139 currentBuild.result = "FAILURE"
140 throw e
Oleksii Zhurbacaa46e12019-04-16 17:38:09 -0500141 }
142 finally {
143 sh "rm -rf ${artifacts_dir}"
Oleksii Zhurba71e97882018-02-16 20:09:43 -0600144 }
145}