blob: 2858e81aba9fe92da3d1b80e0f9965564909cb28 [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' : [
88 "-v /root/qa_results/:/root/qa_results/",
Hanna Arhipovab451e792018-12-18 15:27:35 +020089 "-v ${env.WORKSPACE}/${artifacts_dir}/:${container_workdir}/${artifacts_dir}/",
90 // 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 +020091 "--entrypoint=''", // to override ENTRYPOINT=/bin/bash in Dockerfile of image
92 ],
93
94 'envOpts' : [
Hanna Arhipova3b4b0652018-12-03 14:06:58 +020095 "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 Zhurba71e97882018-02-16 20:09:43 -0600106 }
Oleksii Zhurbad9034952018-09-03 21:06:06 -0500107
Oleksii Zhurba71e97882018-02-16 20:09:43 -0600108 stage ('Publish results') {
109 archiveArtifacts artifacts: "${artifacts_dir}/*"
110 junit "${artifacts_dir}/*.xml"
Oleksii Zhurbad9034952018-09-03 21:06:06 -0500111 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 Zhurba71e97882018-02-16 20:09:43 -0600140 }
141 } catch (Throwable e) {
142 // If there was an error or exception thrown, the build failed
143 currentBuild.result = "FAILURE"
144 throw e
Oleksii Zhurbad9034952018-09-03 21:06:06 -0500145 } 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 Zhurba71e97882018-02-16 20:09:43 -0600150 }
151}