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