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() |
Jakub Josef | 8337931 | 2017-03-29 18:12:34 +0200 | [diff] [blame] | 12 | |
azvyagintsev | c2628ed | 2018-08-16 11:24:10 +0300 | [diff] [blame] | 13 | def gerritRef = env.GERRIT_REFSPEC ?: null |
| 14 | def defaultGitRef = env.DEFAULT_GIT_REF ?: null |
| 15 | def defaultGitUrl = env.DEFAULT_GIT_URL ?: null |
Aleksey Zvyagintsev | 4a80421 | 2019-02-07 13:02:31 +0000 | [diff] [blame] | 16 | def slaveNode = env.SLAVE_NODE ?: 'virtual' |
azvyagintsev | c2628ed | 2018-08-16 11:24:10 +0300 | [diff] [blame] | 17 | def saltVersion = env.SALT_VERSION ?: "" |
Martin Polreich | 4dedbcf | 2019-04-09 09:28:46 +0200 | [diff] [blame] | 18 | gerritBranch = env.GERRIT_BRANCH |
azvyagintsev | c2628ed | 2018-08-16 11:24:10 +0300 | [diff] [blame] | 19 | def dockerLib = new com.mirantis.mk.Docker() |
Jakub Josef | 8337931 | 2017-03-29 18:12:34 +0200 | [diff] [blame] | 20 | |
Jakub Josef | bcd2e90 | 2017-06-13 14:40:41 +0200 | [diff] [blame] | 21 | def checkouted = false |
Jakub Josef | 8337931 | 2017-03-29 18:12:34 +0200 | [diff] [blame] | 22 | |
Martin Polreich | 3ce6d3e | 2019-02-19 15:18:36 +0100 | [diff] [blame] | 23 | envOverrides = [] |
chnyda | 85ea22d | 2017-10-23 13:05:16 +0200 | [diff] [blame] | 24 | futureFormulas = [] |
| 25 | failedFormulas = [] |
Martin Polreich | 5be29e3 | 2019-02-26 11:11:51 +0100 | [diff] [blame] | 26 | kitchenFileName = '' |
chnyda | 85ea22d | 2017-10-23 13:05:16 +0200 | [diff] [blame] | 27 | |
| 28 | def 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) { |
chnyda | 0513b57 | 2018-01-23 13:40:57 +0100 | [diff] [blame] | 41 | 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 | } |
chnyda | 85ea22d | 2017-10-23 13:05:16 +0200 | [diff] [blame] | 48 | } |
| 49 | } |
| 50 | } |
| 51 | } |
| 52 | parallel branches |
| 53 | } |
| 54 | |
| 55 | def 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 Polreich | b3708cf | 2019-04-08 15:39:21 +0200 | [diff] [blame] | 63 | [$class: 'StringParameterValue', name: 'SALT_VERSION', value: SALT_VERSION], |
Martin Polreich | 4dedbcf | 2019-04-09 09:28:46 +0200 | [diff] [blame] | 64 | [$class: 'StringParameterValue', name: 'GERRIT_PARENT_BRANCH', value: gerritBranch] |
chnyda | 85ea22d | 2017-10-23 13:05:16 +0200 | [diff] [blame] | 65 | ] |
| 66 | } |
Martin Polreich | c0995dc | 2018-11-05 14:40:02 +0100 | [diff] [blame] | 67 | |
Martin Polreich | 15ef9f4 | 2018-12-17 14:29:54 +0100 | [diff] [blame] | 68 | timeout(time: 4, unit: 'HOURS') { |
azvyagintsev | c2628ed | 2018-08-16 11:24:10 +0300 | [diff] [blame] | 69 | node(slaveNode) { |
azvyagintsev | 54b49c6 | 2018-12-27 17:38:56 +0200 | [diff] [blame] | 70 | def img = dockerLib.getImage(env.SMOKE_TEST_DOCKER_IMG, "ubuntu:16.04") |
Jakub Josef | a63f986 | 2018-01-11 17:58:38 +0100 | [diff] [blame] | 71 | try { |
azvyagintsev | c2628ed | 2018-08-16 11:24:10 +0300 | [diff] [blame] | 72 | if (fileExists("tests/build")) { |
| 73 | common.infoMsg('Cleaning test env') |
| 74 | sh ("sudo rm -rf tests/build") |
| 75 | } |
Jakub Josef | a63f986 | 2018-01-11 17:58:38 +0100 | [diff] [blame] | 76 | 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 Polreich | a3e3012 | 2017-08-22 12:43:55 +0200 | [diff] [blame] | 86 | } else { |
Jakub Josef | a63f986 | 2018-01-11 17:58:38 +0100 | [diff] [blame] | 87 | // 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 Josef | bcd2e90 | 2017-06-13 14:40:41 +0200 | [diff] [blame] | 96 | } |
Jakub Josef | a63f986 | 2018-01-11 17:58:38 +0100 | [diff] [blame] | 97 | 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 Josef | 740a788 | 2017-06-01 14:54:01 +0200 | [diff] [blame] | 103 | } |
Jakub Josef | c5a223a | 2017-03-01 14:40:08 +0100 | [diff] [blame] | 104 | } |
azvyagintsev | c2628ed | 2018-08-16 11:24:10 +0300 | [diff] [blame] | 105 | 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/ |
azvyagintsev | 1a494b5 | 2018-12-20 14:48:46 +0200 | [diff] [blame] | 113 | 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 |
azvyagintsev | c2628ed | 2018-08-16 11:24:10 +0300 | [diff] [blame] | 115 | 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 Otpuschennikov | e5a1474 | 2018-05-08 11:42:26 +0400 | [diff] [blame] | 127 | } |
azvyagintsev | c2628ed | 2018-08-16 11:24:10 +0300 | [diff] [blame] | 128 | } |
| 129 | finally { |
| 130 | if (fileExists("tests/build")) { |
| 131 | common.infoMsg('Cleaning test env') |
| 132 | sh ("sudo rm -rf tests/build") |
| 133 | } |
Sergey Otpuschennikov | e5a1474 | 2018-05-08 11:42:26 +0400 | [diff] [blame] | 134 | } |
azvyagintsev | 3a2e035 | 2018-01-10 12:41:29 +0200 | [diff] [blame] | 135 | } |
azvyagintsev | c2628ed | 2018-08-16 11:24:10 +0300 | [diff] [blame] | 136 | |
Jakub Josef | c5a223a | 2017-03-01 14:40:08 +0100 | [diff] [blame] | 137 | } |
Jakub Josef | f64c639 | 2017-05-03 13:18:25 +0200 | [diff] [blame] | 138 | stage("kitchen") { |
Martin Polreich | 32b8222 | 2018-11-21 14:01:39 +0100 | [diff] [blame] | 139 | 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] | 140 | if (checkouted) { |
Martin Polreich | c0995dc | 2018-11-05 14:40:02 +0100 | [diff] [blame] | 141 | 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 Josef | a63f986 | 2018-01-11 17:58:38 +0100 | [diff] [blame] | 150 | 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 Kamaldinov | 310ffc0 | 2017-08-08 16:07:42 +0400 | [diff] [blame] | 160 | } |
Jakub Josef | a63f986 | 2018-01-11 17:58:38 +0100 | [diff] [blame] | 161 | 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 Polreich | a3e3012 | 2017-08-22 12:43:55 +0200 | [diff] [blame] | 165 | } |
Jakub Josef | a63f986 | 2018-01-11 17:58:38 +0100 | [diff] [blame] | 166 | 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 Polreich | a3e3012 | 2017-08-22 12:43:55 +0200 | [diff] [blame] | 176 | } |
Martin Polreich | 32b8222 | 2018-11-21 14:01:39 +0100 | [diff] [blame] | 177 | }/** TODO: End of block for removal */ |
| 178 | } else { |
| 179 | if (checkouted) { |
Martin Polreich | 5be29e3 | 2019-02-26 11:11:51 +0100 | [diff] [blame] | 180 | 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 Polreich | 73c8e51 | 2019-02-26 15:16:33 +0100 | [diff] [blame] | 184 | rubyVersion = '2.5.0' |
Martin Polreich | 5be29e3 | 2019-02-26 11:11:51 +0100 | [diff] [blame] | 185 | } else if (fileExists(".kitchen.yml")) { |
| 186 | common.infoMsg("Docker Kitchen test configuration found, running Docker kitchen tests.") |
| 187 | kitchenFileName = ".kitchen.yml" |
Martin Polreich | 73c8e51 | 2019-02-26 15:16:33 +0100 | [diff] [blame] | 188 | rubyVersion = '2.4.1' |
Martin Polreich | 5be29e3 | 2019-02-26 11:11:51 +0100 | [diff] [blame] | 189 | } |
| 190 | if (kitchenFileName) { |
Martin Polreich | 32b8222 | 2018-11-21 14:01:39 +0100 | [diff] [blame] | 191 | def kitchenEnvs = [] |
Martin Polreich | 5be29e3 | 2019-02-26 11:11:51 +0100 | [diff] [blame] | 192 | ruby.ensureRubyEnv(rubyVersion) |
Martin Polreich | 32b8222 | 2018-11-21 14:01:39 +0100 | [diff] [blame] | 193 | 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 Polreich | 3ce6d3e | 2019-02-19 15:18:36 +0100 | [diff] [blame] | 200 | common.infoMsg = ruby.runKitchenCommand("list -b", envOverrides.join(' ')) |
| 201 | kitchenEnvs = ruby.runKitchenCommand("list -b", envOverrides.join(' ')).split() |
Martin Polreich | 32b8222 | 2018-11-21 14:01:39 +0100 | [diff] [blame] | 202 | 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 Polreich | 5be29e3 | 2019-02-26 11:11:51 +0100 | [diff] [blame] | 216 | common.errorMsg("No enviroments defined in the Kitchen file: ${kitchenFileName}") |
| 217 | } |
| 218 | } else { |
Martin Polreich | 32b8222 | 2018-11-21 14:01:39 +0100 | [diff] [blame] | 219 | common.warningMsg(".kitchen.yml nor .kitchen.openstack.yml file not found, no kitchen tests triggered.") |
| 220 | } |
Martin Polreich | 32b8222 | 2018-11-21 14:01:39 +0100 | [diff] [blame] | 221 | } |
Jakub Josef | f64c639 | 2017-05-03 13:18:25 +0200 | [diff] [blame] | 222 | } |
Jakub Josef | f64c639 | 2017-05-03 13:18:25 +0200 | [diff] [blame] | 223 | } |
Jakub Josef | a63f986 | 2018-01-11 17:58:38 +0100 | [diff] [blame] | 224 | 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 |
chnyda | 85ea22d | 2017-10-23 13:05:16 +0200 | [diff] [blame] | 230 | currentBuild.result = "FAILURE" |
Jakub Josef | a63f986 | 2018-01-11 17:58:38 +0100 | [diff] [blame] | 231 | 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 | } |
chnyda | 85ea22d | 2017-10-23 13:05:16 +0200 | [diff] [blame] | 238 | } |
Jakub Josef | c5a223a | 2017-03-01 14:40:08 +0100 | [diff] [blame] | 239 | } |
azvyagintsev | 3a2e035 | 2018-01-10 12:41:29 +0200 | [diff] [blame] | 240 | } |