blob: 37983c0c89d3872182b94706105abcf97b7c468f [file] [log] [blame]
Jakub Josef83379312017-03-29 18:12:34 +02001/**
2 * Test salt formulas pipeline
3 * DEFAULT_GIT_REF
4 * DEFAULT_GIT_URL
5 * CREDENTIALS_ID
Cedric Hnydac87fc6c2017-09-22 13:12:36 +00006 * KITCHEN_TESTS_PARALLEL
Jakub Josef83379312017-03-29 18:12:34 +02007 */
chnyda85ea22d2017-10-23 13:05:16 +02008common = new com.mirantis.mk.Common()
Jakub Josefc5a223a2017-03-01 14:40:08 +01009def gerrit = new com.mirantis.mk.Gerrit()
Jakub Joseff64c6392017-05-03 13:18:25 +020010def ruby = new com.mirantis.mk.Ruby()
Jakub Josef83379312017-03-29 18:12:34 +020011
12def gerritRef
13try {
14 gerritRef = GERRIT_REFSPEC
15} catch (MissingPropertyException e) {
16 gerritRef = null
17}
18
19def defaultGitRef, defaultGitUrl
20try {
Martin Polreicha3e30122017-08-22 12:43:55 +020021 defaultGitRef = DEFAULT_GIT_REF
22 defaultGitUrl = DEFAULT_GIT_URL
Jakub Josef83379312017-03-29 18:12:34 +020023} catch (MissingPropertyException e) {
Martin Polreicha3e30122017-08-22 12:43:55 +020024 defaultGitRef = null
25 defaultGitUrl = null
Jakub Josef83379312017-03-29 18:12:34 +020026}
27
Jakub Josefbcd2e902017-06-13 14:40:41 +020028def checkouted = false
Jakub Josef83379312017-03-29 18:12:34 +020029
chnyda85ea22d2017-10-23 13:05:16 +020030futureFormulas = []
31failedFormulas = []
32
33def 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
55def 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
chnyda3b1d2a62017-06-13 10:08:12 +020067node("python") {
Martin Polreicha3e30122017-08-22 12:43:55 +020068 try {
Jakub Josefc5a223a2017-03-01 14:40:08 +010069 stage("checkout") {
Jakub Josef83379312017-03-29 18:12:34 +020070 if (gerritRef) {
71 // job is triggered by Gerrit
Jakub Josefb2235902017-06-16 12:49:16 +020072 def gerritChange = gerrit.getGerritChange(GERRIT_NAME, GERRIT_HOST, GERRIT_CHANGE_NUMBER, CREDENTIALS_ID, true)
73 // test if gerrit change is already Verified
Martin Polreicha3e30122017-08-22 12:43:55 +020074 if (gerrit.patchsetHasApproval(gerritChange.currentPatchSet, "Verified", "+")) {
Jakub Josef894f1e32017-06-19 16:24:19 +000075 common.successMsg("Gerrit change ${GERRIT_CHANGE_NUMBER} patchset ${GERRIT_PATCHSET_NUMBER} already has Verified, skipping tests") // do nothing
Martin Polreicha3e30122017-08-22 12:43:55 +020076 // test WIP contains in commit message
77 } else if (gerritChange.commitMessage.contains("WIP")) {
Jakub Josef894f1e32017-06-19 16:24:19 +000078 common.successMsg("Commit message contains WIP, skipping tests") // do nothing
Martin Polreicha3e30122017-08-22 12:43:55 +020079 } else {
Jakub Josefbcd2e902017-06-13 14:40:41 +020080 // test if change aren't already merged
81 def merged = gerritChange.status == "MERGED"
Martin Polreicha3e30122017-08-22 12:43:55 +020082 if (!merged) {
83 checkouted = gerrit.gerritPatchsetCheckout([
84 credentialsId: CREDENTIALS_ID
Jakub Josefbcd2e902017-06-13 14:40:41 +020085 ])
Martin Polreicha3e30122017-08-22 12:43:55 +020086 } else {
Jakub Josefbcd2e902017-06-13 14:40:41 +020087 common.successMsg("Change ${GERRIT_CHANGE_NUMBER} is already merged, no need to test them")
88 }
Jakub Josef740a7882017-06-01 14:54:01 +020089 }
Martin Polreich4661cd92017-08-29 19:05:10 +020090 defaultGitUrl = "${GERRIT_SCHEME}://${GERRIT_NAME}@${GERRIT_HOST}:${GERRIT_PORT}/${GERRIT_PROJECT}"
91 defaultGitRef = GERRIT_REFSPEC
Martin Polreicha3e30122017-08-22 12:43:55 +020092 } else if (defaultGitRef && defaultGitUrl) {
93 checkouted = gerrit.gerritPatchsetCheckout(defaultGitUrl, defaultGitRef, "HEAD", CREDENTIALS_ID)
Jakub Josefbcd2e902017-06-13 14:40:41 +020094 } else {
Jakub Josef5ce6a362017-03-31 13:41:17 +020095 throw new Exception("Cannot checkout gerrit patchset, GERRIT_REFSPEC and DEFAULT_GIT_REF is null")
Jakub Josef83379312017-03-29 18:12:34 +020096 }
Jakub Josefc5a223a2017-03-01 14:40:08 +010097 }
98 stage("test") {
Martin Polreicha3e30122017-08-22 12:43:55 +020099 if (checkouted) {
azvyagintsev3a2e0352018-01-10 12:41:29 +0200100 try {
101 saltVersion = SALT_VERSION
102 } catch (MissingPropertyException e) {
103 saltVersion = "latest"
104 }
105 withEnv(["SALT_VERSION=${saltVersion}"]) {
106 sh("make clean && make test")
107 }
Jakub Josefc5a223a2017-03-01 14:40:08 +0100108 }
109 }
Jakub Joseff64c6392017-05-03 13:18:25 +0200110 stage("kitchen") {
Martin Polreicha3e30122017-08-22 12:43:55 +0200111 if (checkouted) {
Jakub Josefbcd2e902017-06-13 14:40:41 +0200112 if (fileExists(".kitchen.yml")) {
113 common.infoMsg(".kitchen.yml found, running kitchen tests")
Jakub Josefbcd2e902017-06-13 14:40:41 +0200114 def kitchenEnvs = []
Martin Polreich9b5f5ab2017-07-12 13:06:14 +0200115 def filteredEnvs = []
Martin Polreicha3e30122017-08-22 12:43:55 +0200116 if (fileExists(".travis.yml")) {
117 common.infoMsg(".travis.yml file found.")
Jakub Josefbcd2e902017-06-13 14:40:41 +0200118 def kitchenConfigYML = readYaml(file: ".travis.yml")
Martin Polreicha3e30122017-08-22 12:43:55 +0200119 if (kitchenConfigYML.containsKey("env")) {
120 kitchenEnvs = kitchenConfigYML["env"]
Ruslan Kamaldinov310ffc02017-08-08 16:07:42 +0400121 }
122 } else {
Martin Polreicha3e30122017-08-22 12:43:55 +0200123 common.warningMsg(".travis.yml file not found, suites must be passed via CUSTOM_KITCHEN_ENVS parameter.")
Jakub Josefbcd2e902017-06-13 14:40:41 +0200124 }
Martin Polreicha3e30122017-08-22 12:43:55 +0200125 common.infoMsg("Running kitchen testing in parallel mode")
126 if (CUSTOM_KITCHEN_ENVS != null && CUSTOM_KITCHEN_ENVS != '') {
127 kitchenEnvs = CUSTOM_KITCHEN_ENVS.tokenize('\n')
128 common.infoMsg("CUSTOM_KITCHEN_ENVS not empty. Running with custom enviroments: ${kitchenEnvs}")
129 }
130 if (kitchenEnvs != null && kitchenEnvs != '') {
Cedric Hnydac87fc6c2017-09-22 13:12:36 +0000131 def acc = 0
Martin Polreicha3e30122017-08-22 12:43:55 +0200132 common.infoMsg("Found " + kitchenEnvs.size() + " environment(s)")
Martin Polreicha3e30122017-08-22 12:43:55 +0200133 for (int i = 0; i < kitchenEnvs.size(); i++) {
chnyda85ea22d2017-10-23 13:05:16 +0200134 futureFormulas << kitchenEnvs[i]
Martin Polreicha3e30122017-08-22 12:43:55 +0200135 }
chnyda85ea22d2017-10-23 13:05:16 +0200136 setupRunner(defaultGitRef, defaultGitUrl)
Martin Polreicha3e30122017-08-22 12:43:55 +0200137 } else {
138 common.warningMsg(".kitchen.yml file not found, no kitchen tests triggered.")
139 }
Jakub Joseff64c6392017-05-03 13:18:25 +0200140 }
Jakub Joseff64c6392017-05-03 13:18:25 +0200141 }
142 }
chnyda85ea22d2017-10-23 13:05:16 +0200143 if (failedFormulas) {
144 currentBuild.result = "FAILURE"
145 common.warningMsg("The following tests failed: ${failedFormulas}")
146 }
Jakub Josefc5a223a2017-03-01 14:40:08 +0100147 } catch (Throwable e) {
Martin Polreicha3e30122017-08-22 12:43:55 +0200148 // If there was an error or exception thrown, the build failed
149 currentBuild.result = "FAILURE"
150 currentBuild.description = currentBuild.description ? e.message + " " + currentBuild.description : e.message
Martin Polreicha3e30122017-08-22 12:43:55 +0200151 throw e
Jakub Josefc5a223a2017-03-01 14:40:08 +0100152 } finally {
Martin Polreicha3e30122017-08-22 12:43:55 +0200153 if (currentBuild.result == "FAILURE" && fileExists(".kitchen/logs/kitchen.log")) {
154 common.errorMsg("----------------KITCHEN LOG:---------------")
155 println readFile(".kitchen/logs/kitchen.log")
156 }
157 common.sendNotification(currentBuild.result, "", ["slack"])
Jakub Josefc5a223a2017-03-01 14:40:08 +0100158 }
azvyagintsev3a2e0352018-01-10 12:41:29 +0200159}