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") ?: [] |
Oleksii Zhurba | 71e9788 | 2018-02-16 20:09:43 -0600 | [diff] [blame] | 19 | |
Oleksii Zhurba | caa46e1 | 2019-04-16 17:38:09 -0500 | [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 | caa46e1 | 2019-04-16 17:38:09 -0500 | [diff] [blame] | 23 | /* |
| 24 | YAML example |
| 25 | ===== |
| 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 |
| 37 | */ |
Oleksii Zhurba | 71e9788 | 2018-02-16 20:09:43 -0600 | [diff] [blame] | 38 | |
Oleksii Zhurba | caa46e1 | 2019-04-16 17:38:09 -0500 | [diff] [blame] | 39 | node (SLAVE_NODE) { |
| 40 | def artifacts_dir = 'validation_artifacts' |
Oleksii Zhurba | 9273075 | 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 | caa46e1 | 2019-04-16 17:38:09 -0500 | [diff] [blame] | 44 | def configRun = [:] |
| 45 | try { |
| 46 | withEnv(env_vars) { |
| 47 | stage('Initialization') { |
| 48 | def container_workdir = '/var/lib' |
Oleksii Zhurba | caa46e1 | 2019-04-16 17:38:09 -0500 | [diff] [blame] | 49 | def workdir = "${container_workdir}/${test_suite_name}" |
Oleksii Zhurba | caa46e1 | 2019-04-16 17:38:09 -0500 | [diff] [blame] | 50 | def tests_set = (env.getProperty('tests_set')) ?: '' |
| 51 | def script = "pytest --junitxml ${container_workdir}/${artifacts_dir}/${xml_file} --tb=short -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 |
| 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 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 | 3b4b065 | 2018-12-03 14:06:58 +0200 | [diff] [blame] | 64 | "SALT_URL=${SALT_MASTER_URL}" |
Oleksii Zhurba | caa46e1 | 2019-04-16 17:38:09 -0500 | [diff] [blame] | 65 | ] + env_vars |
Oleksii Zhurba | d903495 | 2018-09-03 21:06:06 -0500 | [diff] [blame] | 66 | |
Oleksii Zhurba | caa46e1 | 2019-04-16 17:38:09 -0500 | [diff] [blame] | 67 | // 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 Zhurba | 9273075 | 2019-05-30 18:03:41 -0500 | [diff] [blame^] | 95 | file: "${artifacts_dir}/${xml_file}", |
Oleksii Zhurba | caa46e1 | 2019-04-16 17:38:09 -0500 | [diff] [blame] | 96 | nodeType: 'NODESET', |
| 97 | url: '', |
Oleksii Zhurba | 9273075 | 2019-05-30 18:03:41 -0500 | [diff] [blame^] | 98 | xpath: '/testsuite/testcase[@classname="tests.test_glance"]/properties/property']] |
Oleksii Zhurba | caa46e1 | 2019-04-16 17:38:09 -0500 | [diff] [blame] | 99 | plot csvFileName: 'plot-hw2hw.csv', |
| 100 | group: 'SPT', |
| 101 | style: 'line', |
| 102 | title: 'SPT HW2HW results', |
| 103 | xmlSeries: [[ |
Oleksii Zhurba | 9273075 | 2019-05-30 18:03:41 -0500 | [diff] [blame^] | 104 | file: "${artifacts_dir}/${xml_file}", |
Oleksii Zhurba | caa46e1 | 2019-04-16 17:38:09 -0500 | [diff] [blame] | 105 | nodeType: 'NODESET', |
| 106 | url: '', |
Oleksii Zhurba | 9273075 | 2019-05-30 18:03:41 -0500 | [diff] [blame^] | 107 | xpath: '/testsuite/testcase[@classname="tests.test_hw2hw"]/properties/property']] |
Oleksii Zhurba | caa46e1 | 2019-04-16 17:38:09 -0500 | [diff] [blame] | 108 | plot csvFileName: 'plot-vm2vm.csv', |
| 109 | group: 'SPT', |
| 110 | style: 'line', |
| 111 | title: 'SPT VM2VM results', |
| 112 | xmlSeries: [[ |
Oleksii Zhurba | 9273075 | 2019-05-30 18:03:41 -0500 | [diff] [blame^] | 113 | file: "${artifacts_dir}/${xml_file}", |
Oleksii Zhurba | caa46e1 | 2019-04-16 17:38:09 -0500 | [diff] [blame] | 114 | nodeType: 'NODESET', |
| 115 | url: '', |
Oleksii Zhurba | 9273075 | 2019-05-30 18:03:41 -0500 | [diff] [blame^] | 116 | xpath: '/testsuite/testcase[@classname="tests.test_vm2vm"]/properties/property']] |
Oleksii Zhurba | caa46e1 | 2019-04-16 17:38:09 -0500 | [diff] [blame] | 117 | } |
Oleksii Zhurba | d903495 | 2018-09-03 21:06:06 -0500 | [diff] [blame] | 118 | } |
Oleksii Zhurba | 71e9788 | 2018-02-16 20:09:43 -0600 | [diff] [blame] | 119 | } |
Oleksii Zhurba | caa46e1 | 2019-04-16 17:38:09 -0500 | [diff] [blame] | 120 | } |
| 121 | catch (Throwable e) { |
Oleksii Zhurba | 71e9788 | 2018-02-16 20:09:43 -0600 | [diff] [blame] | 122 | currentBuild.result = "FAILURE" |
| 123 | throw e |
Oleksii Zhurba | caa46e1 | 2019-04-16 17:38:09 -0500 | [diff] [blame] | 124 | } |
| 125 | finally { |
| 126 | sh "rm -rf ${artifacts_dir}" |
Oleksii Zhurba | 71e9788 | 2018-02-16 20:09:43 -0600 | [diff] [blame] | 127 | } |
| 128 | } |