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 |
Oleksii Zhurba | d903495 | 2018-09-03 21:06:06 -0500 | [diff] [blame^] | 13 | * TEST_IMAGE Docker image link or name to use for running container with test framework. |
| 14 | * DEBUG_MODE If you need to debug (keep container after test), please enabled this |
Oleksii Zhurba | 71e9788 | 2018-02-16 20:09:43 -0600 | [diff] [blame] | 15 | * |
| 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() |
Oleksii Zhurba | 71e9788 | 2018-02-16 20:09:43 -0600 | [diff] [blame] | 21 | def artifacts_dir = 'validation_artifacts/' |
Oleksii Zhurba | d903495 | 2018-09-03 21:06:06 -0500 | [diff] [blame^] | 22 | def remote_dir = '/root/qa_results/' |
| 23 | def container_workdir = '/var/lib' |
| 24 | def TARGET_NODE = "I@gerrit:client" |
| 25 | def reinstall_env = false |
| 26 | def container_name = "${env.JOB_NAME}" |
| 27 | def saltMaster |
| 28 | def settings |
Oleksii Zhurba | 71e9788 | 2018-02-16 20:09:43 -0600 | [diff] [blame] | 29 | |
| 30 | node() { |
| 31 | try{ |
| 32 | stage('Initialization') { |
Oleksii Zhurba | d903495 | 2018-09-03 21:06:06 -0500 | [diff] [blame^] | 33 | sh "rm -rf ${artifacts_dir}" |
| 34 | if ( TESTS_SETTINGS != "" ) { |
| 35 | for (var in TESTS_SETTINGS.tokenize(";")) { |
| 36 | key = var.tokenize("=")[0].trim() |
| 37 | value = var.tokenize("=")[1].trim() |
| 38 | if (key == 'TARGET_NODE') { |
| 39 | TARGET_NODE = value |
| 40 | common.infoMsg("Node for container is set to ${TARGET_NODE}") |
| 41 | } |
| 42 | if (key == 'REINSTALL_ENV') { |
| 43 | reinstall_env = value.toBoolean() |
| 44 | } |
| 45 | } |
| 46 | } |
| 47 | if ( IMAGE == "" ) { |
| 48 | common.infoMsg("Env for tests will be built on Jenkins slave") |
| 49 | TARGET_NODE = "" |
| 50 | validate.prepareVenv(TESTS_REPO, PROXY) |
| 51 | } else { |
| 52 | saltMaster = salt.connection(SALT_MASTER_URL, SALT_MASTER_CREDENTIALS) |
| 53 | salt.cmdRun(saltMaster, TARGET_NODE, "rm -rf ${remote_dir}") |
| 54 | salt.cmdRun(saltMaster, TARGET_NODE, "mkdir -p ${remote_dir}") |
| 55 | validate.runContainer(saltMaster, TARGET_NODE, IMAGE, container_name) |
| 56 | if ( TESTS_REPO != "") { |
| 57 | salt.cmdRun(saltMaster, TARGET_NODE, "docker exec ${container_name} rm -rf ${container_workdir}/cvp*") |
| 58 | salt.cmdRun(saltMaster, TARGET_NODE, "docker exec ${container_name} git clone ${TESTS_REPO} ${container_workdir}/${container_name}") |
| 59 | TESTS_SET = container_workdir + '/' + container_name + '/' + TESTS_SET |
| 60 | if ( reinstall_env ) { |
| 61 | common.infoMsg("Pip packages in container will be reinstalled based on requirements.txt from ${TESTS_REPO}") |
| 62 | salt.cmdRun(saltMaster, TARGET_NODE, "docker exec ${container_name} pip install --force-reinstall -r ${container_workdir}/${container_name}/requirements.txt") |
| 63 | } |
| 64 | } |
| 65 | } |
Oleksii Zhurba | 71e9788 | 2018-02-16 20:09:43 -0600 | [diff] [blame] | 66 | } |
| 67 | |
| 68 | stage('Run Tests') { |
| 69 | sh "mkdir -p ${artifacts_dir}" |
Oleksii Zhurba | d903495 | 2018-09-03 21:06:06 -0500 | [diff] [blame^] | 70 | validate.runPyTests(SALT_MASTER_URL, SALT_MASTER_CREDENTIALS, TESTS_SET, TESTS_SETTINGS.tokenize(";"), container_name, TARGET_NODE, remote_dir, artifacts_dir) |
Oleksii Zhurba | 71e9788 | 2018-02-16 20:09:43 -0600 | [diff] [blame] | 71 | } |
Oleksii Zhurba | d903495 | 2018-09-03 21:06:06 -0500 | [diff] [blame^] | 72 | |
Oleksii Zhurba | 71e9788 | 2018-02-16 20:09:43 -0600 | [diff] [blame] | 73 | stage ('Publish results') { |
| 74 | archiveArtifacts artifacts: "${artifacts_dir}/*" |
| 75 | junit "${artifacts_dir}/*.xml" |
Oleksii Zhurba | d903495 | 2018-09-03 21:06:06 -0500 | [diff] [blame^] | 76 | if (env.JOB_NAME.contains("cvp-spt")) { |
| 77 | plot csvFileName: 'plot-8634d2fe-dc48-4713-99f9-b69a381483aa.csv', |
| 78 | group: 'SPT', |
| 79 | style: 'line', |
| 80 | title: 'SPT Glance results', |
| 81 | xmlSeries: [[ |
| 82 | file: "${env.JOB_NAME}_report.xml", |
| 83 | nodeType: 'NODESET', |
| 84 | url: '', |
| 85 | xpath: '/testsuite/testcase[@name="test_speed_glance"]/properties/property']] |
| 86 | plot csvFileName: 'plot-8634d2fe-dc48-4713-99f9-b69a381483bb.csv', |
| 87 | group: 'SPT', |
| 88 | style: 'line', |
| 89 | title: 'SPT HW2HW results', |
| 90 | xmlSeries: [[ |
| 91 | file: "${env.JOB_NAME}_report.xml", |
| 92 | nodeType: 'NODESET', |
| 93 | url: '', |
| 94 | xpath: '/testsuite/testcase[@classname="cvp_spt.tests.test_hw2hw"]/properties/property']] |
| 95 | plot csvFileName: 'plot-8634d2fe-dc48-4713-99f9-b69a381483bc.csv', |
| 96 | group: 'SPT', |
| 97 | style: 'line', |
| 98 | title: 'SPT VM2VM results', |
| 99 | xmlSeries: [[ |
| 100 | file: "${env.JOB_NAME}_report.xml", |
| 101 | nodeType: 'NODESET', |
| 102 | url: '', |
| 103 | xpath: '/testsuite/testcase[@classname="cvp_spt.tests.test_vm2vm"]/properties/property']] |
| 104 | } |
Oleksii Zhurba | 71e9788 | 2018-02-16 20:09:43 -0600 | [diff] [blame] | 105 | } |
| 106 | } catch (Throwable e) { |
| 107 | // If there was an error or exception thrown, the build failed |
| 108 | currentBuild.result = "FAILURE" |
| 109 | throw e |
Oleksii Zhurba | d903495 | 2018-09-03 21:06:06 -0500 | [diff] [blame^] | 110 | } finally { |
| 111 | if (DEBUG_MODE == 'false') { |
| 112 | validate.runCleanup(saltMaster, TARGET_NODE, container_name) |
| 113 | salt.cmdRun(saltMaster, TARGET_NODE, "rm -rf ${remote_dir}") |
| 114 | } |
Oleksii Zhurba | 71e9788 | 2018-02-16 20:09:43 -0600 | [diff] [blame] | 115 | } |
| 116 | } |