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 |
Jakub Josef | 0bbbd80 | 2017-05-22 20:28:44 +0200 | [diff] [blame] | 6 | * KITCHEN_TESTS_PARALLEL |
Jakub Josef | 8337931 | 2017-03-29 18:12:34 +0200 | [diff] [blame] | 7 | */ |
Jakub Josef | c5a223a | 2017-03-01 14:40:08 +0100 | [diff] [blame] | 8 | def common = new com.mirantis.mk.Common() |
| 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 { |
| 21 | defaultGitRef = DEFAULT_GIT_REF |
| 22 | defaultGitUrl = DEFAULT_GIT_URL |
| 23 | } catch (MissingPropertyException e) { |
| 24 | defaultGitRef = null |
| 25 | defaultGitUrl = null |
| 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 | 3b1d2a6 | 2017-06-13 10:08:12 +0200 | [diff] [blame] | 30 | node("python") { |
Jakub Josef | c5a223a | 2017-03-01 14:40:08 +0100 | [diff] [blame] | 31 | try{ |
Jakub Josef | 0a02cff | 2017-05-11 14:16:22 +0200 | [diff] [blame] | 32 | 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 Josef | c5a223a | 2017-03-01 14:40:08 +0100 | [diff] [blame] | 41 | stage("checkout") { |
Jakub Josef | 8337931 | 2017-03-29 18:12:34 +0200 | [diff] [blame] | 42 | if (gerritRef) { |
| 43 | // job is triggered by Gerrit |
Jakub Josef | b223590 | 2017-06-16 12:49:16 +0200 | [diff] [blame] | 44 | def gerritChange = gerrit.getGerritChange(GERRIT_NAME, GERRIT_HOST, GERRIT_CHANGE_NUMBER, CREDENTIALS_ID, true) |
| 45 | // test if gerrit change is already Verified |
Jakub Josef | 2ed5081 | 2017-06-16 14:56:58 +0200 | [diff] [blame] | 46 | if(gerrit.patchsetHasApproval(gerritChange.currentPatchSet,"Verified","+")){ |
Jakub Josef | 894f1e3 | 2017-06-19 16:24:19 +0000 | [diff] [blame] | 47 | common.successMsg("Gerrit change ${GERRIT_CHANGE_NUMBER} patchset ${GERRIT_PATCHSET_NUMBER} already has Verified, skipping tests") // do nothing |
Jakub Josef | bcd2e90 | 2017-06-13 14:40:41 +0200 | [diff] [blame] | 48 | // test WIP contains in commit message |
Jakub Josef | b223590 | 2017-06-16 12:49:16 +0200 | [diff] [blame] | 49 | }else if(gerritChange.commitMessage.contains("WIP")){ |
Jakub Josef | 894f1e3 | 2017-06-19 16:24:19 +0000 | [diff] [blame] | 50 | common.successMsg("Commit message contains WIP, skipping tests") // do nothing |
Jakub Josef | bcd2e90 | 2017-06-13 14:40:41 +0200 | [diff] [blame] | 51 | }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 Josef | 740a788 | 2017-06-01 14:54:01 +0200 | [diff] [blame] | 61 | } |
Jakub Josef | 8337931 | 2017-03-29 18:12:34 +0200 | [diff] [blame] | 62 | } else if(defaultGitRef && defaultGitUrl) { |
Jakub Josef | e1407ac | 2017-03-30 14:10:19 +0200 | [diff] [blame] | 63 | checkouted = gerrit.gerritPatchsetCheckout(defaultGitUrl, defaultGitRef, "HEAD", CREDENTIALS_ID) |
Jakub Josef | bcd2e90 | 2017-06-13 14:40:41 +0200 | [diff] [blame] | 64 | } else { |
Jakub Josef | 5ce6a36 | 2017-03-31 13:41:17 +0200 | [diff] [blame] | 65 | 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] | 66 | } |
Jakub Josef | c5a223a | 2017-03-01 14:40:08 +0100 | [diff] [blame] | 67 | } |
| 68 | stage("test") { |
Jakub Josef | 8337931 | 2017-03-29 18:12:34 +0200 | [diff] [blame] | 69 | if(checkouted){ |
Jakub Josef | bcd2e90 | 2017-06-13 14:40:41 +0200 | [diff] [blame] | 70 | wrap([$class: 'AnsiColorBuildWrapper']) { |
| 71 | sh("make clean") |
| 72 | sh("[ $SALT_VERSION != 'latest' ] || export SALT_VERSION=''; make test") |
Jakub Josef | 8337931 | 2017-03-29 18:12:34 +0200 | [diff] [blame] | 73 | } |
Jakub Josef | c5a223a | 2017-03-01 14:40:08 +0100 | [diff] [blame] | 74 | } |
| 75 | } |
Jakub Josef | f64c639 | 2017-05-03 13:18:25 +0200 | [diff] [blame] | 76 | stage("kitchen") { |
Jakub Josef | bcd2e90 | 2017-06-13 14:40:41 +0200 | [diff] [blame] | 77 | if(checkouted){ |
| 78 | if (fileExists(".kitchen.yml")) { |
| 79 | common.infoMsg(".kitchen.yml found, running kitchen tests") |
| 80 | ruby.ensureRubyEnv() |
| 81 | def kitchenEnvs = [] |
Martin Polreich | 9b5f5ab | 2017-07-12 13:06:14 +0200 | [diff] [blame] | 82 | def filteredEnvs = [] |
Jakub Josef | bcd2e90 | 2017-06-13 14:40:41 +0200 | [diff] [blame] | 83 | if(fileExists(".travis.yml")){ |
| 84 | common.infoMsg(".travis.yml found, running custom kitchen init") |
| 85 | def kitchenConfigYML = readYaml(file: ".travis.yml") |
| 86 | kitchenEnvs=kitchenConfigYML["env"] |
| 87 | def kitchenInit = kitchenConfigYML["install"] |
| 88 | def kitchenInstalled = false |
| 89 | if(kitchenInit && !kitchenInit.isEmpty()){ |
| 90 | for(int i=0;i<kitchenInit.size();i++){ |
| 91 | if(kitchenInit[i].trim().startsWith("test -e Gemfile")){ //found Gemfile config |
| 92 | common.infoMsg("Custom Gemfile configuration found, using them") |
| 93 | ruby.installKitchen(kitchenInit[i].trim()) |
| 94 | kitchenInstalled = true |
| 95 | } |
Jakub Josef | cca1e31 | 2017-05-23 15:21:35 +0200 | [diff] [blame] | 96 | } |
Jakub Josef | d672e40 | 2017-05-23 12:03:44 +0200 | [diff] [blame] | 97 | } |
Jakub Josef | bcd2e90 | 2017-06-13 14:40:41 +0200 | [diff] [blame] | 98 | if(!kitchenInstalled){ |
| 99 | ruby.installKitchen() |
Jakub Josef | 0bbbd80 | 2017-05-22 20:28:44 +0200 | [diff] [blame] | 100 | } |
| 101 | }else{ |
Jakub Josef | bcd2e90 | 2017-06-13 14:40:41 +0200 | [diff] [blame] | 102 | common.infoMsg(".travis.yml not found, running default kitchen init") |
| 103 | ruby.installKitchen() |
Jakub Josef | 0bbbd80 | 2017-05-22 20:28:44 +0200 | [diff] [blame] | 104 | } |
Jakub Josef | bcd2e90 | 2017-06-13 14:40:41 +0200 | [diff] [blame] | 105 | common.infoMsg("Running kitchen testing, parallel mode: " + KITCHEN_TESTS_PARALLEL.toBoolean()) |
| 106 | wrap([$class: 'AnsiColorBuildWrapper']) { |
| 107 | if(kitchenEnvs && !kitchenEnvs.isEmpty()){ |
Jakub Josef | bcd2e90 | 2017-06-13 14:40:41 +0200 | [diff] [blame] | 108 | for(int i=0;i<kitchenEnvs.size();i++){ |
Martin Polreich | 27aeb3f | 2017-07-12 14:55:22 +0200 | [diff] [blame] | 109 | filteredEnvs[i] = common.filterKitchenEnvs(kitchenEnvs[i]) |
Martin Polreich | 9b5f5ab | 2017-07-12 13:06:14 +0200 | [diff] [blame] | 110 | } |
| 111 | filteredEnvs = filteredEnvs.unique() |
Martin Polreich | 27aeb3f | 2017-07-12 14:55:22 +0200 | [diff] [blame] | 112 | /* common.infoMsg("Found multiple environment, first running kitchen without custom env") |
| 113 | ruby.runKitchenTests("", KITCHEN_TESTS_PARALLEL.toBoolean()) */ |
Martin Polreich | 9b5f5ab | 2017-07-12 13:06:14 +0200 | [diff] [blame] | 114 | for(int i=0;i<filteredEnvs.size();i++){ |
| 115 | common.infoMsg("Found multiple environment, kitchen running with env: " + filteredEnvs[i]) |
| 116 | ruby.runKitchenTests(filteredEnvs[i], KITCHEN_TESTS_PARALLEL.toBoolean()) |
Jakub Josef | bcd2e90 | 2017-06-13 14:40:41 +0200 | [diff] [blame] | 117 | } |
| 118 | }else{ |
| 119 | ruby.runKitchenTests("", KITCHEN_TESTS_PARALLEL.toBoolean()) |
| 120 | } |
| 121 | } |
| 122 | } else { |
| 123 | common.infoMsg(".kitchen.yml not found") |
Jakub Josef | f64c639 | 2017-05-03 13:18:25 +0200 | [diff] [blame] | 124 | } |
Jakub Josef | f64c639 | 2017-05-03 13:18:25 +0200 | [diff] [blame] | 125 | } |
| 126 | } |
Jakub Josef | c5a223a | 2017-03-01 14:40:08 +0100 | [diff] [blame] | 127 | } catch (Throwable e) { |
| 128 | // If there was an error or exception thrown, the build failed |
| 129 | currentBuild.result = "FAILURE" |
Jakub Josef | d11bdcc | 2017-06-13 17:02:43 +0200 | [diff] [blame] | 130 | ruby.runKitchenCommand("destroy") |
Jakub Josef | c5a223a | 2017-03-01 14:40:08 +0100 | [diff] [blame] | 131 | throw e |
| 132 | } finally { |
Jakub Josef | 52baaa4 | 2017-05-04 12:20:54 +0200 | [diff] [blame] | 133 | if(currentBuild.result == "FAILURE" && fileExists(".kitchen/logs/kitchen.log")){ |
Jakub Josef | f96ebc5 | 2017-05-04 12:37:19 +0200 | [diff] [blame] | 134 | common.errorMsg("----------------KITCHEN LOG:---------------") |
Jakub Josef | 52baaa4 | 2017-05-04 12:20:54 +0200 | [diff] [blame] | 135 | println readFile(".kitchen/logs/kitchen.log") |
| 136 | } |
Jakub Josef | c5a223a | 2017-03-01 14:40:08 +0100 | [diff] [blame] | 137 | common.sendNotification(currentBuild.result,"",["slack"]) |
| 138 | } |
Jakub Josef | d283f8c | 2017-05-10 18:20:11 +0200 | [diff] [blame] | 139 | } |
| 140 | |
| 141 | @NonCPS |
| 142 | def _getRunningTriggeredTestsBuildNumbers(jobName, gerritChangeNumber, excludePatchsetNumber){ |
Jakub Josef | 0a02cff | 2017-05-11 14:16:22 +0200 | [diff] [blame] | 143 | def gerrit = new com.mirantis.mk.Gerrit() |
| 144 | def jenkinsUtils = new com.mirantis.mk.JenkinsUtils() |
| 145 | def triggeredBuilds= gerrit.getGerritTriggeredBuilds(jenkinsUtils.getJobRunningBuilds(jobName), gerritChangeNumber, excludePatchsetNumber) |
| 146 | def buildNums =[] |
| 147 | for(int i=0;i<triggeredBuilds.size();i++){ |
Jakub Josef | d35d556 | 2017-05-12 16:12:22 +0200 | [diff] [blame] | 148 | buildNums.add(triggeredBuilds[i].number) |
Jakub Josef | 0a02cff | 2017-05-11 14:16:22 +0200 | [diff] [blame] | 149 | } |
| 150 | return buildNums |
Tomáš Kukrál | 8d2d6a3 | 2017-05-11 10:18:33 +0200 | [diff] [blame] | 151 | } |
Jakub Josef | b223590 | 2017-06-16 12:49:16 +0200 | [diff] [blame] | 152 | |