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 |
Jakub Josef | fcb615e | 2017-04-10 14:34:40 +0200 | [diff] [blame] | 32 | def merged = false |
chnyda | fa4b0ec | 2017-05-05 15:49:27 +0200 | [diff] [blame] | 33 | node("python&&docker") { |
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 |
| 48 | def gerritChange = gerrit.getGerritChange(GERRIT_NAME, GERRIT_HOST, GERRIT_CHANGE_NUMBER, CREDENTIALS_ID) |
| 49 | merged = gerritChange.status == "MERGED" |
| 50 | if(!merged){ |
| 51 | checkouted = gerrit.gerritPatchsetCheckout ([ |
| 52 | credentialsId : CREDENTIALS_ID |
| 53 | ]) |
chnyda | a9c8870 | 2017-05-09 16:51:07 +0200 | [diff] [blame] | 54 | } else{ |
Jakub Josef | 740a788 | 2017-06-01 14:54:01 +0200 | [diff] [blame] | 55 | common.successMsg("Change ${GERRIT_CHANGE_NUMBER} is already merged, no need to test them") |
Jakub Josef | fcb615e | 2017-04-10 14:34:40 +0200 | [diff] [blame] | 56 | } |
Jakub Josef | 8337931 | 2017-03-29 18:12:34 +0200 | [diff] [blame] | 57 | } else if(defaultGitRef && defaultGitUrl) { |
Jakub Josef | e1407ac | 2017-03-30 14:10:19 +0200 | [diff] [blame] | 58 | checkouted = gerrit.gerritPatchsetCheckout(defaultGitUrl, defaultGitRef, "HEAD", CREDENTIALS_ID) |
Filip Pytloun | 19376a8 | 2017-03-07 12:29:00 +0100 | [diff] [blame] | 59 | } |
Jakub Josef | 8337931 | 2017-03-29 18:12:34 +0200 | [diff] [blame] | 60 | if(checkouted){ |
| 61 | if (fileExists('classes/system')) { |
| 62 | ssh.prepareSshAgentKey(CREDENTIALS_ID) |
Filip Pytloun | 19376a8 | 2017-03-07 12:29:00 +0100 | [diff] [blame] | 63 | dir('classes/system') { |
Jakub Josef | 8337931 | 2017-03-29 18:12:34 +0200 | [diff] [blame] | 64 | remoteUrl = git.getGitRemote() |
| 65 | ssh.ensureKnownHosts(remoteUrl) |
Filip Pytloun | 19376a8 | 2017-03-07 12:29:00 +0100 | [diff] [blame] | 66 | } |
Jakub Josef | 8337931 | 2017-03-29 18:12:34 +0200 | [diff] [blame] | 67 | ssh.agentSh("git submodule init; git submodule sync; git submodule update --recursive") |
Filip Pytloun | 19376a8 | 2017-03-07 12:29:00 +0100 | [diff] [blame] | 68 | } |
Jakub Josef | fcb615e | 2017-04-10 14:34:40 +0200 | [diff] [blame] | 69 | }else if(!merged){ |
Jakub Josef | 5ce6a36 | 2017-03-31 13:41:17 +0200 | [diff] [blame] | 70 | throw new Exception("Cannot checkout gerrit patchset, GERRIT_REFSPEC and DEFAULT_GIT_REF is null") |
Filip Pytloun | 38005aa | 2017-03-06 10:26:38 +0100 | [diff] [blame] | 71 | } |
Jakub Josef | c5a223a | 2017-03-01 14:40:08 +0100 | [diff] [blame] | 72 | } |
chnyda | a9c8870 | 2017-05-09 16:51:07 +0200 | [diff] [blame] | 73 | |
chnyda | 14e4429 | 2017-05-13 19:00:06 +0200 | [diff] [blame] | 74 | stage("test-nodes") { |
Jakub Josef | 740a788 | 2017-06-01 14:54:01 +0200 | [diff] [blame] | 75 | if(!merged){ |
| 76 | def workspace = common.getWorkspace() |
| 77 | def nodes = sh(script: "find ./nodes -type f -name 'cfg*.yml'", returnStdout: true).tokenize() |
| 78 | def buildSteps = [:] |
chnyda | fd51392 | 2017-06-01 16:17:20 +0200 | [diff] [blame^] | 79 | def partitionSize = (nodes.size() <= PARALLEL_NODE_GROUP_SIZE.toInteger()) ? nodes.size() : PARALLEL_NODE_GROUP_SIZE.toInteger() |
| 80 | def partitions = common.partitionList(nodes, partitionSize) |
| 81 | for (int i =0; i < partitions.size();i++) { |
| 82 | def partition = partitions[i] |
| 83 | buildSteps.put("partition-${i}", new HashMap<String,org.jenkinsci.plugins.workflow.cps.CpsClosure2>()) |
| 84 | for(int k=0; k < partition.size;k++){ |
| 85 | def basename = sh(script: "basename ${partition[k]} .yml", returnStdout: true).trim() |
| 86 | buildSteps.get("partition-${i}").put(basename, { saltModelTesting.setupAndTestNode(basename, EXTRA_FORMULAS, workspace) }) |
| 87 | } |
Jakub Josef | 740a788 | 2017-06-01 14:54:01 +0200 | [diff] [blame] | 88 | } |
chnyda | fd51392 | 2017-06-01 16:17:20 +0200 | [diff] [blame^] | 89 | common.serial(buildSteps) |
Jakub Josef | c5a223a | 2017-03-01 14:40:08 +0100 | [diff] [blame] | 90 | } |
chnyda | 51b0314 | 2017-05-10 17:15:27 +0200 | [diff] [blame] | 91 | } |
Jakub Josef | c5a223a | 2017-03-01 14:40:08 +0100 | [diff] [blame] | 92 | } catch (Throwable e) { |
| 93 | // If there was an error or exception thrown, the build failed |
| 94 | currentBuild.result = "FAILURE" |
| 95 | throw e |
| 96 | } finally { |
| 97 | common.sendNotification(currentBuild.result,"",["slack"]) |
| 98 | } |
Tomáš Kukrál | 500c018 | 2017-05-11 13:46:19 +0200 | [diff] [blame] | 99 | } |
Jakub Josef | 8d02477 | 2017-05-15 18:07:45 +0200 | [diff] [blame] | 100 | |
Jakub Josef | 27424bc | 2017-05-22 16:56:27 +0200 | [diff] [blame] | 101 | @NonCPS |
| 102 | def _getRunningTriggeredTestsBuildNumbers(jobName, gerritChangeNumber, excludePatchsetNumber){ |
| 103 | def gerrit = new com.mirantis.mk.Gerrit() |
| 104 | def jenkinsUtils = new com.mirantis.mk.JenkinsUtils() |
| 105 | def triggeredBuilds= gerrit.getGerritTriggeredBuilds(jenkinsUtils.getJobRunningBuilds(jobName), gerritChangeNumber, excludePatchsetNumber) |
| 106 | def buildNums =[] |
| 107 | for(int i=0;i<triggeredBuilds.size();i++){ |
| 108 | buildNums.add(triggeredBuilds[i].number) |
| 109 | } |
| 110 | return buildNums |
Jakub Josef | 8d02477 | 2017-05-15 18:07:45 +0200 | [diff] [blame] | 111 | } |