blob: edbe902a4febc541ebf4341f87a536aabc73af3a [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 Zhurba817a8142018-10-08 21:38:15 +03008 * 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 Zhurba817a8142018-10-08 21:38:15 +030017def EXTRA_PARAMS = readYaml(text: env.getProperty('EXTRA_PARAMS')) ?: [:]
18def env_vars = EXTRA_PARAMS.get("envs") ?: []
Oleksii Zhurba71e97882018-02-16 20:09:43 -060019
Oleksii Zhurba817a8142018-10-08 21:38:15 +030020def 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 Zhurba817a8142018-10-08 21:38:15 +030023/*
24YAML example
25=====
Oleksii Zhurba817a8142018-10-08 21:38:15 +030026# 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
Oleksii Zhurba817a8142018-10-08 21:38:15 +030037*/
Hanna Arhipova3b4b0652018-12-03 14:06:58 +020038
Oleksii Zhurba817a8142018-10-08 21:38:15 +030039node (SLAVE_NODE) {
40 def artifacts_dir = 'validation_artifacts'
Oleksii Zhurbafe27db72019-05-30 18:03:41 -050041 def test_suite_name = "${env.JOB_NAME}"
42 def xml_file = "${test_suite_name}_report.xml"
43
Oleksii Zhurba817a8142018-10-08 21:38:15 +030044 def configRun = [:]
45 try {
46 withEnv(env_vars) {
47 stage('Initialization') {
48 def container_workdir = '/var/lib'
Oleksii Zhurba817a8142018-10-08 21:38:15 +030049 def workdir = "${container_workdir}/${test_suite_name}"
Oleksii Zhurba817a8142018-10-08 21:38:15 +030050 def tests_set = (env.getProperty('tests_set')) ?: ''
Dmitriy Kruglov6749a372019-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 Zhurba817a8142018-10-08 21:38:15 +030053 sh "mkdir -p ${artifacts_dir}"
54
55 // 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}") } ] }
58
59 // 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 Arhipova16d95af2019-06-04 12:39:30 +030064 "SALT_URL=${SALT_MASTER_URL}",
65 "REQUESTS_CA_BUNDLE=/etc/ssl/certs/ca-certificates.crt"
Oleksii Zhurba817a8142018-10-08 21:38:15 +030066 ] + env_vars
Oleksii Zhurbad9034952018-09-03 21:06:06 -050067
Oleksii Zhurba817a8142018-10-08 21:38:15 +030068 // Generating final config
Oleksii Zhurbab7be9ff2019-07-22 11:15:02 -050069 def force_pull = (env.getProperty('force_pull')) ?: false
Oleksii Zhurba817a8142018-10-08 21:38:15 +030070 configRun = [
71 'image': IMAGE,
Oleksii Zhurbab7be9ff2019-07-22 11:15:02 -050072 'dockerPull': force_pull.toBoolean(),
Oleksii Zhurba817a8142018-10-08 21:38:15 +030073 'baseRepoPreConfig': false,
74 'dockerMaxCpus': 2,
75 'dockerExtraOpts' : [
Hanna Arhipova5a380532019-02-07 12:17:37 +020076 "--network=host",
Oleksii Zhurba817a8142018-10-08 21:38:15 +030077 "-v /root/qa_results/:/root/qa_results/",
Hanna Arhipova16d95af2019-06-04 12:39:30 +030078 "-v /etc/ssl/certs/:/etc/ssl/certs/:ro",
Oleksii Zhurba817a8142018-10-08 21:38:15 +030079 "-v ${env.WORKSPACE}/${artifacts_dir}/:${container_workdir}/${artifacts_dir}/",
80 ],
81 'envOpts' : env_vars_list,
82 'runCommands' : commands_list
83 ]
84 }
85
86 stage('Run Tests') {
87 salt_testing.setupDockerAndTest(configRun)
88 }
89
90 stage ('Publish results') {
91 archiveArtifacts artifacts: "${artifacts_dir}/*"
92 junit "${artifacts_dir}/*.xml"
93 if (env.JOB_NAME.contains("cvp-spt")) {
94 plot csvFileName: 'plot-glance.csv',
95 group: 'SPT',
96 style: 'line',
97 title: 'SPT Glance results',
98 xmlSeries: [[
Oleksii Zhurbafe27db72019-05-30 18:03:41 -050099 file: "${artifacts_dir}/${xml_file}",
Oleksii Zhurba817a8142018-10-08 21:38:15 +0300100 nodeType: 'NODESET',
101 url: '',
Oleksii Zhurbafe27db72019-05-30 18:03:41 -0500102 xpath: '/testsuite/testcase[@classname="tests.test_glance"]/properties/property']]
Oleksii Zhurba817a8142018-10-08 21:38:15 +0300103 plot csvFileName: 'plot-hw2hw.csv',
104 group: 'SPT',
105 style: 'line',
106 title: 'SPT HW2HW results',
107 xmlSeries: [[
Oleksii Zhurbafe27db72019-05-30 18:03:41 -0500108 file: "${artifacts_dir}/${xml_file}",
Oleksii Zhurba817a8142018-10-08 21:38:15 +0300109 nodeType: 'NODESET',
110 url: '',
Oleksii Zhurbafe27db72019-05-30 18:03:41 -0500111 xpath: '/testsuite/testcase[@classname="tests.test_hw2hw"]/properties/property']]
Oleksii Zhurba817a8142018-10-08 21:38:15 +0300112 plot csvFileName: 'plot-vm2vm.csv',
113 group: 'SPT',
114 style: 'line',
115 title: 'SPT VM2VM results',
116 xmlSeries: [[
Oleksii Zhurbafe27db72019-05-30 18:03:41 -0500117 file: "${artifacts_dir}/${xml_file}",
Oleksii Zhurba817a8142018-10-08 21:38:15 +0300118 nodeType: 'NODESET',
119 url: '',
Oleksii Zhurbafe27db72019-05-30 18:03:41 -0500120 xpath: '/testsuite/testcase[@classname="tests.test_vm2vm"]/properties/property']]
Oleksii Zhurba817a8142018-10-08 21:38:15 +0300121 }
Oleksii Zhurbad9034952018-09-03 21:06:06 -0500122 }
Oleksii Zhurba71e97882018-02-16 20:09:43 -0600123 }
Oleksii Zhurba817a8142018-10-08 21:38:15 +0300124 }
125 catch (Throwable e) {
Oleksii Zhurba71e97882018-02-16 20:09:43 -0600126 currentBuild.result = "FAILURE"
127 throw e
Oleksii Zhurba817a8142018-10-08 21:38:15 +0300128 }
129 finally {
130 sh "rm -rf ${artifacts_dir}"
Oleksii Zhurba71e97882018-02-16 20:09:43 -0600131 }
132}