blob: 6baa25e8e36bb3e28c6f7e431afb289b51c703c3 [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
44 checkouted = gerrit.gerritPatchsetCheckout ([
Jakub Josefc5a223a2017-03-01 14:40:08 +010045 credentialsId : CREDENTIALS_ID
Jakub Josef83379312017-03-29 18:12:34 +020046 ])
47 } else if(defaultGitRef && defaultGitUrl) {
Jakub Josefe1407ac2017-03-30 14:10:19 +020048 checkouted = gerrit.gerritPatchsetCheckout(defaultGitUrl, defaultGitRef, "HEAD", CREDENTIALS_ID)
Jakub Josef83379312017-03-29 18:12:34 +020049 }
50 if(!checkouted){
Jakub Josef5ce6a362017-03-31 13:41:17 +020051 throw new Exception("Cannot checkout gerrit patchset, GERRIT_REFSPEC and DEFAULT_GIT_REF is null")
Jakub Josef83379312017-03-29 18:12:34 +020052 }
Jakub Josefc5a223a2017-03-01 14:40:08 +010053 }
54 stage("test") {
Jakub Josef83379312017-03-29 18:12:34 +020055 if(checkouted){
56 wrap([$class: 'AnsiColorBuildWrapper']) {
57 sh("make clean")
58 sh("[ $SALT_VERSION != 'latest' ] || export SALT_VERSION=''; make test")
59 }
Jakub Josefc5a223a2017-03-01 14:40:08 +010060 }
61 }
Jakub Joseff64c6392017-05-03 13:18:25 +020062 stage("kitchen") {
63 if (fileExists(".kitchen.yml")) {
Jakub Josef52baaa42017-05-04 12:20:54 +020064 common.infoMsg(".kitchen.yml found, running kitchen tests")
Jakub Joseff64c6392017-05-03 13:18:25 +020065 ruby.ensureRubyEnv()
Jakub Josef0bbbd802017-05-22 20:28:44 +020066 def kitchenEnvs = []
67 if(fileExists(".travis.yml")){
68 common.infoMsg(".travis.yml found, running custom kitchen init")
69 def kitchenConfigYML = readYaml(file: ".travis.yml")
70 kitchenEnvs=kitchenConfigYML["env"]
71 def kitchenInit = kitchenConfigYML["install"]
Jakub Josefcca1e312017-05-23 15:21:35 +020072 def kitchenInstalled = false
73 if(kitchenInit && !kitchenInit.isEmpty()){
74 for(int i=0;i<kitchenInit.size();i++){
75 if(kitchenInit[i].trim().startsWith("test -e Gemfile")){ //found Gemfile config
Jakub Joseff3736852017-05-23 15:39:15 +020076 common.infoMsg("Custom Gemfile configuration found, using them")
Jakub Josefcca1e312017-05-23 15:21:35 +020077 ruby.installKitchen(kitchenInit[i].trim())
78 kitchenInstalled = true
79 }
Jakub Josefd672e402017-05-23 12:03:44 +020080 }
Jakub Josef0bbbd802017-05-22 20:28:44 +020081 }
Jakub Josefcca1e312017-05-23 15:21:35 +020082 if(!kitchenInstalled){
83 ruby.installKitchen()
84 }
Jakub Josef0bbbd802017-05-22 20:28:44 +020085 }else{
86 common.infoMsg(".travis.yml not found, running default kitchen init")
87 ruby.installKitchen()
88 }
Jakub Joseff64c6392017-05-03 13:18:25 +020089 wrap([$class: 'AnsiColorBuildWrapper']) {
Jakub Joseff3736852017-05-23 15:39:15 +020090 common.infoMsg("Running kitchen testing, parallel mode: " + KITCHEN_TESTS_PARALLEL.toBoolean())
Jakub Josef0bbbd802017-05-22 20:28:44 +020091 if(!kitchenEnvs.isEmpty()){
92 for(int i=0;i<kitchenEnvs.size();i++){
Jakub Joseff3736852017-05-23 15:39:15 +020093 common.infoMsg("Found multiple environment, kitchen running with env: " + kitchenEnvs[i])
Jakub Josef0bbbd802017-05-22 20:28:44 +020094 ruby.runKitchenTests(kitchenEnvs[i], KITCHEN_TESTS_PARALLEL.toBoolean())
95 }
96 }else{
97 ruby.runKitchenTests("", KITCHEN_TESTS_PARALLEL.toBoolean())
98 }
Jakub Joseff64c6392017-05-03 13:18:25 +020099 }
100 } else {
101 common.infoMsg(".kitchen.yml not found")
102 }
103 }
Jakub Josefc5a223a2017-03-01 14:40:08 +0100104 } catch (Throwable e) {
105 // If there was an error or exception thrown, the build failed
106 currentBuild.result = "FAILURE"
107 throw e
108 } finally {
Jakub Josef52baaa42017-05-04 12:20:54 +0200109 if(currentBuild.result == "FAILURE" && fileExists(".kitchen/logs/kitchen.log")){
Jakub Joseff96ebc52017-05-04 12:37:19 +0200110 common.errorMsg("----------------KITCHEN LOG:---------------")
Jakub Josef52baaa42017-05-04 12:20:54 +0200111 println readFile(".kitchen/logs/kitchen.log")
112 }
Jakub Josefc5a223a2017-03-01 14:40:08 +0100113 common.sendNotification(currentBuild.result,"",["slack"])
114 }
Jakub Josefd283f8c2017-05-10 18:20:11 +0200115}
116
117@NonCPS
118def _getRunningTriggeredTestsBuildNumbers(jobName, gerritChangeNumber, excludePatchsetNumber){
Jakub Josef0a02cff2017-05-11 14:16:22 +0200119 def gerrit = new com.mirantis.mk.Gerrit()
120 def jenkinsUtils = new com.mirantis.mk.JenkinsUtils()
121 def triggeredBuilds= gerrit.getGerritTriggeredBuilds(jenkinsUtils.getJobRunningBuilds(jobName), gerritChangeNumber, excludePatchsetNumber)
122 def buildNums =[]
123 for(int i=0;i<triggeredBuilds.size();i++){
Jakub Josefd35d5562017-05-12 16:12:22 +0200124 buildNums.add(triggeredBuilds[i].number)
Jakub Josef0a02cff2017-05-11 14:16:22 +0200125 }
126 return buildNums
Tomáš Kukrál8d2d6a32017-05-11 10:18:33 +0200127}