blob: b4c3451005b318b0b128bb05aa0c998f56a458ae [file] [log] [blame]
Jakub Josef83379312017-03-29 18:12:34 +02001/**
2 * Test salt formulas pipeline
3 * DEFAULT_GIT_REF
4 * DEFAULT_GIT_URL
5 * CREDENTIALS_ID
Cedric Hnydac87fc6c2017-09-22 13:12:36 +00006 * KITCHEN_TESTS_PARALLEL
Sergey Otpuschennikove5a14742018-05-08 11:42:26 +04007 * SMOKE_TEST_DOCKER_IMG Docker image for run test (default "ubuntu:16.04")
Jakub Josef83379312017-03-29 18:12:34 +02008 */
chnyda85ea22d2017-10-23 13:05:16 +02009common = new com.mirantis.mk.Common()
Jakub Josefc5a223a2017-03-01 14:40:08 +010010def gerrit = new com.mirantis.mk.Gerrit()
Jakub Joseff64c6392017-05-03 13:18:25 +020011def ruby = new com.mirantis.mk.Ruby()
Jakub Josef83379312017-03-29 18:12:34 +020012
azvyagintsevc2628ed2018-08-16 11:24:10 +030013def gerritRef = env.GERRIT_REFSPEC ?: null
14def defaultGitRef = env.DEFAULT_GIT_REF ?: null
15def defaultGitUrl = env.DEFAULT_GIT_URL ?: null
azvyagintsevaa7b43a2019-02-02 10:17:58 +020016// def slaveNode = env.SLAVE_NODE ?: 'python&&docker'
17def slaveNode = env.SLAVE_NODE ?: 'hardware'
azvyagintsevc2628ed2018-08-16 11:24:10 +030018def saltVersion = env.SALT_VERSION ?: ""
19def dockerLib = new com.mirantis.mk.Docker()
Jakub Josef83379312017-03-29 18:12:34 +020020
Jakub Josefbcd2e902017-06-13 14:40:41 +020021def checkouted = false
Jakub Josef83379312017-03-29 18:12:34 +020022
chnyda85ea22d2017-10-23 13:05:16 +020023futureFormulas = []
24failedFormulas = []
25
26def setupRunner(defaultGitRef, defaultGitUrl) {
27 def branches = [:]
28 for (int i = 0; i < PARALLEL_GROUP_SIZE.toInteger() && i < futureFormulas.size(); i++) {
29 branches["Runner ${i}"] = {
30 while (futureFormulas && !failedFormulas) {
31 def currentFormula = futureFormulas[0] ? futureFormulas[0] : null
32 if (!currentFormula) {
33 continue
34 }
35 futureFormulas.remove(currentFormula)
36 try {
37 triggerTestFormulaJob(currentFormula, defaultGitRef, defaultGitUrl)
38 } catch (Exception e) {
chnyda0513b572018-01-23 13:40:57 +010039 if (e.getMessage().contains("completed with status ABORTED")) {
40 common.warningMsg("Test of ${currentFormula} was aborted and will be retriggered")
41 futureFormulas << currentFormula
42 } else {
43 failedFormulas << currentFormula
44 common.warningMsg("Test of ${currentFormula} failed : ${e}")
45 }
chnyda85ea22d2017-10-23 13:05:16 +020046 }
47 }
48 }
49 }
50 parallel branches
51}
52
53def triggerTestFormulaJob(testEnv, defaultGitRef, defaultGitUrl) {
54 common.infoMsg("Test of ${testEnv} starts")
55 build job: "test-salt-formulas-env", parameters: [
56 [$class: 'StringParameterValue', name: 'CREDENTIALS_ID', value: CREDENTIALS_ID],
57 [$class: 'StringParameterValue', name: 'KITCHEN_ENV', value: testEnv],
58 [$class: 'StringParameterValue', name: 'DEFAULT_GIT_REF', value: defaultGitRef],
59 [$class: 'StringParameterValue', name: 'DEFAULT_GIT_URL', value: defaultGitUrl],
60 [$class: 'StringParameterValue', name: 'SALT_OPTS', value: SALT_OPTS],
61 [$class: 'StringParameterValue', name: 'SALT_VERSION', value: SALT_VERSION]
62 ]
63}
Martin Polreichc0995dc2018-11-05 14:40:02 +010064
Martin Polreich15ef9f42018-12-17 14:29:54 +010065timeout(time: 4, unit: 'HOURS') {
azvyagintsevc2628ed2018-08-16 11:24:10 +030066 node(slaveNode) {
azvyagintsev54b49c62018-12-27 17:38:56 +020067 def img = dockerLib.getImage(env.SMOKE_TEST_DOCKER_IMG, "ubuntu:16.04")
Jakub Josefa63f9862018-01-11 17:58:38 +010068 try {
azvyagintsevc2628ed2018-08-16 11:24:10 +030069 if (fileExists("tests/build")) {
70 common.infoMsg('Cleaning test env')
71 sh ("sudo rm -rf tests/build")
72 }
Jakub Josefa63f9862018-01-11 17:58:38 +010073 stage("checkout") {
74 if (gerritRef) {
75 // job is triggered by Gerrit
76 def gerritChange = gerrit.getGerritChange(GERRIT_NAME, GERRIT_HOST, GERRIT_CHANGE_NUMBER, CREDENTIALS_ID, true)
77 // test if gerrit change is already Verified
78 if (gerrit.patchsetHasApproval(gerritChange.currentPatchSet, "Verified", "+")) {
79 common.successMsg("Gerrit change ${GERRIT_CHANGE_NUMBER} patchset ${GERRIT_PATCHSET_NUMBER} already has Verified, skipping tests") // do nothing
80 // test WIP contains in commit message
81 } else if (gerritChange.commitMessage.contains("WIP")) {
82 common.successMsg("Commit message contains WIP, skipping tests") // do nothing
Martin Polreicha3e30122017-08-22 12:43:55 +020083 } else {
Jakub Josefa63f9862018-01-11 17:58:38 +010084 // test if change aren't already merged
85 def merged = gerritChange.status == "MERGED"
86 if (!merged) {
87 checkouted = gerrit.gerritPatchsetCheckout([
88 credentialsId: CREDENTIALS_ID
89 ])
90 } else {
91 common.successMsg("Change ${GERRIT_CHANGE_NUMBER} is already merged, no need to test them")
92 }
Jakub Josefbcd2e902017-06-13 14:40:41 +020093 }
Jakub Josefa63f9862018-01-11 17:58:38 +010094 defaultGitUrl = "${GERRIT_SCHEME}://${GERRIT_NAME}@${GERRIT_HOST}:${GERRIT_PORT}/${GERRIT_PROJECT}"
95 defaultGitRef = GERRIT_REFSPEC
96 } else if (defaultGitRef && defaultGitUrl) {
97 checkouted = gerrit.gerritPatchsetCheckout(defaultGitUrl, defaultGitRef, "HEAD", CREDENTIALS_ID)
98 } else {
99 throw new Exception("Cannot checkout gerrit patchset, GERRIT_REFSPEC and DEFAULT_GIT_REF is null")
Jakub Josef740a7882017-06-01 14:54:01 +0200100 }
Jakub Josefc5a223a2017-03-01 14:40:08 +0100101 }
azvyagintsevc2628ed2018-08-16 11:24:10 +0300102 stage("test") {
103 if (checkouted) {
104 try {
105 // TODO add try\finally for image-stuck case. (copy-paste from SaltModelTesting)
106 withEnv(["SALT_VERSION=${saltVersion}"]) {
107 img.inside("-v ${env.WORKSPACE}/:/formula/ -u root:root --cpus=4 --ulimit nofile=4096:8192") {
108 sh('''#!/bin/bash -xe
109 cd /etc/apt/
azvyagintsev1a494b52018-12-20 14:48:46 +0200110 echo "deb [arch=amd64] http://mirror.mirantis.com/nightly/ubuntu xenial main restricted universe" > sources.list
111 echo "deb [arch=amd64] http://mirror.mirantis.com/nightly/ubuntu xenial-updates main restricted universe" >> sources.list
azvyagintsevc2628ed2018-08-16 11:24:10 +0300112 echo 'Acquire::Languages "none";' > apt.conf.d/docker-no-languages
113 echo 'Acquire::GzipIndexes "true"; Acquire::CompressionTypes::Order:: "gz";' > apt.conf.d/docker-gzip-indexes
114 echo 'APT::Get::Install-Recommends "false"; APT::Get::Install-Suggests "false";' > apt.conf.d/docker-recommends
115 apt-get update
116 apt-get install -y git-core wget curl apt-transport-https
117 apt-get install -y python-pip python3-pip python-virtualenv python3-virtualenv python-yaml autoconf build-essential
118 cd /formula/
119 make clean
120 make test
121 make clean
122 ''')
123 }
Sergey Otpuschennikove5a14742018-05-08 11:42:26 +0400124 }
azvyagintsevc2628ed2018-08-16 11:24:10 +0300125 }
126 finally {
127 if (fileExists("tests/build")) {
128 common.infoMsg('Cleaning test env')
129 sh ("sudo rm -rf tests/build")
130 }
Sergey Otpuschennikove5a14742018-05-08 11:42:26 +0400131 }
azvyagintsev3a2e0352018-01-10 12:41:29 +0200132 }
azvyagintsevc2628ed2018-08-16 11:24:10 +0300133
Jakub Josefc5a223a2017-03-01 14:40:08 +0100134 }
Jakub Joseff64c6392017-05-03 13:18:25 +0200135 stage("kitchen") {
Martin Polreich32b82222018-11-21 14:01:39 +0100136 if (fileExists(".travis.yml")) {/** TODO: Remove this legacy block once formulas are switched to new configuration */
Jakub Josefa63f9862018-01-11 17:58:38 +0100137 if (checkouted) {
Martin Polreichc0995dc2018-11-05 14:40:02 +0100138 if (fileExists(".kitchen.yml") || fileExists(".kitchen.openstack.yml")) {
139 if (fileExists(".kitchen.openstack.yml")) {
140 common.infoMsg("Openstack Kitchen test configuration found, running Openstack kitchen tests.")
141 if (fileExists(".kitchen.yml")) {
142 common.infoMsg("Ignoring the docker Kitchen test configuration file.")
143 }
144 } else {
145 common.infoMsg("Docker Kitchen test configuration found, running Docker kitchen tests.")
146 }
Jakub Josefa63f9862018-01-11 17:58:38 +0100147 def kitchenEnvs = []
148 def filteredEnvs = []
149 if (fileExists(".travis.yml")) {
150 common.infoMsg(".travis.yml file found.")
151 def kitchenConfigYML = readYaml(file: ".travis.yml")
152 if (kitchenConfigYML.containsKey("env")) {
153 kitchenEnvs = kitchenConfigYML["env"]
154 }
155 } else {
156 common.warningMsg(".travis.yml file not found, suites must be passed via CUSTOM_KITCHEN_ENVS parameter.")
Ruslan Kamaldinov310ffc02017-08-08 16:07:42 +0400157 }
Jakub Josefa63f9862018-01-11 17:58:38 +0100158 common.infoMsg("Running kitchen testing in parallel mode")
159 if (CUSTOM_KITCHEN_ENVS != null && CUSTOM_KITCHEN_ENVS != '') {
160 kitchenEnvs = CUSTOM_KITCHEN_ENVS.tokenize('\n')
161 common.infoMsg("CUSTOM_KITCHEN_ENVS not empty. Running with custom enviroments: ${kitchenEnvs}")
Martin Polreicha3e30122017-08-22 12:43:55 +0200162 }
Jakub Josefa63f9862018-01-11 17:58:38 +0100163 if (kitchenEnvs != null && kitchenEnvs != '') {
164 def acc = 0
165 common.infoMsg("Found " + kitchenEnvs.size() + " environment(s)")
166 for (int i = 0; i < kitchenEnvs.size(); i++) {
167 futureFormulas << kitchenEnvs[i]
168 }
169 setupRunner(defaultGitRef, defaultGitUrl)
170 } else {
171 common.warningMsg(".kitchen.yml file not found, no kitchen tests triggered.")
172 }
Martin Polreicha3e30122017-08-22 12:43:55 +0200173 }
Martin Polreich32b82222018-11-21 14:01:39 +0100174 }/** TODO: End of block for removal */
175 } else {
176 if (checkouted) {
177 if (fileExists(".kitchen.yml") || fileExists(".kitchen.openstack.yml")) {
178 if (fileExists(".kitchen.openstack.yml")) {
179 common.infoMsg("Openstack Kitchen test configuration found, running Openstack kitchen tests.")
180 if (fileExists(".kitchen.yml")) {
181 common.infoMsg("Ignoring the docker Kitchen test configuration file.")
182 }
183 } else {
184 common.infoMsg("Docker Kitchen test configuration found, running Docker kitchen tests.")
185 }
186 def kitchenEnvs = []
187 ruby.ensureRubyEnv()
188 if (!fileExists("Gemfile")) {
189 sh("curl -s -o ./Gemfile 'https://gerrit.mcp.mirantis.com/gitweb?p=salt-formulas/salt-formulas-scripts.git;a=blob_plain;f=Gemfile;hb=refs/heads/master'")
190 ruby.installKitchen()
191 } else {
192 common.infoMsg("Override Gemfile found in the kitchen directory, using it.")
193 ruby.installKitchen()
194 }
195 common.infoMsg = ruby.runKitchenCommand("list -b")
196 kitchenEnvs = ruby.runKitchenCommand("list -b").split()
197 common.infoMsg(kitchenEnvs)
198 common.infoMsg("Running kitchen testing in parallel mode")
199 if (CUSTOM_KITCHEN_ENVS != null && CUSTOM_KITCHEN_ENVS != '') {
200 kitchenEnvs = CUSTOM_KITCHEN_ENVS.tokenize('\n')
201 common.infoMsg("CUSTOM_KITCHEN_ENVS not empty. Running with custom enviroments: ${kitchenEnvs}")
202 }
203 if (kitchenEnvs != null && kitchenEnvs != '') {
204 def acc = 0
205 common.infoMsg("Found " + kitchenEnvs.size() + " environment(s)")
206 for (int i = 0; i < kitchenEnvs.size(); i++) {
207 futureFormulas << kitchenEnvs[i]
208 }
209 setupRunner(defaultGitRef, defaultGitUrl)
210 } else {
211 common.warningMsg(".kitchen.yml nor .kitchen.openstack.yml file not found, no kitchen tests triggered.")
212 }
213 }
214 }
Jakub Joseff64c6392017-05-03 13:18:25 +0200215 }
Jakub Joseff64c6392017-05-03 13:18:25 +0200216 }
Jakub Josefa63f9862018-01-11 17:58:38 +0100217 if (failedFormulas) {
218 currentBuild.result = "FAILURE"
219 common.warningMsg("The following tests failed: ${failedFormulas}")
220 }
221 } catch (Throwable e) {
222 // If there was an error or exception thrown, the build failed
chnyda85ea22d2017-10-23 13:05:16 +0200223 currentBuild.result = "FAILURE"
Jakub Josefa63f9862018-01-11 17:58:38 +0100224 currentBuild.description = currentBuild.description ? e.message + " " + currentBuild.description : e.message
225 throw e
226 } finally {
227 if (currentBuild.result == "FAILURE" && fileExists(".kitchen/logs/kitchen.log")) {
228 common.errorMsg("----------------KITCHEN LOG:---------------")
229 println readFile(".kitchen/logs/kitchen.log")
230 }
chnyda85ea22d2017-10-23 13:05:16 +0200231 }
Jakub Josefc5a223a2017-03-01 14:40:08 +0100232 }
azvyagintsev3a2e0352018-01-10 12:41:29 +0200233}