| /** |
| * |
| * Launch pytest frameworks in Jenkins |
| * |
| * Expected parameters: |
| * SALT_MASTER_URL URL of Salt master |
| * SALT_MASTER_CREDENTIALS Credentials to the Salt API |
| * IMAGE Docker image link to use for running container with test framework. |
| * EXTRA_PARAMS Yaml context which contains additional setting for job |
| * |
| */ |
| |
| common = new com.mirantis.mk.Common() |
| validate = new com.mirantis.mcp.Validate() |
| salt_testing = new com.mirantis.mk.SaltModelTesting() |
| |
| def EXTRA_PARAMS = readYaml(text: env.getProperty('EXTRA_PARAMS')) ?: [:] |
| def env_vars = EXTRA_PARAMS.get("envs") ?: [] |
| def override_config = env.getProperty('EXTRA_PARAMS') ?: "" |
| |
| def IMAGE = (env.getProperty('IMAGE')) ?: 'docker-prod-local.docker.mirantis.net/mirantis/cvp/cvp-sanity-checks:stable' |
| def SLAVE_NODE = (env.getProperty('SLAVE_NODE')) ?: 'docker' |
| |
| /* |
| YAML example |
| ===== |
| # commands is a map of commands which looks like step_name: shell_command |
| commands: |
| 001_prepare: rm /var/lib/g.txt |
| 002_prepare: git clone http://repo_with_tests.git |
| 003_test: cd repo_with_tests && pytest /var/lib/ --collect-only |
| 004_collect: cp cvp-spt /var/lib/validation_artifacts/ |
| # envs is a list of new environment variables |
| envs: |
| - SALT_USERNAME=admin |
| - SALT_PASSWORD=password |
| - drivetrain_version=testing |
| */ |
| |
| node (SLAVE_NODE) { |
| def artifacts_dir = 'validation_artifacts' |
| def test_suite_name = "${env.JOB_NAME}" |
| def xml_file = "${test_suite_name}_report.xml" |
| |
| def configRun = [:] |
| try { |
| withEnv(env_vars) { |
| stage('Initialization') { |
| def container_workdir = '/var/lib' |
| def tests_set = (env.getProperty('tests_set')) ?: '' |
| def script = "pytest --junitxml ${container_workdir}/${artifacts_dir}/${xml_file} -vv ${tests_set}" |
| |
| sh "mkdir -p ${artifacts_dir}" |
| |
| // Enrichment for docker commands |
| def commands = EXTRA_PARAMS.get("commands") ?: ['010_start_tests': "tox -e ${test_suite_name} -- ${script}"] |
| def commands_list = commands.collectEntries{ [ (it.key) : { sh("${it.value}") } ] } |
| |
| // Enrichment for env variables |
| def creds = common.getCredentials(SALT_MASTER_CREDENTIALS) |
| def env_vars_list = [ |
| "SALT_USERNAME=${creds.username}", |
| "SALT_PASSWORD=${creds.password}", |
| "SALT_URL=${SALT_MASTER_URL}", |
| "REQUESTS_CA_BUNDLE=/etc/ssl/certs/ca-certificates.crt", |
| "OVERRIDE_CONFIG=${override_config}" |
| ] + env_vars |
| |
| // Generating final config |
| def force_pull = (env.getProperty('force_pull')) ?: false |
| configRun = [ |
| 'image': IMAGE, |
| 'dockerPull': force_pull.toBoolean(), |
| 'baseRepoPreConfig': false, |
| 'dockerMaxCpus': 2, |
| 'dockerExtraOpts' : [ |
| "--network=host", |
| "-v /root/qa_results/:/root/qa_results/", |
| "-v /etc/ssl/certs/:/etc/ssl/certs/:ro", |
| "-v ${env.WORKSPACE}/${artifacts_dir}/:${container_workdir}/${artifacts_dir}/", |
| ], |
| 'envOpts' : env_vars_list, |
| 'runCommands' : commands_list |
| ] |
| } |
| |
| stage('Run Tests') { |
| salt_testing.setupDockerAndTest(configRun) |
| } |
| |
| stage ('Publish results') { |
| archiveArtifacts artifacts: "${artifacts_dir}/*" |
| junit "${artifacts_dir}/*.xml" |
| if (env.JOB_NAME.contains("cvp-spt")) { |
| plot csvFileName: 'plot-glance.csv', |
| group: 'SPT', |
| style: 'line', |
| title: 'SPT Glance results', |
| xmlSeries: [[ |
| file: "${artifacts_dir}/${xml_file}", |
| nodeType: 'NODESET', |
| url: '', |
| xpath: '/testsuites/testsuite/testcase[@classname="tests.test_glance"]/properties/property']] |
| plot csvFileName: 'plot-hw2hw.csv', |
| group: 'SPT', |
| style: 'line', |
| title: 'SPT HW2HW results', |
| xmlSeries: [[ |
| file: "${artifacts_dir}/${xml_file}", |
| nodeType: 'NODESET', |
| url: '', |
| xpath: '/testsuites/testsuite/testcase[@classname="tests.test_hw2hw"]/properties/property']] |
| plot csvFileName: 'plot-vm2vm.csv', |
| group: 'SPT', |
| style: 'line', |
| title: 'SPT VM2VM results', |
| xmlSeries: [[ |
| file: "${artifacts_dir}/${xml_file}", |
| nodeType: 'NODESET', |
| url: '', |
| xpath: '/testsuites/testsuite/testcase[@classname="tests.test_vm2vm"]/properties/property']] |
| } |
| try { |
| sh """ |
| for i in ${artifacts_dir}/*.xml; do |
| grep 'failures="0"' \$i |
| grep 'errors="0"' \$i |
| done |
| """ |
| } catch(err) { |
| currentBuild.result = "FAILURE" |
| common.errorMsg("[ERROR] Failures or errors is not zero in ${artifacts_dir}/*.xml") |
| throw err |
| } |
| } |
| } |
| } |
| catch (Throwable e) { |
| currentBuild.result = "FAILURE" |
| throw e |
| } |
| finally { |
| sh "rm -rf ${artifacts_dir}" |
| } |
| } |