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 |
Jakub Josef | 8337931 | 2017-03-29 18:12:34 +0200 | [diff] [blame] | 7 | */ |
chnyda | 85ea22d | 2017-10-23 13:05:16 +0200 | [diff] [blame] | 8 | common = new com.mirantis.mk.Common() |
Jakub Josef | c5a223a | 2017-03-01 14:40:08 +0100 | [diff] [blame] | 9 | def gerrit = new com.mirantis.mk.Gerrit() |
Jakub Josef | f64c639 | 2017-05-03 13:18:25 +0200 | [diff] [blame] | 10 | def ruby = new com.mirantis.mk.Ruby() |
Jakub Josef | 8337931 | 2017-03-29 18:12:34 +0200 | [diff] [blame] | 11 | |
| 12 | def gerritRef |
| 13 | try { |
| 14 | gerritRef = GERRIT_REFSPEC |
| 15 | } catch (MissingPropertyException e) { |
| 16 | gerritRef = null |
| 17 | } |
| 18 | |
| 19 | def defaultGitRef, defaultGitUrl |
| 20 | try { |
Martin Polreich | a3e3012 | 2017-08-22 12:43:55 +0200 | [diff] [blame] | 21 | defaultGitRef = DEFAULT_GIT_REF |
| 22 | defaultGitUrl = DEFAULT_GIT_URL |
Jakub Josef | 8337931 | 2017-03-29 18:12:34 +0200 | [diff] [blame] | 23 | } catch (MissingPropertyException e) { |
Martin Polreich | a3e3012 | 2017-08-22 12:43:55 +0200 | [diff] [blame] | 24 | defaultGitRef = null |
| 25 | defaultGitUrl = null |
Jakub Josef | 8337931 | 2017-03-29 18:12:34 +0200 | [diff] [blame] | 26 | } |
| 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 | |
chnyda | 85ea22d | 2017-10-23 13:05:16 +0200 | [diff] [blame] | 30 | futureFormulas = [] |
| 31 | failedFormulas = [] |
| 32 | |
| 33 | def setupRunner(defaultGitRef, defaultGitUrl) { |
| 34 | def branches = [:] |
| 35 | for (int i = 0; i < PARALLEL_GROUP_SIZE.toInteger() && i < futureFormulas.size(); i++) { |
| 36 | branches["Runner ${i}"] = { |
| 37 | while (futureFormulas && !failedFormulas) { |
| 38 | def currentFormula = futureFormulas[0] ? futureFormulas[0] : null |
| 39 | if (!currentFormula) { |
| 40 | continue |
| 41 | } |
| 42 | futureFormulas.remove(currentFormula) |
| 43 | try { |
| 44 | triggerTestFormulaJob(currentFormula, defaultGitRef, defaultGitUrl) |
| 45 | } catch (Exception e) { |
chnyda | 0513b57 | 2018-01-23 13:40:57 +0100 | [diff] [blame] | 46 | if (e.getMessage().contains("completed with status ABORTED")) { |
| 47 | common.warningMsg("Test of ${currentFormula} was aborted and will be retriggered") |
| 48 | futureFormulas << currentFormula |
| 49 | } else { |
| 50 | failedFormulas << currentFormula |
| 51 | common.warningMsg("Test of ${currentFormula} failed : ${e}") |
| 52 | } |
chnyda | 85ea22d | 2017-10-23 13:05:16 +0200 | [diff] [blame] | 53 | } |
| 54 | } |
| 55 | } |
| 56 | } |
| 57 | parallel branches |
| 58 | } |
| 59 | |
| 60 | def triggerTestFormulaJob(testEnv, defaultGitRef, defaultGitUrl) { |
| 61 | common.infoMsg("Test of ${testEnv} starts") |
| 62 | build job: "test-salt-formulas-env", parameters: [ |
| 63 | [$class: 'StringParameterValue', name: 'CREDENTIALS_ID', value: CREDENTIALS_ID], |
| 64 | [$class: 'StringParameterValue', name: 'KITCHEN_ENV', value: testEnv], |
| 65 | [$class: 'StringParameterValue', name: 'DEFAULT_GIT_REF', value: defaultGitRef], |
| 66 | [$class: 'StringParameterValue', name: 'DEFAULT_GIT_URL', value: defaultGitUrl], |
| 67 | [$class: 'StringParameterValue', name: 'SALT_OPTS', value: SALT_OPTS], |
| 68 | [$class: 'StringParameterValue', name: 'SALT_VERSION', value: SALT_VERSION] |
| 69 | ] |
| 70 | } |
Jakub Josef | a63f986 | 2018-01-11 17:58:38 +0100 | [diff] [blame] | 71 | timeout(time: 12, unit: 'HOURS') { |
| 72 | node("python") { |
| 73 | try { |
| 74 | stage("checkout") { |
| 75 | if (gerritRef) { |
| 76 | // job is triggered by Gerrit |
| 77 | def gerritChange = gerrit.getGerritChange(GERRIT_NAME, GERRIT_HOST, GERRIT_CHANGE_NUMBER, CREDENTIALS_ID, true) |
| 78 | // test if gerrit change is already Verified |
| 79 | if (gerrit.patchsetHasApproval(gerritChange.currentPatchSet, "Verified", "+")) { |
| 80 | common.successMsg("Gerrit change ${GERRIT_CHANGE_NUMBER} patchset ${GERRIT_PATCHSET_NUMBER} already has Verified, skipping tests") // do nothing |
| 81 | // test WIP contains in commit message |
| 82 | } else if (gerritChange.commitMessage.contains("WIP")) { |
| 83 | common.successMsg("Commit message contains WIP, skipping tests") // do nothing |
Martin Polreich | a3e3012 | 2017-08-22 12:43:55 +0200 | [diff] [blame] | 84 | } else { |
Jakub Josef | a63f986 | 2018-01-11 17:58:38 +0100 | [diff] [blame] | 85 | // test if change aren't already merged |
| 86 | def merged = gerritChange.status == "MERGED" |
| 87 | if (!merged) { |
| 88 | checkouted = gerrit.gerritPatchsetCheckout([ |
| 89 | credentialsId: CREDENTIALS_ID |
| 90 | ]) |
| 91 | } else { |
| 92 | common.successMsg("Change ${GERRIT_CHANGE_NUMBER} is already merged, no need to test them") |
| 93 | } |
Jakub Josef | bcd2e90 | 2017-06-13 14:40:41 +0200 | [diff] [blame] | 94 | } |
Jakub Josef | a63f986 | 2018-01-11 17:58:38 +0100 | [diff] [blame] | 95 | defaultGitUrl = "${GERRIT_SCHEME}://${GERRIT_NAME}@${GERRIT_HOST}:${GERRIT_PORT}/${GERRIT_PROJECT}" |
| 96 | defaultGitRef = GERRIT_REFSPEC |
| 97 | } else if (defaultGitRef && defaultGitUrl) { |
| 98 | checkouted = gerrit.gerritPatchsetCheckout(defaultGitUrl, defaultGitRef, "HEAD", CREDENTIALS_ID) |
| 99 | } else { |
| 100 | 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] | 101 | } |
Jakub Josef | c5a223a | 2017-03-01 14:40:08 +0100 | [diff] [blame] | 102 | } |
| 103 | stage("test") { |
Martin Polreich | a3e3012 | 2017-08-22 12:43:55 +0200 | [diff] [blame] | 104 | if (checkouted) { |
azvyagintsev | 3a2e035 | 2018-01-10 12:41:29 +0200 | [diff] [blame] | 105 | try { |
| 106 | saltVersion = SALT_VERSION |
Jakub Josef | a63f986 | 2018-01-11 17:58:38 +0100 | [diff] [blame] | 107 | } catch (MissingPropertyException e) { |
Jakub Josef | 566dc4e | 2018-02-07 11:29:30 +0100 | [diff] [blame] | 108 | saltVersion = "" // default value is empty string, means latest |
azvyagintsev | 3a2e035 | 2018-01-10 12:41:29 +0200 | [diff] [blame] | 109 | } |
| 110 | withEnv(["SALT_VERSION=${saltVersion}"]) { |
| 111 | sh("make clean && make test") |
| 112 | } |
Jakub Josef | c5a223a | 2017-03-01 14:40:08 +0100 | [diff] [blame] | 113 | } |
| 114 | } |
Jakub Josef | f64c639 | 2017-05-03 13:18:25 +0200 | [diff] [blame] | 115 | stage("kitchen") { |
Jakub Josef | a63f986 | 2018-01-11 17:58:38 +0100 | [diff] [blame] | 116 | if (checkouted) { |
| 117 | if (fileExists(".kitchen.yml")) { |
| 118 | common.infoMsg(".kitchen.yml found, running kitchen tests") |
| 119 | def kitchenEnvs = [] |
| 120 | def filteredEnvs = [] |
| 121 | if (fileExists(".travis.yml")) { |
| 122 | common.infoMsg(".travis.yml file found.") |
| 123 | def kitchenConfigYML = readYaml(file: ".travis.yml") |
| 124 | if (kitchenConfigYML.containsKey("env")) { |
| 125 | kitchenEnvs = kitchenConfigYML["env"] |
| 126 | } |
| 127 | } else { |
| 128 | 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] | 129 | } |
Jakub Josef | a63f986 | 2018-01-11 17:58:38 +0100 | [diff] [blame] | 130 | common.infoMsg("Running kitchen testing in parallel mode") |
| 131 | if (CUSTOM_KITCHEN_ENVS != null && CUSTOM_KITCHEN_ENVS != '') { |
| 132 | kitchenEnvs = CUSTOM_KITCHEN_ENVS.tokenize('\n') |
| 133 | common.infoMsg("CUSTOM_KITCHEN_ENVS not empty. Running with custom enviroments: ${kitchenEnvs}") |
Martin Polreich | a3e3012 | 2017-08-22 12:43:55 +0200 | [diff] [blame] | 134 | } |
Jakub Josef | a63f986 | 2018-01-11 17:58:38 +0100 | [diff] [blame] | 135 | if (kitchenEnvs != null && kitchenEnvs != '') { |
| 136 | def acc = 0 |
| 137 | common.infoMsg("Found " + kitchenEnvs.size() + " environment(s)") |
| 138 | for (int i = 0; i < kitchenEnvs.size(); i++) { |
| 139 | futureFormulas << kitchenEnvs[i] |
| 140 | } |
| 141 | setupRunner(defaultGitRef, defaultGitUrl) |
| 142 | } else { |
| 143 | common.warningMsg(".kitchen.yml file not found, no kitchen tests triggered.") |
| 144 | } |
Martin Polreich | a3e3012 | 2017-08-22 12:43:55 +0200 | [diff] [blame] | 145 | } |
Jakub Josef | f64c639 | 2017-05-03 13:18:25 +0200 | [diff] [blame] | 146 | } |
Jakub Josef | f64c639 | 2017-05-03 13:18:25 +0200 | [diff] [blame] | 147 | } |
Jakub Josef | a63f986 | 2018-01-11 17:58:38 +0100 | [diff] [blame] | 148 | if (failedFormulas) { |
| 149 | currentBuild.result = "FAILURE" |
| 150 | common.warningMsg("The following tests failed: ${failedFormulas}") |
| 151 | } |
| 152 | } catch (Throwable e) { |
| 153 | // If there was an error or exception thrown, the build failed |
chnyda | 85ea22d | 2017-10-23 13:05:16 +0200 | [diff] [blame] | 154 | currentBuild.result = "FAILURE" |
Jakub Josef | a63f986 | 2018-01-11 17:58:38 +0100 | [diff] [blame] | 155 | currentBuild.description = currentBuild.description ? e.message + " " + currentBuild.description : e.message |
| 156 | throw e |
| 157 | } finally { |
| 158 | if (currentBuild.result == "FAILURE" && fileExists(".kitchen/logs/kitchen.log")) { |
| 159 | common.errorMsg("----------------KITCHEN LOG:---------------") |
| 160 | println readFile(".kitchen/logs/kitchen.log") |
| 161 | } |
| 162 | common.sendNotification(currentBuild.result, "", ["slack"]) |
chnyda | 85ea22d | 2017-10-23 13:05:16 +0200 | [diff] [blame] | 163 | } |
Jakub Josef | c5a223a | 2017-03-01 14:40:08 +0100 | [diff] [blame] | 164 | } |
azvyagintsev | 3a2e035 | 2018-01-10 12:41:29 +0200 | [diff] [blame] | 165 | } |