| 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 | 81a3c90 | 2021-02-01 18:51:15 +0400 | [diff] [blame] | 17 | def slaveNode = env.SLAVE_NODE ?: 'old16.04' | 
| azvyagintsev | c2628ed | 2018-08-16 11:24:10 +0300 | [diff] [blame] | 18 | def saltVersion = env.SALT_VERSION ?: "" | 
| Martin Polreich | 4e682fc | 2019-04-09 16:01:38 +0200 | [diff] [blame] | 19 |  | 
|  | 20 | gerritBranch = 'master' | 
|  | 21 | if (common.validInputParam('GERRIT_BRANCH')) { | 
|  | 22 | gerritBranch = env.GERRIT_BRANCH | 
| Martin Polreich | c8a6a38 | 2019-04-10 09:14:22 +0200 | [diff] [blame] | 23 | } else if (common.validInputParam('GATING_GERRIT_BRANCH')) { | 
|  | 24 | gerritBranch = env.GATING_GERRIT_BRANCH | 
|  | 25 | } | 
| Martin Polreich | 4e682fc | 2019-04-09 16:01:38 +0200 | [diff] [blame] | 26 |  | 
| Jakub Josef | 8337931 | 2017-03-29 18:12:34 +0200 | [diff] [blame] | 27 |  | 
| Jakub Josef | bcd2e90 | 2017-06-13 14:40:41 +0200 | [diff] [blame] | 28 | def checkouted = false | 
| Jakub Josef | 8337931 | 2017-03-29 18:12:34 +0200 | [diff] [blame] | 29 |  | 
| Martin Polreich | 3ce6d3e | 2019-02-19 15:18:36 +0100 | [diff] [blame] | 30 | envOverrides = [] | 
| chnyda | 85ea22d | 2017-10-23 13:05:16 +0200 | [diff] [blame] | 31 | futureFormulas = [] | 
|  | 32 | failedFormulas = [] | 
| Martin Polreich | 5be29e3 | 2019-02-26 11:11:51 +0100 | [diff] [blame] | 33 | kitchenFileName = '' | 
| chnyda | 85ea22d | 2017-10-23 13:05:16 +0200 | [diff] [blame] | 34 |  | 
|  | 35 | def 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) { | 
| chnyda | 0513b57 | 2018-01-23 13:40:57 +0100 | [diff] [blame] | 48 | 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 | } | 
| chnyda | 85ea22d | 2017-10-23 13:05:16 +0200 | [diff] [blame] | 55 | } | 
|  | 56 | } | 
|  | 57 | } | 
|  | 58 | } | 
|  | 59 | parallel branches | 
|  | 60 | } | 
|  | 61 |  | 
|  | 62 | def 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 Polreich | b3708cf | 2019-04-08 15:39:21 +0200 | [diff] [blame] | 70 | [$class: 'StringParameterValue', name: 'SALT_VERSION', value: SALT_VERSION], | 
| Martin Polreich | 4dedbcf | 2019-04-09 09:28:46 +0200 | [diff] [blame] | 71 | [$class: 'StringParameterValue', name: 'GERRIT_PARENT_BRANCH', value: gerritBranch] | 
| chnyda | 85ea22d | 2017-10-23 13:05:16 +0200 | [diff] [blame] | 72 | ] | 
|  | 73 | } | 
| Martin Polreich | c0995dc | 2018-11-05 14:40:02 +0100 | [diff] [blame] | 74 |  | 
| Martin Polreich | 15ef9f4 | 2018-12-17 14:29:54 +0100 | [diff] [blame] | 75 | timeout(time: 4, unit: 'HOURS') { | 
| azvyagintsev | c2628ed | 2018-08-16 11:24:10 +0300 | [diff] [blame] | 76 | node(slaveNode) { | 
| azvyagintsev | 54b49c6 | 2018-12-27 17:38:56 +0200 | [diff] [blame] | 77 | def img = dockerLib.getImage(env.SMOKE_TEST_DOCKER_IMG, "ubuntu:16.04") | 
| Jakub Josef | a63f986 | 2018-01-11 17:58:38 +0100 | [diff] [blame] | 78 | try { | 
| azvyagintsev | c2628ed | 2018-08-16 11:24:10 +0300 | [diff] [blame] | 79 | if (fileExists("tests/build")) { | 
|  | 80 | common.infoMsg('Cleaning test env') | 
|  | 81 | sh ("sudo rm -rf tests/build") | 
|  | 82 | } | 
| Jakub Josef | a63f986 | 2018-01-11 17:58:38 +0100 | [diff] [blame] | 83 | 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 Polreich | a3e3012 | 2017-08-22 12:43:55 +0200 | [diff] [blame] | 93 | } else { | 
| Jakub Josef | a63f986 | 2018-01-11 17:58:38 +0100 | [diff] [blame] | 94 | // 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 Josef | bcd2e90 | 2017-06-13 14:40:41 +0200 | [diff] [blame] | 103 | } | 
| Jakub Josef | a63f986 | 2018-01-11 17:58:38 +0100 | [diff] [blame] | 104 | 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 Josef | 740a788 | 2017-06-01 14:54:01 +0200 | [diff] [blame] | 110 | } | 
| Jakub Josef | c5a223a | 2017-03-01 14:40:08 +0100 | [diff] [blame] | 111 | } | 
| azvyagintsev | c2628ed | 2018-08-16 11:24:10 +0300 | [diff] [blame] | 112 | 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/ | 
| azvyagintsev | 1a494b5 | 2018-12-20 14:48:46 +0200 | [diff] [blame] | 120 | 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 | 
| azvyagintsev | c2628ed | 2018-08-16 11:24:10 +0300 | [diff] [blame] | 122 | 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 Otpuschennikov | e5a1474 | 2018-05-08 11:42:26 +0400 | [diff] [blame] | 134 | } | 
| azvyagintsev | c2628ed | 2018-08-16 11:24:10 +0300 | [diff] [blame] | 135 | } | 
|  | 136 | finally { | 
|  | 137 | if (fileExists("tests/build")) { | 
|  | 138 | common.infoMsg('Cleaning test env') | 
|  | 139 | sh ("sudo rm -rf tests/build") | 
|  | 140 | } | 
| Sergey Otpuschennikov | e5a1474 | 2018-05-08 11:42:26 +0400 | [diff] [blame] | 141 | } | 
| azvyagintsev | 3a2e035 | 2018-01-10 12:41:29 +0200 | [diff] [blame] | 142 | } | 
| azvyagintsev | c2628ed | 2018-08-16 11:24:10 +0300 | [diff] [blame] | 143 |  | 
| Jakub Josef | c5a223a | 2017-03-01 14:40:08 +0100 | [diff] [blame] | 144 | } | 
| Jakub Josef | f64c639 | 2017-05-03 13:18:25 +0200 | [diff] [blame] | 145 | stage("kitchen") { | 
| Martin Polreich | 32b8222 | 2018-11-21 14:01:39 +0100 | [diff] [blame] | 146 | 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] | 147 | if (checkouted) { | 
| Martin Polreich | c0995dc | 2018-11-05 14:40:02 +0100 | [diff] [blame] | 148 | 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 Josef | a63f986 | 2018-01-11 17:58:38 +0100 | [diff] [blame] | 157 | 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 Kamaldinov | 310ffc0 | 2017-08-08 16:07:42 +0400 | [diff] [blame] | 167 | } | 
| Jakub Josef | a63f986 | 2018-01-11 17:58:38 +0100 | [diff] [blame] | 168 | 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 Polreich | a3e3012 | 2017-08-22 12:43:55 +0200 | [diff] [blame] | 172 | } | 
| Jakub Josef | a63f986 | 2018-01-11 17:58:38 +0100 | [diff] [blame] | 173 | 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 Polreich | a3e3012 | 2017-08-22 12:43:55 +0200 | [diff] [blame] | 183 | } | 
| Martin Polreich | 32b8222 | 2018-11-21 14:01:39 +0100 | [diff] [blame] | 184 | }/** TODO: End of block for removal */ | 
|  | 185 | } else { | 
|  | 186 | if (checkouted) { | 
| Martin Polreich | 5be29e3 | 2019-02-26 11:11:51 +0100 | [diff] [blame] | 187 | 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}") | 
|  | 191 | } else if (fileExists(".kitchen.yml")) { | 
|  | 192 | common.infoMsg("Docker Kitchen test configuration found, running Docker kitchen tests.") | 
|  | 193 | kitchenFileName = ".kitchen.yml" | 
|  | 194 | } | 
|  | 195 | if (kitchenFileName) { | 
| Martin Polreich | 32b8222 | 2018-11-21 14:01:39 +0100 | [diff] [blame] | 196 | def kitchenEnvs = [] | 
| Denis Egorenko | 5884e31 | 2020-12-16 15:46:20 +0400 | [diff] [blame] | 197 | ruby.ensureRubyEnv() | 
| Martin Polreich | 32b8222 | 2018-11-21 14:01:39 +0100 | [diff] [blame] | 198 | if (!fileExists("Gemfile")) { | 
|  | 199 | 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'") | 
|  | 200 | ruby.installKitchen() | 
|  | 201 | } else { | 
|  | 202 | common.infoMsg("Override Gemfile found in the kitchen directory, using it.") | 
|  | 203 | ruby.installKitchen() | 
|  | 204 | } | 
| Martin Polreich | 3ce6d3e | 2019-02-19 15:18:36 +0100 | [diff] [blame] | 205 | common.infoMsg = ruby.runKitchenCommand("list -b", envOverrides.join(' ')) | 
|  | 206 | kitchenEnvs = ruby.runKitchenCommand("list -b", envOverrides.join(' ')).split() | 
| Martin Polreich | 32b8222 | 2018-11-21 14:01:39 +0100 | [diff] [blame] | 207 | common.infoMsg(kitchenEnvs) | 
|  | 208 | common.infoMsg("Running kitchen testing in parallel mode") | 
|  | 209 | if (CUSTOM_KITCHEN_ENVS != null && CUSTOM_KITCHEN_ENVS != '') { | 
|  | 210 | kitchenEnvs = CUSTOM_KITCHEN_ENVS.tokenize('\n') | 
|  | 211 | common.infoMsg("CUSTOM_KITCHEN_ENVS not empty. Running with custom enviroments: ${kitchenEnvs}") | 
|  | 212 | } | 
|  | 213 | if (kitchenEnvs != null && kitchenEnvs != '') { | 
|  | 214 | def acc = 0 | 
|  | 215 | common.infoMsg("Found " + kitchenEnvs.size() + " environment(s)") | 
|  | 216 | for (int i = 0; i < kitchenEnvs.size(); i++) { | 
|  | 217 | futureFormulas << kitchenEnvs[i] | 
|  | 218 | } | 
|  | 219 | setupRunner(defaultGitRef, defaultGitUrl) | 
|  | 220 | } else { | 
| Martin Polreich | 5be29e3 | 2019-02-26 11:11:51 +0100 | [diff] [blame] | 221 | common.errorMsg("No enviroments defined in the Kitchen file: ${kitchenFileName}") | 
|  | 222 | } | 
|  | 223 | } else { | 
| Martin Polreich | 32b8222 | 2018-11-21 14:01:39 +0100 | [diff] [blame] | 224 | common.warningMsg(".kitchen.yml nor .kitchen.openstack.yml file not found, no kitchen tests triggered.") | 
|  | 225 | } | 
| Martin Polreich | 32b8222 | 2018-11-21 14:01:39 +0100 | [diff] [blame] | 226 | } | 
| Jakub Josef | f64c639 | 2017-05-03 13:18:25 +0200 | [diff] [blame] | 227 | } | 
| Jakub Josef | f64c639 | 2017-05-03 13:18:25 +0200 | [diff] [blame] | 228 | } | 
| Jakub Josef | a63f986 | 2018-01-11 17:58:38 +0100 | [diff] [blame] | 229 | if (failedFormulas) { | 
|  | 230 | currentBuild.result = "FAILURE" | 
|  | 231 | common.warningMsg("The following tests failed: ${failedFormulas}") | 
|  | 232 | } | 
|  | 233 | } catch (Throwable e) { | 
|  | 234 | // If there was an error or exception thrown, the build failed | 
| chnyda | 85ea22d | 2017-10-23 13:05:16 +0200 | [diff] [blame] | 235 | currentBuild.result = "FAILURE" | 
| Jakub Josef | a63f986 | 2018-01-11 17:58:38 +0100 | [diff] [blame] | 236 | currentBuild.description = currentBuild.description ? e.message + " " + currentBuild.description : e.message | 
|  | 237 | throw e | 
|  | 238 | } finally { | 
|  | 239 | if (currentBuild.result == "FAILURE" && fileExists(".kitchen/logs/kitchen.log")) { | 
|  | 240 | common.errorMsg("----------------KITCHEN LOG:---------------") | 
|  | 241 | println readFile(".kitchen/logs/kitchen.log") | 
|  | 242 | } | 
| chnyda | 85ea22d | 2017-10-23 13:05:16 +0200 | [diff] [blame] | 243 | } | 
| Jakub Josef | c5a223a | 2017-03-01 14:40:08 +0100 | [diff] [blame] | 244 | } | 
| azvyagintsev | 3a2e035 | 2018-01-10 12:41:29 +0200 | [diff] [blame] | 245 | } |