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 |
| 8 | * |
| 9 | * TESTS_SET Leave empty for full run or choose a file (test) |
| 10 | * TESTS_REPO Repo to clone |
| 11 | * TESTS_SETTINGS Additional environment varibales to apply |
| 12 | * PROXY Proxy to use for cloning repo or for pip |
Hanna Arhipova | 3b4b065 | 2018-12-03 14:06:58 +0200 | [diff] [blame] | 13 | * IMAGE Docker image to use for running container with test framework. |
Oleksii Zhurba | d903495 | 2018-09-03 21:06:06 -0500 | [diff] [blame] | 14 | * DEBUG_MODE If you need to debug (keep container after test), please enabled this |
Hanna Arhipova | 3b4b065 | 2018-12-03 14:06:58 +0200 | [diff] [blame] | 15 | * To launch tests from cvp_spt docker images need to set IMAGE and left TESTS_REPO empty |
Oleksii Zhurba | 71e9788 | 2018-02-16 20:09:43 -0600 | [diff] [blame] | 16 | */ |
| 17 | |
Oleksii Zhurba | d903495 | 2018-09-03 21:06:06 -0500 | [diff] [blame] | 18 | common = new com.mirantis.mk.Common() |
Oleksii Zhurba | 71e9788 | 2018-02-16 20:09:43 -0600 | [diff] [blame] | 19 | validate = new com.mirantis.mcp.Validate() |
Oleksii Zhurba | d903495 | 2018-09-03 21:06:06 -0500 | [diff] [blame] | 20 | salt = new com.mirantis.mk.Salt() |
Hanna Arhipova | 3b4b065 | 2018-12-03 14:06:58 +0200 | [diff] [blame] | 21 | salt_testing = new com.mirantis.mk.SaltModelTesting() |
| 22 | def artifacts_dir = "validation_artifacts/" |
| 23 | def remote_dir = '/root/qa_results' |
Oleksii Zhurba | d903495 | 2018-09-03 21:06:06 -0500 | [diff] [blame] | 24 | def container_workdir = '/var/lib' |
Hanna Arhipova | 3b4b065 | 2018-12-03 14:06:58 +0200 | [diff] [blame] | 25 | def name = 'cvp-spt' |
| 26 | def xml_file = "${name}_report.xml" |
Oleksii Zhurba | d903495 | 2018-09-03 21:06:06 -0500 | [diff] [blame] | 27 | def TARGET_NODE = "I@gerrit:client" |
| 28 | def reinstall_env = false |
| 29 | def container_name = "${env.JOB_NAME}" |
| 30 | def saltMaster |
| 31 | def settings |
Oleksii Zhurba | 71e9788 | 2018-02-16 20:09:43 -0600 | [diff] [blame] | 32 | |
Hanna Arhipova | 3b4b065 | 2018-12-03 14:06:58 +0200 | [diff] [blame] | 33 | slaveNode = (env.getProperty('SLAVE_NODE')) ?: 'docker' |
| 34 | imageName = (env.getProperty('IMAGE')) ?: 'docker-prod-local.docker.mirantis.net/mirantis/cvp/cvp-spt:stable' |
| 35 | |
| 36 | node(slaveNode) { |
Oleksii Zhurba | 71e9788 | 2018-02-16 20:09:43 -0600 | [diff] [blame] | 37 | try{ |
| 38 | stage('Initialization') { |
Oleksii Zhurba | d903495 | 2018-09-03 21:06:06 -0500 | [diff] [blame] | 39 | sh "rm -rf ${artifacts_dir}" |
Hanna Arhipova | 3b4b065 | 2018-12-03 14:06:58 +0200 | [diff] [blame] | 40 | // TODO collaps TESTS_SETTINGS flow into EXTRA variables map |
Oleksii Zhurba | d903495 | 2018-09-03 21:06:06 -0500 | [diff] [blame] | 41 | if ( TESTS_SETTINGS != "" ) { |
| 42 | for (var in TESTS_SETTINGS.tokenize(";")) { |
| 43 | key = var.tokenize("=")[0].trim() |
| 44 | value = var.tokenize("=")[1].trim() |
| 45 | if (key == 'TARGET_NODE') { |
| 46 | TARGET_NODE = value |
| 47 | common.infoMsg("Node for container is set to ${TARGET_NODE}") |
| 48 | } |
| 49 | if (key == 'REINSTALL_ENV') { |
| 50 | reinstall_env = value.toBoolean() |
| 51 | } |
| 52 | } |
| 53 | } |
| 54 | if ( IMAGE == "" ) { |
| 55 | common.infoMsg("Env for tests will be built on Jenkins slave") |
| 56 | TARGET_NODE = "" |
| 57 | validate.prepareVenv(TESTS_REPO, PROXY) |
| 58 | } else { |
| 59 | saltMaster = salt.connection(SALT_MASTER_URL, SALT_MASTER_CREDENTIALS) |
Hanna Arhipova | 3b4b065 | 2018-12-03 14:06:58 +0200 | [diff] [blame] | 60 | salt.cmdRun(saltMaster, TARGET_NODE, "rm -rf ${remote_dir}/") |
| 61 | salt.cmdRun(saltMaster, TARGET_NODE, "mkdir -p ${remote_dir}/") |
Oleksii Zhurba | d903495 | 2018-09-03 21:06:06 -0500 | [diff] [blame] | 62 | validate.runContainer(saltMaster, TARGET_NODE, IMAGE, container_name) |
| 63 | if ( TESTS_REPO != "") { |
Hanna Arhipova | 3b4b065 | 2018-12-03 14:06:58 +0200 | [diff] [blame] | 64 | salt.cmdRun(saltMaster, TARGET_NODE, "docker exec ${container_name} rm -rf ${container_workdir}/${container_name}") |
Oleksii Zhurba | d903495 | 2018-09-03 21:06:06 -0500 | [diff] [blame] | 65 | salt.cmdRun(saltMaster, TARGET_NODE, "docker exec ${container_name} git clone ${TESTS_REPO} ${container_workdir}/${container_name}") |
| 66 | TESTS_SET = container_workdir + '/' + container_name + '/' + TESTS_SET |
| 67 | if ( reinstall_env ) { |
| 68 | common.infoMsg("Pip packages in container will be reinstalled based on requirements.txt from ${TESTS_REPO}") |
| 69 | salt.cmdRun(saltMaster, TARGET_NODE, "docker exec ${container_name} pip install --force-reinstall -r ${container_workdir}/${container_name}/requirements.txt") |
| 70 | } |
| 71 | } |
| 72 | } |
Oleksii Zhurba | 71e9788 | 2018-02-16 20:09:43 -0600 | [diff] [blame] | 73 | } |
| 74 | |
| 75 | stage('Run Tests') { |
Hanna Arhipova | 3b4b065 | 2018-12-03 14:06:58 +0200 | [diff] [blame] | 76 | def creds = common.getCredentials(SALT_MASTER_CREDENTIALS) |
| 77 | def username = creds.username |
| 78 | def password = creds.password |
| 79 | def script = "pytest --junitxml ${container_workdir}/${artifacts_dir}/${xml_file} --tb=short -sv ${container_workdir}/${TESTS_SET}" |
| 80 | |
Oleksii Zhurba | 71e9788 | 2018-02-16 20:09:43 -0600 | [diff] [blame] | 81 | sh "mkdir -p ${artifacts_dir}" |
Hanna Arhipova | 3b4b065 | 2018-12-03 14:06:58 +0200 | [diff] [blame] | 82 | |
| 83 | def configRun = [ |
| 84 | 'image': imageName, |
| 85 | 'baseRepoPreConfig': false, |
| 86 | 'dockerMaxCpus': 2, |
| 87 | 'dockerExtraOpts' : [ |
| 88 | "-v /root/qa_results/:/root/qa_results/", |
| 89 | "-v ${env.WORKSPACE}/validation_artifacts/:${container_workdir}/validation_artifacts/", |
| 90 | "--entrypoint=''", // to override ENTRYPOINT=/bin/bash in Dockerfile of image |
| 91 | ], |
| 92 | |
| 93 | 'envOpts' : [ |
| 94 | "WORKSPACE=${container_workdir}/${name}", |
| 95 | "SALT_USERNAME=${username}", |
| 96 | "SALT_PASSWORD=${password}", |
| 97 | "SALT_URL=${SALT_MASTER_URL}" |
| 98 | ] + TESTS_SETTINGS.replaceAll('\\"', '').tokenize(";"), |
| 99 | 'runCommands' : [ |
| 100 | '010_start_tests' : { |
| 101 | sh("cd ${container_workdir} && ${script}") |
| 102 | } |
| 103 | ] |
| 104 | ] |
| 105 | salt_testing.setupDockerAndTest(configRun) |
Oleksii Zhurba | 71e9788 | 2018-02-16 20:09:43 -0600 | [diff] [blame] | 106 | } |
Oleksii Zhurba | d903495 | 2018-09-03 21:06:06 -0500 | [diff] [blame] | 107 | |
Oleksii Zhurba | 71e9788 | 2018-02-16 20:09:43 -0600 | [diff] [blame] | 108 | stage ('Publish results') { |
| 109 | archiveArtifacts artifacts: "${artifacts_dir}/*" |
| 110 | junit "${artifacts_dir}/*.xml" |
Oleksii Zhurba | d903495 | 2018-09-03 21:06:06 -0500 | [diff] [blame] | 111 | if (env.JOB_NAME.contains("cvp-spt")) { |
| 112 | plot csvFileName: 'plot-8634d2fe-dc48-4713-99f9-b69a381483aa.csv', |
| 113 | group: 'SPT', |
| 114 | style: 'line', |
| 115 | title: 'SPT Glance results', |
| 116 | xmlSeries: [[ |
| 117 | file: "${env.JOB_NAME}_report.xml", |
| 118 | nodeType: 'NODESET', |
| 119 | url: '', |
| 120 | xpath: '/testsuite/testcase[@name="test_speed_glance"]/properties/property']] |
| 121 | plot csvFileName: 'plot-8634d2fe-dc48-4713-99f9-b69a381483bb.csv', |
| 122 | group: 'SPT', |
| 123 | style: 'line', |
| 124 | title: 'SPT HW2HW results', |
| 125 | xmlSeries: [[ |
| 126 | file: "${env.JOB_NAME}_report.xml", |
| 127 | nodeType: 'NODESET', |
| 128 | url: '', |
| 129 | xpath: '/testsuite/testcase[@classname="cvp_spt.tests.test_hw2hw"]/properties/property']] |
| 130 | plot csvFileName: 'plot-8634d2fe-dc48-4713-99f9-b69a381483bc.csv', |
| 131 | group: 'SPT', |
| 132 | style: 'line', |
| 133 | title: 'SPT VM2VM results', |
| 134 | xmlSeries: [[ |
| 135 | file: "${env.JOB_NAME}_report.xml", |
| 136 | nodeType: 'NODESET', |
| 137 | url: '', |
| 138 | xpath: '/testsuite/testcase[@classname="cvp_spt.tests.test_vm2vm"]/properties/property']] |
| 139 | } |
Oleksii Zhurba | 71e9788 | 2018-02-16 20:09:43 -0600 | [diff] [blame] | 140 | } |
| 141 | } catch (Throwable e) { |
| 142 | // If there was an error or exception thrown, the build failed |
| 143 | currentBuild.result = "FAILURE" |
| 144 | throw e |
Oleksii Zhurba | d903495 | 2018-09-03 21:06:06 -0500 | [diff] [blame] | 145 | } finally { |
| 146 | if (DEBUG_MODE == 'false') { |
| 147 | validate.runCleanup(saltMaster, TARGET_NODE, container_name) |
| 148 | salt.cmdRun(saltMaster, TARGET_NODE, "rm -rf ${remote_dir}") |
| 149 | } |
Oleksii Zhurba | 71e9788 | 2018-02-16 20:09:43 -0600 | [diff] [blame] | 150 | } |
| 151 | } |