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