blob: f21ab298bd41103f4a8aa074851f8ba902fe5ff6 [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()
Martin Polreich4e682fc2019-04-09 16:01:38 +020012def dockerLib = new com.mirantis.mk.Docker()
Jakub Josef83379312017-03-29 18:12:34 +020013
azvyagintsevc2628ed2018-08-16 11:24:10 +030014def gerritRef = env.GERRIT_REFSPEC ?: null
15def defaultGitRef = env.DEFAULT_GIT_REF ?: null
16def defaultGitUrl = env.DEFAULT_GIT_URL ?: null
Aleksey Zvyagintsev4a804212019-02-07 13:02:31 +000017def slaveNode = env.SLAVE_NODE ?: 'virtual'
azvyagintsevc2628ed2018-08-16 11:24:10 +030018def saltVersion = env.SALT_VERSION ?: ""
Martin Polreich4e682fc2019-04-09 16:01:38 +020019
20gerritBranch = 'master'
21if (common.validInputParam('GERRIT_BRANCH')) {
22 gerritBranch = env.GERRIT_BRANCH
23}
24
Jakub Josef83379312017-03-29 18:12:34 +020025
Jakub Josefbcd2e902017-06-13 14:40:41 +020026def checkouted = false
Jakub Josef83379312017-03-29 18:12:34 +020027
Martin Polreich3ce6d3e2019-02-19 15:18:36 +010028envOverrides = []
chnyda85ea22d2017-10-23 13:05:16 +020029futureFormulas = []
30failedFormulas = []
Martin Polreich5be29e32019-02-26 11:11:51 +010031kitchenFileName = ''
chnyda85ea22d2017-10-23 13:05:16 +020032
33def setupRunner(defaultGitRef, defaultGitUrl) {
34 def branches = [:]
35 for (int i = 0; i < PARALLEL_GROUP_SIZE.toInteger() && i < futureFormulas.size(); i++) {
36 branches["Runner ${i}"] = {
37 while (futureFormulas && !failedFormulas) {
38 def currentFormula = futureFormulas[0] ? futureFormulas[0] : null
39 if (!currentFormula) {
40 continue
41 }
42 futureFormulas.remove(currentFormula)
43 try {
44 triggerTestFormulaJob(currentFormula, defaultGitRef, defaultGitUrl)
45 } catch (Exception e) {
chnyda0513b572018-01-23 13:40:57 +010046 if (e.getMessage().contains("completed with status ABORTED")) {
47 common.warningMsg("Test of ${currentFormula} was aborted and will be retriggered")
48 futureFormulas << currentFormula
49 } else {
50 failedFormulas << currentFormula
51 common.warningMsg("Test of ${currentFormula} failed : ${e}")
52 }
chnyda85ea22d2017-10-23 13:05:16 +020053 }
54 }
55 }
56 }
57 parallel branches
58}
59
60def triggerTestFormulaJob(testEnv, defaultGitRef, defaultGitUrl) {
61 common.infoMsg("Test of ${testEnv} starts")
62 build job: "test-salt-formulas-env", parameters: [
63 [$class: 'StringParameterValue', name: 'CREDENTIALS_ID', value: CREDENTIALS_ID],
64 [$class: 'StringParameterValue', name: 'KITCHEN_ENV', value: testEnv],
65 [$class: 'StringParameterValue', name: 'DEFAULT_GIT_REF', value: defaultGitRef],
66 [$class: 'StringParameterValue', name: 'DEFAULT_GIT_URL', value: defaultGitUrl],
67 [$class: 'StringParameterValue', name: 'SALT_OPTS', value: SALT_OPTS],
Martin Polreichb3708cf2019-04-08 15:39:21 +020068 [$class: 'StringParameterValue', name: 'SALT_VERSION', value: SALT_VERSION],
Martin Polreich4dedbcf2019-04-09 09:28:46 +020069 [$class: 'StringParameterValue', name: 'GERRIT_PARENT_BRANCH', value: gerritBranch]
chnyda85ea22d2017-10-23 13:05:16 +020070 ]
71}
Martin Polreichc0995dc2018-11-05 14:40:02 +010072
Martin Polreich15ef9f42018-12-17 14:29:54 +010073timeout(time: 4, unit: 'HOURS') {
azvyagintsevc2628ed2018-08-16 11:24:10 +030074 node(slaveNode) {
azvyagintsev54b49c62018-12-27 17:38:56 +020075 def img = dockerLib.getImage(env.SMOKE_TEST_DOCKER_IMG, "ubuntu:16.04")
Jakub Josefa63f9862018-01-11 17:58:38 +010076 try {
azvyagintsevc2628ed2018-08-16 11:24:10 +030077 if (fileExists("tests/build")) {
78 common.infoMsg('Cleaning test env')
79 sh ("sudo rm -rf tests/build")
80 }
Jakub Josefa63f9862018-01-11 17:58:38 +010081 stage("checkout") {
82 if (gerritRef) {
83 // job is triggered by Gerrit
84 def gerritChange = gerrit.getGerritChange(GERRIT_NAME, GERRIT_HOST, GERRIT_CHANGE_NUMBER, CREDENTIALS_ID, true)
85 // test if gerrit change is already Verified
86 if (gerrit.patchsetHasApproval(gerritChange.currentPatchSet, "Verified", "+")) {
87 common.successMsg("Gerrit change ${GERRIT_CHANGE_NUMBER} patchset ${GERRIT_PATCHSET_NUMBER} already has Verified, skipping tests") // do nothing
88 // test WIP contains in commit message
89 } else if (gerritChange.commitMessage.contains("WIP")) {
90 common.successMsg("Commit message contains WIP, skipping tests") // do nothing
Martin Polreicha3e30122017-08-22 12:43:55 +020091 } else {
Jakub Josefa63f9862018-01-11 17:58:38 +010092 // test if change aren't already merged
93 def merged = gerritChange.status == "MERGED"
94 if (!merged) {
95 checkouted = gerrit.gerritPatchsetCheckout([
96 credentialsId: CREDENTIALS_ID
97 ])
98 } else {
99 common.successMsg("Change ${GERRIT_CHANGE_NUMBER} is already merged, no need to test them")
100 }
Jakub Josefbcd2e902017-06-13 14:40:41 +0200101 }
Jakub Josefa63f9862018-01-11 17:58:38 +0100102 defaultGitUrl = "${GERRIT_SCHEME}://${GERRIT_NAME}@${GERRIT_HOST}:${GERRIT_PORT}/${GERRIT_PROJECT}"
103 defaultGitRef = GERRIT_REFSPEC
104 } else if (defaultGitRef && defaultGitUrl) {
105 checkouted = gerrit.gerritPatchsetCheckout(defaultGitUrl, defaultGitRef, "HEAD", CREDENTIALS_ID)
106 } else {
107 throw new Exception("Cannot checkout gerrit patchset, GERRIT_REFSPEC and DEFAULT_GIT_REF is null")
Jakub Josef740a7882017-06-01 14:54:01 +0200108 }
Jakub Josefc5a223a2017-03-01 14:40:08 +0100109 }
azvyagintsevc2628ed2018-08-16 11:24:10 +0300110 stage("test") {
111 if (checkouted) {
112 try {
113 // TODO add try\finally for image-stuck case. (copy-paste from SaltModelTesting)
114 withEnv(["SALT_VERSION=${saltVersion}"]) {
115 img.inside("-v ${env.WORKSPACE}/:/formula/ -u root:root --cpus=4 --ulimit nofile=4096:8192") {
116 sh('''#!/bin/bash -xe
117 cd /etc/apt/
azvyagintsev1a494b52018-12-20 14:48:46 +0200118 echo "deb [arch=amd64] http://mirror.mirantis.com/nightly/ubuntu xenial main restricted universe" > sources.list
119 echo "deb [arch=amd64] http://mirror.mirantis.com/nightly/ubuntu xenial-updates main restricted universe" >> sources.list
azvyagintsevc2628ed2018-08-16 11:24:10 +0300120 echo 'Acquire::Languages "none";' > apt.conf.d/docker-no-languages
121 echo 'Acquire::GzipIndexes "true"; Acquire::CompressionTypes::Order:: "gz";' > apt.conf.d/docker-gzip-indexes
122 echo 'APT::Get::Install-Recommends "false"; APT::Get::Install-Suggests "false";' > apt.conf.d/docker-recommends
123 apt-get update
124 apt-get install -y git-core wget curl apt-transport-https
125 apt-get install -y python-pip python3-pip python-virtualenv python3-virtualenv python-yaml autoconf build-essential
126 cd /formula/
127 make clean
128 make test
129 make clean
130 ''')
131 }
Sergey Otpuschennikove5a14742018-05-08 11:42:26 +0400132 }
azvyagintsevc2628ed2018-08-16 11:24:10 +0300133 }
134 finally {
135 if (fileExists("tests/build")) {
136 common.infoMsg('Cleaning test env')
137 sh ("sudo rm -rf tests/build")
138 }
Sergey Otpuschennikove5a14742018-05-08 11:42:26 +0400139 }
azvyagintsev3a2e0352018-01-10 12:41:29 +0200140 }
azvyagintsevc2628ed2018-08-16 11:24:10 +0300141
Jakub Josefc5a223a2017-03-01 14:40:08 +0100142 }
Jakub Joseff64c6392017-05-03 13:18:25 +0200143 stage("kitchen") {
Martin Polreich32b82222018-11-21 14:01:39 +0100144 if (fileExists(".travis.yml")) {/** TODO: Remove this legacy block once formulas are switched to new configuration */
Jakub Josefa63f9862018-01-11 17:58:38 +0100145 if (checkouted) {
Martin Polreichc0995dc2018-11-05 14:40:02 +0100146 if (fileExists(".kitchen.yml") || fileExists(".kitchen.openstack.yml")) {
147 if (fileExists(".kitchen.openstack.yml")) {
148 common.infoMsg("Openstack Kitchen test configuration found, running Openstack kitchen tests.")
149 if (fileExists(".kitchen.yml")) {
150 common.infoMsg("Ignoring the docker Kitchen test configuration file.")
151 }
152 } else {
153 common.infoMsg("Docker Kitchen test configuration found, running Docker kitchen tests.")
154 }
Jakub Josefa63f9862018-01-11 17:58:38 +0100155 def kitchenEnvs = []
156 def filteredEnvs = []
157 if (fileExists(".travis.yml")) {
158 common.infoMsg(".travis.yml file found.")
159 def kitchenConfigYML = readYaml(file: ".travis.yml")
160 if (kitchenConfigYML.containsKey("env")) {
161 kitchenEnvs = kitchenConfigYML["env"]
162 }
163 } else {
164 common.warningMsg(".travis.yml file not found, suites must be passed via CUSTOM_KITCHEN_ENVS parameter.")
Ruslan Kamaldinov310ffc02017-08-08 16:07:42 +0400165 }
Jakub Josefa63f9862018-01-11 17:58:38 +0100166 common.infoMsg("Running kitchen testing in parallel mode")
167 if (CUSTOM_KITCHEN_ENVS != null && CUSTOM_KITCHEN_ENVS != '') {
168 kitchenEnvs = CUSTOM_KITCHEN_ENVS.tokenize('\n')
169 common.infoMsg("CUSTOM_KITCHEN_ENVS not empty. Running with custom enviroments: ${kitchenEnvs}")
Martin Polreicha3e30122017-08-22 12:43:55 +0200170 }
Jakub Josefa63f9862018-01-11 17:58:38 +0100171 if (kitchenEnvs != null && kitchenEnvs != '') {
172 def acc = 0
173 common.infoMsg("Found " + kitchenEnvs.size() + " environment(s)")
174 for (int i = 0; i < kitchenEnvs.size(); i++) {
175 futureFormulas << kitchenEnvs[i]
176 }
177 setupRunner(defaultGitRef, defaultGitUrl)
178 } else {
179 common.warningMsg(".kitchen.yml file not found, no kitchen tests triggered.")
180 }
Martin Polreicha3e30122017-08-22 12:43:55 +0200181 }
Martin Polreich32b82222018-11-21 14:01:39 +0100182 }/** TODO: End of block for removal */
183 } else {
184 if (checkouted) {
Martin Polreich5be29e32019-02-26 11:11:51 +0100185 if (fileExists(".kitchen.openstack.yml")) {
186 common.infoMsg("Openstack Kitchen test configuration found, running Openstack kitchen tests.")
187 kitchenFileName = ".kitchen.openstack.yml"
188 envOverrides.add("KITCHEN_YAML=${kitchenFileName}")
Martin Polreich73c8e512019-02-26 15:16:33 +0100189 rubyVersion = '2.5.0'
Martin Polreich5be29e32019-02-26 11:11:51 +0100190 } else if (fileExists(".kitchen.yml")) {
191 common.infoMsg("Docker Kitchen test configuration found, running Docker kitchen tests.")
192 kitchenFileName = ".kitchen.yml"
Martin Polreich73c8e512019-02-26 15:16:33 +0100193 rubyVersion = '2.4.1'
Martin Polreich5be29e32019-02-26 11:11:51 +0100194 }
195 if (kitchenFileName) {
Martin Polreich32b82222018-11-21 14:01:39 +0100196 def kitchenEnvs = []
Martin Polreich5be29e32019-02-26 11:11:51 +0100197 ruby.ensureRubyEnv(rubyVersion)
Martin Polreich32b82222018-11-21 14:01:39 +0100198 if (!fileExists("Gemfile")) {
199 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'")
200 ruby.installKitchen()
201 } else {
202 common.infoMsg("Override Gemfile found in the kitchen directory, using it.")
203 ruby.installKitchen()
204 }
Martin Polreich3ce6d3e2019-02-19 15:18:36 +0100205 common.infoMsg = ruby.runKitchenCommand("list -b", envOverrides.join(' '))
206 kitchenEnvs = ruby.runKitchenCommand("list -b", envOverrides.join(' ')).split()
Martin Polreich32b82222018-11-21 14:01:39 +0100207 common.infoMsg(kitchenEnvs)
208 common.infoMsg("Running kitchen testing in parallel mode")
209 if (CUSTOM_KITCHEN_ENVS != null && CUSTOM_KITCHEN_ENVS != '') {
210 kitchenEnvs = CUSTOM_KITCHEN_ENVS.tokenize('\n')
211 common.infoMsg("CUSTOM_KITCHEN_ENVS not empty. Running with custom enviroments: ${kitchenEnvs}")
212 }
213 if (kitchenEnvs != null && kitchenEnvs != '') {
214 def acc = 0
215 common.infoMsg("Found " + kitchenEnvs.size() + " environment(s)")
216 for (int i = 0; i < kitchenEnvs.size(); i++) {
217 futureFormulas << kitchenEnvs[i]
218 }
219 setupRunner(defaultGitRef, defaultGitUrl)
220 } else {
Martin Polreich5be29e32019-02-26 11:11:51 +0100221 common.errorMsg("No enviroments defined in the Kitchen file: ${kitchenFileName}")
222 }
223 } else {
Martin Polreich32b82222018-11-21 14:01:39 +0100224 common.warningMsg(".kitchen.yml nor .kitchen.openstack.yml file not found, no kitchen tests triggered.")
225 }
Martin Polreich32b82222018-11-21 14:01:39 +0100226 }
Jakub Joseff64c6392017-05-03 13:18:25 +0200227 }
Jakub Joseff64c6392017-05-03 13:18:25 +0200228 }
Jakub Josefa63f9862018-01-11 17:58:38 +0100229 if (failedFormulas) {
230 currentBuild.result = "FAILURE"
231 common.warningMsg("The following tests failed: ${failedFormulas}")
232 }
233 } catch (Throwable e) {
234 // If there was an error or exception thrown, the build failed
chnyda85ea22d2017-10-23 13:05:16 +0200235 currentBuild.result = "FAILURE"
Jakub Josefa63f9862018-01-11 17:58:38 +0100236 currentBuild.description = currentBuild.description ? e.message + " " + currentBuild.description : e.message
237 throw e
238 } finally {
239 if (currentBuild.result == "FAILURE" && fileExists(".kitchen/logs/kitchen.log")) {
240 common.errorMsg("----------------KITCHEN LOG:---------------")
241 println readFile(".kitchen/logs/kitchen.log")
242 }
chnyda85ea22d2017-10-23 13:05:16 +0200243 }
Jakub Josefc5a223a2017-03-01 14:40:08 +0100244 }
azvyagintsev3a2e0352018-01-10 12:41:29 +0200245}