| Jakub Josef | 8337931 | 2017-03-29 18:12:34 +0200 | [diff] [blame] | 1 |  | 
|  | 2 | /** | 
|  | 3 | *  Test salt models pipeline | 
|  | 4 | *  DEFAULT_GIT_REF | 
|  | 5 | *  DEFAULT_GIT_URL | 
|  | 6 | *  CREDENTIALS_ID | 
| Jakub Josef | 27424bc | 2017-05-22 16:56:27 +0200 | [diff] [blame] | 7 | *  EXTRA_FORMULAS | 
| chnyda | d66d6fa | 2017-06-22 09:34:43 +0200 | [diff] [blame^] | 8 | *  SYSTEM_GIT_URL | 
|  | 9 | *  SYSTEM_GIT_REF | 
| Jakub Josef | 8337931 | 2017-03-29 18:12:34 +0200 | [diff] [blame] | 10 | */ | 
|  | 11 |  | 
| Jakub Josef | c5a223a | 2017-03-01 14:40:08 +0100 | [diff] [blame] | 12 | def common = new com.mirantis.mk.Common() | 
|  | 13 | def gerrit = new com.mirantis.mk.Gerrit() | 
| Filip Pytloun | 38005aa | 2017-03-06 10:26:38 +0100 | [diff] [blame] | 14 | def ssh = new com.mirantis.mk.Ssh() | 
|  | 15 | def git = new com.mirantis.mk.Git() | 
| Jakub Josef | c5a223a | 2017-03-01 14:40:08 +0100 | [diff] [blame] | 16 |  | 
| Filip Pytloun | 19376a8 | 2017-03-07 12:29:00 +0100 | [diff] [blame] | 17 | def gerritRef | 
|  | 18 | try { | 
|  | 19 | gerritRef = GERRIT_REFSPEC | 
|  | 20 | } catch (MissingPropertyException e) { | 
|  | 21 | gerritRef = null | 
|  | 22 | } | 
|  | 23 |  | 
| Jakub Josef | 8337931 | 2017-03-29 18:12:34 +0200 | [diff] [blame] | 24 | def defaultGitRef, defaultGitUrl | 
| Filip Pytloun | 19376a8 | 2017-03-07 12:29:00 +0100 | [diff] [blame] | 25 | try { | 
| Jakub Josef | 8337931 | 2017-03-29 18:12:34 +0200 | [diff] [blame] | 26 | defaultGitRef = DEFAULT_GIT_REF | 
|  | 27 | defaultGitUrl = DEFAULT_GIT_URL | 
| Filip Pytloun | 19376a8 | 2017-03-07 12:29:00 +0100 | [diff] [blame] | 28 | } catch (MissingPropertyException e) { | 
| Jakub Josef | 8337931 | 2017-03-29 18:12:34 +0200 | [diff] [blame] | 29 | defaultGitRef = null | 
|  | 30 | defaultGitUrl = null | 
| Filip Pytloun | 19376a8 | 2017-03-07 12:29:00 +0100 | [diff] [blame] | 31 | } | 
| Jakub Josef | 8337931 | 2017-03-29 18:12:34 +0200 | [diff] [blame] | 32 | def checkouted = false | 
| chnyda | 3b1d2a6 | 2017-06-13 10:08:12 +0200 | [diff] [blame] | 33 | node("python") { | 
| Jakub Josef | c5a223a | 2017-03-01 14:40:08 +0100 | [diff] [blame] | 34 | try{ | 
| Jakub Josef | 27424bc | 2017-05-22 16:56:27 +0200 | [diff] [blame] | 35 | stage("stop old tests"){ | 
|  | 36 | if (gerritRef) { | 
|  | 37 | def runningTestBuildNums = _getRunningTriggeredTestsBuildNumbers(env["JOB_NAME"], GERRIT_CHANGE_NUMBER, GERRIT_PATCHSET_NUMBER) | 
|  | 38 | for(int i=0; i<runningTestBuildNums.size(); i++){ | 
|  | 39 | common.infoMsg("Old test with run number ${runningTestBuildNums[i]} found, stopping") | 
|  | 40 | Jenkins.instance.getItemByFullName(env["JOB_NAME"]).getBuildByNumber(runningTestBuildNums[i]).finish(hudson.model.Result.ABORTED, new java.io.IOException("Aborting build")); | 
|  | 41 | } | 
|  | 42 | } | 
|  | 43 | } | 
| Jakub Josef | c5a223a | 2017-03-01 14:40:08 +0100 | [diff] [blame] | 44 | stage("checkout") { | 
| Filip Pytloun | 19376a8 | 2017-03-07 12:29:00 +0100 | [diff] [blame] | 45 | if (gerritRef) { | 
| Jakub Josef | 8337931 | 2017-03-29 18:12:34 +0200 | [diff] [blame] | 46 | // job is triggered by Gerrit | 
| Jakub Josef | fcb615e | 2017-04-10 14:34:40 +0200 | [diff] [blame] | 47 | // test if change aren't already merged | 
| Jakub Josef | b223590 | 2017-06-16 12:49:16 +0200 | [diff] [blame] | 48 | def gerritChange = gerrit.getGerritChange(GERRIT_NAME, GERRIT_HOST, GERRIT_CHANGE_NUMBER, CREDENTIALS_ID, true) | 
|  | 49 | // test if gerrit change is already Verified | 
| Jakub Josef | 2ed5081 | 2017-06-16 14:56:58 +0200 | [diff] [blame] | 50 | if(gerrit.patchsetHasApproval(gerritChange.currentPatchSet,"Verified", "+")){ | 
| Jakub Josef | 894f1e3 | 2017-06-19 16:24:19 +0000 | [diff] [blame] | 51 | 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] | 52 | // test WIP contains in commit message | 
| Jakub Josef | b223590 | 2017-06-16 12:49:16 +0200 | [diff] [blame] | 53 | }else if (gerritChange.commitMessage.contains("WIP")) { | 
| Jakub Josef | 894f1e3 | 2017-06-19 16:24:19 +0000 | [diff] [blame] | 54 | common.successMsg("Commit message contains WIP, skipping tests") // do nothing | 
| Jakub Josef | bcd2e90 | 2017-06-13 14:40:41 +0200 | [diff] [blame] | 55 | } else { | 
|  | 56 | def merged = gerritChange.status == "MERGED" | 
|  | 57 | if(!merged){ | 
|  | 58 | checkouted = gerrit.gerritPatchsetCheckout ([ | 
|  | 59 | credentialsId : CREDENTIALS_ID | 
|  | 60 | ]) | 
|  | 61 | } else{ | 
|  | 62 | common.successMsg("Change ${GERRIT_CHANGE_NUMBER} is already merged, no need to test them") | 
|  | 63 | } | 
| Jakub Josef | fcb615e | 2017-04-10 14:34:40 +0200 | [diff] [blame] | 64 | } | 
| chnyda | d66d6fa | 2017-06-22 09:34:43 +0200 | [diff] [blame^] | 65 | // defaultGitUrl is passed to the triggered job | 
|  | 66 | defaultGitUrl = "${GERRIT_SCHEME}://${GERRIT_NAME}@${GERRIT_HOST}:${GERRIT_PORT}/${GERRIT_PROJECT}" | 
|  | 67 | defaultGitRef = GERRIT_REFSPEC | 
| Jakub Josef | 8337931 | 2017-03-29 18:12:34 +0200 | [diff] [blame] | 68 | } else if(defaultGitRef && defaultGitUrl) { | 
| Jakub Josef | e1407ac | 2017-03-30 14:10:19 +0200 | [diff] [blame] | 69 | checkouted = gerrit.gerritPatchsetCheckout(defaultGitUrl, defaultGitRef, "HEAD", CREDENTIALS_ID) | 
| Jakub Josef | bcd2e90 | 2017-06-13 14:40:41 +0200 | [diff] [blame] | 70 | } else { | 
|  | 71 | throw new Exception("Cannot checkout gerrit patchset, GERRIT_REFSPEC and DEFAULT_GIT_REF is null") | 
| Filip Pytloun | 19376a8 | 2017-03-07 12:29:00 +0100 | [diff] [blame] | 72 | } | 
| Jakub Josef | c5a223a | 2017-03-01 14:40:08 +0100 | [diff] [blame] | 73 | } | 
| chnyda | a9c8870 | 2017-05-09 16:51:07 +0200 | [diff] [blame] | 74 |  | 
| chnyda | 14e4429 | 2017-05-13 19:00:06 +0200 | [diff] [blame] | 75 | stage("test-nodes") { | 
| Jakub Josef | bcd2e90 | 2017-06-13 14:40:41 +0200 | [diff] [blame] | 76 | if(checkouted) { | 
| Jakub Josef | 740a788 | 2017-06-01 14:54:01 +0200 | [diff] [blame] | 77 | def nodes = sh(script: "find ./nodes -type f -name 'cfg*.yml'", returnStdout: true).tokenize() | 
| chnyda | d66d6fa | 2017-06-22 09:34:43 +0200 | [diff] [blame^] | 78 | def branches = [:] | 
|  | 79 | for (int i = 0; i < nodes.size(); i++) { | 
|  | 80 |  | 
|  | 81 | def testTarget = sh(scipt: "basename ${nodes[i]}.yml", returnStdout: true).trim() | 
|  | 82 | branches[${testTarget}] = { | 
|  | 83 | build job: "test-salt-model-node", parameters: [ | 
|  | 84 | [$class: 'StringParameterValue', name: 'DEFAULT_GIT_URL', value: defaultGitUrl], | 
|  | 85 | [$class: 'StringParameterValue', name: 'DEFAULT_GIT_REF', value: defaultGitRef], | 
|  | 86 | [$class: 'StringParameterValue', name: 'NODE_TARGET', value: testTarget], | 
|  | 87 | [$class: 'StringParameterValue', name: 'EXTRA_FORMULAS', value: EXTRA_FORMULAS], | 
|  | 88 | [$class: 'StringParameterValue', name: 'CREDENTIALS_ID', value: CREDENTIALS_ID], | 
|  | 89 | [$class: 'StringParameterValue', name: 'SYSTEM_GIT_URL', value: SYSTEM_GIT_URL], | 
|  | 90 | [$class: 'StringParameterValue', name: 'SYSTEM_GIT_REF', value: SYSTEM_GIT_REF] | 
|  | 91 | ]} | 
|  | 92 | parallel branches | 
| Jakub Josef | 740a788 | 2017-06-01 14:54:01 +0200 | [diff] [blame] | 93 | } | 
| Jakub Josef | c5a223a | 2017-03-01 14:40:08 +0100 | [diff] [blame] | 94 | } | 
| chnyda | 51b0314 | 2017-05-10 17:15:27 +0200 | [diff] [blame] | 95 | } | 
| Jakub Josef | c5a223a | 2017-03-01 14:40:08 +0100 | [diff] [blame] | 96 | } catch (Throwable e) { | 
| Jakub Josef | c5a223a | 2017-03-01 14:40:08 +0100 | [diff] [blame] | 97 | currentBuild.result = "FAILURE" | 
|  | 98 | throw e | 
|  | 99 | } finally { | 
|  | 100 | common.sendNotification(currentBuild.result,"",["slack"]) | 
|  | 101 | } | 
| Tomáš Kukrál | 500c018 | 2017-05-11 13:46:19 +0200 | [diff] [blame] | 102 | } | 
| Jakub Josef | 8d02477 | 2017-05-15 18:07:45 +0200 | [diff] [blame] | 103 |  | 
| Jakub Josef | 27424bc | 2017-05-22 16:56:27 +0200 | [diff] [blame] | 104 | @NonCPS | 
|  | 105 | def _getRunningTriggeredTestsBuildNumbers(jobName, gerritChangeNumber, excludePatchsetNumber){ | 
|  | 106 | def gerrit = new com.mirantis.mk.Gerrit() | 
|  | 107 | def jenkinsUtils = new com.mirantis.mk.JenkinsUtils() | 
|  | 108 | def triggeredBuilds= gerrit.getGerritTriggeredBuilds(jenkinsUtils.getJobRunningBuilds(jobName), gerritChangeNumber, excludePatchsetNumber) | 
|  | 109 | def buildNums =[] | 
| Jakub Josef | bcd2e90 | 2017-06-13 14:40:41 +0200 | [diff] [blame] | 110 | for (int i=0; i<triggeredBuilds.size(); i++) { | 
| Jakub Josef | 27424bc | 2017-05-22 16:56:27 +0200 | [diff] [blame] | 111 | buildNums.add(triggeredBuilds[i].number) | 
|  | 112 | } | 
|  | 113 | return buildNums | 
| Jakub Josef | 8d02477 | 2017-05-15 18:07:45 +0200 | [diff] [blame] | 114 | } |