blob: 640cfedaad7a4056be1cc4007516e06a3cfc9f0d [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
Martin Polreichc8a6a382019-04-10 09:14:22 +020023} else if (common.validInputParam('GATING_GERRIT_BRANCH')) {
24 gerritBranch = env.GATING_GERRIT_BRANCH
25 }
Martin Polreich4e682fc2019-04-09 16:01:38 +020026
Jakub Josef83379312017-03-29 18:12:34 +020027
Jakub Josefbcd2e902017-06-13 14:40:41 +020028def checkouted = false
Jakub Josef83379312017-03-29 18:12:34 +020029
Martin Polreich3ce6d3e2019-02-19 15:18:36 +010030envOverrides = []
chnyda85ea22d2017-10-23 13:05:16 +020031futureFormulas = []
32failedFormulas = []
Martin Polreich5be29e32019-02-26 11:11:51 +010033kitchenFileName = ''
chnyda85ea22d2017-10-23 13:05:16 +020034
35def setupRunner(defaultGitRef, defaultGitUrl) {
36 def branches = [:]
37 for (int i = 0; i < PARALLEL_GROUP_SIZE.toInteger() && i < futureFormulas.size(); i++) {
38 branches["Runner ${i}"] = {
39 while (futureFormulas && !failedFormulas) {
40 def currentFormula = futureFormulas[0] ? futureFormulas[0] : null
41 if (!currentFormula) {
42 continue
43 }
44 futureFormulas.remove(currentFormula)
45 try {
46 triggerTestFormulaJob(currentFormula, defaultGitRef, defaultGitUrl)
47 } catch (Exception e) {
chnyda0513b572018-01-23 13:40:57 +010048 if (e.getMessage().contains("completed with status ABORTED")) {
49 common.warningMsg("Test of ${currentFormula} was aborted and will be retriggered")
50 futureFormulas << currentFormula
51 } else {
52 failedFormulas << currentFormula
53 common.warningMsg("Test of ${currentFormula} failed : ${e}")
54 }
chnyda85ea22d2017-10-23 13:05:16 +020055 }
56 }
57 }
58 }
59 parallel branches
60}
61
62def triggerTestFormulaJob(testEnv, defaultGitRef, defaultGitUrl) {
63 common.infoMsg("Test of ${testEnv} starts")
64 build job: "test-salt-formulas-env", parameters: [
65 [$class: 'StringParameterValue', name: 'CREDENTIALS_ID', value: CREDENTIALS_ID],
66 [$class: 'StringParameterValue', name: 'KITCHEN_ENV', value: testEnv],
67 [$class: 'StringParameterValue', name: 'DEFAULT_GIT_REF', value: defaultGitRef],
68 [$class: 'StringParameterValue', name: 'DEFAULT_GIT_URL', value: defaultGitUrl],
69 [$class: 'StringParameterValue', name: 'SALT_OPTS', value: SALT_OPTS],
Martin Polreichb3708cf2019-04-08 15:39:21 +020070 [$class: 'StringParameterValue', name: 'SALT_VERSION', value: SALT_VERSION],
Martin Polreich4dedbcf2019-04-09 09:28:46 +020071 [$class: 'StringParameterValue', name: 'GERRIT_PARENT_BRANCH', value: gerritBranch]
chnyda85ea22d2017-10-23 13:05:16 +020072 ]
73}
Martin Polreichc0995dc2018-11-05 14:40:02 +010074
Martin Polreich15ef9f42018-12-17 14:29:54 +010075timeout(time: 4, unit: 'HOURS') {
azvyagintsevc2628ed2018-08-16 11:24:10 +030076 node(slaveNode) {
azvyagintsev54b49c62018-12-27 17:38:56 +020077 def img = dockerLib.getImage(env.SMOKE_TEST_DOCKER_IMG, "ubuntu:16.04")
Jakub Josefa63f9862018-01-11 17:58:38 +010078 try {
azvyagintsevc2628ed2018-08-16 11:24:10 +030079 if (fileExists("tests/build")) {
80 common.infoMsg('Cleaning test env')
81 sh ("sudo rm -rf tests/build")
82 }
Jakub Josefa63f9862018-01-11 17:58:38 +010083 stage("checkout") {
84 if (gerritRef) {
85 // job is triggered by Gerrit
86 def gerritChange = gerrit.getGerritChange(GERRIT_NAME, GERRIT_HOST, GERRIT_CHANGE_NUMBER, CREDENTIALS_ID, true)
87 // test if gerrit change is already Verified
88 if (gerrit.patchsetHasApproval(gerritChange.currentPatchSet, "Verified", "+")) {
89 common.successMsg("Gerrit change ${GERRIT_CHANGE_NUMBER} patchset ${GERRIT_PATCHSET_NUMBER} already has Verified, skipping tests") // do nothing
90 // test WIP contains in commit message
91 } else if (gerritChange.commitMessage.contains("WIP")) {
92 common.successMsg("Commit message contains WIP, skipping tests") // do nothing
Martin Polreicha3e30122017-08-22 12:43:55 +020093 } else {
Jakub Josefa63f9862018-01-11 17:58:38 +010094 // test if change aren't already merged
95 def merged = gerritChange.status == "MERGED"
96 if (!merged) {
97 checkouted = gerrit.gerritPatchsetCheckout([
98 credentialsId: CREDENTIALS_ID
99 ])
100 } else {
101 common.successMsg("Change ${GERRIT_CHANGE_NUMBER} is already merged, no need to test them")
102 }
Jakub Josefbcd2e902017-06-13 14:40:41 +0200103 }
Jakub Josefa63f9862018-01-11 17:58:38 +0100104 defaultGitUrl = "${GERRIT_SCHEME}://${GERRIT_NAME}@${GERRIT_HOST}:${GERRIT_PORT}/${GERRIT_PROJECT}"
105 defaultGitRef = GERRIT_REFSPEC
106 } else if (defaultGitRef && defaultGitUrl) {
107 checkouted = gerrit.gerritPatchsetCheckout(defaultGitUrl, defaultGitRef, "HEAD", CREDENTIALS_ID)
108 } else {
109 throw new Exception("Cannot checkout gerrit patchset, GERRIT_REFSPEC and DEFAULT_GIT_REF is null")
Jakub Josef740a7882017-06-01 14:54:01 +0200110 }
Jakub Josefc5a223a2017-03-01 14:40:08 +0100111 }
azvyagintsevc2628ed2018-08-16 11:24:10 +0300112 stage("test") {
113 if (checkouted) {
114 try {
115 // TODO add try\finally for image-stuck case. (copy-paste from SaltModelTesting)
116 withEnv(["SALT_VERSION=${saltVersion}"]) {
117 img.inside("-v ${env.WORKSPACE}/:/formula/ -u root:root --cpus=4 --ulimit nofile=4096:8192") {
118 sh('''#!/bin/bash -xe
119 cd /etc/apt/
azvyagintsev1a494b52018-12-20 14:48:46 +0200120 echo "deb [arch=amd64] http://mirror.mirantis.com/nightly/ubuntu xenial main restricted universe" > sources.list
121 echo "deb [arch=amd64] http://mirror.mirantis.com/nightly/ubuntu xenial-updates main restricted universe" >> sources.list
azvyagintsevc2628ed2018-08-16 11:24:10 +0300122 echo 'Acquire::Languages "none";' > apt.conf.d/docker-no-languages
123 echo 'Acquire::GzipIndexes "true"; Acquire::CompressionTypes::Order:: "gz";' > apt.conf.d/docker-gzip-indexes
124 echo 'APT::Get::Install-Recommends "false"; APT::Get::Install-Suggests "false";' > apt.conf.d/docker-recommends
125 apt-get update
126 apt-get install -y git-core wget curl apt-transport-https
127 apt-get install -y python-pip python3-pip python-virtualenv python3-virtualenv python-yaml autoconf build-essential
128 cd /formula/
129 make clean
130 make test
131 make clean
132 ''')
133 }
Sergey Otpuschennikove5a14742018-05-08 11:42:26 +0400134 }
azvyagintsevc2628ed2018-08-16 11:24:10 +0300135 }
136 finally {
137 if (fileExists("tests/build")) {
138 common.infoMsg('Cleaning test env')
139 sh ("sudo rm -rf tests/build")
140 }
Sergey Otpuschennikove5a14742018-05-08 11:42:26 +0400141 }
azvyagintsev3a2e0352018-01-10 12:41:29 +0200142 }
azvyagintsevc2628ed2018-08-16 11:24:10 +0300143
Jakub Josefc5a223a2017-03-01 14:40:08 +0100144 }
Jakub Joseff64c6392017-05-03 13:18:25 +0200145 stage("kitchen") {
Martin Polreich32b82222018-11-21 14:01:39 +0100146 if (fileExists(".travis.yml")) {/** TODO: Remove this legacy block once formulas are switched to new configuration */
Jakub Josefa63f9862018-01-11 17:58:38 +0100147 if (checkouted) {
Martin Polreichc0995dc2018-11-05 14:40:02 +0100148 if (fileExists(".kitchen.yml") || fileExists(".kitchen.openstack.yml")) {
149 if (fileExists(".kitchen.openstack.yml")) {
150 common.infoMsg("Openstack Kitchen test configuration found, running Openstack kitchen tests.")
151 if (fileExists(".kitchen.yml")) {
152 common.infoMsg("Ignoring the docker Kitchen test configuration file.")
153 }
154 } else {
155 common.infoMsg("Docker Kitchen test configuration found, running Docker kitchen tests.")
156 }
Jakub Josefa63f9862018-01-11 17:58:38 +0100157 def kitchenEnvs = []
158 def filteredEnvs = []
159 if (fileExists(".travis.yml")) {
160 common.infoMsg(".travis.yml file found.")
161 def kitchenConfigYML = readYaml(file: ".travis.yml")
162 if (kitchenConfigYML.containsKey("env")) {
163 kitchenEnvs = kitchenConfigYML["env"]
164 }
165 } else {
166 common.warningMsg(".travis.yml file not found, suites must be passed via CUSTOM_KITCHEN_ENVS parameter.")
Ruslan Kamaldinov310ffc02017-08-08 16:07:42 +0400167 }
Jakub Josefa63f9862018-01-11 17:58:38 +0100168 common.infoMsg("Running kitchen testing in parallel mode")
169 if (CUSTOM_KITCHEN_ENVS != null && CUSTOM_KITCHEN_ENVS != '') {
170 kitchenEnvs = CUSTOM_KITCHEN_ENVS.tokenize('\n')
171 common.infoMsg("CUSTOM_KITCHEN_ENVS not empty. Running with custom enviroments: ${kitchenEnvs}")
Martin Polreicha3e30122017-08-22 12:43:55 +0200172 }
Jakub Josefa63f9862018-01-11 17:58:38 +0100173 if (kitchenEnvs != null && kitchenEnvs != '') {
174 def acc = 0
175 common.infoMsg("Found " + kitchenEnvs.size() + " environment(s)")
176 for (int i = 0; i < kitchenEnvs.size(); i++) {
177 futureFormulas << kitchenEnvs[i]
178 }
179 setupRunner(defaultGitRef, defaultGitUrl)
180 } else {
181 common.warningMsg(".kitchen.yml file not found, no kitchen tests triggered.")
182 }
Martin Polreicha3e30122017-08-22 12:43:55 +0200183 }
Martin Polreich32b82222018-11-21 14:01:39 +0100184 }/** TODO: End of block for removal */
185 } else {
186 if (checkouted) {
Martin Polreich5be29e32019-02-26 11:11:51 +0100187 if (fileExists(".kitchen.openstack.yml")) {
188 common.infoMsg("Openstack Kitchen test configuration found, running Openstack kitchen tests.")
189 kitchenFileName = ".kitchen.openstack.yml"
190 envOverrides.add("KITCHEN_YAML=${kitchenFileName}")
Martin Polreich73c8e512019-02-26 15:16:33 +0100191 rubyVersion = '2.5.0'
Martin Polreich5be29e32019-02-26 11:11:51 +0100192 } else if (fileExists(".kitchen.yml")) {
193 common.infoMsg("Docker Kitchen test configuration found, running Docker kitchen tests.")
194 kitchenFileName = ".kitchen.yml"
Martin Polreich73c8e512019-02-26 15:16:33 +0100195 rubyVersion = '2.4.1'
Martin Polreich5be29e32019-02-26 11:11:51 +0100196 }
197 if (kitchenFileName) {
Martin Polreich32b82222018-11-21 14:01:39 +0100198 def kitchenEnvs = []
Martin Polreich5be29e32019-02-26 11:11:51 +0100199 ruby.ensureRubyEnv(rubyVersion)
Martin Polreich32b82222018-11-21 14:01:39 +0100200 if (!fileExists("Gemfile")) {
201 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'")
202 ruby.installKitchen()
203 } else {
204 common.infoMsg("Override Gemfile found in the kitchen directory, using it.")
205 ruby.installKitchen()
206 }
Martin Polreich3ce6d3e2019-02-19 15:18:36 +0100207 common.infoMsg = ruby.runKitchenCommand("list -b", envOverrides.join(' '))
208 kitchenEnvs = ruby.runKitchenCommand("list -b", envOverrides.join(' ')).split()
Martin Polreich32b82222018-11-21 14:01:39 +0100209 common.infoMsg(kitchenEnvs)
210 common.infoMsg("Running kitchen testing in parallel mode")
211 if (CUSTOM_KITCHEN_ENVS != null && CUSTOM_KITCHEN_ENVS != '') {
212 kitchenEnvs = CUSTOM_KITCHEN_ENVS.tokenize('\n')
213 common.infoMsg("CUSTOM_KITCHEN_ENVS not empty. Running with custom enviroments: ${kitchenEnvs}")
214 }
215 if (kitchenEnvs != null && kitchenEnvs != '') {
216 def acc = 0
217 common.infoMsg("Found " + kitchenEnvs.size() + " environment(s)")
218 for (int i = 0; i < kitchenEnvs.size(); i++) {
219 futureFormulas << kitchenEnvs[i]
220 }
221 setupRunner(defaultGitRef, defaultGitUrl)
222 } else {
Martin Polreich5be29e32019-02-26 11:11:51 +0100223 common.errorMsg("No enviroments defined in the Kitchen file: ${kitchenFileName}")
224 }
225 } else {
Martin Polreich32b82222018-11-21 14:01:39 +0100226 common.warningMsg(".kitchen.yml nor .kitchen.openstack.yml file not found, no kitchen tests triggered.")
227 }
Martin Polreich32b82222018-11-21 14:01:39 +0100228 }
Jakub Joseff64c6392017-05-03 13:18:25 +0200229 }
Jakub Joseff64c6392017-05-03 13:18:25 +0200230 }
Jakub Josefa63f9862018-01-11 17:58:38 +0100231 if (failedFormulas) {
232 currentBuild.result = "FAILURE"
233 common.warningMsg("The following tests failed: ${failedFormulas}")
234 }
235 } catch (Throwable e) {
236 // If there was an error or exception thrown, the build failed
chnyda85ea22d2017-10-23 13:05:16 +0200237 currentBuild.result = "FAILURE"
Jakub Josefa63f9862018-01-11 17:58:38 +0100238 currentBuild.description = currentBuild.description ? e.message + " " + currentBuild.description : e.message
239 throw e
240 } finally {
241 if (currentBuild.result == "FAILURE" && fileExists(".kitchen/logs/kitchen.log")) {
242 common.errorMsg("----------------KITCHEN LOG:---------------")
243 println readFile(".kitchen/logs/kitchen.log")
244 }
chnyda85ea22d2017-10-23 13:05:16 +0200245 }
Jakub Josefc5a223a2017-03-01 14:40:08 +0100246 }
azvyagintsev3a2e0352018-01-10 12:41:29 +0200247}