blob: d305b16e3382b53f5b258280e37293d197ff252f [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
28def checkouted = false;
29
Jakub Josef52baaa42017-05-04 12:20:54 +020030node("python&&docker") {
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 Josef740a7882017-06-01 14:54:01 +020044 // test if change aren't already merged
45 def gerritChange = gerrit.getGerritChange(GERRIT_NAME, GERRIT_HOST, GERRIT_CHANGE_NUMBER, CREDENTIALS_ID)
46 def merged = gerritChange.status == "MERGED"
47 if(!merged){
48 checkouted = gerrit.gerritPatchsetCheckout ([
49 credentialsId : CREDENTIALS_ID
50 ])
51 } else{
52 common.successMsg("Change ${GERRIT_CHANGE_NUMBER} is already merged, no need to test them")
53 }
Jakub Josef83379312017-03-29 18:12:34 +020054 } else if(defaultGitRef && defaultGitUrl) {
Jakub Josefe1407ac2017-03-30 14:10:19 +020055 checkouted = gerrit.gerritPatchsetCheckout(defaultGitUrl, defaultGitRef, "HEAD", CREDENTIALS_ID)
Jakub Josef83379312017-03-29 18:12:34 +020056 }
57 if(!checkouted){
Jakub Josef5ce6a362017-03-31 13:41:17 +020058 throw new Exception("Cannot checkout gerrit patchset, GERRIT_REFSPEC and DEFAULT_GIT_REF is null")
Jakub Josef83379312017-03-29 18:12:34 +020059 }
Jakub Josefc5a223a2017-03-01 14:40:08 +010060 }
61 stage("test") {
Jakub Josef83379312017-03-29 18:12:34 +020062 if(checkouted){
63 wrap([$class: 'AnsiColorBuildWrapper']) {
64 sh("make clean")
65 sh("[ $SALT_VERSION != 'latest' ] || export SALT_VERSION=''; make test")
66 }
Jakub Josefc5a223a2017-03-01 14:40:08 +010067 }
68 }
Jakub Joseff64c6392017-05-03 13:18:25 +020069 stage("kitchen") {
70 if (fileExists(".kitchen.yml")) {
Jakub Josef52baaa42017-05-04 12:20:54 +020071 common.infoMsg(".kitchen.yml found, running kitchen tests")
Jakub Joseff64c6392017-05-03 13:18:25 +020072 ruby.ensureRubyEnv()
Jakub Josef0bbbd802017-05-22 20:28:44 +020073 def kitchenEnvs = []
74 if(fileExists(".travis.yml")){
75 common.infoMsg(".travis.yml found, running custom kitchen init")
76 def kitchenConfigYML = readYaml(file: ".travis.yml")
77 kitchenEnvs=kitchenConfigYML["env"]
78 def kitchenInit = kitchenConfigYML["install"]
Jakub Josefcca1e312017-05-23 15:21:35 +020079 def kitchenInstalled = false
80 if(kitchenInit && !kitchenInit.isEmpty()){
81 for(int i=0;i<kitchenInit.size();i++){
82 if(kitchenInit[i].trim().startsWith("test -e Gemfile")){ //found Gemfile config
Jakub Joseff3736852017-05-23 15:39:15 +020083 common.infoMsg("Custom Gemfile configuration found, using them")
Jakub Josefcca1e312017-05-23 15:21:35 +020084 ruby.installKitchen(kitchenInit[i].trim())
85 kitchenInstalled = true
86 }
Jakub Josefd672e402017-05-23 12:03:44 +020087 }
Jakub Josef0bbbd802017-05-22 20:28:44 +020088 }
Jakub Josefcca1e312017-05-23 15:21:35 +020089 if(!kitchenInstalled){
90 ruby.installKitchen()
91 }
Jakub Josef0bbbd802017-05-22 20:28:44 +020092 }else{
93 common.infoMsg(".travis.yml not found, running default kitchen init")
94 ruby.installKitchen()
95 }
Jakub Josef700aee22017-06-05 18:49:03 +020096 common.infoMsg("Running kitchen testing, parallel mode: " + KITCHEN_TESTS_PARALLEL.toBoolean())
Jakub Joseff64c6392017-05-03 13:18:25 +020097 wrap([$class: 'AnsiColorBuildWrapper']) {
Jakub Josef56dc7ce2017-06-07 12:41:06 +020098 if(kitchenEnvs && !kitchenEnvs.isEmpty()){
Jakub Josef700aee22017-06-05 18:49:03 +020099 common.infoMsg("Found multiple environment, first running kitchen without custom env")
100 ruby.runKitchenTests("", KITCHEN_TESTS_PARALLEL.toBoolean())
Jakub Josef0bbbd802017-05-22 20:28:44 +0200101 for(int i=0;i<kitchenEnvs.size();i++){
Jakub Joseff3736852017-05-23 15:39:15 +0200102 common.infoMsg("Found multiple environment, kitchen running with env: " + kitchenEnvs[i])
Jakub Josef0bbbd802017-05-22 20:28:44 +0200103 ruby.runKitchenTests(kitchenEnvs[i], KITCHEN_TESTS_PARALLEL.toBoolean())
104 }
105 }else{
106 ruby.runKitchenTests("", KITCHEN_TESTS_PARALLEL.toBoolean())
107 }
Jakub Joseff64c6392017-05-03 13:18:25 +0200108 }
109 } else {
110 common.infoMsg(".kitchen.yml not found")
111 }
112 }
Jakub Josefc5a223a2017-03-01 14:40:08 +0100113 } catch (Throwable e) {
114 // If there was an error or exception thrown, the build failed
115 currentBuild.result = "FAILURE"
116 throw e
117 } finally {
Jakub Josef52baaa42017-05-04 12:20:54 +0200118 if(currentBuild.result == "FAILURE" && fileExists(".kitchen/logs/kitchen.log")){
Jakub Joseff96ebc52017-05-04 12:37:19 +0200119 common.errorMsg("----------------KITCHEN LOG:---------------")
Jakub Josef52baaa42017-05-04 12:20:54 +0200120 println readFile(".kitchen/logs/kitchen.log")
121 }
Jakub Josefc5a223a2017-03-01 14:40:08 +0100122 common.sendNotification(currentBuild.result,"",["slack"])
123 }
Jakub Josefd283f8c2017-05-10 18:20:11 +0200124}
125
126@NonCPS
127def _getRunningTriggeredTestsBuildNumbers(jobName, gerritChangeNumber, excludePatchsetNumber){
Jakub Josef0a02cff2017-05-11 14:16:22 +0200128 def gerrit = new com.mirantis.mk.Gerrit()
129 def jenkinsUtils = new com.mirantis.mk.JenkinsUtils()
130 def triggeredBuilds= gerrit.getGerritTriggeredBuilds(jenkinsUtils.getJobRunningBuilds(jobName), gerritChangeNumber, excludePatchsetNumber)
131 def buildNums =[]
132 for(int i=0;i<triggeredBuilds.size();i++){
Jakub Josefd35d5562017-05-12 16:12:22 +0200133 buildNums.add(triggeredBuilds[i].number)
Jakub Josef0a02cff2017-05-11 14:16:22 +0200134 }
135 return buildNums
Tomáš Kukrál8d2d6a32017-05-11 10:18:33 +0200136}