blob: 2bfdc7d058ed356fca54d5a61aa7ac86a454c1ac [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 ?: ""
Martin Polreich4dedbcf2019-04-09 09:28:46 +020018gerritBranch = env.GERRIT_BRANCH
azvyagintsevc2628ed2018-08-16 11:24:10 +030019def 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
Martin Polreich3ce6d3e2019-02-19 15:18:36 +010023envOverrides = []
chnyda85ea22d2017-10-23 13:05:16 +020024futureFormulas = []
25failedFormulas = []
Martin Polreich5be29e32019-02-26 11:11:51 +010026kitchenFileName = ''
chnyda85ea22d2017-10-23 13:05:16 +020027
28def setupRunner(defaultGitRef, defaultGitUrl) {
29 def branches = [:]
30 for (int i = 0; i < PARALLEL_GROUP_SIZE.toInteger() && i < futureFormulas.size(); i++) {
31 branches["Runner ${i}"] = {
32 while (futureFormulas && !failedFormulas) {
33 def currentFormula = futureFormulas[0] ? futureFormulas[0] : null
34 if (!currentFormula) {
35 continue
36 }
37 futureFormulas.remove(currentFormula)
38 try {
39 triggerTestFormulaJob(currentFormula, defaultGitRef, defaultGitUrl)
40 } catch (Exception e) {
chnyda0513b572018-01-23 13:40:57 +010041 if (e.getMessage().contains("completed with status ABORTED")) {
42 common.warningMsg("Test of ${currentFormula} was aborted and will be retriggered")
43 futureFormulas << currentFormula
44 } else {
45 failedFormulas << currentFormula
46 common.warningMsg("Test of ${currentFormula} failed : ${e}")
47 }
chnyda85ea22d2017-10-23 13:05:16 +020048 }
49 }
50 }
51 }
52 parallel branches
53}
54
55def triggerTestFormulaJob(testEnv, defaultGitRef, defaultGitUrl) {
56 common.infoMsg("Test of ${testEnv} starts")
57 build job: "test-salt-formulas-env", parameters: [
58 [$class: 'StringParameterValue', name: 'CREDENTIALS_ID', value: CREDENTIALS_ID],
59 [$class: 'StringParameterValue', name: 'KITCHEN_ENV', value: testEnv],
60 [$class: 'StringParameterValue', name: 'DEFAULT_GIT_REF', value: defaultGitRef],
61 [$class: 'StringParameterValue', name: 'DEFAULT_GIT_URL', value: defaultGitUrl],
62 [$class: 'StringParameterValue', name: 'SALT_OPTS', value: SALT_OPTS],
Martin Polreichb3708cf2019-04-08 15:39:21 +020063 [$class: 'StringParameterValue', name: 'SALT_VERSION', value: SALT_VERSION],
Martin Polreich4dedbcf2019-04-09 09:28:46 +020064 [$class: 'StringParameterValue', name: 'GERRIT_PARENT_BRANCH', value: gerritBranch]
chnyda85ea22d2017-10-23 13:05:16 +020065 ]
66}
Martin Polreichc0995dc2018-11-05 14:40:02 +010067
Martin Polreich15ef9f42018-12-17 14:29:54 +010068timeout(time: 4, unit: 'HOURS') {
azvyagintsevc2628ed2018-08-16 11:24:10 +030069 node(slaveNode) {
azvyagintsev54b49c62018-12-27 17:38:56 +020070 def img = dockerLib.getImage(env.SMOKE_TEST_DOCKER_IMG, "ubuntu:16.04")
Jakub Josefa63f9862018-01-11 17:58:38 +010071 try {
azvyagintsevc2628ed2018-08-16 11:24:10 +030072 if (fileExists("tests/build")) {
73 common.infoMsg('Cleaning test env')
74 sh ("sudo rm -rf tests/build")
75 }
Jakub Josefa63f9862018-01-11 17:58:38 +010076 stage("checkout") {
77 if (gerritRef) {
78 // job is triggered by Gerrit
79 def gerritChange = gerrit.getGerritChange(GERRIT_NAME, GERRIT_HOST, GERRIT_CHANGE_NUMBER, CREDENTIALS_ID, true)
80 // test if gerrit change is already Verified
81 if (gerrit.patchsetHasApproval(gerritChange.currentPatchSet, "Verified", "+")) {
82 common.successMsg("Gerrit change ${GERRIT_CHANGE_NUMBER} patchset ${GERRIT_PATCHSET_NUMBER} already has Verified, skipping tests") // do nothing
83 // test WIP contains in commit message
84 } else if (gerritChange.commitMessage.contains("WIP")) {
85 common.successMsg("Commit message contains WIP, skipping tests") // do nothing
Martin Polreicha3e30122017-08-22 12:43:55 +020086 } else {
Jakub Josefa63f9862018-01-11 17:58:38 +010087 // test if change aren't already merged
88 def merged = gerritChange.status == "MERGED"
89 if (!merged) {
90 checkouted = gerrit.gerritPatchsetCheckout([
91 credentialsId: CREDENTIALS_ID
92 ])
93 } else {
94 common.successMsg("Change ${GERRIT_CHANGE_NUMBER} is already merged, no need to test them")
95 }
Jakub Josefbcd2e902017-06-13 14:40:41 +020096 }
Jakub Josefa63f9862018-01-11 17:58:38 +010097 defaultGitUrl = "${GERRIT_SCHEME}://${GERRIT_NAME}@${GERRIT_HOST}:${GERRIT_PORT}/${GERRIT_PROJECT}"
98 defaultGitRef = GERRIT_REFSPEC
99 } else if (defaultGitRef && defaultGitUrl) {
100 checkouted = gerrit.gerritPatchsetCheckout(defaultGitUrl, defaultGitRef, "HEAD", CREDENTIALS_ID)
101 } else {
102 throw new Exception("Cannot checkout gerrit patchset, GERRIT_REFSPEC and DEFAULT_GIT_REF is null")
Jakub Josef740a7882017-06-01 14:54:01 +0200103 }
Jakub Josefc5a223a2017-03-01 14:40:08 +0100104 }
azvyagintsevc2628ed2018-08-16 11:24:10 +0300105 stage("test") {
106 if (checkouted) {
107 try {
108 // TODO add try\finally for image-stuck case. (copy-paste from SaltModelTesting)
109 withEnv(["SALT_VERSION=${saltVersion}"]) {
110 img.inside("-v ${env.WORKSPACE}/:/formula/ -u root:root --cpus=4 --ulimit nofile=4096:8192") {
111 sh('''#!/bin/bash -xe
112 cd /etc/apt/
azvyagintsev1a494b52018-12-20 14:48:46 +0200113 echo "deb [arch=amd64] http://mirror.mirantis.com/nightly/ubuntu xenial main restricted universe" > sources.list
114 echo "deb [arch=amd64] http://mirror.mirantis.com/nightly/ubuntu xenial-updates main restricted universe" >> sources.list
azvyagintsevc2628ed2018-08-16 11:24:10 +0300115 echo 'Acquire::Languages "none";' > apt.conf.d/docker-no-languages
116 echo 'Acquire::GzipIndexes "true"; Acquire::CompressionTypes::Order:: "gz";' > apt.conf.d/docker-gzip-indexes
117 echo 'APT::Get::Install-Recommends "false"; APT::Get::Install-Suggests "false";' > apt.conf.d/docker-recommends
118 apt-get update
119 apt-get install -y git-core wget curl apt-transport-https
120 apt-get install -y python-pip python3-pip python-virtualenv python3-virtualenv python-yaml autoconf build-essential
121 cd /formula/
122 make clean
123 make test
124 make clean
125 ''')
126 }
Sergey Otpuschennikove5a14742018-05-08 11:42:26 +0400127 }
azvyagintsevc2628ed2018-08-16 11:24:10 +0300128 }
129 finally {
130 if (fileExists("tests/build")) {
131 common.infoMsg('Cleaning test env')
132 sh ("sudo rm -rf tests/build")
133 }
Sergey Otpuschennikove5a14742018-05-08 11:42:26 +0400134 }
azvyagintsev3a2e0352018-01-10 12:41:29 +0200135 }
azvyagintsevc2628ed2018-08-16 11:24:10 +0300136
Jakub Josefc5a223a2017-03-01 14:40:08 +0100137 }
Jakub Joseff64c6392017-05-03 13:18:25 +0200138 stage("kitchen") {
Martin Polreich32b82222018-11-21 14:01:39 +0100139 if (fileExists(".travis.yml")) {/** TODO: Remove this legacy block once formulas are switched to new configuration */
Jakub Josefa63f9862018-01-11 17:58:38 +0100140 if (checkouted) {
Martin Polreichc0995dc2018-11-05 14:40:02 +0100141 if (fileExists(".kitchen.yml") || fileExists(".kitchen.openstack.yml")) {
142 if (fileExists(".kitchen.openstack.yml")) {
143 common.infoMsg("Openstack Kitchen test configuration found, running Openstack kitchen tests.")
144 if (fileExists(".kitchen.yml")) {
145 common.infoMsg("Ignoring the docker Kitchen test configuration file.")
146 }
147 } else {
148 common.infoMsg("Docker Kitchen test configuration found, running Docker kitchen tests.")
149 }
Jakub Josefa63f9862018-01-11 17:58:38 +0100150 def kitchenEnvs = []
151 def filteredEnvs = []
152 if (fileExists(".travis.yml")) {
153 common.infoMsg(".travis.yml file found.")
154 def kitchenConfigYML = readYaml(file: ".travis.yml")
155 if (kitchenConfigYML.containsKey("env")) {
156 kitchenEnvs = kitchenConfigYML["env"]
157 }
158 } else {
159 common.warningMsg(".travis.yml file not found, suites must be passed via CUSTOM_KITCHEN_ENVS parameter.")
Ruslan Kamaldinov310ffc02017-08-08 16:07:42 +0400160 }
Jakub Josefa63f9862018-01-11 17:58:38 +0100161 common.infoMsg("Running kitchen testing in parallel mode")
162 if (CUSTOM_KITCHEN_ENVS != null && CUSTOM_KITCHEN_ENVS != '') {
163 kitchenEnvs = CUSTOM_KITCHEN_ENVS.tokenize('\n')
164 common.infoMsg("CUSTOM_KITCHEN_ENVS not empty. Running with custom enviroments: ${kitchenEnvs}")
Martin Polreicha3e30122017-08-22 12:43:55 +0200165 }
Jakub Josefa63f9862018-01-11 17:58:38 +0100166 if (kitchenEnvs != null && kitchenEnvs != '') {
167 def acc = 0
168 common.infoMsg("Found " + kitchenEnvs.size() + " environment(s)")
169 for (int i = 0; i < kitchenEnvs.size(); i++) {
170 futureFormulas << kitchenEnvs[i]
171 }
172 setupRunner(defaultGitRef, defaultGitUrl)
173 } else {
174 common.warningMsg(".kitchen.yml file not found, no kitchen tests triggered.")
175 }
Martin Polreicha3e30122017-08-22 12:43:55 +0200176 }
Martin Polreich32b82222018-11-21 14:01:39 +0100177 }/** TODO: End of block for removal */
178 } else {
179 if (checkouted) {
Martin Polreich5be29e32019-02-26 11:11:51 +0100180 if (fileExists(".kitchen.openstack.yml")) {
181 common.infoMsg("Openstack Kitchen test configuration found, running Openstack kitchen tests.")
182 kitchenFileName = ".kitchen.openstack.yml"
183 envOverrides.add("KITCHEN_YAML=${kitchenFileName}")
Martin Polreich73c8e512019-02-26 15:16:33 +0100184 rubyVersion = '2.5.0'
Martin Polreich5be29e32019-02-26 11:11:51 +0100185 } else if (fileExists(".kitchen.yml")) {
186 common.infoMsg("Docker Kitchen test configuration found, running Docker kitchen tests.")
187 kitchenFileName = ".kitchen.yml"
Martin Polreich73c8e512019-02-26 15:16:33 +0100188 rubyVersion = '2.4.1'
Martin Polreich5be29e32019-02-26 11:11:51 +0100189 }
190 if (kitchenFileName) {
Martin Polreich32b82222018-11-21 14:01:39 +0100191 def kitchenEnvs = []
Martin Polreich5be29e32019-02-26 11:11:51 +0100192 ruby.ensureRubyEnv(rubyVersion)
Martin Polreich32b82222018-11-21 14:01:39 +0100193 if (!fileExists("Gemfile")) {
194 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'")
195 ruby.installKitchen()
196 } else {
197 common.infoMsg("Override Gemfile found in the kitchen directory, using it.")
198 ruby.installKitchen()
199 }
Martin Polreich3ce6d3e2019-02-19 15:18:36 +0100200 common.infoMsg = ruby.runKitchenCommand("list -b", envOverrides.join(' '))
201 kitchenEnvs = ruby.runKitchenCommand("list -b", envOverrides.join(' ')).split()
Martin Polreich32b82222018-11-21 14:01:39 +0100202 common.infoMsg(kitchenEnvs)
203 common.infoMsg("Running kitchen testing in parallel mode")
204 if (CUSTOM_KITCHEN_ENVS != null && CUSTOM_KITCHEN_ENVS != '') {
205 kitchenEnvs = CUSTOM_KITCHEN_ENVS.tokenize('\n')
206 common.infoMsg("CUSTOM_KITCHEN_ENVS not empty. Running with custom enviroments: ${kitchenEnvs}")
207 }
208 if (kitchenEnvs != null && kitchenEnvs != '') {
209 def acc = 0
210 common.infoMsg("Found " + kitchenEnvs.size() + " environment(s)")
211 for (int i = 0; i < kitchenEnvs.size(); i++) {
212 futureFormulas << kitchenEnvs[i]
213 }
214 setupRunner(defaultGitRef, defaultGitUrl)
215 } else {
Martin Polreich5be29e32019-02-26 11:11:51 +0100216 common.errorMsg("No enviroments defined in the Kitchen file: ${kitchenFileName}")
217 }
218 } else {
Martin Polreich32b82222018-11-21 14:01:39 +0100219 common.warningMsg(".kitchen.yml nor .kitchen.openstack.yml file not found, no kitchen tests triggered.")
220 }
Martin Polreich32b82222018-11-21 14:01:39 +0100221 }
Jakub Joseff64c6392017-05-03 13:18:25 +0200222 }
Jakub Joseff64c6392017-05-03 13:18:25 +0200223 }
Jakub Josefa63f9862018-01-11 17:58:38 +0100224 if (failedFormulas) {
225 currentBuild.result = "FAILURE"
226 common.warningMsg("The following tests failed: ${failedFormulas}")
227 }
228 } catch (Throwable e) {
229 // If there was an error or exception thrown, the build failed
chnyda85ea22d2017-10-23 13:05:16 +0200230 currentBuild.result = "FAILURE"
Jakub Josefa63f9862018-01-11 17:58:38 +0100231 currentBuild.description = currentBuild.description ? e.message + " " + currentBuild.description : e.message
232 throw e
233 } finally {
234 if (currentBuild.result == "FAILURE" && fileExists(".kitchen/logs/kitchen.log")) {
235 common.errorMsg("----------------KITCHEN LOG:---------------")
236 println readFile(".kitchen/logs/kitchen.log")
237 }
chnyda85ea22d2017-10-23 13:05:16 +0200238 }
Jakub Josefc5a223a2017-03-01 14:40:08 +0100239 }
azvyagintsev3a2e0352018-01-10 12:41:29 +0200240}