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 | 71e9788 | 2018-02-16 20:09:43 -0600 | [diff] [blame] | 26 | |
Oleksii Zhurba | 817a814 | 2018-10-08 21:38:15 +0300 | [diff] [blame] | 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 |
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 | */ |
Hanna Arhipova | 3b4b065 | 2018-12-03 14:06:58 +0200 | [diff] [blame] | 40 | |
Oleksii Zhurba | 817a814 | 2018-10-08 21:38:15 +0300 | [diff] [blame] | 41 | node (SLAVE_NODE) { |
| 42 | def artifacts_dir = 'validation_artifacts' |
| 43 | def configRun = [:] |
| 44 | try { |
| 45 | withEnv(env_vars) { |
| 46 | stage('Initialization') { |
| 47 | def container_workdir = '/var/lib' |
| 48 | def test_suite_name = "${env.JOB_NAME}" |
| 49 | def workdir = "${container_workdir}/${test_suite_name}" |
| 50 | def xml_file = "${test_suite_name}_report.xml" |
| 51 | def tests_set = (env.getProperty('tests_set')) ?: '' |
| 52 | def script = "pytest --junitxml ${container_workdir}/${artifacts_dir}/${xml_file} --tb=short -sv -vv ${tests_set}" |
Hanna Arhipova | 3b4b065 | 2018-12-03 14:06:58 +0200 | [diff] [blame] | 53 | |
Oleksii Zhurba | 817a814 | 2018-10-08 21:38:15 +0300 | [diff] [blame] | 54 | sh "mkdir -p ${artifacts_dir}" |
| 55 | |
| 56 | // Enrichment for docker commands |
| 57 | def commands = EXTRA_PARAMS.get("commands") ?: ['010_start_tests': "cd ${workdir} && with_venv.sh ${script}"] |
| 58 | def commands_list = commands.collectEntries{ [ (it.key) : { sh("${it.value}") } ] } |
| 59 | |
| 60 | // Enrichment for env variables |
| 61 | def creds = common.getCredentials(SALT_MASTER_CREDENTIALS) |
| 62 | def env_vars_list = [ |
| 63 | "SALT_USERNAME=${creds.username}", |
| 64 | "SALT_PASSWORD=${creds.password}", |
Hanna Arhipova | 3b4b065 | 2018-12-03 14:06:58 +0200 | [diff] [blame] | 65 | "SALT_URL=${SALT_MASTER_URL}" |
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 |
| 69 | configRun = [ |
| 70 | 'image': IMAGE, |
| 71 | 'baseRepoPreConfig': false, |
| 72 | 'dockerMaxCpus': 2, |
| 73 | 'dockerExtraOpts' : [ |
Hanna Arhipova | 5a38053 | 2019-02-07 12:17:37 +0200 | [diff] [blame^] | 74 | "--network=host", |
Oleksii Zhurba | 817a814 | 2018-10-08 21:38:15 +0300 | [diff] [blame] | 75 | "-v /root/qa_results/:/root/qa_results/", |
| 76 | "-v ${env.WORKSPACE}/${artifacts_dir}/:${container_workdir}/${artifacts_dir}/", |
| 77 | ], |
| 78 | 'envOpts' : env_vars_list, |
| 79 | 'runCommands' : commands_list |
| 80 | ] |
| 81 | } |
| 82 | |
| 83 | stage('Run Tests') { |
| 84 | salt_testing.setupDockerAndTest(configRun) |
| 85 | } |
| 86 | |
| 87 | stage ('Publish results') { |
| 88 | archiveArtifacts artifacts: "${artifacts_dir}/*" |
| 89 | junit "${artifacts_dir}/*.xml" |
| 90 | if (env.JOB_NAME.contains("cvp-spt")) { |
| 91 | plot csvFileName: 'plot-glance.csv', |
| 92 | group: 'SPT', |
| 93 | style: 'line', |
| 94 | title: 'SPT Glance results', |
| 95 | xmlSeries: [[ |
| 96 | file: "${env.JOB_NAME}_report.xml", |
| 97 | nodeType: 'NODESET', |
| 98 | url: '', |
| 99 | xpath: '/testsuite/testcase[@name="test_speed_glance"]/properties/property']] |
| 100 | plot csvFileName: 'plot-hw2hw.csv', |
| 101 | group: 'SPT', |
| 102 | style: 'line', |
| 103 | title: 'SPT HW2HW results', |
| 104 | xmlSeries: [[ |
| 105 | file: "${env.JOB_NAME}_report.xml", |
| 106 | nodeType: 'NODESET', |
| 107 | url: '', |
| 108 | xpath: '/testsuite/testcase[@classname="cvp_spt.tests.test_hw2hw"]/properties/property']] |
| 109 | plot csvFileName: 'plot-vm2vm.csv', |
| 110 | group: 'SPT', |
| 111 | style: 'line', |
| 112 | title: 'SPT VM2VM results', |
| 113 | xmlSeries: [[ |
| 114 | file: "${env.JOB_NAME}_report.xml", |
| 115 | nodeType: 'NODESET', |
| 116 | url: '', |
| 117 | xpath: '/testsuite/testcase[@classname="cvp_spt.tests.test_vm2vm"]/properties/property']] |
| 118 | } |
Oleksii Zhurba | d903495 | 2018-09-03 21:06:06 -0500 | [diff] [blame] | 119 | } |
Oleksii Zhurba | 71e9788 | 2018-02-16 20:09:43 -0600 | [diff] [blame] | 120 | } |
Oleksii Zhurba | 817a814 | 2018-10-08 21:38:15 +0300 | [diff] [blame] | 121 | } |
| 122 | catch (Throwable e) { |
Oleksii Zhurba | 71e9788 | 2018-02-16 20:09:43 -0600 | [diff] [blame] | 123 | currentBuild.result = "FAILURE" |
| 124 | throw e |
Oleksii Zhurba | 817a814 | 2018-10-08 21:38:15 +0300 | [diff] [blame] | 125 | } |
| 126 | finally { |
| 127 | sh "rm -rf ${artifacts_dir}" |
Oleksii Zhurba | 71e9788 | 2018-02-16 20:09:43 -0600 | [diff] [blame] | 128 | } |
| 129 | } |