blob: d63a78c8c132ea02254ae1f9b492749c6494614b [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
Jakub Josef0bbbd802017-05-22 20:28:44 +02006 * KITCHEN_TESTS_PARALLEL
Jakub Josef83379312017-03-29 18:12:34 +02007 */
Jakub Josefc5a223a2017-03-01 14:40:08 +01008def common = new com.mirantis.mk.Common()
9def 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 {
21 defaultGitRef = DEFAULT_GIT_REF
22 defaultGitUrl = DEFAULT_GIT_URL
23} catch (MissingPropertyException e) {
24 defaultGitRef = null
25 defaultGitUrl = null
26}
27
Jakub Josefbcd2e902017-06-13 14:40:41 +020028def checkouted = false
Jakub Josef83379312017-03-29 18:12:34 +020029
chnyda3b1d2a62017-06-13 10:08:12 +020030node("python") {
Jakub Josefc5a223a2017-03-01 14:40:08 +010031 try{
Jakub Josef0a02cff2017-05-11 14:16:22 +020032 stage("stop old tests"){
33 if (gerritRef) {
34 def runningTestBuildNums = _getRunningTriggeredTestsBuildNumbers(env["JOB_NAME"], GERRIT_CHANGE_NUMBER, GERRIT_PATCHSET_NUMBER)
35 for(int i=0; i<runningTestBuildNums.size(); i++){
36 common.infoMsg("Old test with run number ${runningTestBuildNums[i]} found, stopping")
37 Jenkins.instance.getItemByFullName(env["JOB_NAME"]).getBuildByNumber(runningTestBuildNums[i]).finish(hudson.model.Result.ABORTED, new java.io.IOException("Aborting build"));
38 }
39 }
40 }
Jakub Josefc5a223a2017-03-01 14:40:08 +010041 stage("checkout") {
Jakub Josef83379312017-03-29 18:12:34 +020042 if (gerritRef) {
43 // job is triggered by Gerrit
Jakub Josefb2235902017-06-16 12:49:16 +020044 def gerritChange = gerrit.getGerritChange(GERRIT_NAME, GERRIT_HOST, GERRIT_CHANGE_NUMBER, CREDENTIALS_ID, true)
45 // test if gerrit change is already Verified
Jakub Josef2ed50812017-06-16 14:56:58 +020046 if(gerrit.patchsetHasApproval(gerritChange.currentPatchSet,"Verified","+")){
Jakub Josef894f1e32017-06-19 16:24:19 +000047 common.successMsg("Gerrit change ${GERRIT_CHANGE_NUMBER} patchset ${GERRIT_PATCHSET_NUMBER} already has Verified, skipping tests") // do nothing
Jakub Josefbcd2e902017-06-13 14:40:41 +020048 // test WIP contains in commit message
Jakub Josefb2235902017-06-16 12:49:16 +020049 }else if(gerritChange.commitMessage.contains("WIP")){
Jakub Josef894f1e32017-06-19 16:24:19 +000050 common.successMsg("Commit message contains WIP, skipping tests") // do nothing
Jakub Josefbcd2e902017-06-13 14:40:41 +020051 }else{
52 // test if change aren't already merged
53 def merged = gerritChange.status == "MERGED"
54 if(!merged){
55 checkouted = gerrit.gerritPatchsetCheckout ([
56 credentialsId : CREDENTIALS_ID
57 ])
58 } else{
59 common.successMsg("Change ${GERRIT_CHANGE_NUMBER} is already merged, no need to test them")
60 }
Jakub Josef740a7882017-06-01 14:54:01 +020061 }
Jakub Josef83379312017-03-29 18:12:34 +020062 } else if(defaultGitRef && defaultGitUrl) {
Jakub Josefe1407ac2017-03-30 14:10:19 +020063 checkouted = gerrit.gerritPatchsetCheckout(defaultGitUrl, defaultGitRef, "HEAD", CREDENTIALS_ID)
Jakub Josefbcd2e902017-06-13 14:40:41 +020064 } else {
Jakub Josef5ce6a362017-03-31 13:41:17 +020065 throw new Exception("Cannot checkout gerrit patchset, GERRIT_REFSPEC and DEFAULT_GIT_REF is null")
Jakub Josef83379312017-03-29 18:12:34 +020066 }
Jakub Josefc5a223a2017-03-01 14:40:08 +010067 }
68 stage("test") {
Jakub Josef83379312017-03-29 18:12:34 +020069 if(checkouted){
Jakub Josefbcd2e902017-06-13 14:40:41 +020070 wrap([$class: 'AnsiColorBuildWrapper']) {
71 sh("make clean")
72 sh("[ $SALT_VERSION != 'latest' ] || export SALT_VERSION=''; make test")
Jakub Josef83379312017-03-29 18:12:34 +020073 }
Jakub Josefc5a223a2017-03-01 14:40:08 +010074 }
75 }
Jakub Joseff64c6392017-05-03 13:18:25 +020076 stage("kitchen") {
Jakub Josefbcd2e902017-06-13 14:40:41 +020077 if(checkouted){
78 if (fileExists(".kitchen.yml")) {
79 common.infoMsg(".kitchen.yml found, running kitchen tests")
80 ruby.ensureRubyEnv()
81 def kitchenEnvs = []
Martin Polreich9b5f5ab2017-07-12 13:06:14 +020082 def filteredEnvs = []
Jakub Josefbcd2e902017-06-13 14:40:41 +020083 if(fileExists(".travis.yml")){
84 common.infoMsg(".travis.yml found, running custom kitchen init")
85 def kitchenConfigYML = readYaml(file: ".travis.yml")
Jakub Josef904972f2017-07-17 15:57:35 +020086 if(kitchenConfigYML.containsKey("env")){
87 kitchenEnvs=kitchenConfigYML["env"]
88 }
Jakub Josefbcd2e902017-06-13 14:40:41 +020089 def kitchenInit = kitchenConfigYML["install"]
90 def kitchenInstalled = false
91 if(kitchenInit && !kitchenInit.isEmpty()){
Jakub Josef96254e22017-07-17 14:36:02 +020092 for(int i=0; i<kitchenInit.size(); i++){
Jakub Josefbcd2e902017-06-13 14:40:41 +020093 if(kitchenInit[i].trim().startsWith("test -e Gemfile")){ //found Gemfile config
94 common.infoMsg("Custom Gemfile configuration found, using them")
95 ruby.installKitchen(kitchenInit[i].trim())
96 kitchenInstalled = true
97 }
Jakub Josefcca1e312017-05-23 15:21:35 +020098 }
Jakub Josefd672e402017-05-23 12:03:44 +020099 }
Jakub Josefbcd2e902017-06-13 14:40:41 +0200100 if(!kitchenInstalled){
101 ruby.installKitchen()
Jakub Josef0bbbd802017-05-22 20:28:44 +0200102 }
103 }else{
Jakub Josefbcd2e902017-06-13 14:40:41 +0200104 common.infoMsg(".travis.yml not found, running default kitchen init")
105 ruby.installKitchen()
Jakub Josef0bbbd802017-05-22 20:28:44 +0200106 }
Jakub Josefbcd2e902017-06-13 14:40:41 +0200107 common.infoMsg("Running kitchen testing, parallel mode: " + KITCHEN_TESTS_PARALLEL.toBoolean())
108 wrap([$class: 'AnsiColorBuildWrapper']) {
Martin Polreich62336792017-07-18 14:12:14 +0200109 if(CUSTOM_KITCHEN_ENVS != null && CUSTOM_KITCHEN_ENVS != ''){
110 filteredEnvs = CUSTOM_KITCHEN_ENVS.tokenize('\n')
111 } else {
112 filteredEnvs = ruby.filterKitchenEnvs(kitchenEnvs).unique()
Jakub Josefbcd2e902017-06-13 14:40:41 +0200113 }
Martin Polreich62336792017-07-18 14:12:14 +0200114 // Allow custom filteredEnvs in case of empty kitchenEnvs
115 if((kitchenEnvs && !kitchenEnvs.isEmpty() && !filteredEnvs.isEmpty()) || ((kitchenEnvs==null || kitchenEnvs=='') && !filteredEnvs.isEmpty())){
116 for(int i=0; i<filteredEnvs.size(); i++){
Martin Polreich9ad6c132017-07-19 15:01:09 +0200117 common.infoMsg("Found " + filteredEnvs.size() + " environment, kitchen running with env number " + (i+1) + ": " + filteredEnvs[i].trim())
Martin Polreich62336792017-07-18 14:12:14 +0200118 ruby.runKitchenTests(filteredEnvs[i].trim(), KITCHEN_TESTS_PARALLEL.toBoolean())
119 }
120 } else {
Jakub Josefbcd2e902017-06-13 14:40:41 +0200121 ruby.runKitchenTests("", KITCHEN_TESTS_PARALLEL.toBoolean())
122 }
123 }
124 } else {
125 common.infoMsg(".kitchen.yml not found")
Jakub Joseff64c6392017-05-03 13:18:25 +0200126 }
Jakub Joseff64c6392017-05-03 13:18:25 +0200127 }
128 }
Jakub Josefc5a223a2017-03-01 14:40:08 +0100129 } catch (Throwable e) {
130 // If there was an error or exception thrown, the build failed
131 currentBuild.result = "FAILURE"
Jakub Josefd11bdcc2017-06-13 17:02:43 +0200132 ruby.runKitchenCommand("destroy")
Jakub Josefc5a223a2017-03-01 14:40:08 +0100133 throw e
134 } finally {
Jakub Josef52baaa42017-05-04 12:20:54 +0200135 if(currentBuild.result == "FAILURE" && fileExists(".kitchen/logs/kitchen.log")){
Jakub Joseff96ebc52017-05-04 12:37:19 +0200136 common.errorMsg("----------------KITCHEN LOG:---------------")
Jakub Josef52baaa42017-05-04 12:20:54 +0200137 println readFile(".kitchen/logs/kitchen.log")
138 }
Jakub Josefc5a223a2017-03-01 14:40:08 +0100139 common.sendNotification(currentBuild.result,"",["slack"])
140 }
Jakub Josefd283f8c2017-05-10 18:20:11 +0200141}
142
143@NonCPS
144def _getRunningTriggeredTestsBuildNumbers(jobName, gerritChangeNumber, excludePatchsetNumber){
Jakub Josef0a02cff2017-05-11 14:16:22 +0200145 def gerrit = new com.mirantis.mk.Gerrit()
146 def jenkinsUtils = new com.mirantis.mk.JenkinsUtils()
147 def triggeredBuilds= gerrit.getGerritTriggeredBuilds(jenkinsUtils.getJobRunningBuilds(jobName), gerritChangeNumber, excludePatchsetNumber)
148 def buildNums =[]
149 for(int i=0;i<triggeredBuilds.size();i++){
Jakub Josefd35d5562017-05-12 16:12:22 +0200150 buildNums.add(triggeredBuilds[i].number)
Jakub Josef0a02cff2017-05-11 14:16:22 +0200151 }
152 return buildNums
Tomáš Kukrál8d2d6a32017-05-11 10:18:33 +0200153}
Jakub Josefb2235902017-06-16 12:49:16 +0200154