| /** |
| * |
| * Launch pytest frameworks in Jenkins |
| * |
| * Expected parameters: |
| * SALT_MASTER_URL URL of Salt master |
| * SALT_MASTER_CREDENTIALS Credentials to the Salt API |
| * |
| * TESTS_SET Leave empty for full run or choose a file (test) |
| * TESTS_REPO Repo to clone |
| * TESTS_SETTINGS Additional environment varibales to apply |
| * PROXY Proxy to use for cloning repo or for pip |
| * IMAGE Docker image to use for running container with test framework. |
| * DEBUG_MODE If you need to debug (keep container after test), please enabled this |
| * To launch tests from cvp_spt docker images need to set IMAGE and left TESTS_REPO empty |
| */ |
| |
| common = new com.mirantis.mk.Common() |
| validate = new com.mirantis.mcp.Validate() |
| salt = new com.mirantis.mk.Salt() |
| salt_testing = new com.mirantis.mk.SaltModelTesting() |
| def artifacts_dir = "validation_artifacts/" |
| def remote_dir = '/root/qa_results' |
| def container_workdir = '/var/lib' |
| def name = 'cvp-spt' |
| def xml_file = "${name}_report.xml" |
| def TARGET_NODE = "I@gerrit:client" |
| def reinstall_env = false |
| def container_name = "${env.JOB_NAME}" |
| def saltMaster |
| def settings |
| |
| slaveNode = (env.getProperty('SLAVE_NODE')) ?: 'docker' |
| imageName = (env.getProperty('IMAGE')) ?: 'docker-prod-local.docker.mirantis.net/mirantis/cvp/cvp-spt:stable' |
| |
| node(slaveNode) { |
| try{ |
| stage('Initialization') { |
| sh "rm -rf ${artifacts_dir}" |
| // TODO collaps TESTS_SETTINGS flow into EXTRA variables map |
| if ( TESTS_SETTINGS != "" ) { |
| for (var in TESTS_SETTINGS.tokenize(";")) { |
| key = var.tokenize("=")[0].trim() |
| value = var.tokenize("=")[1].trim() |
| if (key == 'TARGET_NODE') { |
| TARGET_NODE = value |
| common.infoMsg("Node for container is set to ${TARGET_NODE}") |
| } |
| if (key == 'REINSTALL_ENV') { |
| reinstall_env = value.toBoolean() |
| } |
| } |
| } |
| if ( IMAGE == "" ) { |
| common.infoMsg("Env for tests will be built on Jenkins slave") |
| TARGET_NODE = "" |
| validate.prepareVenv(TESTS_REPO, PROXY) |
| } else { |
| saltMaster = salt.connection(SALT_MASTER_URL, SALT_MASTER_CREDENTIALS) |
| salt.cmdRun(saltMaster, TARGET_NODE, "rm -rf ${remote_dir}/") |
| salt.cmdRun(saltMaster, TARGET_NODE, "mkdir -p ${remote_dir}/") |
| validate.runContainer(saltMaster, TARGET_NODE, IMAGE, container_name) |
| if ( TESTS_REPO != "") { |
| salt.cmdRun(saltMaster, TARGET_NODE, "docker exec ${container_name} rm -rf ${container_workdir}/${container_name}") |
| salt.cmdRun(saltMaster, TARGET_NODE, "docker exec ${container_name} git clone ${TESTS_REPO} ${container_workdir}/${container_name}") |
| TESTS_SET = container_workdir + '/' + container_name + '/' + TESTS_SET |
| if ( reinstall_env ) { |
| common.infoMsg("Pip packages in container will be reinstalled based on requirements.txt from ${TESTS_REPO}") |
| salt.cmdRun(saltMaster, TARGET_NODE, "docker exec ${container_name} pip install --force-reinstall -r ${container_workdir}/${container_name}/requirements.txt") |
| } |
| } |
| } |
| } |
| |
| stage('Run Tests') { |
| def creds = common.getCredentials(SALT_MASTER_CREDENTIALS) |
| def username = creds.username |
| def password = creds.password |
| def script = "pytest --junitxml ${container_workdir}/${artifacts_dir}/${xml_file} --tb=short -sv ${container_workdir}/${TESTS_SET}" |
| |
| sh "mkdir -p ${artifacts_dir}" |
| |
| def configRun = [ |
| 'image': imageName, |
| 'baseRepoPreConfig': false, |
| 'dockerMaxCpus': 2, |
| 'dockerExtraOpts' : [ |
| "-v /root/qa_results/:/root/qa_results/", |
| "-v ${env.WORKSPACE}/validation_artifacts/:${container_workdir}/validation_artifacts/", |
| "--entrypoint=''", // to override ENTRYPOINT=/bin/bash in Dockerfile of image |
| ], |
| |
| 'envOpts' : [ |
| "WORKSPACE=${container_workdir}/${name}", |
| "SALT_USERNAME=${username}", |
| "SALT_PASSWORD=${password}", |
| "SALT_URL=${SALT_MASTER_URL}" |
| ] + TESTS_SETTINGS.replaceAll('\\"', '').tokenize(";"), |
| 'runCommands' : [ |
| '010_start_tests' : { |
| sh("cd ${container_workdir} && ${script}") |
| } |
| ] |
| ] |
| 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-8634d2fe-dc48-4713-99f9-b69a381483aa.csv', |
| group: 'SPT', |
| style: 'line', |
| title: 'SPT Glance results', |
| xmlSeries: [[ |
| file: "${env.JOB_NAME}_report.xml", |
| nodeType: 'NODESET', |
| url: '', |
| xpath: '/testsuite/testcase[@name="test_speed_glance"]/properties/property']] |
| plot csvFileName: 'plot-8634d2fe-dc48-4713-99f9-b69a381483bb.csv', |
| group: 'SPT', |
| style: 'line', |
| title: 'SPT HW2HW results', |
| xmlSeries: [[ |
| file: "${env.JOB_NAME}_report.xml", |
| nodeType: 'NODESET', |
| url: '', |
| xpath: '/testsuite/testcase[@classname="cvp_spt.tests.test_hw2hw"]/properties/property']] |
| plot csvFileName: 'plot-8634d2fe-dc48-4713-99f9-b69a381483bc.csv', |
| group: 'SPT', |
| style: 'line', |
| title: 'SPT VM2VM results', |
| xmlSeries: [[ |
| file: "${env.JOB_NAME}_report.xml", |
| nodeType: 'NODESET', |
| url: '', |
| xpath: '/testsuite/testcase[@classname="cvp_spt.tests.test_vm2vm"]/properties/property']] |
| } |
| } |
| } catch (Throwable e) { |
| // If there was an error or exception thrown, the build failed |
| currentBuild.result = "FAILURE" |
| throw e |
| } finally { |
| if (DEBUG_MODE == 'false') { |
| validate.runCleanup(saltMaster, TARGET_NODE, container_name) |
| salt.cmdRun(saltMaster, TARGET_NODE, "rm -rf ${remote_dir}") |
| } |
| } |
| } |