blob: 6d099b751541aaac4444dcc6c6df438be2714e3d [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") ?: []
Oleksii Zhurba71e97882018-02-16 20:09:43 -060019
Oleksii Zhurbacaa46e12019-04-16 17:38:09 -050020def IMAGE = (env.getProperty('IMAGE')) ?: 'docker-prod-local.docker.mirantis.net/mirantis/cvp/cvp-sanity-checks:stable'
21def SLAVE_NODE = (env.getProperty('SLAVE_NODE')) ?: 'docker'
Hanna Arhipova3b4b0652018-12-03 14:06:58 +020022
Oleksii Zhurbacaa46e12019-04-16 17:38:09 -050023/*
24YAML example
25=====
26# commands is a map of commands which looks like step_name: shell_command
27commands:
28 001_prepare: rm /var/lib/g.txt
29 002_prepare: git clone http://repo_with_tests.git
30 003_test: cd repo_with_tests && pytest /var/lib/ --collect-only
31 004_collect: cp cvp-spt /var/lib/validation_artifacts/
32# envs is a list of new environment variables
33envs:
34 - SALT_USERNAME=admin
35 - SALT_PASSWORD=password
36 - drivetrain_version=testing
37*/
Oleksii Zhurba71e97882018-02-16 20:09:43 -060038
Oleksii Zhurbacaa46e12019-04-16 17:38:09 -050039node (SLAVE_NODE) {
40 def artifacts_dir = 'validation_artifacts'
Oleksii Zhurba92730752019-05-30 18:03:41 -050041 def test_suite_name = "${env.JOB_NAME}"
42 def xml_file = "${test_suite_name}_report.xml"
43
Oleksii Zhurbacaa46e12019-04-16 17:38:09 -050044 def configRun = [:]
45 try {
46 withEnv(env_vars) {
47 stage('Initialization') {
48 def container_workdir = '/var/lib'
Oleksii Zhurbacaa46e12019-04-16 17:38:09 -050049 def workdir = "${container_workdir}/${test_suite_name}"
Oleksii Zhurbacaa46e12019-04-16 17:38:09 -050050 def tests_set = (env.getProperty('tests_set')) ?: ''
51 def script = "pytest --junitxml ${container_workdir}/${artifacts_dir}/${xml_file} --tb=short -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
56 def commands = EXTRA_PARAMS.get("commands") ?: ['010_start_tests': "cd ${workdir} && with_venv.sh ${script}"]
57 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 Arhipova3b4b0652018-12-03 14:06:58 +020064 "SALT_URL=${SALT_MASTER_URL}"
Oleksii Zhurbacaa46e12019-04-16 17:38:09 -050065 ] + env_vars
Oleksii Zhurbad9034952018-09-03 21:06:06 -050066
Oleksii Zhurbacaa46e12019-04-16 17:38:09 -050067 // Generating final config
68 configRun = [
69 'image': IMAGE,
70 'baseRepoPreConfig': false,
71 'dockerMaxCpus': 2,
72 'dockerExtraOpts' : [
73 "--network=host",
74 "-v /root/qa_results/:/root/qa_results/",
75 "-v ${env.WORKSPACE}/${artifacts_dir}/:${container_workdir}/${artifacts_dir}/",
76 ],
77 'envOpts' : env_vars_list,
78 'runCommands' : commands_list
79 ]
80 }
81
82 stage('Run Tests') {
83 salt_testing.setupDockerAndTest(configRun)
84 }
85
86 stage ('Publish results') {
87 archiveArtifacts artifacts: "${artifacts_dir}/*"
88 junit "${artifacts_dir}/*.xml"
89 if (env.JOB_NAME.contains("cvp-spt")) {
90 plot csvFileName: 'plot-glance.csv',
91 group: 'SPT',
92 style: 'line',
93 title: 'SPT Glance results',
94 xmlSeries: [[
Oleksii Zhurba92730752019-05-30 18:03:41 -050095 file: "${artifacts_dir}/${xml_file}",
Oleksii Zhurbacaa46e12019-04-16 17:38:09 -050096 nodeType: 'NODESET',
97 url: '',
Oleksii Zhurba92730752019-05-30 18:03:41 -050098 xpath: '/testsuite/testcase[@classname="tests.test_glance"]/properties/property']]
Oleksii Zhurbacaa46e12019-04-16 17:38:09 -050099 plot csvFileName: 'plot-hw2hw.csv',
100 group: 'SPT',
101 style: 'line',
102 title: 'SPT HW2HW results',
103 xmlSeries: [[
Oleksii Zhurba92730752019-05-30 18:03:41 -0500104 file: "${artifacts_dir}/${xml_file}",
Oleksii Zhurbacaa46e12019-04-16 17:38:09 -0500105 nodeType: 'NODESET',
106 url: '',
Oleksii Zhurba92730752019-05-30 18:03:41 -0500107 xpath: '/testsuite/testcase[@classname="tests.test_hw2hw"]/properties/property']]
Oleksii Zhurbacaa46e12019-04-16 17:38:09 -0500108 plot csvFileName: 'plot-vm2vm.csv',
109 group: 'SPT',
110 style: 'line',
111 title: 'SPT VM2VM results',
112 xmlSeries: [[
Oleksii Zhurba92730752019-05-30 18:03:41 -0500113 file: "${artifacts_dir}/${xml_file}",
Oleksii Zhurbacaa46e12019-04-16 17:38:09 -0500114 nodeType: 'NODESET',
115 url: '',
Oleksii Zhurba92730752019-05-30 18:03:41 -0500116 xpath: '/testsuite/testcase[@classname="tests.test_vm2vm"]/properties/property']]
Oleksii Zhurbacaa46e12019-04-16 17:38:09 -0500117 }
Oleksii Zhurbad9034952018-09-03 21:06:06 -0500118 }
Oleksii Zhurba71e97882018-02-16 20:09:43 -0600119 }
Oleksii Zhurbacaa46e12019-04-16 17:38:09 -0500120 }
121 catch (Throwable e) {
Oleksii Zhurba71e97882018-02-16 20:09:43 -0600122 currentBuild.result = "FAILURE"
123 throw e
Oleksii Zhurbacaa46e12019-04-16 17:38:09 -0500124 }
125 finally {
126 sh "rm -rf ${artifacts_dir}"
Oleksii Zhurba71e97882018-02-16 20:09:43 -0600127 }
128}