blob: e84386ae804eeed54584df8dd93c96f38ef58614 [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
Vladimir Khlyunev8407afd2020-06-15 15:42:23 +040017// vkhlyunev: New slaves breaks old jobs, sticking to ols slaves until fixed
18//def slaveNode = env.SLAVE_NODE ?: 'virtual'
19def slaveNode = env.SLAVE_NODE ?: 'old16.04'
azvyagintsevc2628ed2018-08-16 11:24:10 +030020def saltVersion = env.SALT_VERSION ?: ""
Martin Polreich4e682fc2019-04-09 16:01:38 +020021
22gerritBranch = 'master'
23if (common.validInputParam('GERRIT_BRANCH')) {
24 gerritBranch = env.GERRIT_BRANCH
Martin Polreichc8a6a382019-04-10 09:14:22 +020025} else if (common.validInputParam('GATING_GERRIT_BRANCH')) {
26 gerritBranch = env.GATING_GERRIT_BRANCH
27 }
Martin Polreich4e682fc2019-04-09 16:01:38 +020028
Jakub Josef83379312017-03-29 18:12:34 +020029
Jakub Josefbcd2e902017-06-13 14:40:41 +020030def checkouted = false
Jakub Josef83379312017-03-29 18:12:34 +020031
Martin Polreich3ce6d3e2019-02-19 15:18:36 +010032envOverrides = []
chnyda85ea22d2017-10-23 13:05:16 +020033futureFormulas = []
34failedFormulas = []
Martin Polreich5be29e32019-02-26 11:11:51 +010035kitchenFileName = ''
chnyda85ea22d2017-10-23 13:05:16 +020036
37def setupRunner(defaultGitRef, defaultGitUrl) {
38 def branches = [:]
39 for (int i = 0; i < PARALLEL_GROUP_SIZE.toInteger() && i < futureFormulas.size(); i++) {
40 branches["Runner ${i}"] = {
41 while (futureFormulas && !failedFormulas) {
42 def currentFormula = futureFormulas[0] ? futureFormulas[0] : null
43 if (!currentFormula) {
44 continue
45 }
46 futureFormulas.remove(currentFormula)
47 try {
48 triggerTestFormulaJob(currentFormula, defaultGitRef, defaultGitUrl)
49 } catch (Exception e) {
chnyda0513b572018-01-23 13:40:57 +010050 if (e.getMessage().contains("completed with status ABORTED")) {
51 common.warningMsg("Test of ${currentFormula} was aborted and will be retriggered")
52 futureFormulas << currentFormula
53 } else {
54 failedFormulas << currentFormula
55 common.warningMsg("Test of ${currentFormula} failed : ${e}")
56 }
chnyda85ea22d2017-10-23 13:05:16 +020057 }
58 }
59 }
60 }
61 parallel branches
62}
63
64def triggerTestFormulaJob(testEnv, defaultGitRef, defaultGitUrl) {
65 common.infoMsg("Test of ${testEnv} starts")
66 build job: "test-salt-formulas-env", parameters: [
67 [$class: 'StringParameterValue', name: 'CREDENTIALS_ID', value: CREDENTIALS_ID],
68 [$class: 'StringParameterValue', name: 'KITCHEN_ENV', value: testEnv],
69 [$class: 'StringParameterValue', name: 'DEFAULT_GIT_REF', value: defaultGitRef],
70 [$class: 'StringParameterValue', name: 'DEFAULT_GIT_URL', value: defaultGitUrl],
71 [$class: 'StringParameterValue', name: 'SALT_OPTS', value: SALT_OPTS],
Martin Polreichb3708cf2019-04-08 15:39:21 +020072 [$class: 'StringParameterValue', name: 'SALT_VERSION', value: SALT_VERSION],
Martin Polreich4dedbcf2019-04-09 09:28:46 +020073 [$class: 'StringParameterValue', name: 'GERRIT_PARENT_BRANCH', value: gerritBranch]
chnyda85ea22d2017-10-23 13:05:16 +020074 ]
75}
Martin Polreichc0995dc2018-11-05 14:40:02 +010076
Martin Polreich15ef9f42018-12-17 14:29:54 +010077timeout(time: 4, unit: 'HOURS') {
azvyagintsevc2628ed2018-08-16 11:24:10 +030078 node(slaveNode) {
azvyagintsev54b49c62018-12-27 17:38:56 +020079 def img = dockerLib.getImage(env.SMOKE_TEST_DOCKER_IMG, "ubuntu:16.04")
Jakub Josefa63f9862018-01-11 17:58:38 +010080 try {
azvyagintsevc2628ed2018-08-16 11:24:10 +030081 if (fileExists("tests/build")) {
82 common.infoMsg('Cleaning test env')
83 sh ("sudo rm -rf tests/build")
84 }
Jakub Josefa63f9862018-01-11 17:58:38 +010085 stage("checkout") {
86 if (gerritRef) {
87 // job is triggered by Gerrit
88 def gerritChange = gerrit.getGerritChange(GERRIT_NAME, GERRIT_HOST, GERRIT_CHANGE_NUMBER, CREDENTIALS_ID, true)
89 // test if gerrit change is already Verified
90 if (gerrit.patchsetHasApproval(gerritChange.currentPatchSet, "Verified", "+")) {
91 common.successMsg("Gerrit change ${GERRIT_CHANGE_NUMBER} patchset ${GERRIT_PATCHSET_NUMBER} already has Verified, skipping tests") // do nothing
92 // test WIP contains in commit message
93 } else if (gerritChange.commitMessage.contains("WIP")) {
94 common.successMsg("Commit message contains WIP, skipping tests") // do nothing
Martin Polreicha3e30122017-08-22 12:43:55 +020095 } else {
Jakub Josefa63f9862018-01-11 17:58:38 +010096 // test if change aren't already merged
97 def merged = gerritChange.status == "MERGED"
98 if (!merged) {
99 checkouted = gerrit.gerritPatchsetCheckout([
100 credentialsId: CREDENTIALS_ID
101 ])
102 } else {
103 common.successMsg("Change ${GERRIT_CHANGE_NUMBER} is already merged, no need to test them")
104 }
Jakub Josefbcd2e902017-06-13 14:40:41 +0200105 }
Jakub Josefa63f9862018-01-11 17:58:38 +0100106 defaultGitUrl = "${GERRIT_SCHEME}://${GERRIT_NAME}@${GERRIT_HOST}:${GERRIT_PORT}/${GERRIT_PROJECT}"
107 defaultGitRef = GERRIT_REFSPEC
108 } else if (defaultGitRef && defaultGitUrl) {
109 checkouted = gerrit.gerritPatchsetCheckout(defaultGitUrl, defaultGitRef, "HEAD", CREDENTIALS_ID)
110 } else {
111 throw new Exception("Cannot checkout gerrit patchset, GERRIT_REFSPEC and DEFAULT_GIT_REF is null")
Jakub Josef740a7882017-06-01 14:54:01 +0200112 }
Jakub Josefc5a223a2017-03-01 14:40:08 +0100113 }
azvyagintsevc2628ed2018-08-16 11:24:10 +0300114 stage("test") {
115 if (checkouted) {
116 try {
117 // TODO add try\finally for image-stuck case. (copy-paste from SaltModelTesting)
118 withEnv(["SALT_VERSION=${saltVersion}"]) {
119 img.inside("-v ${env.WORKSPACE}/:/formula/ -u root:root --cpus=4 --ulimit nofile=4096:8192") {
120 sh('''#!/bin/bash -xe
121 cd /etc/apt/
azvyagintsev1a494b52018-12-20 14:48:46 +0200122 echo "deb [arch=amd64] http://mirror.mirantis.com/nightly/ubuntu xenial main restricted universe" > sources.list
123 echo "deb [arch=amd64] http://mirror.mirantis.com/nightly/ubuntu xenial-updates main restricted universe" >> sources.list
azvyagintsevc2628ed2018-08-16 11:24:10 +0300124 echo 'Acquire::Languages "none";' > apt.conf.d/docker-no-languages
125 echo 'Acquire::GzipIndexes "true"; Acquire::CompressionTypes::Order:: "gz";' > apt.conf.d/docker-gzip-indexes
126 echo 'APT::Get::Install-Recommends "false"; APT::Get::Install-Suggests "false";' > apt.conf.d/docker-recommends
127 apt-get update
128 apt-get install -y git-core wget curl apt-transport-https
129 apt-get install -y python-pip python3-pip python-virtualenv python3-virtualenv python-yaml autoconf build-essential
130 cd /formula/
131 make clean
132 make test
133 make clean
134 ''')
135 }
Sergey Otpuschennikove5a14742018-05-08 11:42:26 +0400136 }
azvyagintsevc2628ed2018-08-16 11:24:10 +0300137 }
138 finally {
139 if (fileExists("tests/build")) {
140 common.infoMsg('Cleaning test env')
141 sh ("sudo rm -rf tests/build")
142 }
Sergey Otpuschennikove5a14742018-05-08 11:42:26 +0400143 }
azvyagintsev3a2e0352018-01-10 12:41:29 +0200144 }
azvyagintsevc2628ed2018-08-16 11:24:10 +0300145
Jakub Josefc5a223a2017-03-01 14:40:08 +0100146 }
Jakub Joseff64c6392017-05-03 13:18:25 +0200147 stage("kitchen") {
Martin Polreich32b82222018-11-21 14:01:39 +0100148 if (fileExists(".travis.yml")) {/** TODO: Remove this legacy block once formulas are switched to new configuration */
Jakub Josefa63f9862018-01-11 17:58:38 +0100149 if (checkouted) {
Martin Polreichc0995dc2018-11-05 14:40:02 +0100150 if (fileExists(".kitchen.yml") || fileExists(".kitchen.openstack.yml")) {
151 if (fileExists(".kitchen.openstack.yml")) {
152 common.infoMsg("Openstack Kitchen test configuration found, running Openstack kitchen tests.")
153 if (fileExists(".kitchen.yml")) {
154 common.infoMsg("Ignoring the docker Kitchen test configuration file.")
155 }
156 } else {
157 common.infoMsg("Docker Kitchen test configuration found, running Docker kitchen tests.")
158 }
Jakub Josefa63f9862018-01-11 17:58:38 +0100159 def kitchenEnvs = []
160 def filteredEnvs = []
161 if (fileExists(".travis.yml")) {
162 common.infoMsg(".travis.yml file found.")
163 def kitchenConfigYML = readYaml(file: ".travis.yml")
164 if (kitchenConfigYML.containsKey("env")) {
165 kitchenEnvs = kitchenConfigYML["env"]
166 }
167 } else {
168 common.warningMsg(".travis.yml file not found, suites must be passed via CUSTOM_KITCHEN_ENVS parameter.")
Ruslan Kamaldinov310ffc02017-08-08 16:07:42 +0400169 }
Jakub Josefa63f9862018-01-11 17:58:38 +0100170 common.infoMsg("Running kitchen testing in parallel mode")
171 if (CUSTOM_KITCHEN_ENVS != null && CUSTOM_KITCHEN_ENVS != '') {
172 kitchenEnvs = CUSTOM_KITCHEN_ENVS.tokenize('\n')
173 common.infoMsg("CUSTOM_KITCHEN_ENVS not empty. Running with custom enviroments: ${kitchenEnvs}")
Martin Polreicha3e30122017-08-22 12:43:55 +0200174 }
Jakub Josefa63f9862018-01-11 17:58:38 +0100175 if (kitchenEnvs != null && kitchenEnvs != '') {
176 def acc = 0
177 common.infoMsg("Found " + kitchenEnvs.size() + " environment(s)")
178 for (int i = 0; i < kitchenEnvs.size(); i++) {
179 futureFormulas << kitchenEnvs[i]
180 }
181 setupRunner(defaultGitRef, defaultGitUrl)
182 } else {
183 common.warningMsg(".kitchen.yml file not found, no kitchen tests triggered.")
184 }
Martin Polreicha3e30122017-08-22 12:43:55 +0200185 }
Martin Polreich32b82222018-11-21 14:01:39 +0100186 }/** TODO: End of block for removal */
187 } else {
188 if (checkouted) {
Martin Polreich5be29e32019-02-26 11:11:51 +0100189 if (fileExists(".kitchen.openstack.yml")) {
190 common.infoMsg("Openstack Kitchen test configuration found, running Openstack kitchen tests.")
191 kitchenFileName = ".kitchen.openstack.yml"
192 envOverrides.add("KITCHEN_YAML=${kitchenFileName}")
Martin Polreich73c8e512019-02-26 15:16:33 +0100193 rubyVersion = '2.5.0'
Martin Polreich5be29e32019-02-26 11:11:51 +0100194 } else if (fileExists(".kitchen.yml")) {
195 common.infoMsg("Docker Kitchen test configuration found, running Docker kitchen tests.")
196 kitchenFileName = ".kitchen.yml"
Martin Polreich73c8e512019-02-26 15:16:33 +0100197 rubyVersion = '2.4.1'
Martin Polreich5be29e32019-02-26 11:11:51 +0100198 }
199 if (kitchenFileName) {
Martin Polreich32b82222018-11-21 14:01:39 +0100200 def kitchenEnvs = []
Martin Polreich5be29e32019-02-26 11:11:51 +0100201 ruby.ensureRubyEnv(rubyVersion)
Martin Polreich32b82222018-11-21 14:01:39 +0100202 if (!fileExists("Gemfile")) {
203 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'")
204 ruby.installKitchen()
205 } else {
206 common.infoMsg("Override Gemfile found in the kitchen directory, using it.")
207 ruby.installKitchen()
208 }
Martin Polreich3ce6d3e2019-02-19 15:18:36 +0100209 common.infoMsg = ruby.runKitchenCommand("list -b", envOverrides.join(' '))
210 kitchenEnvs = ruby.runKitchenCommand("list -b", envOverrides.join(' ')).split()
Martin Polreich32b82222018-11-21 14:01:39 +0100211 common.infoMsg(kitchenEnvs)
212 common.infoMsg("Running kitchen testing in parallel mode")
213 if (CUSTOM_KITCHEN_ENVS != null && CUSTOM_KITCHEN_ENVS != '') {
214 kitchenEnvs = CUSTOM_KITCHEN_ENVS.tokenize('\n')
215 common.infoMsg("CUSTOM_KITCHEN_ENVS not empty. Running with custom enviroments: ${kitchenEnvs}")
216 }
217 if (kitchenEnvs != null && kitchenEnvs != '') {
218 def acc = 0
219 common.infoMsg("Found " + kitchenEnvs.size() + " environment(s)")
220 for (int i = 0; i < kitchenEnvs.size(); i++) {
221 futureFormulas << kitchenEnvs[i]
222 }
223 setupRunner(defaultGitRef, defaultGitUrl)
224 } else {
Martin Polreich5be29e32019-02-26 11:11:51 +0100225 common.errorMsg("No enviroments defined in the Kitchen file: ${kitchenFileName}")
226 }
227 } else {
Martin Polreich32b82222018-11-21 14:01:39 +0100228 common.warningMsg(".kitchen.yml nor .kitchen.openstack.yml file not found, no kitchen tests triggered.")
229 }
Martin Polreich32b82222018-11-21 14:01:39 +0100230 }
Jakub Joseff64c6392017-05-03 13:18:25 +0200231 }
Jakub Joseff64c6392017-05-03 13:18:25 +0200232 }
Jakub Josefa63f9862018-01-11 17:58:38 +0100233 if (failedFormulas) {
234 currentBuild.result = "FAILURE"
235 common.warningMsg("The following tests failed: ${failedFormulas}")
236 }
237 } catch (Throwable e) {
238 // If there was an error or exception thrown, the build failed
chnyda85ea22d2017-10-23 13:05:16 +0200239 currentBuild.result = "FAILURE"
Jakub Josefa63f9862018-01-11 17:58:38 +0100240 currentBuild.description = currentBuild.description ? e.message + " " + currentBuild.description : e.message
241 throw e
242 } finally {
243 if (currentBuild.result == "FAILURE" && fileExists(".kitchen/logs/kitchen.log")) {
244 common.errorMsg("----------------KITCHEN LOG:---------------")
245 println readFile(".kitchen/logs/kitchen.log")
246 }
chnyda85ea22d2017-10-23 13:05:16 +0200247 }
Jakub Josefc5a223a2017-03-01 14:40:08 +0100248 }
azvyagintsev3a2e0352018-01-10 12:41:29 +0200249}