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 | 817a814 | 2018-10-08 21:38:15 +0300 | [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 | 817a814 | 2018-10-08 21:38:15 +0300 | [diff] [blame] | 17 | def EXTRA_PARAMS = readYaml(text: env.getProperty('EXTRA_PARAMS')) ?: [:] |
| 18 | def env_vars = EXTRA_PARAMS.get("envs") ?: [] |
Oleksii Zhurba | 71e9788 | 2018-02-16 20:09:43 -0600 | [diff] [blame] | 19 | |
Oleksii Zhurba | 817a814 | 2018-10-08 21:38:15 +0300 | [diff] [blame] | 20 | def IMAGE = (env.getProperty('IMAGE')) ?: 'docker-prod-local.docker.mirantis.net/mirantis/cvp/cvp-sanity-checks:stable' |
| 21 | def SLAVE_NODE = (env.getProperty('SLAVE_NODE')) ?: 'docker' |
Hanna Arhipova | 3b4b065 | 2018-12-03 14:06:58 +0200 | [diff] [blame] | 22 | |
Oleksii Zhurba | 817a814 | 2018-10-08 21:38:15 +0300 | [diff] [blame] | 23 | /* |
| 24 | YAML example |
| 25 | ===== |
Oleksii Zhurba | 817a814 | 2018-10-08 21:38:15 +0300 | [diff] [blame] | 26 | # commands is a map of commands which looks like step_name: shell_command |
| 27 | commands: |
| 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 |
| 33 | envs: |
| 34 | - SALT_USERNAME=admin |
| 35 | - SALT_PASSWORD=password |
| 36 | - drivetrain_version=testing |
Oleksii Zhurba | 817a814 | 2018-10-08 21:38:15 +0300 | [diff] [blame] | 37 | */ |
Hanna Arhipova | 3b4b065 | 2018-12-03 14:06:58 +0200 | [diff] [blame] | 38 | |
Oleksii Zhurba | 817a814 | 2018-10-08 21:38:15 +0300 | [diff] [blame] | 39 | node (SLAVE_NODE) { |
| 40 | def artifacts_dir = 'validation_artifacts' |
Oleksii Zhurba | fe27db7 | 2019-05-30 18:03:41 -0500 | [diff] [blame] | 41 | def test_suite_name = "${env.JOB_NAME}" |
| 42 | def xml_file = "${test_suite_name}_report.xml" |
| 43 | |
Oleksii Zhurba | 817a814 | 2018-10-08 21:38:15 +0300 | [diff] [blame] | 44 | def configRun = [:] |
| 45 | try { |
| 46 | withEnv(env_vars) { |
| 47 | stage('Initialization') { |
| 48 | def container_workdir = '/var/lib' |
Oleksii Zhurba | 817a814 | 2018-10-08 21:38:15 +0300 | [diff] [blame] | 49 | def workdir = "${container_workdir}/${test_suite_name}" |
Oleksii Zhurba | 817a814 | 2018-10-08 21:38:15 +0300 | [diff] [blame] | 50 | def tests_set = (env.getProperty('tests_set')) ?: '' |
Dmitriy Kruglov | 6749a37 | 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 | 817a814 | 2018-10-08 21:38:15 +0300 | [diff] [blame] | 53 | 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 Arhipova | 16d95af | 2019-06-04 12:39:30 +0300 | [diff] [blame] | 64 | "SALT_URL=${SALT_MASTER_URL}", |
| 65 | "REQUESTS_CA_BUNDLE=/etc/ssl/certs/ca-certificates.crt" |
Oleksii Zhurba | 817a814 | 2018-10-08 21:38:15 +0300 | [diff] [blame] | 66 | ] + env_vars |
Oleksii Zhurba | d903495 | 2018-09-03 21:06:06 -0500 | [diff] [blame] | 67 | |
Oleksii Zhurba | 817a814 | 2018-10-08 21:38:15 +0300 | [diff] [blame] | 68 | // Generating final config |
Oleksii Zhurba | b7be9ff | 2019-07-22 11:15:02 -0500 | [diff] [blame] | 69 | def force_pull = (env.getProperty('force_pull')) ?: false |
Oleksii Zhurba | 817a814 | 2018-10-08 21:38:15 +0300 | [diff] [blame] | 70 | configRun = [ |
| 71 | 'image': IMAGE, |
Oleksii Zhurba | b7be9ff | 2019-07-22 11:15:02 -0500 | [diff] [blame] | 72 | 'dockerPull': force_pull.toBoolean(), |
Oleksii Zhurba | 817a814 | 2018-10-08 21:38:15 +0300 | [diff] [blame] | 73 | 'baseRepoPreConfig': false, |
| 74 | 'dockerMaxCpus': 2, |
| 75 | 'dockerExtraOpts' : [ |
Hanna Arhipova | 5a38053 | 2019-02-07 12:17:37 +0200 | [diff] [blame] | 76 | "--network=host", |
Oleksii Zhurba | 817a814 | 2018-10-08 21:38:15 +0300 | [diff] [blame] | 77 | "-v /root/qa_results/:/root/qa_results/", |
Hanna Arhipova | 16d95af | 2019-06-04 12:39:30 +0300 | [diff] [blame] | 78 | "-v /etc/ssl/certs/:/etc/ssl/certs/:ro", |
Oleksii Zhurba | 817a814 | 2018-10-08 21:38:15 +0300 | [diff] [blame] | 79 | "-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 Zhurba | fe27db7 | 2019-05-30 18:03:41 -0500 | [diff] [blame] | 99 | file: "${artifacts_dir}/${xml_file}", |
Oleksii Zhurba | 817a814 | 2018-10-08 21:38:15 +0300 | [diff] [blame] | 100 | nodeType: 'NODESET', |
| 101 | url: '', |
Oleksii Zhurba | fe27db7 | 2019-05-30 18:03:41 -0500 | [diff] [blame] | 102 | xpath: '/testsuite/testcase[@classname="tests.test_glance"]/properties/property']] |
Oleksii Zhurba | 817a814 | 2018-10-08 21:38:15 +0300 | [diff] [blame] | 103 | plot csvFileName: 'plot-hw2hw.csv', |
| 104 | group: 'SPT', |
| 105 | style: 'line', |
| 106 | title: 'SPT HW2HW results', |
| 107 | xmlSeries: [[ |
Oleksii Zhurba | fe27db7 | 2019-05-30 18:03:41 -0500 | [diff] [blame] | 108 | file: "${artifacts_dir}/${xml_file}", |
Oleksii Zhurba | 817a814 | 2018-10-08 21:38:15 +0300 | [diff] [blame] | 109 | nodeType: 'NODESET', |
| 110 | url: '', |
Oleksii Zhurba | fe27db7 | 2019-05-30 18:03:41 -0500 | [diff] [blame] | 111 | xpath: '/testsuite/testcase[@classname="tests.test_hw2hw"]/properties/property']] |
Oleksii Zhurba | 817a814 | 2018-10-08 21:38:15 +0300 | [diff] [blame] | 112 | plot csvFileName: 'plot-vm2vm.csv', |
| 113 | group: 'SPT', |
| 114 | style: 'line', |
| 115 | title: 'SPT VM2VM results', |
| 116 | xmlSeries: [[ |
Oleksii Zhurba | fe27db7 | 2019-05-30 18:03:41 -0500 | [diff] [blame] | 117 | file: "${artifacts_dir}/${xml_file}", |
Oleksii Zhurba | 817a814 | 2018-10-08 21:38:15 +0300 | [diff] [blame] | 118 | nodeType: 'NODESET', |
| 119 | url: '', |
Oleksii Zhurba | fe27db7 | 2019-05-30 18:03:41 -0500 | [diff] [blame] | 120 | xpath: '/testsuite/testcase[@classname="tests.test_vm2vm"]/properties/property']] |
Oleksii Zhurba | 817a814 | 2018-10-08 21:38:15 +0300 | [diff] [blame] | 121 | } |
Oleksii Zhurba | d903495 | 2018-09-03 21:06:06 -0500 | [diff] [blame] | 122 | } |
Oleksii Zhurba | 71e9788 | 2018-02-16 20:09:43 -0600 | [diff] [blame] | 123 | } |
Oleksii Zhurba | 817a814 | 2018-10-08 21:38:15 +0300 | [diff] [blame] | 124 | } |
| 125 | catch (Throwable e) { |
Oleksii Zhurba | 71e9788 | 2018-02-16 20:09:43 -0600 | [diff] [blame] | 126 | currentBuild.result = "FAILURE" |
| 127 | throw e |
Oleksii Zhurba | 817a814 | 2018-10-08 21:38:15 +0300 | [diff] [blame] | 128 | } |
| 129 | finally { |
| 130 | sh "rm -rf ${artifacts_dir}" |
Oleksii Zhurba | 71e9788 | 2018-02-16 20:09:43 -0600 | [diff] [blame] | 131 | } |
| 132 | } |