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 |
Vladimir Khlyunev | 522d9f9 | 2021-02-01 18:51:15 +0400 | [diff] [blame] | 16 | def slaveNode = env.SLAVE_NODE ?: 'old16.04' |
azvyagintsev | c2628ed | 2018-08-16 11:24:10 +0300 | [diff] [blame] | 17 | def saltVersion = env.SALT_VERSION ?: "" |
| 18 | def dockerLib = new com.mirantis.mk.Docker() |
Jakub Josef | 8337931 | 2017-03-29 18:12:34 +0200 | [diff] [blame] | 19 | |
Jakub Josef | bcd2e90 | 2017-06-13 14:40:41 +0200 | [diff] [blame] | 20 | def checkouted = false |
Jakub Josef | 8337931 | 2017-03-29 18:12:34 +0200 | [diff] [blame] | 21 | |
chnyda | 85ea22d | 2017-10-23 13:05:16 +0200 | [diff] [blame] | 22 | futureFormulas = [] |
| 23 | failedFormulas = [] |
| 24 | |
| 25 | def setupRunner(defaultGitRef, defaultGitUrl) { |
| 26 | def branches = [:] |
| 27 | for (int i = 0; i < PARALLEL_GROUP_SIZE.toInteger() && i < futureFormulas.size(); i++) { |
| 28 | branches["Runner ${i}"] = { |
| 29 | while (futureFormulas && !failedFormulas) { |
| 30 | def currentFormula = futureFormulas[0] ? futureFormulas[0] : null |
| 31 | if (!currentFormula) { |
| 32 | continue |
| 33 | } |
| 34 | futureFormulas.remove(currentFormula) |
| 35 | try { |
| 36 | triggerTestFormulaJob(currentFormula, defaultGitRef, defaultGitUrl) |
| 37 | } catch (Exception e) { |
chnyda | 0513b57 | 2018-01-23 13:40:57 +0100 | [diff] [blame] | 38 | if (e.getMessage().contains("completed with status ABORTED")) { |
| 39 | common.warningMsg("Test of ${currentFormula} was aborted and will be retriggered") |
| 40 | futureFormulas << currentFormula |
| 41 | } else { |
| 42 | failedFormulas << currentFormula |
| 43 | common.warningMsg("Test of ${currentFormula} failed : ${e}") |
| 44 | } |
chnyda | 85ea22d | 2017-10-23 13:05:16 +0200 | [diff] [blame] | 45 | } |
| 46 | } |
| 47 | } |
| 48 | } |
| 49 | parallel branches |
| 50 | } |
| 51 | |
| 52 | def triggerTestFormulaJob(testEnv, defaultGitRef, defaultGitUrl) { |
| 53 | common.infoMsg("Test of ${testEnv} starts") |
| 54 | build job: "test-salt-formulas-env", parameters: [ |
| 55 | [$class: 'StringParameterValue', name: 'CREDENTIALS_ID', value: CREDENTIALS_ID], |
| 56 | [$class: 'StringParameterValue', name: 'KITCHEN_ENV', value: testEnv], |
| 57 | [$class: 'StringParameterValue', name: 'DEFAULT_GIT_REF', value: defaultGitRef], |
| 58 | [$class: 'StringParameterValue', name: 'DEFAULT_GIT_URL', value: defaultGitUrl], |
| 59 | [$class: 'StringParameterValue', name: 'SALT_OPTS', value: SALT_OPTS], |
| 60 | [$class: 'StringParameterValue', name: 'SALT_VERSION', value: SALT_VERSION] |
| 61 | ] |
| 62 | } |
Martin Polreich | c0995dc | 2018-11-05 14:40:02 +0100 | [diff] [blame] | 63 | |
Martin Polreich | 15ef9f4 | 2018-12-17 14:29:54 +0100 | [diff] [blame] | 64 | timeout(time: 4, unit: 'HOURS') { |
azvyagintsev | c2628ed | 2018-08-16 11:24:10 +0300 | [diff] [blame] | 65 | node(slaveNode) { |
azvyagintsev | 54b49c6 | 2018-12-27 17:38:56 +0200 | [diff] [blame] | 66 | def img = dockerLib.getImage(env.SMOKE_TEST_DOCKER_IMG, "ubuntu:16.04") |
Jakub Josef | a63f986 | 2018-01-11 17:58:38 +0100 | [diff] [blame] | 67 | try { |
azvyagintsev | c2628ed | 2018-08-16 11:24:10 +0300 | [diff] [blame] | 68 | if (fileExists("tests/build")) { |
| 69 | common.infoMsg('Cleaning test env') |
| 70 | sh ("sudo rm -rf tests/build") |
| 71 | } |
Jakub Josef | a63f986 | 2018-01-11 17:58:38 +0100 | [diff] [blame] | 72 | stage("checkout") { |
| 73 | if (gerritRef) { |
| 74 | // job is triggered by Gerrit |
| 75 | def gerritChange = gerrit.getGerritChange(GERRIT_NAME, GERRIT_HOST, GERRIT_CHANGE_NUMBER, CREDENTIALS_ID, true) |
| 76 | // test if gerrit change is already Verified |
| 77 | if (gerrit.patchsetHasApproval(gerritChange.currentPatchSet, "Verified", "+")) { |
| 78 | common.successMsg("Gerrit change ${GERRIT_CHANGE_NUMBER} patchset ${GERRIT_PATCHSET_NUMBER} already has Verified, skipping tests") // do nothing |
| 79 | // test WIP contains in commit message |
| 80 | } else if (gerritChange.commitMessage.contains("WIP")) { |
| 81 | common.successMsg("Commit message contains WIP, skipping tests") // do nothing |
Martin Polreich | a3e3012 | 2017-08-22 12:43:55 +0200 | [diff] [blame] | 82 | } else { |
Jakub Josef | a63f986 | 2018-01-11 17:58:38 +0100 | [diff] [blame] | 83 | // test if change aren't already merged |
| 84 | def merged = gerritChange.status == "MERGED" |
| 85 | if (!merged) { |
| 86 | checkouted = gerrit.gerritPatchsetCheckout([ |
| 87 | credentialsId: CREDENTIALS_ID |
| 88 | ]) |
| 89 | } else { |
| 90 | common.successMsg("Change ${GERRIT_CHANGE_NUMBER} is already merged, no need to test them") |
| 91 | } |
Jakub Josef | bcd2e90 | 2017-06-13 14:40:41 +0200 | [diff] [blame] | 92 | } |
Jakub Josef | a63f986 | 2018-01-11 17:58:38 +0100 | [diff] [blame] | 93 | defaultGitUrl = "${GERRIT_SCHEME}://${GERRIT_NAME}@${GERRIT_HOST}:${GERRIT_PORT}/${GERRIT_PROJECT}" |
| 94 | defaultGitRef = GERRIT_REFSPEC |
| 95 | } else if (defaultGitRef && defaultGitUrl) { |
| 96 | checkouted = gerrit.gerritPatchsetCheckout(defaultGitUrl, defaultGitRef, "HEAD", CREDENTIALS_ID) |
| 97 | } else { |
| 98 | 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] | 99 | } |
Jakub Josef | c5a223a | 2017-03-01 14:40:08 +0100 | [diff] [blame] | 100 | } |
azvyagintsev | c2628ed | 2018-08-16 11:24:10 +0300 | [diff] [blame] | 101 | stage("test") { |
| 102 | if (checkouted) { |
| 103 | try { |
| 104 | // TODO add try\finally for image-stuck case. (copy-paste from SaltModelTesting) |
| 105 | withEnv(["SALT_VERSION=${saltVersion}"]) { |
| 106 | img.inside("-v ${env.WORKSPACE}/:/formula/ -u root:root --cpus=4 --ulimit nofile=4096:8192") { |
| 107 | sh('''#!/bin/bash -xe |
| 108 | cd /etc/apt/ |
azvyagintsev | 1a494b5 | 2018-12-20 14:48:46 +0200 | [diff] [blame] | 109 | echo "deb [arch=amd64] http://mirror.mirantis.com/nightly/ubuntu xenial main restricted universe" > sources.list |
| 110 | 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] | 111 | echo 'Acquire::Languages "none";' > apt.conf.d/docker-no-languages |
| 112 | echo 'Acquire::GzipIndexes "true"; Acquire::CompressionTypes::Order:: "gz";' > apt.conf.d/docker-gzip-indexes |
| 113 | echo 'APT::Get::Install-Recommends "false"; APT::Get::Install-Suggests "false";' > apt.conf.d/docker-recommends |
| 114 | apt-get update |
| 115 | apt-get install -y git-core wget curl apt-transport-https |
| 116 | apt-get install -y python-pip python3-pip python-virtualenv python3-virtualenv python-yaml autoconf build-essential |
| 117 | cd /formula/ |
| 118 | make clean |
| 119 | make test |
| 120 | make clean |
| 121 | ''') |
| 122 | } |
Sergey Otpuschennikov | e5a1474 | 2018-05-08 11:42:26 +0400 | [diff] [blame] | 123 | } |
azvyagintsev | c2628ed | 2018-08-16 11:24:10 +0300 | [diff] [blame] | 124 | } |
| 125 | finally { |
| 126 | if (fileExists("tests/build")) { |
| 127 | common.infoMsg('Cleaning test env') |
| 128 | sh ("sudo rm -rf tests/build") |
| 129 | } |
Sergey Otpuschennikov | e5a1474 | 2018-05-08 11:42:26 +0400 | [diff] [blame] | 130 | } |
azvyagintsev | 3a2e035 | 2018-01-10 12:41:29 +0200 | [diff] [blame] | 131 | } |
azvyagintsev | c2628ed | 2018-08-16 11:24:10 +0300 | [diff] [blame] | 132 | |
Jakub Josef | c5a223a | 2017-03-01 14:40:08 +0100 | [diff] [blame] | 133 | } |
Jakub Josef | f64c639 | 2017-05-03 13:18:25 +0200 | [diff] [blame] | 134 | stage("kitchen") { |
Martin Polreich | 32b8222 | 2018-11-21 14:01:39 +0100 | [diff] [blame] | 135 | 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] | 136 | if (checkouted) { |
Martin Polreich | c0995dc | 2018-11-05 14:40:02 +0100 | [diff] [blame] | 137 | if (fileExists(".kitchen.yml") || fileExists(".kitchen.openstack.yml")) { |
| 138 | if (fileExists(".kitchen.openstack.yml")) { |
| 139 | common.infoMsg("Openstack Kitchen test configuration found, running Openstack kitchen tests.") |
| 140 | if (fileExists(".kitchen.yml")) { |
| 141 | common.infoMsg("Ignoring the docker Kitchen test configuration file.") |
| 142 | } |
| 143 | } else { |
| 144 | common.infoMsg("Docker Kitchen test configuration found, running Docker kitchen tests.") |
| 145 | } |
Jakub Josef | a63f986 | 2018-01-11 17:58:38 +0100 | [diff] [blame] | 146 | def kitchenEnvs = [] |
| 147 | def filteredEnvs = [] |
| 148 | if (fileExists(".travis.yml")) { |
| 149 | common.infoMsg(".travis.yml file found.") |
| 150 | def kitchenConfigYML = readYaml(file: ".travis.yml") |
| 151 | if (kitchenConfigYML.containsKey("env")) { |
| 152 | kitchenEnvs = kitchenConfigYML["env"] |
| 153 | } |
| 154 | } else { |
| 155 | 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] | 156 | } |
Jakub Josef | a63f986 | 2018-01-11 17:58:38 +0100 | [diff] [blame] | 157 | common.infoMsg("Running kitchen testing in parallel mode") |
| 158 | if (CUSTOM_KITCHEN_ENVS != null && CUSTOM_KITCHEN_ENVS != '') { |
| 159 | kitchenEnvs = CUSTOM_KITCHEN_ENVS.tokenize('\n') |
| 160 | common.infoMsg("CUSTOM_KITCHEN_ENVS not empty. Running with custom enviroments: ${kitchenEnvs}") |
Martin Polreich | a3e3012 | 2017-08-22 12:43:55 +0200 | [diff] [blame] | 161 | } |
Jakub Josef | a63f986 | 2018-01-11 17:58:38 +0100 | [diff] [blame] | 162 | if (kitchenEnvs != null && kitchenEnvs != '') { |
| 163 | def acc = 0 |
| 164 | common.infoMsg("Found " + kitchenEnvs.size() + " environment(s)") |
| 165 | for (int i = 0; i < kitchenEnvs.size(); i++) { |
| 166 | futureFormulas << kitchenEnvs[i] |
| 167 | } |
| 168 | setupRunner(defaultGitRef, defaultGitUrl) |
| 169 | } else { |
| 170 | common.warningMsg(".kitchen.yml file not found, no kitchen tests triggered.") |
| 171 | } |
Martin Polreich | a3e3012 | 2017-08-22 12:43:55 +0200 | [diff] [blame] | 172 | } |
Martin Polreich | 32b8222 | 2018-11-21 14:01:39 +0100 | [diff] [blame] | 173 | }/** TODO: End of block for removal */ |
| 174 | } else { |
| 175 | if (checkouted) { |
| 176 | if (fileExists(".kitchen.yml") || fileExists(".kitchen.openstack.yml")) { |
| 177 | if (fileExists(".kitchen.openstack.yml")) { |
| 178 | common.infoMsg("Openstack Kitchen test configuration found, running Openstack kitchen tests.") |
| 179 | if (fileExists(".kitchen.yml")) { |
| 180 | common.infoMsg("Ignoring the docker Kitchen test configuration file.") |
| 181 | } |
| 182 | } else { |
| 183 | common.infoMsg("Docker Kitchen test configuration found, running Docker kitchen tests.") |
| 184 | } |
| 185 | def kitchenEnvs = [] |
| 186 | ruby.ensureRubyEnv() |
| 187 | if (!fileExists("Gemfile")) { |
| 188 | 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'") |
| 189 | ruby.installKitchen() |
| 190 | } else { |
| 191 | common.infoMsg("Override Gemfile found in the kitchen directory, using it.") |
| 192 | ruby.installKitchen() |
| 193 | } |
| 194 | common.infoMsg = ruby.runKitchenCommand("list -b") |
| 195 | kitchenEnvs = ruby.runKitchenCommand("list -b").split() |
| 196 | common.infoMsg(kitchenEnvs) |
| 197 | common.infoMsg("Running kitchen testing in parallel mode") |
| 198 | if (CUSTOM_KITCHEN_ENVS != null && CUSTOM_KITCHEN_ENVS != '') { |
| 199 | kitchenEnvs = CUSTOM_KITCHEN_ENVS.tokenize('\n') |
| 200 | common.infoMsg("CUSTOM_KITCHEN_ENVS not empty. Running with custom enviroments: ${kitchenEnvs}") |
| 201 | } |
| 202 | if (kitchenEnvs != null && kitchenEnvs != '') { |
| 203 | def acc = 0 |
| 204 | common.infoMsg("Found " + kitchenEnvs.size() + " environment(s)") |
| 205 | for (int i = 0; i < kitchenEnvs.size(); i++) { |
| 206 | futureFormulas << kitchenEnvs[i] |
| 207 | } |
| 208 | setupRunner(defaultGitRef, defaultGitUrl) |
| 209 | } else { |
| 210 | common.warningMsg(".kitchen.yml nor .kitchen.openstack.yml file not found, no kitchen tests triggered.") |
| 211 | } |
| 212 | } |
| 213 | } |
Jakub Josef | f64c639 | 2017-05-03 13:18:25 +0200 | [diff] [blame] | 214 | } |
Jakub Josef | f64c639 | 2017-05-03 13:18:25 +0200 | [diff] [blame] | 215 | } |
Jakub Josef | a63f986 | 2018-01-11 17:58:38 +0100 | [diff] [blame] | 216 | if (failedFormulas) { |
| 217 | currentBuild.result = "FAILURE" |
| 218 | common.warningMsg("The following tests failed: ${failedFormulas}") |
| 219 | } |
| 220 | } catch (Throwable e) { |
| 221 | // If there was an error or exception thrown, the build failed |
chnyda | 85ea22d | 2017-10-23 13:05:16 +0200 | [diff] [blame] | 222 | currentBuild.result = "FAILURE" |
Jakub Josef | a63f986 | 2018-01-11 17:58:38 +0100 | [diff] [blame] | 223 | currentBuild.description = currentBuild.description ? e.message + " " + currentBuild.description : e.message |
| 224 | throw e |
| 225 | } finally { |
| 226 | if (currentBuild.result == "FAILURE" && fileExists(".kitchen/logs/kitchen.log")) { |
| 227 | common.errorMsg("----------------KITCHEN LOG:---------------") |
| 228 | println readFile(".kitchen/logs/kitchen.log") |
| 229 | } |
chnyda | 85ea22d | 2017-10-23 13:05:16 +0200 | [diff] [blame] | 230 | } |
Jakub Josef | c5a223a | 2017-03-01 14:40:08 +0100 | [diff] [blame] | 231 | } |
azvyagintsev | 3a2e035 | 2018-01-10 12:41:29 +0200 | [diff] [blame] | 232 | } |