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 | * RUN_TEST_IN_DOCKER If true, run test stage in docker |
| 8 | * 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] | 9 | */ |
chnyda | 85ea22d | 2017-10-23 13:05:16 +0200 | [diff] [blame] | 10 | common = new com.mirantis.mk.Common() |
Jakub Josef | c5a223a | 2017-03-01 14:40:08 +0100 | [diff] [blame] | 11 | def gerrit = new com.mirantis.mk.Gerrit() |
Jakub Josef | f64c639 | 2017-05-03 13:18:25 +0200 | [diff] [blame] | 12 | def ruby = new com.mirantis.mk.Ruby() |
Jakub Josef | 8337931 | 2017-03-29 18:12:34 +0200 | [diff] [blame] | 13 | |
| 14 | def gerritRef |
| 15 | try { |
| 16 | gerritRef = GERRIT_REFSPEC |
| 17 | } catch (MissingPropertyException e) { |
| 18 | gerritRef = null |
| 19 | } |
| 20 | |
| 21 | def defaultGitRef, defaultGitUrl |
| 22 | try { |
Martin Polreich | a3e3012 | 2017-08-22 12:43:55 +0200 | [diff] [blame] | 23 | defaultGitRef = DEFAULT_GIT_REF |
| 24 | defaultGitUrl = DEFAULT_GIT_URL |
Jakub Josef | 8337931 | 2017-03-29 18:12:34 +0200 | [diff] [blame] | 25 | } catch (MissingPropertyException e) { |
Martin Polreich | a3e3012 | 2017-08-22 12:43:55 +0200 | [diff] [blame] | 26 | defaultGitRef = null |
| 27 | defaultGitUrl = null |
Jakub Josef | 8337931 | 2017-03-29 18:12:34 +0200 | [diff] [blame] | 28 | } |
| 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 | |
chnyda | 85ea22d | 2017-10-23 13:05:16 +0200 | [diff] [blame] | 32 | futureFormulas = [] |
| 33 | failedFormulas = [] |
| 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], |
| 70 | [$class: 'StringParameterValue', name: 'SALT_VERSION', value: SALT_VERSION] |
| 71 | ] |
| 72 | } |
Jakub Josef | a63f986 | 2018-01-11 17:58:38 +0100 | [diff] [blame] | 73 | timeout(time: 12, unit: 'HOURS') { |
| 74 | node("python") { |
| 75 | try { |
| 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 | } |
| 105 | stage("test") { |
Martin Polreich | a3e3012 | 2017-08-22 12:43:55 +0200 | [diff] [blame] | 106 | if (checkouted) { |
azvyagintsev | 3a2e035 | 2018-01-10 12:41:29 +0200 | [diff] [blame] | 107 | try { |
| 108 | saltVersion = SALT_VERSION |
Jakub Josef | a63f986 | 2018-01-11 17:58:38 +0100 | [diff] [blame] | 109 | } catch (MissingPropertyException e) { |
Jakub Josef | 566dc4e | 2018-02-07 11:29:30 +0100 | [diff] [blame] | 110 | saltVersion = "" // default value is empty string, means latest |
azvyagintsev | 3a2e035 | 2018-01-10 12:41:29 +0200 | [diff] [blame] | 111 | } |
| 112 | withEnv(["SALT_VERSION=${saltVersion}"]) { |
Sergey Otpuschennikov | b570b4f | 2018-05-28 13:18:16 +0400 | [diff] [blame] | 113 | boolean run_test_in_docker = (env.RUN_TEST_IN_DOCKER ?: false).toBoolean() |
Sergey Otpuschennikov | e5a1474 | 2018-05-08 11:42:26 +0400 | [diff] [blame] | 114 | if (run_test_in_docker) { |
| 115 | def dockerLib = new com.mirantis.mk.Docker() |
| 116 | def img = dockerLib.getImage(env.SMOKE_TEST_DOCKER_IMG, "ubuntu:16.04") |
| 117 | def workspace = common.getWorkspace() |
| 118 | img.inside("-u root:root -v ${workspace}/:/formula/") { |
| 119 | sh("""cd /etc/apt/ && echo > sources.list \ |
| 120 | && echo "deb [arch=amd64] http://cz.archive.ubuntu.com/ubuntu xenial main restricted universe multiverse" >> sources.list \ |
| 121 | && echo "deb [arch=amd64] http://cz.archive.ubuntu.com/ubuntu xenial-updates main restricted universe multiverse" >> sources.list \ |
| 122 | && echo "deb [arch=amd64] http://cz.archive.ubuntu.com/ubuntu xenial-backports main restricted universe multiverse" >> sources.list \ |
| 123 | && echo 'Acquire::Languages "none";' > apt.conf.d/docker-no-languages \ |
| 124 | && echo 'Acquire::GzipIndexes "true"; Acquire::CompressionTypes::Order:: "gz";' > apt.conf.d/docker-gzip-indexes \ |
| 125 | && echo 'APT::Get::Install-Recommends "false"; APT::Get::Install-Suggests "false";' > apt.conf.d/docker-recommends \ |
| 126 | && apt-get update \ |
| 127 | && apt-get install -y git-core wget curl apt-transport-https \ |
| 128 | && apt-get install -y python-pip python3-pip python-virtualenv python3-virtualenv python-yaml autoconf build-essential""") |
| 129 | sh("cd /formula/ && make clean && make test") |
| 130 | } |
| 131 | } else { |
| 132 | common.warningMsg("Those tests should be always be run in clean env! Recommends to use docker env!") |
| 133 | sh("make clean && make test") |
| 134 | } |
azvyagintsev | 3a2e035 | 2018-01-10 12:41:29 +0200 | [diff] [blame] | 135 | } |
Jakub Josef | c5a223a | 2017-03-01 14:40:08 +0100 | [diff] [blame] | 136 | } |
| 137 | } |
Jakub Josef | f64c639 | 2017-05-03 13:18:25 +0200 | [diff] [blame] | 138 | stage("kitchen") { |
Jakub Josef | a63f986 | 2018-01-11 17:58:38 +0100 | [diff] [blame] | 139 | if (checkouted) { |
| 140 | if (fileExists(".kitchen.yml")) { |
| 141 | common.infoMsg(".kitchen.yml found, running kitchen tests") |
| 142 | def kitchenEnvs = [] |
| 143 | def filteredEnvs = [] |
| 144 | if (fileExists(".travis.yml")) { |
| 145 | common.infoMsg(".travis.yml file found.") |
| 146 | def kitchenConfigYML = readYaml(file: ".travis.yml") |
| 147 | if (kitchenConfigYML.containsKey("env")) { |
| 148 | kitchenEnvs = kitchenConfigYML["env"] |
| 149 | } |
| 150 | } else { |
| 151 | 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] | 152 | } |
Jakub Josef | a63f986 | 2018-01-11 17:58:38 +0100 | [diff] [blame] | 153 | common.infoMsg("Running kitchen testing in parallel mode") |
| 154 | if (CUSTOM_KITCHEN_ENVS != null && CUSTOM_KITCHEN_ENVS != '') { |
| 155 | kitchenEnvs = CUSTOM_KITCHEN_ENVS.tokenize('\n') |
| 156 | common.infoMsg("CUSTOM_KITCHEN_ENVS not empty. Running with custom enviroments: ${kitchenEnvs}") |
Martin Polreich | a3e3012 | 2017-08-22 12:43:55 +0200 | [diff] [blame] | 157 | } |
Jakub Josef | a63f986 | 2018-01-11 17:58:38 +0100 | [diff] [blame] | 158 | if (kitchenEnvs != null && kitchenEnvs != '') { |
| 159 | def acc = 0 |
| 160 | common.infoMsg("Found " + kitchenEnvs.size() + " environment(s)") |
| 161 | for (int i = 0; i < kitchenEnvs.size(); i++) { |
| 162 | futureFormulas << kitchenEnvs[i] |
| 163 | } |
| 164 | setupRunner(defaultGitRef, defaultGitUrl) |
| 165 | } else { |
| 166 | common.warningMsg(".kitchen.yml file not found, no kitchen tests triggered.") |
| 167 | } |
Martin Polreich | a3e3012 | 2017-08-22 12:43:55 +0200 | [diff] [blame] | 168 | } |
Jakub Josef | f64c639 | 2017-05-03 13:18:25 +0200 | [diff] [blame] | 169 | } |
Jakub Josef | f64c639 | 2017-05-03 13:18:25 +0200 | [diff] [blame] | 170 | } |
Jakub Josef | a63f986 | 2018-01-11 17:58:38 +0100 | [diff] [blame] | 171 | if (failedFormulas) { |
| 172 | currentBuild.result = "FAILURE" |
| 173 | common.warningMsg("The following tests failed: ${failedFormulas}") |
| 174 | } |
| 175 | } catch (Throwable e) { |
| 176 | // If there was an error or exception thrown, the build failed |
chnyda | 85ea22d | 2017-10-23 13:05:16 +0200 | [diff] [blame] | 177 | currentBuild.result = "FAILURE" |
Jakub Josef | a63f986 | 2018-01-11 17:58:38 +0100 | [diff] [blame] | 178 | currentBuild.description = currentBuild.description ? e.message + " " + currentBuild.description : e.message |
| 179 | throw e |
| 180 | } finally { |
| 181 | if (currentBuild.result == "FAILURE" && fileExists(".kitchen/logs/kitchen.log")) { |
| 182 | common.errorMsg("----------------KITCHEN LOG:---------------") |
| 183 | println readFile(".kitchen/logs/kitchen.log") |
| 184 | } |
Kirill Mashchenko | 4ccd6e0 | 2018-05-22 14:32:22 +0300 | [diff] [blame] | 185 | def slack = new com.mirantis.mcp.SlackNotification() |
| 186 | slack.jobResultNotification("success", "#test_reclass_notify") |
Jakub Josef | a63f986 | 2018-01-11 17:58:38 +0100 | [diff] [blame] | 187 | common.sendNotification(currentBuild.result, "", ["slack"]) |
chnyda | 85ea22d | 2017-10-23 13:05:16 +0200 | [diff] [blame] | 188 | } |
Jakub Josef | c5a223a | 2017-03-01 14:40:08 +0100 | [diff] [blame] | 189 | } |
azvyagintsev | 3a2e035 | 2018-01-10 12:41:29 +0200 | [diff] [blame] | 190 | } |