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 |
Jakub Josef | 8337931 | 2017-03-29 18:12:34 +0200 | [diff] [blame] | 8 | */ |
| 9 | |
Jakub Josef | c5a223a | 2017-03-01 14:40:08 +0100 | [diff] [blame] | 10 | def common = new com.mirantis.mk.Common() |
| 11 | def gerrit = new com.mirantis.mk.Gerrit() |
Filip Pytloun | 38005aa | 2017-03-06 10:26:38 +0100 | [diff] [blame] | 12 | def ssh = new com.mirantis.mk.Ssh() |
| 13 | def git = new com.mirantis.mk.Git() |
chnyda | 8ab8af8 | 2017-05-26 15:49:51 +0200 | [diff] [blame] | 14 | def saltModelTesting = new com.mirantis.mk.SaltModelTesting() |
Jakub Josef | c5a223a | 2017-03-01 14:40:08 +0100 | [diff] [blame] | 15 | |
Filip Pytloun | 19376a8 | 2017-03-07 12:29:00 +0100 | [diff] [blame] | 16 | def gerritRef |
| 17 | try { |
| 18 | gerritRef = GERRIT_REFSPEC |
| 19 | } catch (MissingPropertyException e) { |
| 20 | gerritRef = null |
| 21 | } |
| 22 | |
Jakub Josef | 8337931 | 2017-03-29 18:12:34 +0200 | [diff] [blame] | 23 | def defaultGitRef, defaultGitUrl |
Filip Pytloun | 19376a8 | 2017-03-07 12:29:00 +0100 | [diff] [blame] | 24 | try { |
Jakub Josef | 8337931 | 2017-03-29 18:12:34 +0200 | [diff] [blame] | 25 | defaultGitRef = DEFAULT_GIT_REF |
| 26 | defaultGitUrl = DEFAULT_GIT_URL |
Filip Pytloun | 19376a8 | 2017-03-07 12:29:00 +0100 | [diff] [blame] | 27 | } catch (MissingPropertyException e) { |
Jakub Josef | 8337931 | 2017-03-29 18:12:34 +0200 | [diff] [blame] | 28 | defaultGitRef = null |
| 29 | defaultGitUrl = null |
Filip Pytloun | 19376a8 | 2017-03-07 12:29:00 +0100 | [diff] [blame] | 30 | } |
Jakub Josef | 8337931 | 2017-03-29 18:12:34 +0200 | [diff] [blame] | 31 | def checkouted = false |
chnyda | 3b1d2a6 | 2017-06-13 10:08:12 +0200 | [diff] [blame] | 32 | node("python") { |
Jakub Josef | c5a223a | 2017-03-01 14:40:08 +0100 | [diff] [blame] | 33 | try{ |
Jakub Josef | 27424bc | 2017-05-22 16:56:27 +0200 | [diff] [blame] | 34 | stage("stop old tests"){ |
| 35 | if (gerritRef) { |
| 36 | def runningTestBuildNums = _getRunningTriggeredTestsBuildNumbers(env["JOB_NAME"], GERRIT_CHANGE_NUMBER, GERRIT_PATCHSET_NUMBER) |
| 37 | for(int i=0; i<runningTestBuildNums.size(); i++){ |
| 38 | common.infoMsg("Old test with run number ${runningTestBuildNums[i]} found, stopping") |
| 39 | Jenkins.instance.getItemByFullName(env["JOB_NAME"]).getBuildByNumber(runningTestBuildNums[i]).finish(hudson.model.Result.ABORTED, new java.io.IOException("Aborting build")); |
| 40 | } |
| 41 | } |
| 42 | } |
Jakub Josef | c5a223a | 2017-03-01 14:40:08 +0100 | [diff] [blame] | 43 | stage("checkout") { |
Filip Pytloun | 19376a8 | 2017-03-07 12:29:00 +0100 | [diff] [blame] | 44 | if (gerritRef) { |
Jakub Josef | 8337931 | 2017-03-29 18:12:34 +0200 | [diff] [blame] | 45 | // job is triggered by Gerrit |
Jakub Josef | fcb615e | 2017-04-10 14:34:40 +0200 | [diff] [blame] | 46 | // test if change aren't already merged |
Jakub Josef | b223590 | 2017-06-16 12:49:16 +0200 | [diff] [blame] | 47 | def gerritChange = gerrit.getGerritChange(GERRIT_NAME, GERRIT_HOST, GERRIT_CHANGE_NUMBER, CREDENTIALS_ID, true) |
| 48 | // test if gerrit change is already Verified |
Jakub Josef | 2ed5081 | 2017-06-16 14:56:58 +0200 | [diff] [blame] | 49 | if(gerrit.patchsetHasApproval(gerritChange.currentPatchSet,"Verified", "+")){ |
Jakub Josef | 38fbda3 | 2017-06-19 18:17:27 +0200 | [diff] [blame^] | 50 | common.successMsg("Gerrit change ${GERRIT_CHANGE_NUMBER} patchset ${GERRIT_PATCHSET_NUMBER} already has Verified, skipping tests") |
| 51 | currentBuild.result = 'ABORTED' |
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 | 38fbda3 | 2017-06-19 18:17:27 +0200 | [diff] [blame^] | 54 | common.successMsg("Commit message contains WIP, skipping tests") |
| 55 | currentBuild.result = 'ABORTED' |
Jakub Josef | bcd2e90 | 2017-06-13 14:40:41 +0200 | [diff] [blame] | 56 | } else { |
| 57 | def merged = gerritChange.status == "MERGED" |
| 58 | if(!merged){ |
| 59 | checkouted = gerrit.gerritPatchsetCheckout ([ |
| 60 | credentialsId : CREDENTIALS_ID |
| 61 | ]) |
| 62 | } else{ |
| 63 | common.successMsg("Change ${GERRIT_CHANGE_NUMBER} is already merged, no need to test them") |
Jakub Josef | 38fbda3 | 2017-06-19 18:17:27 +0200 | [diff] [blame^] | 64 | currentBuild.result = 'ABORTED' |
Jakub Josef | bcd2e90 | 2017-06-13 14:40:41 +0200 | [diff] [blame] | 65 | } |
Jakub Josef | fcb615e | 2017-04-10 14:34:40 +0200 | [diff] [blame] | 66 | } |
Jakub Josef | 8337931 | 2017-03-29 18:12:34 +0200 | [diff] [blame] | 67 | } else if(defaultGitRef && defaultGitUrl) { |
Jakub Josef | e1407ac | 2017-03-30 14:10:19 +0200 | [diff] [blame] | 68 | checkouted = gerrit.gerritPatchsetCheckout(defaultGitUrl, defaultGitRef, "HEAD", CREDENTIALS_ID) |
Jakub Josef | bcd2e90 | 2017-06-13 14:40:41 +0200 | [diff] [blame] | 69 | } else { |
| 70 | 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] | 71 | } |
Jakub Josef | bcd2e90 | 2017-06-13 14:40:41 +0200 | [diff] [blame] | 72 | if(checkouted) { |
Jakub Josef | 8337931 | 2017-03-29 18:12:34 +0200 | [diff] [blame] | 73 | if (fileExists('classes/system')) { |
| 74 | ssh.prepareSshAgentKey(CREDENTIALS_ID) |
Filip Pytloun | 19376a8 | 2017-03-07 12:29:00 +0100 | [diff] [blame] | 75 | dir('classes/system') { |
Jakub Josef | 8337931 | 2017-03-29 18:12:34 +0200 | [diff] [blame] | 76 | remoteUrl = git.getGitRemote() |
| 77 | ssh.ensureKnownHosts(remoteUrl) |
Filip Pytloun | 19376a8 | 2017-03-07 12:29:00 +0100 | [diff] [blame] | 78 | } |
Jakub Josef | 8337931 | 2017-03-29 18:12:34 +0200 | [diff] [blame] | 79 | ssh.agentSh("git submodule init; git submodule sync; git submodule update --recursive") |
Filip Pytloun | 19376a8 | 2017-03-07 12:29:00 +0100 | [diff] [blame] | 80 | } |
Filip Pytloun | 38005aa | 2017-03-06 10:26:38 +0100 | [diff] [blame] | 81 | } |
Jakub Josef | c5a223a | 2017-03-01 14:40:08 +0100 | [diff] [blame] | 82 | } |
chnyda | a9c8870 | 2017-05-09 16:51:07 +0200 | [diff] [blame] | 83 | |
chnyda | 14e4429 | 2017-05-13 19:00:06 +0200 | [diff] [blame] | 84 | stage("test-nodes") { |
Jakub Josef | bcd2e90 | 2017-06-13 14:40:41 +0200 | [diff] [blame] | 85 | if(checkouted) { |
Jakub Josef | 740a788 | 2017-06-01 14:54:01 +0200 | [diff] [blame] | 86 | def workspace = common.getWorkspace() |
| 87 | def nodes = sh(script: "find ./nodes -type f -name 'cfg*.yml'", returnStdout: true).tokenize() |
| 88 | def buildSteps = [:] |
chnyda | fd51392 | 2017-06-01 16:17:20 +0200 | [diff] [blame] | 89 | def partitionSize = (nodes.size() <= PARALLEL_NODE_GROUP_SIZE.toInteger()) ? nodes.size() : PARALLEL_NODE_GROUP_SIZE.toInteger() |
| 90 | def partitions = common.partitionList(nodes, partitionSize) |
| 91 | for (int i =0; i < partitions.size();i++) { |
| 92 | def partition = partitions[i] |
| 93 | buildSteps.put("partition-${i}", new HashMap<String,org.jenkinsci.plugins.workflow.cps.CpsClosure2>()) |
| 94 | for(int k=0; k < partition.size;k++){ |
| 95 | def basename = sh(script: "basename ${partition[k]} .yml", returnStdout: true).trim() |
| 96 | buildSteps.get("partition-${i}").put(basename, { saltModelTesting.setupAndTestNode(basename, EXTRA_FORMULAS, workspace) }) |
| 97 | } |
Jakub Josef | 740a788 | 2017-06-01 14:54:01 +0200 | [diff] [blame] | 98 | } |
chnyda | fd51392 | 2017-06-01 16:17:20 +0200 | [diff] [blame] | 99 | common.serial(buildSteps) |
Jakub Josef | c5a223a | 2017-03-01 14:40:08 +0100 | [diff] [blame] | 100 | } |
chnyda | 51b0314 | 2017-05-10 17:15:27 +0200 | [diff] [blame] | 101 | } |
Jakub Josef | c5a223a | 2017-03-01 14:40:08 +0100 | [diff] [blame] | 102 | } catch (Throwable e) { |
| 103 | // If there was an error or exception thrown, the build failed |
| 104 | currentBuild.result = "FAILURE" |
| 105 | throw e |
| 106 | } finally { |
| 107 | common.sendNotification(currentBuild.result,"",["slack"]) |
| 108 | } |
Tomáš Kukrál | 500c018 | 2017-05-11 13:46:19 +0200 | [diff] [blame] | 109 | } |
Jakub Josef | 8d02477 | 2017-05-15 18:07:45 +0200 | [diff] [blame] | 110 | |
Jakub Josef | 27424bc | 2017-05-22 16:56:27 +0200 | [diff] [blame] | 111 | @NonCPS |
| 112 | def _getRunningTriggeredTestsBuildNumbers(jobName, gerritChangeNumber, excludePatchsetNumber){ |
| 113 | def gerrit = new com.mirantis.mk.Gerrit() |
| 114 | def jenkinsUtils = new com.mirantis.mk.JenkinsUtils() |
| 115 | def triggeredBuilds= gerrit.getGerritTriggeredBuilds(jenkinsUtils.getJobRunningBuilds(jobName), gerritChangeNumber, excludePatchsetNumber) |
| 116 | def buildNums =[] |
Jakub Josef | bcd2e90 | 2017-06-13 14:40:41 +0200 | [diff] [blame] | 117 | for (int i=0; i<triggeredBuilds.size(); i++) { |
Jakub Josef | 27424bc | 2017-05-22 16:56:27 +0200 | [diff] [blame] | 118 | buildNums.add(triggeredBuilds[i].number) |
| 119 | } |
| 120 | return buildNums |
Jakub Josef | 8d02477 | 2017-05-15 18:07:45 +0200 | [diff] [blame] | 121 | } |