blob: 22aa9d0c8c005de2d542cc14d03c35d1bee08680 [file] [log] [blame]
Oleksii Zhurba71e97882018-02-16 20:09:43 -06001/**
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 Arhipova3b4b0652018-12-03 14:06:58 +020013 * IMAGE Docker image to use for running container with test framework.
Oleksii Zhurbad9034952018-09-03 21:06:06 -050014 * DEBUG_MODE If you need to debug (keep container after test), please enabled this
Hanna Arhipovab451e792018-12-18 15:27:35 +020015 * To launch tests from docker images need to set IMAGE and left TESTS_REPO empty
Oleksii Zhurba71e97882018-02-16 20:09:43 -060016 */
17
Oleksii Zhurbad9034952018-09-03 21:06:06 -050018common = new com.mirantis.mk.Common()
Oleksii Zhurba71e97882018-02-16 20:09:43 -060019validate = new com.mirantis.mcp.Validate()
Oleksii Zhurbad9034952018-09-03 21:06:06 -050020salt = new com.mirantis.mk.Salt()
Hanna Arhipova3b4b0652018-12-03 14:06:58 +020021salt_testing = new com.mirantis.mk.SaltModelTesting()
Hanna Arhipovab451e792018-12-18 15:27:35 +020022def artifacts_dir = "validation_artifacts"
Hanna Arhipova3b4b0652018-12-03 14:06:58 +020023def remote_dir = '/root/qa_results'
Oleksii Zhurbad9034952018-09-03 21:06:06 -050024def container_workdir = '/var/lib'
Hanna Arhipovab451e792018-12-18 15:27:35 +020025def container_name = "${env.JOB_NAME}"
26def xml_file = "${container_name}_report.xml"
Oleksii Zhurbad9034952018-09-03 21:06:06 -050027def TARGET_NODE = "I@gerrit:client"
28def reinstall_env = false
Hanna Arhipovab451e792018-12-18 15:27:35 +020029
Oleksii Zhurbad9034952018-09-03 21:06:06 -050030def saltMaster
31def settings
Oleksii Zhurba71e97882018-02-16 20:09:43 -060032
Hanna Arhipova3b4b0652018-12-03 14:06:58 +020033slaveNode = (env.getProperty('SLAVE_NODE')) ?: 'docker'
34imageName = (env.getProperty('IMAGE')) ?: 'docker-prod-local.docker.mirantis.net/mirantis/cvp/cvp-spt:stable'
35
36node(slaveNode) {
Oleksii Zhurba71e97882018-02-16 20:09:43 -060037 try{
38 stage('Initialization') {
Oleksii Zhurbad9034952018-09-03 21:06:06 -050039 sh "rm -rf ${artifacts_dir}"
Hanna Arhipova3b4b0652018-12-03 14:06:58 +020040 // TODO collaps TESTS_SETTINGS flow into EXTRA variables map
Oleksii Zhurbad9034952018-09-03 21:06:06 -050041 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 Arhipova3b4b0652018-12-03 14:06:58 +020060 salt.cmdRun(saltMaster, TARGET_NODE, "rm -rf ${remote_dir}/")
61 salt.cmdRun(saltMaster, TARGET_NODE, "mkdir -p ${remote_dir}/")
Oleksii Zhurbad9034952018-09-03 21:06:06 -050062 validate.runContainer(saltMaster, TARGET_NODE, IMAGE, container_name)
63 if ( TESTS_REPO != "") {
Hanna Arhipova3b4b0652018-12-03 14:06:58 +020064 salt.cmdRun(saltMaster, TARGET_NODE, "docker exec ${container_name} rm -rf ${container_workdir}/${container_name}")
Oleksii Zhurbad9034952018-09-03 21:06:06 -050065 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 Zhurba71e97882018-02-16 20:09:43 -060073 }
74
75 stage('Run Tests') {
Hanna Arhipova3b4b0652018-12-03 14:06:58 +020076 def creds = common.getCredentials(SALT_MASTER_CREDENTIALS)
77 def username = creds.username
78 def password = creds.password
Hanna Arhipovab451e792018-12-18 15:27:35 +020079 def script = "pytest --junitxml ${container_workdir}/${artifacts_dir}/${xml_file} --tb=short -sv ${container_workdir}/${TESTS_SET} -vv"
Hanna Arhipova3b4b0652018-12-03 14:06:58 +020080
Oleksii Zhurba71e97882018-02-16 20:09:43 -060081 sh "mkdir -p ${artifacts_dir}"
Hanna Arhipova3b4b0652018-12-03 14:06:58 +020082
83 def configRun = [
84 'image': imageName,
85 'baseRepoPreConfig': false,
86 'dockerMaxCpus': 2,
87 'dockerExtraOpts' : [
Hanna Arhipova17e3e7d2019-02-07 12:17:37 +020088 "--network=host",
Hanna Arhipova3b4b0652018-12-03 14:06:58 +020089 "-v /root/qa_results/:/root/qa_results/",
Hanna Arhipovab451e792018-12-18 15:27:35 +020090 "-v ${env.WORKSPACE}/${artifacts_dir}/:${container_workdir}/${artifacts_dir}/",
91 // TODO remove if all docker images with tests (like cvp-spt) will be transferred into new architucture (like cvp-sanity)
Hanna Arhipova3b4b0652018-12-03 14:06:58 +020092 "--entrypoint=''", // to override ENTRYPOINT=/bin/bash in Dockerfile of image
93 ],
94
95 'envOpts' : [
Hanna Arhipova3b4b0652018-12-03 14:06:58 +020096 "SALT_USERNAME=${username}",
97 "SALT_PASSWORD=${password}",
98 "SALT_URL=${SALT_MASTER_URL}"
99 ] + TESTS_SETTINGS.replaceAll('\\"', '').tokenize(";"),
100 'runCommands' : [
101 '010_start_tests' : {
102 sh("cd ${container_workdir} && ${script}")
103 }
104 ]
105 ]
106 salt_testing.setupDockerAndTest(configRun)
Oleksii Zhurba71e97882018-02-16 20:09:43 -0600107 }
Oleksii Zhurbad9034952018-09-03 21:06:06 -0500108
Oleksii Zhurba71e97882018-02-16 20:09:43 -0600109 stage ('Publish results') {
110 archiveArtifacts artifacts: "${artifacts_dir}/*"
111 junit "${artifacts_dir}/*.xml"
Oleksii Zhurbad9034952018-09-03 21:06:06 -0500112 if (env.JOB_NAME.contains("cvp-spt")) {
113 plot csvFileName: 'plot-8634d2fe-dc48-4713-99f9-b69a381483aa.csv',
114 group: 'SPT',
115 style: 'line',
116 title: 'SPT Glance results',
117 xmlSeries: [[
118 file: "${env.JOB_NAME}_report.xml",
119 nodeType: 'NODESET',
120 url: '',
121 xpath: '/testsuite/testcase[@name="test_speed_glance"]/properties/property']]
122 plot csvFileName: 'plot-8634d2fe-dc48-4713-99f9-b69a381483bb.csv',
123 group: 'SPT',
124 style: 'line',
125 title: 'SPT HW2HW results',
126 xmlSeries: [[
127 file: "${env.JOB_NAME}_report.xml",
128 nodeType: 'NODESET',
129 url: '',
130 xpath: '/testsuite/testcase[@classname="cvp_spt.tests.test_hw2hw"]/properties/property']]
131 plot csvFileName: 'plot-8634d2fe-dc48-4713-99f9-b69a381483bc.csv',
132 group: 'SPT',
133 style: 'line',
134 title: 'SPT VM2VM results',
135 xmlSeries: [[
136 file: "${env.JOB_NAME}_report.xml",
137 nodeType: 'NODESET',
138 url: '',
139 xpath: '/testsuite/testcase[@classname="cvp_spt.tests.test_vm2vm"]/properties/property']]
140 }
Oleksii Zhurba71e97882018-02-16 20:09:43 -0600141 }
142 } catch (Throwable e) {
143 // If there was an error or exception thrown, the build failed
144 currentBuild.result = "FAILURE"
145 throw e
Oleksii Zhurbad9034952018-09-03 21:06:06 -0500146 } finally {
147 if (DEBUG_MODE == 'false') {
148 validate.runCleanup(saltMaster, TARGET_NODE, container_name)
149 salt.cmdRun(saltMaster, TARGET_NODE, "rm -rf ${remote_dir}")
150 }
Oleksii Zhurba71e97882018-02-16 20:09:43 -0600151 }
152}