blob: 43264335c7d09ec50157a23aef8839d5f69a8e68 [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
Aleksey Zvyagintsev4a804212019-02-07 13:02:31 +000016def slaveNode = env.SLAVE_NODE ?: 'virtual'
azvyagintsevc2628ed2018-08-16 11:24:10 +030017def saltVersion = env.SALT_VERSION ?: ""
18def dockerLib = new com.mirantis.mk.Docker()
Jakub Josef83379312017-03-29 18:12:34 +020019
Jakub Josefbcd2e902017-06-13 14:40:41 +020020def checkouted = false
Jakub Josef83379312017-03-29 18:12:34 +020021
Martin Polreich3ce6d3e2019-02-19 15:18:36 +010022envOverrides = []
chnyda85ea22d2017-10-23 13:05:16 +020023futureFormulas = []
24failedFormulas = []
Martin Polreich5be29e32019-02-26 11:11:51 +010025kitchenFileName = ''
chnyda85ea22d2017-10-23 13:05:16 +020026
27def setupRunner(defaultGitRef, defaultGitUrl) {
28 def branches = [:]
29 for (int i = 0; i < PARALLEL_GROUP_SIZE.toInteger() && i < futureFormulas.size(); i++) {
30 branches["Runner ${i}"] = {
31 while (futureFormulas && !failedFormulas) {
32 def currentFormula = futureFormulas[0] ? futureFormulas[0] : null
33 if (!currentFormula) {
34 continue
35 }
36 futureFormulas.remove(currentFormula)
37 try {
38 triggerTestFormulaJob(currentFormula, defaultGitRef, defaultGitUrl)
39 } catch (Exception e) {
chnyda0513b572018-01-23 13:40:57 +010040 if (e.getMessage().contains("completed with status ABORTED")) {
41 common.warningMsg("Test of ${currentFormula} was aborted and will be retriggered")
42 futureFormulas << currentFormula
43 } else {
44 failedFormulas << currentFormula
45 common.warningMsg("Test of ${currentFormula} failed : ${e}")
46 }
chnyda85ea22d2017-10-23 13:05:16 +020047 }
48 }
49 }
50 }
51 parallel branches
52}
53
54def triggerTestFormulaJob(testEnv, defaultGitRef, defaultGitUrl) {
55 common.infoMsg("Test of ${testEnv} starts")
56 build job: "test-salt-formulas-env", parameters: [
57 [$class: 'StringParameterValue', name: 'CREDENTIALS_ID', value: CREDENTIALS_ID],
58 [$class: 'StringParameterValue', name: 'KITCHEN_ENV', value: testEnv],
59 [$class: 'StringParameterValue', name: 'DEFAULT_GIT_REF', value: defaultGitRef],
60 [$class: 'StringParameterValue', name: 'DEFAULT_GIT_URL', value: defaultGitUrl],
61 [$class: 'StringParameterValue', name: 'SALT_OPTS', value: SALT_OPTS],
62 [$class: 'StringParameterValue', name: 'SALT_VERSION', value: SALT_VERSION]
63 ]
64}
Martin Polreichc0995dc2018-11-05 14:40:02 +010065
Martin Polreich15ef9f42018-12-17 14:29:54 +010066timeout(time: 4, unit: 'HOURS') {
azvyagintsevc2628ed2018-08-16 11:24:10 +030067 node(slaveNode) {
azvyagintsev54b49c62018-12-27 17:38:56 +020068 def img = dockerLib.getImage(env.SMOKE_TEST_DOCKER_IMG, "ubuntu:16.04")
Jakub Josefa63f9862018-01-11 17:58:38 +010069 try {
azvyagintsevc2628ed2018-08-16 11:24:10 +030070 if (fileExists("tests/build")) {
71 common.infoMsg('Cleaning test env')
72 sh ("sudo rm -rf tests/build")
73 }
Jakub Josefa63f9862018-01-11 17:58:38 +010074 stage("checkout") {
75 if (gerritRef) {
76 // job is triggered by Gerrit
77 def gerritChange = gerrit.getGerritChange(GERRIT_NAME, GERRIT_HOST, GERRIT_CHANGE_NUMBER, CREDENTIALS_ID, true)
78 // test if gerrit change is already Verified
79 if (gerrit.patchsetHasApproval(gerritChange.currentPatchSet, "Verified", "+")) {
80 common.successMsg("Gerrit change ${GERRIT_CHANGE_NUMBER} patchset ${GERRIT_PATCHSET_NUMBER} already has Verified, skipping tests") // do nothing
81 // test WIP contains in commit message
82 } else if (gerritChange.commitMessage.contains("WIP")) {
83 common.successMsg("Commit message contains WIP, skipping tests") // do nothing
Martin Polreicha3e30122017-08-22 12:43:55 +020084 } else {
Jakub Josefa63f9862018-01-11 17:58:38 +010085 // test if change aren't already merged
86 def merged = gerritChange.status == "MERGED"
87 if (!merged) {
88 checkouted = gerrit.gerritPatchsetCheckout([
89 credentialsId: CREDENTIALS_ID
90 ])
91 } else {
92 common.successMsg("Change ${GERRIT_CHANGE_NUMBER} is already merged, no need to test them")
93 }
Jakub Josefbcd2e902017-06-13 14:40:41 +020094 }
Jakub Josefa63f9862018-01-11 17:58:38 +010095 defaultGitUrl = "${GERRIT_SCHEME}://${GERRIT_NAME}@${GERRIT_HOST}:${GERRIT_PORT}/${GERRIT_PROJECT}"
96 defaultGitRef = GERRIT_REFSPEC
97 } else if (defaultGitRef && defaultGitUrl) {
98 checkouted = gerrit.gerritPatchsetCheckout(defaultGitUrl, defaultGitRef, "HEAD", CREDENTIALS_ID)
99 } else {
100 throw new Exception("Cannot checkout gerrit patchset, GERRIT_REFSPEC and DEFAULT_GIT_REF is null")
Jakub Josef740a7882017-06-01 14:54:01 +0200101 }
Jakub Josefc5a223a2017-03-01 14:40:08 +0100102 }
azvyagintsevc2628ed2018-08-16 11:24:10 +0300103 stage("test") {
104 if (checkouted) {
105 try {
106 // TODO add try\finally for image-stuck case. (copy-paste from SaltModelTesting)
107 withEnv(["SALT_VERSION=${saltVersion}"]) {
108 img.inside("-v ${env.WORKSPACE}/:/formula/ -u root:root --cpus=4 --ulimit nofile=4096:8192") {
109 sh('''#!/bin/bash -xe
110 cd /etc/apt/
azvyagintsev1a494b52018-12-20 14:48:46 +0200111 echo "deb [arch=amd64] http://mirror.mirantis.com/nightly/ubuntu xenial main restricted universe" > sources.list
112 echo "deb [arch=amd64] http://mirror.mirantis.com/nightly/ubuntu xenial-updates main restricted universe" >> sources.list
azvyagintsevc2628ed2018-08-16 11:24:10 +0300113 echo 'Acquire::Languages "none";' > apt.conf.d/docker-no-languages
114 echo 'Acquire::GzipIndexes "true"; Acquire::CompressionTypes::Order:: "gz";' > apt.conf.d/docker-gzip-indexes
115 echo 'APT::Get::Install-Recommends "false"; APT::Get::Install-Suggests "false";' > apt.conf.d/docker-recommends
116 apt-get update
117 apt-get install -y git-core wget curl apt-transport-https
118 apt-get install -y python-pip python3-pip python-virtualenv python3-virtualenv python-yaml autoconf build-essential
119 cd /formula/
120 make clean
121 make test
122 make clean
123 ''')
124 }
Sergey Otpuschennikove5a14742018-05-08 11:42:26 +0400125 }
azvyagintsevc2628ed2018-08-16 11:24:10 +0300126 }
127 finally {
128 if (fileExists("tests/build")) {
129 common.infoMsg('Cleaning test env')
130 sh ("sudo rm -rf tests/build")
131 }
Sergey Otpuschennikove5a14742018-05-08 11:42:26 +0400132 }
azvyagintsev3a2e0352018-01-10 12:41:29 +0200133 }
azvyagintsevc2628ed2018-08-16 11:24:10 +0300134
Jakub Josefc5a223a2017-03-01 14:40:08 +0100135 }
Jakub Joseff64c6392017-05-03 13:18:25 +0200136 stage("kitchen") {
Martin Polreich32b82222018-11-21 14:01:39 +0100137 if (fileExists(".travis.yml")) {/** TODO: Remove this legacy block once formulas are switched to new configuration */
Jakub Josefa63f9862018-01-11 17:58:38 +0100138 if (checkouted) {
Martin Polreichc0995dc2018-11-05 14:40:02 +0100139 if (fileExists(".kitchen.yml") || fileExists(".kitchen.openstack.yml")) {
140 if (fileExists(".kitchen.openstack.yml")) {
141 common.infoMsg("Openstack Kitchen test configuration found, running Openstack kitchen tests.")
142 if (fileExists(".kitchen.yml")) {
143 common.infoMsg("Ignoring the docker Kitchen test configuration file.")
144 }
145 } else {
146 common.infoMsg("Docker Kitchen test configuration found, running Docker kitchen tests.")
147 }
Jakub Josefa63f9862018-01-11 17:58:38 +0100148 def kitchenEnvs = []
149 def filteredEnvs = []
150 if (fileExists(".travis.yml")) {
151 common.infoMsg(".travis.yml file found.")
152 def kitchenConfigYML = readYaml(file: ".travis.yml")
153 if (kitchenConfigYML.containsKey("env")) {
154 kitchenEnvs = kitchenConfigYML["env"]
155 }
156 } else {
157 common.warningMsg(".travis.yml file not found, suites must be passed via CUSTOM_KITCHEN_ENVS parameter.")
Ruslan Kamaldinov310ffc02017-08-08 16:07:42 +0400158 }
Jakub Josefa63f9862018-01-11 17:58:38 +0100159 common.infoMsg("Running kitchen testing in parallel mode")
160 if (CUSTOM_KITCHEN_ENVS != null && CUSTOM_KITCHEN_ENVS != '') {
161 kitchenEnvs = CUSTOM_KITCHEN_ENVS.tokenize('\n')
162 common.infoMsg("CUSTOM_KITCHEN_ENVS not empty. Running with custom enviroments: ${kitchenEnvs}")
Martin Polreicha3e30122017-08-22 12:43:55 +0200163 }
Jakub Josefa63f9862018-01-11 17:58:38 +0100164 if (kitchenEnvs != null && kitchenEnvs != '') {
165 def acc = 0
166 common.infoMsg("Found " + kitchenEnvs.size() + " environment(s)")
167 for (int i = 0; i < kitchenEnvs.size(); i++) {
168 futureFormulas << kitchenEnvs[i]
169 }
170 setupRunner(defaultGitRef, defaultGitUrl)
171 } else {
172 common.warningMsg(".kitchen.yml file not found, no kitchen tests triggered.")
173 }
Martin Polreicha3e30122017-08-22 12:43:55 +0200174 }
Martin Polreich32b82222018-11-21 14:01:39 +0100175 }/** TODO: End of block for removal */
176 } else {
177 if (checkouted) {
Martin Polreich5be29e32019-02-26 11:11:51 +0100178 if (fileExists(".kitchen.openstack.yml")) {
179 common.infoMsg("Openstack Kitchen test configuration found, running Openstack kitchen tests.")
180 kitchenFileName = ".kitchen.openstack.yml"
181 envOverrides.add("KITCHEN_YAML=${kitchenFileName}")
Martin Polreich73c8e512019-02-26 15:16:33 +0100182 rubyVersion = '2.5.0'
Martin Polreich5be29e32019-02-26 11:11:51 +0100183 } else if (fileExists(".kitchen.yml")) {
184 common.infoMsg("Docker Kitchen test configuration found, running Docker kitchen tests.")
185 kitchenFileName = ".kitchen.yml"
Martin Polreich73c8e512019-02-26 15:16:33 +0100186 rubyVersion = '2.4.1'
Martin Polreich5be29e32019-02-26 11:11:51 +0100187 }
188 if (kitchenFileName) {
Martin Polreich32b82222018-11-21 14:01:39 +0100189 def kitchenEnvs = []
Martin Polreich5be29e32019-02-26 11:11:51 +0100190 ruby.ensureRubyEnv(rubyVersion)
Martin Polreich32b82222018-11-21 14:01:39 +0100191 if (!fileExists("Gemfile")) {
192 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'")
193 ruby.installKitchen()
194 } else {
195 common.infoMsg("Override Gemfile found in the kitchen directory, using it.")
196 ruby.installKitchen()
197 }
Martin Polreich3ce6d3e2019-02-19 15:18:36 +0100198 common.infoMsg = ruby.runKitchenCommand("list -b", envOverrides.join(' '))
199 kitchenEnvs = ruby.runKitchenCommand("list -b", envOverrides.join(' ')).split()
Martin Polreich32b82222018-11-21 14:01:39 +0100200 common.infoMsg(kitchenEnvs)
201 common.infoMsg("Running kitchen testing in parallel mode")
202 if (CUSTOM_KITCHEN_ENVS != null && CUSTOM_KITCHEN_ENVS != '') {
203 kitchenEnvs = CUSTOM_KITCHEN_ENVS.tokenize('\n')
204 common.infoMsg("CUSTOM_KITCHEN_ENVS not empty. Running with custom enviroments: ${kitchenEnvs}")
205 }
206 if (kitchenEnvs != null && kitchenEnvs != '') {
207 def acc = 0
208 common.infoMsg("Found " + kitchenEnvs.size() + " environment(s)")
209 for (int i = 0; i < kitchenEnvs.size(); i++) {
210 futureFormulas << kitchenEnvs[i]
211 }
212 setupRunner(defaultGitRef, defaultGitUrl)
213 } else {
Martin Polreich5be29e32019-02-26 11:11:51 +0100214 common.errorMsg("No enviroments defined in the Kitchen file: ${kitchenFileName}")
215 }
216 } else {
Martin Polreich32b82222018-11-21 14:01:39 +0100217 common.warningMsg(".kitchen.yml nor .kitchen.openstack.yml file not found, no kitchen tests triggered.")
218 }
Martin Polreich32b82222018-11-21 14:01:39 +0100219 }
Jakub Joseff64c6392017-05-03 13:18:25 +0200220 }
Jakub Joseff64c6392017-05-03 13:18:25 +0200221 }
Jakub Josefa63f9862018-01-11 17:58:38 +0100222 if (failedFormulas) {
223 currentBuild.result = "FAILURE"
224 common.warningMsg("The following tests failed: ${failedFormulas}")
225 }
226 } catch (Throwable e) {
227 // If there was an error or exception thrown, the build failed
chnyda85ea22d2017-10-23 13:05:16 +0200228 currentBuild.result = "FAILURE"
Jakub Josefa63f9862018-01-11 17:58:38 +0100229 currentBuild.description = currentBuild.description ? e.message + " " + currentBuild.description : e.message
230 throw e
231 } finally {
232 if (currentBuild.result == "FAILURE" && fileExists(".kitchen/logs/kitchen.log")) {
233 common.errorMsg("----------------KITCHEN LOG:---------------")
234 println readFile(".kitchen/logs/kitchen.log")
235 }
chnyda85ea22d2017-10-23 13:05:16 +0200236 }
Jakub Josefc5a223a2017-03-01 14:40:08 +0100237 }
azvyagintsev3a2e0352018-01-10 12:41:29 +0200238}