Oleksii Zhurba | 71e9788 | 2018-02-16 20:09:43 -0600 | [diff] [blame] | 1 | /** |
| 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 Zhurba | caa46e1 | 2019-04-16 17:38:09 -0500 | [diff] [blame] | 8 | * IMAGE Docker image link to use for running container with test framework. |
| 9 | * EXTRA_PARAMS Yaml context which contains additional setting for job |
Oleksii Zhurba | 71e9788 | 2018-02-16 20:09:43 -0600 | [diff] [blame] | 10 | * |
Oleksii Zhurba | 71e9788 | 2018-02-16 20:09:43 -0600 | [diff] [blame] | 11 | */ |
| 12 | |
Oleksii Zhurba | d903495 | 2018-09-03 21:06:06 -0500 | [diff] [blame] | 13 | common = new com.mirantis.mk.Common() |
Oleksii Zhurba | 71e9788 | 2018-02-16 20:09:43 -0600 | [diff] [blame] | 14 | validate = new com.mirantis.mcp.Validate() |
Hanna Arhipova | 3b4b065 | 2018-12-03 14:06:58 +0200 | [diff] [blame] | 15 | salt_testing = new com.mirantis.mk.SaltModelTesting() |
Hanna Arhipova | b451e79 | 2018-12-18 15:27:35 +0200 | [diff] [blame] | 16 | |
Oleksii Zhurba | caa46e1 | 2019-04-16 17:38:09 -0500 | [diff] [blame] | 17 | def EXTRA_PARAMS = readYaml(text: env.getProperty('EXTRA_PARAMS')) ?: [:] |
| 18 | def env_vars = EXTRA_PARAMS.get("envs") ?: [] |
Ievgeniia Zadorozhna | f32758b | 2019-11-13 18:32:59 +0300 | [diff] [blame] | 19 | def override_config = env.getProperty('EXTRA_PARAMS') ?: "" |
Oleksii Zhurba | 71e9788 | 2018-02-16 20:09:43 -0600 | [diff] [blame] | 20 | |
Oleksii Zhurba | caa46e1 | 2019-04-16 17:38:09 -0500 | [diff] [blame] | 21 | def IMAGE = (env.getProperty('IMAGE')) ?: 'docker-prod-local.docker.mirantis.net/mirantis/cvp/cvp-sanity-checks:stable' |
| 22 | def SLAVE_NODE = (env.getProperty('SLAVE_NODE')) ?: 'docker' |
Hanna Arhipova | 3b4b065 | 2018-12-03 14:06:58 +0200 | [diff] [blame] | 23 | |
Oleksii Zhurba | caa46e1 | 2019-04-16 17:38:09 -0500 | [diff] [blame] | 24 | /* |
| 25 | YAML example |
| 26 | ===== |
| 27 | # commands is a map of commands which looks like step_name: shell_command |
| 28 | commands: |
| 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 |
| 34 | envs: |
| 35 | - SALT_USERNAME=admin |
| 36 | - SALT_PASSWORD=password |
| 37 | - drivetrain_version=testing |
| 38 | */ |
Oleksii Zhurba | 71e9788 | 2018-02-16 20:09:43 -0600 | [diff] [blame] | 39 | |
Oleksii Zhurba | caa46e1 | 2019-04-16 17:38:09 -0500 | [diff] [blame] | 40 | node (SLAVE_NODE) { |
| 41 | def artifacts_dir = 'validation_artifacts' |
Oleksii Zhurba | 9273075 | 2019-05-30 18:03:41 -0500 | [diff] [blame] | 42 | def test_suite_name = "${env.JOB_NAME}" |
| 43 | def xml_file = "${test_suite_name}_report.xml" |
| 44 | |
Oleksii Zhurba | caa46e1 | 2019-04-16 17:38:09 -0500 | [diff] [blame] | 45 | def configRun = [:] |
| 46 | try { |
| 47 | withEnv(env_vars) { |
| 48 | stage('Initialization') { |
| 49 | def container_workdir = '/var/lib' |
Oleksii Zhurba | caa46e1 | 2019-04-16 17:38:09 -0500 | [diff] [blame] | 50 | def tests_set = (env.getProperty('tests_set')) ?: '' |
Dmitriy Kruglov | b60e170 | 2019-08-19 12:53:41 +0200 | [diff] [blame] | 51 | def script = "pytest --junitxml ${container_workdir}/${artifacts_dir}/${xml_file} -vv ${tests_set}" |
Hanna Arhipova | 3b4b065 | 2018-12-03 14:06:58 +0200 | [diff] [blame] | 52 | |
Oleksii Zhurba | caa46e1 | 2019-04-16 17:38:09 -0500 | [diff] [blame] | 53 | sh "mkdir -p ${artifacts_dir}" |
Hanna Arhipova | 3b4b065 | 2018-12-03 14:06:58 +0200 | [diff] [blame] | 54 | |
Oleksii Zhurba | caa46e1 | 2019-04-16 17:38:09 -0500 | [diff] [blame] | 55 | // Enrichment for docker commands |
Hanna Arhipova | a5fff2c | 2020-02-11 22:27:26 +0200 | [diff] [blame] | 56 | def commands = EXTRA_PARAMS.get("commands") ?: ['010_start_tests': "tox -e ${test_suite_name} -- ${script}"] |
Oleksii Zhurba | caa46e1 | 2019-04-16 17:38:09 -0500 | [diff] [blame] | 57 | def commands_list = commands.collectEntries{ [ (it.key) : { sh("${it.value}") } ] } |
Hanna Arhipova | 3b4b065 | 2018-12-03 14:06:58 +0200 | [diff] [blame] | 58 | |
Oleksii Zhurba | caa46e1 | 2019-04-16 17:38:09 -0500 | [diff] [blame] | 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 Arhipova | 6cda486 | 2019-06-04 12:39:30 +0300 | [diff] [blame] | 64 | "SALT_URL=${SALT_MASTER_URL}", |
Ievgeniia Zadorozhna | f32758b | 2019-11-13 18:32:59 +0300 | [diff] [blame] | 65 | "REQUESTS_CA_BUNDLE=/etc/ssl/certs/ca-certificates.crt", |
| 66 | "OVERRIDE_CONFIG=${override_config}" |
Oleksii Zhurba | caa46e1 | 2019-04-16 17:38:09 -0500 | [diff] [blame] | 67 | ] + env_vars |
Oleksii Zhurba | d903495 | 2018-09-03 21:06:06 -0500 | [diff] [blame] | 68 | |
Oleksii Zhurba | caa46e1 | 2019-04-16 17:38:09 -0500 | [diff] [blame] | 69 | // Generating final config |
Oleksii Zhurba | 1116744 | 2019-07-22 11:15:02 -0500 | [diff] [blame] | 70 | def force_pull = (env.getProperty('force_pull')) ?: false |
Oleksii Zhurba | caa46e1 | 2019-04-16 17:38:09 -0500 | [diff] [blame] | 71 | configRun = [ |
| 72 | 'image': IMAGE, |
Oleksii Zhurba | 1116744 | 2019-07-22 11:15:02 -0500 | [diff] [blame] | 73 | 'dockerPull': force_pull.toBoolean(), |
Oleksii Zhurba | caa46e1 | 2019-04-16 17:38:09 -0500 | [diff] [blame] | 74 | 'baseRepoPreConfig': false, |
| 75 | 'dockerMaxCpus': 2, |
| 76 | 'dockerExtraOpts' : [ |
| 77 | "--network=host", |
| 78 | "-v /root/qa_results/:/root/qa_results/", |
Hanna Arhipova | 6cda486 | 2019-06-04 12:39:30 +0300 | [diff] [blame] | 79 | "-v /etc/ssl/certs/:/etc/ssl/certs/:ro", |
Oleksii Zhurba | caa46e1 | 2019-04-16 17:38:09 -0500 | [diff] [blame] | 80 | "-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 Zhurba | 9273075 | 2019-05-30 18:03:41 -0500 | [diff] [blame] | 100 | file: "${artifacts_dir}/${xml_file}", |
Oleksii Zhurba | caa46e1 | 2019-04-16 17:38:09 -0500 | [diff] [blame] | 101 | nodeType: 'NODESET', |
| 102 | url: '', |
Hanna Arhipova | 39484d0 | 2020-03-02 15:43:31 +0200 | [diff] [blame] | 103 | xpath: '/testsuites/testsuite/testcase[@classname="tests.test_glance"]/properties/property']] |
Oleksii Zhurba | caa46e1 | 2019-04-16 17:38:09 -0500 | [diff] [blame] | 104 | plot csvFileName: 'plot-hw2hw.csv', |
| 105 | group: 'SPT', |
| 106 | style: 'line', |
| 107 | title: 'SPT HW2HW results', |
| 108 | xmlSeries: [[ |
Oleksii Zhurba | 9273075 | 2019-05-30 18:03:41 -0500 | [diff] [blame] | 109 | file: "${artifacts_dir}/${xml_file}", |
Oleksii Zhurba | caa46e1 | 2019-04-16 17:38:09 -0500 | [diff] [blame] | 110 | nodeType: 'NODESET', |
| 111 | url: '', |
Hanna Arhipova | 39484d0 | 2020-03-02 15:43:31 +0200 | [diff] [blame] | 112 | xpath: '/testsuites/testsuite/testcase[@classname="tests.test_hw2hw"]/properties/property']] |
Oleksii Zhurba | caa46e1 | 2019-04-16 17:38:09 -0500 | [diff] [blame] | 113 | plot csvFileName: 'plot-vm2vm.csv', |
| 114 | group: 'SPT', |
| 115 | style: 'line', |
| 116 | title: 'SPT VM2VM results', |
| 117 | xmlSeries: [[ |
Oleksii Zhurba | 9273075 | 2019-05-30 18:03:41 -0500 | [diff] [blame] | 118 | file: "${artifacts_dir}/${xml_file}", |
Oleksii Zhurba | caa46e1 | 2019-04-16 17:38:09 -0500 | [diff] [blame] | 119 | nodeType: 'NODESET', |
| 120 | url: '', |
Hanna Arhipova | 39484d0 | 2020-03-02 15:43:31 +0200 | [diff] [blame] | 121 | xpath: '/testsuites/testsuite/testcase[@classname="tests.test_vm2vm"]/properties/property']] |
Oleksii Zhurba | caa46e1 | 2019-04-16 17:38:09 -0500 | [diff] [blame] | 122 | } |
Sergey Galkin | aef13c7 | 2020-01-27 21:03:25 +0400 | [diff] [blame] | 123 | 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 Zhurba | d903495 | 2018-09-03 21:06:06 -0500 | [diff] [blame] | 135 | } |
Oleksii Zhurba | 71e9788 | 2018-02-16 20:09:43 -0600 | [diff] [blame] | 136 | } |
Oleksii Zhurba | caa46e1 | 2019-04-16 17:38:09 -0500 | [diff] [blame] | 137 | } |
| 138 | catch (Throwable e) { |
Oleksii Zhurba | 71e9788 | 2018-02-16 20:09:43 -0600 | [diff] [blame] | 139 | currentBuild.result = "FAILURE" |
| 140 | throw e |
Oleksii Zhurba | caa46e1 | 2019-04-16 17:38:09 -0500 | [diff] [blame] | 141 | } |
| 142 | finally { |
| 143 | sh "rm -rf ${artifacts_dir}" |
Oleksii Zhurba | 71e9788 | 2018-02-16 20:09:43 -0600 | [diff] [blame] | 144 | } |
| 145 | } |