blob: 72966f55083a844b3dbee52b9ad91b0957c12fa9 [file] [log] [blame]
Jakub Josef83379312017-03-29 18:12:34 +02001
2/**
3 * Test salt models pipeline
4 * DEFAULT_GIT_REF
5 * DEFAULT_GIT_URL
6 * CREDENTIALS_ID
Jakub Josef27424bc2017-05-22 16:56:27 +02007 * EXTRA_FORMULAS
Jakub Josef83379312017-03-29 18:12:34 +02008 */
9
Jakub Josefc5a223a2017-03-01 14:40:08 +010010def common = new com.mirantis.mk.Common()
11def gerrit = new com.mirantis.mk.Gerrit()
Filip Pytloun38005aa2017-03-06 10:26:38 +010012def ssh = new com.mirantis.mk.Ssh()
13def git = new com.mirantis.mk.Git()
chnyda8ab8af82017-05-26 15:49:51 +020014def saltModelTesting = new com.mirantis.mk.SaltModelTesting()
Jakub Josefc5a223a2017-03-01 14:40:08 +010015
Filip Pytloun19376a82017-03-07 12:29:00 +010016def gerritRef
17try {
18 gerritRef = GERRIT_REFSPEC
19} catch (MissingPropertyException e) {
20 gerritRef = null
21}
22
Jakub Josef83379312017-03-29 18:12:34 +020023def defaultGitRef, defaultGitUrl
Filip Pytloun19376a82017-03-07 12:29:00 +010024try {
Jakub Josef83379312017-03-29 18:12:34 +020025 defaultGitRef = DEFAULT_GIT_REF
26 defaultGitUrl = DEFAULT_GIT_URL
Filip Pytloun19376a82017-03-07 12:29:00 +010027} catch (MissingPropertyException e) {
Jakub Josef83379312017-03-29 18:12:34 +020028 defaultGitRef = null
29 defaultGitUrl = null
Filip Pytloun19376a82017-03-07 12:29:00 +010030}
Jakub Josef83379312017-03-29 18:12:34 +020031def checkouted = false
Jakub Joseffcb615e2017-04-10 14:34:40 +020032def merged = false
chnydafa4b0ec2017-05-05 15:49:27 +020033node("python&&docker") {
Jakub Josefc5a223a2017-03-01 14:40:08 +010034 try{
Jakub Josef27424bc2017-05-22 16:56:27 +020035 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 Josefc5a223a2017-03-01 14:40:08 +010044 stage("checkout") {
Filip Pytloun19376a82017-03-07 12:29:00 +010045 if (gerritRef) {
Jakub Josef83379312017-03-29 18:12:34 +020046 // job is triggered by Gerrit
Jakub Joseffcb615e2017-04-10 14:34:40 +020047 // 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 ])
chnydaa9c88702017-05-09 16:51:07 +020054 } else{
Jakub Josef740a7882017-06-01 14:54:01 +020055 common.successMsg("Change ${GERRIT_CHANGE_NUMBER} is already merged, no need to test them")
Jakub Joseffcb615e2017-04-10 14:34:40 +020056 }
Jakub Josef83379312017-03-29 18:12:34 +020057 } else if(defaultGitRef && defaultGitUrl) {
Jakub Josefe1407ac2017-03-30 14:10:19 +020058 checkouted = gerrit.gerritPatchsetCheckout(defaultGitUrl, defaultGitRef, "HEAD", CREDENTIALS_ID)
Filip Pytloun19376a82017-03-07 12:29:00 +010059 }
Jakub Josef83379312017-03-29 18:12:34 +020060 if(checkouted){
61 if (fileExists('classes/system')) {
62 ssh.prepareSshAgentKey(CREDENTIALS_ID)
Filip Pytloun19376a82017-03-07 12:29:00 +010063 dir('classes/system') {
Jakub Josef83379312017-03-29 18:12:34 +020064 remoteUrl = git.getGitRemote()
65 ssh.ensureKnownHosts(remoteUrl)
Filip Pytloun19376a82017-03-07 12:29:00 +010066 }
Jakub Josef83379312017-03-29 18:12:34 +020067 ssh.agentSh("git submodule init; git submodule sync; git submodule update --recursive")
Filip Pytloun19376a82017-03-07 12:29:00 +010068 }
Jakub Joseffcb615e2017-04-10 14:34:40 +020069 }else if(!merged){
Jakub Josef5ce6a362017-03-31 13:41:17 +020070 throw new Exception("Cannot checkout gerrit patchset, GERRIT_REFSPEC and DEFAULT_GIT_REF is null")
Filip Pytloun38005aa2017-03-06 10:26:38 +010071 }
Jakub Josefc5a223a2017-03-01 14:40:08 +010072 }
chnydaa9c88702017-05-09 16:51:07 +020073
chnyda14e44292017-05-13 19:00:06 +020074 stage("test-nodes") {
Jakub Josef740a7882017-06-01 14:54:01 +020075 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 = [:]
chnydafd513922017-06-01 16:17:20 +020079 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 Josef740a7882017-06-01 14:54:01 +020088 }
chnydafd513922017-06-01 16:17:20 +020089 common.serial(buildSteps)
Jakub Josefc5a223a2017-03-01 14:40:08 +010090 }
chnyda51b03142017-05-10 17:15:27 +020091 }
Jakub Josefc5a223a2017-03-01 14:40:08 +010092 } 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ál500c0182017-05-11 13:46:19 +020099}
Jakub Josef8d024772017-05-15 18:07:45 +0200100
Jakub Josef27424bc2017-05-22 16:56:27 +0200101@NonCPS
102def _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 Josef8d024772017-05-15 18:07:45 +0200111}