Jakub Josef | 8337931 | 2017-03-29 18:12:34 +0200 | [diff] [blame] | 1 | /** |
| 2 | * Test salt formulas pipeline |
| 3 | * DEFAULT_GIT_REF |
| 4 | * DEFAULT_GIT_URL |
| 5 | * CREDENTIALS_ID |
Cedric Hnyda | c87fc6c | 2017-09-22 13:12:36 +0000 | [diff] [blame] | 6 | * KITCHEN_TESTS_PARALLEL |
Sergey Otpuschennikov | e5a1474 | 2018-05-08 11:42:26 +0400 | [diff] [blame] | 7 | * SMOKE_TEST_DOCKER_IMG Docker image for run test (default "ubuntu:16.04") |
Jakub Josef | 8337931 | 2017-03-29 18:12:34 +0200 | [diff] [blame] | 8 | */ |
chnyda | 85ea22d | 2017-10-23 13:05:16 +0200 | [diff] [blame] | 9 | common = new com.mirantis.mk.Common() |
Jakub Josef | c5a223a | 2017-03-01 14:40:08 +0100 | [diff] [blame] | 10 | def gerrit = new com.mirantis.mk.Gerrit() |
Jakub Josef | f64c639 | 2017-05-03 13:18:25 +0200 | [diff] [blame] | 11 | def ruby = new com.mirantis.mk.Ruby() |
Martin Polreich | 4e682fc | 2019-04-09 16:01:38 +0200 | [diff] [blame] | 12 | def dockerLib = new com.mirantis.mk.Docker() |
Jakub Josef | 8337931 | 2017-03-29 18:12:34 +0200 | [diff] [blame] | 13 | |
azvyagintsev | c2628ed | 2018-08-16 11:24:10 +0300 | [diff] [blame] | 14 | def gerritRef = env.GERRIT_REFSPEC ?: null |
| 15 | def defaultGitRef = env.DEFAULT_GIT_REF ?: null |
| 16 | def defaultGitUrl = env.DEFAULT_GIT_URL ?: null |
Vladimir Khlyunev | 8407afd | 2020-06-15 15:42:23 +0400 | [diff] [blame] | 17 | // vkhlyunev: New slaves breaks old jobs, sticking to ols slaves until fixed |
| 18 | //def slaveNode = env.SLAVE_NODE ?: 'virtual' |
| 19 | def slaveNode = env.SLAVE_NODE ?: 'old16.04' |
azvyagintsev | c2628ed | 2018-08-16 11:24:10 +0300 | [diff] [blame] | 20 | def saltVersion = env.SALT_VERSION ?: "" |
Martin Polreich | 4e682fc | 2019-04-09 16:01:38 +0200 | [diff] [blame] | 21 | |
| 22 | gerritBranch = 'master' |
| 23 | if (common.validInputParam('GERRIT_BRANCH')) { |
| 24 | gerritBranch = env.GERRIT_BRANCH |
Martin Polreich | c8a6a38 | 2019-04-10 09:14:22 +0200 | [diff] [blame] | 25 | } else if (common.validInputParam('GATING_GERRIT_BRANCH')) { |
| 26 | gerritBranch = env.GATING_GERRIT_BRANCH |
| 27 | } |
Martin Polreich | 4e682fc | 2019-04-09 16:01:38 +0200 | [diff] [blame] | 28 | |
Jakub Josef | 8337931 | 2017-03-29 18:12:34 +0200 | [diff] [blame] | 29 | |
Jakub Josef | bcd2e90 | 2017-06-13 14:40:41 +0200 | [diff] [blame] | 30 | def checkouted = false |
Jakub Josef | 8337931 | 2017-03-29 18:12:34 +0200 | [diff] [blame] | 31 | |
Martin Polreich | 3ce6d3e | 2019-02-19 15:18:36 +0100 | [diff] [blame] | 32 | envOverrides = [] |
chnyda | 85ea22d | 2017-10-23 13:05:16 +0200 | [diff] [blame] | 33 | futureFormulas = [] |
| 34 | failedFormulas = [] |
Martin Polreich | 5be29e3 | 2019-02-26 11:11:51 +0100 | [diff] [blame] | 35 | kitchenFileName = '' |
chnyda | 85ea22d | 2017-10-23 13:05:16 +0200 | [diff] [blame] | 36 | |
| 37 | def 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) { |
chnyda | 0513b57 | 2018-01-23 13:40:57 +0100 | [diff] [blame] | 50 | 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 | } |
chnyda | 85ea22d | 2017-10-23 13:05:16 +0200 | [diff] [blame] | 57 | } |
| 58 | } |
| 59 | } |
| 60 | } |
| 61 | parallel branches |
| 62 | } |
| 63 | |
| 64 | def 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 Polreich | b3708cf | 2019-04-08 15:39:21 +0200 | [diff] [blame] | 72 | [$class: 'StringParameterValue', name: 'SALT_VERSION', value: SALT_VERSION], |
Martin Polreich | 4dedbcf | 2019-04-09 09:28:46 +0200 | [diff] [blame] | 73 | [$class: 'StringParameterValue', name: 'GERRIT_PARENT_BRANCH', value: gerritBranch] |
chnyda | 85ea22d | 2017-10-23 13:05:16 +0200 | [diff] [blame] | 74 | ] |
| 75 | } |
Martin Polreich | c0995dc | 2018-11-05 14:40:02 +0100 | [diff] [blame] | 76 | |
Martin Polreich | 15ef9f4 | 2018-12-17 14:29:54 +0100 | [diff] [blame] | 77 | timeout(time: 4, unit: 'HOURS') { |
azvyagintsev | c2628ed | 2018-08-16 11:24:10 +0300 | [diff] [blame] | 78 | node(slaveNode) { |
azvyagintsev | 54b49c6 | 2018-12-27 17:38:56 +0200 | [diff] [blame] | 79 | def img = dockerLib.getImage(env.SMOKE_TEST_DOCKER_IMG, "ubuntu:16.04") |
Jakub Josef | a63f986 | 2018-01-11 17:58:38 +0100 | [diff] [blame] | 80 | try { |
azvyagintsev | c2628ed | 2018-08-16 11:24:10 +0300 | [diff] [blame] | 81 | if (fileExists("tests/build")) { |
| 82 | common.infoMsg('Cleaning test env') |
| 83 | sh ("sudo rm -rf tests/build") |
| 84 | } |
Jakub Josef | a63f986 | 2018-01-11 17:58:38 +0100 | [diff] [blame] | 85 | 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 Polreich | a3e3012 | 2017-08-22 12:43:55 +0200 | [diff] [blame] | 95 | } else { |
Jakub Josef | a63f986 | 2018-01-11 17:58:38 +0100 | [diff] [blame] | 96 | // 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 Josef | bcd2e90 | 2017-06-13 14:40:41 +0200 | [diff] [blame] | 105 | } |
Jakub Josef | a63f986 | 2018-01-11 17:58:38 +0100 | [diff] [blame] | 106 | 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 Josef | 740a788 | 2017-06-01 14:54:01 +0200 | [diff] [blame] | 112 | } |
Jakub Josef | c5a223a | 2017-03-01 14:40:08 +0100 | [diff] [blame] | 113 | } |
azvyagintsev | c2628ed | 2018-08-16 11:24:10 +0300 | [diff] [blame] | 114 | 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/ |
azvyagintsev | 1a494b5 | 2018-12-20 14:48:46 +0200 | [diff] [blame] | 122 | 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 |
azvyagintsev | c2628ed | 2018-08-16 11:24:10 +0300 | [diff] [blame] | 124 | 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 Otpuschennikov | e5a1474 | 2018-05-08 11:42:26 +0400 | [diff] [blame] | 136 | } |
azvyagintsev | c2628ed | 2018-08-16 11:24:10 +0300 | [diff] [blame] | 137 | } |
| 138 | finally { |
| 139 | if (fileExists("tests/build")) { |
| 140 | common.infoMsg('Cleaning test env') |
| 141 | sh ("sudo rm -rf tests/build") |
| 142 | } |
Sergey Otpuschennikov | e5a1474 | 2018-05-08 11:42:26 +0400 | [diff] [blame] | 143 | } |
azvyagintsev | 3a2e035 | 2018-01-10 12:41:29 +0200 | [diff] [blame] | 144 | } |
azvyagintsev | c2628ed | 2018-08-16 11:24:10 +0300 | [diff] [blame] | 145 | |
Jakub Josef | c5a223a | 2017-03-01 14:40:08 +0100 | [diff] [blame] | 146 | } |
Jakub Josef | f64c639 | 2017-05-03 13:18:25 +0200 | [diff] [blame] | 147 | stage("kitchen") { |
Martin Polreich | 32b8222 | 2018-11-21 14:01:39 +0100 | [diff] [blame] | 148 | if (fileExists(".travis.yml")) {/** TODO: Remove this legacy block once formulas are switched to new configuration */ |
Jakub Josef | a63f986 | 2018-01-11 17:58:38 +0100 | [diff] [blame] | 149 | if (checkouted) { |
Martin Polreich | c0995dc | 2018-11-05 14:40:02 +0100 | [diff] [blame] | 150 | 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 Josef | a63f986 | 2018-01-11 17:58:38 +0100 | [diff] [blame] | 159 | 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 Kamaldinov | 310ffc0 | 2017-08-08 16:07:42 +0400 | [diff] [blame] | 169 | } |
Jakub Josef | a63f986 | 2018-01-11 17:58:38 +0100 | [diff] [blame] | 170 | 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 Polreich | a3e3012 | 2017-08-22 12:43:55 +0200 | [diff] [blame] | 174 | } |
Jakub Josef | a63f986 | 2018-01-11 17:58:38 +0100 | [diff] [blame] | 175 | 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 Polreich | a3e3012 | 2017-08-22 12:43:55 +0200 | [diff] [blame] | 185 | } |
Martin Polreich | 32b8222 | 2018-11-21 14:01:39 +0100 | [diff] [blame] | 186 | }/** TODO: End of block for removal */ |
| 187 | } else { |
| 188 | if (checkouted) { |
Martin Polreich | 5be29e3 | 2019-02-26 11:11:51 +0100 | [diff] [blame] | 189 | 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 Polreich | 73c8e51 | 2019-02-26 15:16:33 +0100 | [diff] [blame] | 193 | rubyVersion = '2.5.0' |
Martin Polreich | 5be29e3 | 2019-02-26 11:11:51 +0100 | [diff] [blame] | 194 | } else if (fileExists(".kitchen.yml")) { |
| 195 | common.infoMsg("Docker Kitchen test configuration found, running Docker kitchen tests.") |
| 196 | kitchenFileName = ".kitchen.yml" |
Martin Polreich | 73c8e51 | 2019-02-26 15:16:33 +0100 | [diff] [blame] | 197 | rubyVersion = '2.4.1' |
Martin Polreich | 5be29e3 | 2019-02-26 11:11:51 +0100 | [diff] [blame] | 198 | } |
| 199 | if (kitchenFileName) { |
Martin Polreich | 32b8222 | 2018-11-21 14:01:39 +0100 | [diff] [blame] | 200 | def kitchenEnvs = [] |
Martin Polreich | 5be29e3 | 2019-02-26 11:11:51 +0100 | [diff] [blame] | 201 | ruby.ensureRubyEnv(rubyVersion) |
Martin Polreich | 32b8222 | 2018-11-21 14:01:39 +0100 | [diff] [blame] | 202 | 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 Polreich | 3ce6d3e | 2019-02-19 15:18:36 +0100 | [diff] [blame] | 209 | common.infoMsg = ruby.runKitchenCommand("list -b", envOverrides.join(' ')) |
| 210 | kitchenEnvs = ruby.runKitchenCommand("list -b", envOverrides.join(' ')).split() |
Martin Polreich | 32b8222 | 2018-11-21 14:01:39 +0100 | [diff] [blame] | 211 | 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 Polreich | 5be29e3 | 2019-02-26 11:11:51 +0100 | [diff] [blame] | 225 | common.errorMsg("No enviroments defined in the Kitchen file: ${kitchenFileName}") |
| 226 | } |
| 227 | } else { |
Martin Polreich | 32b8222 | 2018-11-21 14:01:39 +0100 | [diff] [blame] | 228 | common.warningMsg(".kitchen.yml nor .kitchen.openstack.yml file not found, no kitchen tests triggered.") |
| 229 | } |
Martin Polreich | 32b8222 | 2018-11-21 14:01:39 +0100 | [diff] [blame] | 230 | } |
Jakub Josef | f64c639 | 2017-05-03 13:18:25 +0200 | [diff] [blame] | 231 | } |
Jakub Josef | f64c639 | 2017-05-03 13:18:25 +0200 | [diff] [blame] | 232 | } |
Jakub Josef | a63f986 | 2018-01-11 17:58:38 +0100 | [diff] [blame] | 233 | 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 |
chnyda | 85ea22d | 2017-10-23 13:05:16 +0200 | [diff] [blame] | 239 | currentBuild.result = "FAILURE" |
Jakub Josef | a63f986 | 2018-01-11 17:58:38 +0100 | [diff] [blame] | 240 | 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 | } |
chnyda | 85ea22d | 2017-10-23 13:05:16 +0200 | [diff] [blame] | 247 | } |
Jakub Josef | c5a223a | 2017-03-01 14:40:08 +0100 | [diff] [blame] | 248 | } |
azvyagintsev | 3a2e035 | 2018-01-10 12:41:29 +0200 | [diff] [blame] | 249 | } |