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) { |
| 46 | failedFormulas << currentFormula |
| 47 | common.warningMsg("Test of ${currentFormula} failed : ${e}") |
| 48 | } |
| 49 | } |
| 50 | } |
| 51 | } |
| 52 | parallel branches |
| 53 | } |
| 54 | |
| 55 | def triggerTestFormulaJob(testEnv, defaultGitRef, defaultGitUrl) { |
| 56 | common.infoMsg("Test of ${testEnv} starts") |
| 57 | build job: "test-salt-formulas-env", parameters: [ |
| 58 | [$class: 'StringParameterValue', name: 'CREDENTIALS_ID', value: CREDENTIALS_ID], |
| 59 | [$class: 'StringParameterValue', name: 'KITCHEN_ENV', value: testEnv], |
| 60 | [$class: 'StringParameterValue', name: 'DEFAULT_GIT_REF', value: defaultGitRef], |
| 61 | [$class: 'StringParameterValue', name: 'DEFAULT_GIT_URL', value: defaultGitUrl], |
| 62 | [$class: 'StringParameterValue', name: 'SALT_OPTS', value: SALT_OPTS], |
| 63 | [$class: 'StringParameterValue', name: 'SALT_VERSION', value: SALT_VERSION] |
| 64 | ] |
| 65 | } |
| 66 | |
chnyda | 3b1d2a6 | 2017-06-13 10:08:12 +0200 | [diff] [blame] | 67 | node("python") { |
Martin Polreich | a3e3012 | 2017-08-22 12:43:55 +0200 | [diff] [blame] | 68 | try { |
Jakub Josef | c5a223a | 2017-03-01 14:40:08 +0100 | [diff] [blame] | 69 | stage("checkout") { |
Jakub Josef | 8337931 | 2017-03-29 18:12:34 +0200 | [diff] [blame] | 70 | if (gerritRef) { |
| 71 | // job is triggered by Gerrit |
Jakub Josef | b223590 | 2017-06-16 12:49:16 +0200 | [diff] [blame] | 72 | def gerritChange = gerrit.getGerritChange(GERRIT_NAME, GERRIT_HOST, GERRIT_CHANGE_NUMBER, CREDENTIALS_ID, true) |
| 73 | // test if gerrit change is already Verified |
Martin Polreich | a3e3012 | 2017-08-22 12:43:55 +0200 | [diff] [blame] | 74 | if (gerrit.patchsetHasApproval(gerritChange.currentPatchSet, "Verified", "+")) { |
Jakub Josef | 894f1e3 | 2017-06-19 16:24:19 +0000 | [diff] [blame] | 75 | common.successMsg("Gerrit change ${GERRIT_CHANGE_NUMBER} patchset ${GERRIT_PATCHSET_NUMBER} already has Verified, skipping tests") // do nothing |
Martin Polreich | a3e3012 | 2017-08-22 12:43:55 +0200 | [diff] [blame] | 76 | // test WIP contains in commit message |
| 77 | } else if (gerritChange.commitMessage.contains("WIP")) { |
Jakub Josef | 894f1e3 | 2017-06-19 16:24:19 +0000 | [diff] [blame] | 78 | common.successMsg("Commit message contains WIP, skipping tests") // do nothing |
Martin Polreich | a3e3012 | 2017-08-22 12:43:55 +0200 | [diff] [blame] | 79 | } else { |
Jakub Josef | bcd2e90 | 2017-06-13 14:40:41 +0200 | [diff] [blame] | 80 | // test if change aren't already merged |
| 81 | def merged = gerritChange.status == "MERGED" |
Martin Polreich | a3e3012 | 2017-08-22 12:43:55 +0200 | [diff] [blame] | 82 | if (!merged) { |
| 83 | checkouted = gerrit.gerritPatchsetCheckout([ |
| 84 | credentialsId: CREDENTIALS_ID |
Jakub Josef | bcd2e90 | 2017-06-13 14:40:41 +0200 | [diff] [blame] | 85 | ]) |
Martin Polreich | a3e3012 | 2017-08-22 12:43:55 +0200 | [diff] [blame] | 86 | } else { |
Jakub Josef | bcd2e90 | 2017-06-13 14:40:41 +0200 | [diff] [blame] | 87 | common.successMsg("Change ${GERRIT_CHANGE_NUMBER} is already merged, no need to test them") |
| 88 | } |
Jakub Josef | 740a788 | 2017-06-01 14:54:01 +0200 | [diff] [blame] | 89 | } |
Martin Polreich | 4661cd9 | 2017-08-29 19:05:10 +0200 | [diff] [blame] | 90 | defaultGitUrl = "${GERRIT_SCHEME}://${GERRIT_NAME}@${GERRIT_HOST}:${GERRIT_PORT}/${GERRIT_PROJECT}" |
| 91 | defaultGitRef = GERRIT_REFSPEC |
Martin Polreich | a3e3012 | 2017-08-22 12:43:55 +0200 | [diff] [blame] | 92 | } else if (defaultGitRef && defaultGitUrl) { |
| 93 | checkouted = gerrit.gerritPatchsetCheckout(defaultGitUrl, defaultGitRef, "HEAD", CREDENTIALS_ID) |
Jakub Josef | bcd2e90 | 2017-06-13 14:40:41 +0200 | [diff] [blame] | 94 | } else { |
Jakub Josef | 5ce6a36 | 2017-03-31 13:41:17 +0200 | [diff] [blame] | 95 | throw new Exception("Cannot checkout gerrit patchset, GERRIT_REFSPEC and DEFAULT_GIT_REF is null") |
Jakub Josef | 8337931 | 2017-03-29 18:12:34 +0200 | [diff] [blame] | 96 | } |
Jakub Josef | c5a223a | 2017-03-01 14:40:08 +0100 | [diff] [blame] | 97 | } |
| 98 | stage("test") { |
Martin Polreich | a3e3012 | 2017-08-22 12:43:55 +0200 | [diff] [blame] | 99 | if (checkouted) { |
Ruslan Kamaldinov | 310ffc0 | 2017-08-08 16:07:42 +0400 | [diff] [blame] | 100 | sh("make clean") |
| 101 | sh("[ $SALT_VERSION != 'latest' ] || export SALT_VERSION=''; make test") |
Jakub Josef | c5a223a | 2017-03-01 14:40:08 +0100 | [diff] [blame] | 102 | } |
| 103 | } |
Jakub Josef | f64c639 | 2017-05-03 13:18:25 +0200 | [diff] [blame] | 104 | stage("kitchen") { |
Martin Polreich | a3e3012 | 2017-08-22 12:43:55 +0200 | [diff] [blame] | 105 | if (checkouted) { |
Jakub Josef | bcd2e90 | 2017-06-13 14:40:41 +0200 | [diff] [blame] | 106 | if (fileExists(".kitchen.yml")) { |
| 107 | common.infoMsg(".kitchen.yml found, running kitchen tests") |
Jakub Josef | bcd2e90 | 2017-06-13 14:40:41 +0200 | [diff] [blame] | 108 | def kitchenEnvs = [] |
Martin Polreich | 9b5f5ab | 2017-07-12 13:06:14 +0200 | [diff] [blame] | 109 | def filteredEnvs = [] |
Martin Polreich | a3e3012 | 2017-08-22 12:43:55 +0200 | [diff] [blame] | 110 | if (fileExists(".travis.yml")) { |
| 111 | common.infoMsg(".travis.yml file found.") |
Jakub Josef | bcd2e90 | 2017-06-13 14:40:41 +0200 | [diff] [blame] | 112 | def kitchenConfigYML = readYaml(file: ".travis.yml") |
Martin Polreich | a3e3012 | 2017-08-22 12:43:55 +0200 | [diff] [blame] | 113 | if (kitchenConfigYML.containsKey("env")) { |
| 114 | kitchenEnvs = kitchenConfigYML["env"] |
Ruslan Kamaldinov | 310ffc0 | 2017-08-08 16:07:42 +0400 | [diff] [blame] | 115 | } |
| 116 | } else { |
Martin Polreich | a3e3012 | 2017-08-22 12:43:55 +0200 | [diff] [blame] | 117 | common.warningMsg(".travis.yml file not found, suites must be passed via CUSTOM_KITCHEN_ENVS parameter.") |
Jakub Josef | bcd2e90 | 2017-06-13 14:40:41 +0200 | [diff] [blame] | 118 | } |
Martin Polreich | a3e3012 | 2017-08-22 12:43:55 +0200 | [diff] [blame] | 119 | common.infoMsg("Running kitchen testing in parallel mode") |
| 120 | if (CUSTOM_KITCHEN_ENVS != null && CUSTOM_KITCHEN_ENVS != '') { |
| 121 | kitchenEnvs = CUSTOM_KITCHEN_ENVS.tokenize('\n') |
| 122 | common.infoMsg("CUSTOM_KITCHEN_ENVS not empty. Running with custom enviroments: ${kitchenEnvs}") |
| 123 | } |
| 124 | if (kitchenEnvs != null && kitchenEnvs != '') { |
Cedric Hnyda | c87fc6c | 2017-09-22 13:12:36 +0000 | [diff] [blame] | 125 | def acc = 0 |
Martin Polreich | a3e3012 | 2017-08-22 12:43:55 +0200 | [diff] [blame] | 126 | common.infoMsg("Found " + kitchenEnvs.size() + " environment(s)") |
Martin Polreich | a3e3012 | 2017-08-22 12:43:55 +0200 | [diff] [blame] | 127 | for (int i = 0; i < kitchenEnvs.size(); i++) { |
chnyda | 85ea22d | 2017-10-23 13:05:16 +0200 | [diff] [blame] | 128 | futureFormulas << kitchenEnvs[i] |
Martin Polreich | a3e3012 | 2017-08-22 12:43:55 +0200 | [diff] [blame] | 129 | } |
chnyda | 85ea22d | 2017-10-23 13:05:16 +0200 | [diff] [blame] | 130 | setupRunner(defaultGitRef, defaultGitUrl) |
Martin Polreich | a3e3012 | 2017-08-22 12:43:55 +0200 | [diff] [blame] | 131 | } else { |
| 132 | common.warningMsg(".kitchen.yml file not found, no kitchen tests triggered.") |
| 133 | } |
Jakub Josef | f64c639 | 2017-05-03 13:18:25 +0200 | [diff] [blame] | 134 | } |
Jakub Josef | f64c639 | 2017-05-03 13:18:25 +0200 | [diff] [blame] | 135 | } |
| 136 | } |
chnyda | 85ea22d | 2017-10-23 13:05:16 +0200 | [diff] [blame] | 137 | if (failedFormulas) { |
| 138 | currentBuild.result = "FAILURE" |
| 139 | common.warningMsg("The following tests failed: ${failedFormulas}") |
| 140 | } |
Jakub Josef | c5a223a | 2017-03-01 14:40:08 +0100 | [diff] [blame] | 141 | } catch (Throwable e) { |
Martin Polreich | a3e3012 | 2017-08-22 12:43:55 +0200 | [diff] [blame] | 142 | // If there was an error or exception thrown, the build failed |
| 143 | currentBuild.result = "FAILURE" |
| 144 | currentBuild.description = currentBuild.description ? e.message + " " + currentBuild.description : e.message |
Martin Polreich | a3e3012 | 2017-08-22 12:43:55 +0200 | [diff] [blame] | 145 | throw e |
Jakub Josef | c5a223a | 2017-03-01 14:40:08 +0100 | [diff] [blame] | 146 | } finally { |
Martin Polreich | a3e3012 | 2017-08-22 12:43:55 +0200 | [diff] [blame] | 147 | if (currentBuild.result == "FAILURE" && fileExists(".kitchen/logs/kitchen.log")) { |
| 148 | common.errorMsg("----------------KITCHEN LOG:---------------") |
| 149 | println readFile(".kitchen/logs/kitchen.log") |
| 150 | } |
| 151 | common.sendNotification(currentBuild.result, "", ["slack"]) |
Jakub Josef | c5a223a | 2017-03-01 14:40:08 +0100 | [diff] [blame] | 152 | } |
Martin Polreich | a3e3012 | 2017-08-22 12:43:55 +0200 | [diff] [blame] | 153 | } |