blob: 96ef843089d0538ba9babd3fd4263894223c1817 [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
chnyda3b1d2a62017-06-13 10:08:12 +020032node("python") {
Jakub Josefc5a223a2017-03-01 14:40:08 +010033 try{
Jakub Josef27424bc2017-05-22 16:56:27 +020034 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 Josefc5a223a2017-03-01 14:40:08 +010043 stage("checkout") {
Filip Pytloun19376a82017-03-07 12:29:00 +010044 if (gerritRef) {
Jakub Josef83379312017-03-29 18:12:34 +020045 // job is triggered by Gerrit
Jakub Joseffcb615e2017-04-10 14:34:40 +020046 // test if change aren't already merged
Jakub Josefb2235902017-06-16 12:49:16 +020047 def gerritChange = gerrit.getGerritChange(GERRIT_NAME, GERRIT_HOST, GERRIT_CHANGE_NUMBER, CREDENTIALS_ID, true)
48 // test if gerrit change is already Verified
Jakub Josef2ed50812017-06-16 14:56:58 +020049 if(gerrit.patchsetHasApproval(gerritChange.currentPatchSet,"Verified", "+")){
Jakub Josef38fbda32017-06-19 18:17:27 +020050 common.successMsg("Gerrit change ${GERRIT_CHANGE_NUMBER} patchset ${GERRIT_PATCHSET_NUMBER} already has Verified, skipping tests")
51 currentBuild.result = 'ABORTED'
Jakub Josefbcd2e902017-06-13 14:40:41 +020052 // test WIP contains in commit message
Jakub Josefb2235902017-06-16 12:49:16 +020053 }else if (gerritChange.commitMessage.contains("WIP")) {
Jakub Josef38fbda32017-06-19 18:17:27 +020054 common.successMsg("Commit message contains WIP, skipping tests")
55 currentBuild.result = 'ABORTED'
Jakub Josefbcd2e902017-06-13 14:40:41 +020056 } 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 Josef38fbda32017-06-19 18:17:27 +020064 currentBuild.result = 'ABORTED'
Jakub Josefbcd2e902017-06-13 14:40:41 +020065 }
Jakub Joseffcb615e2017-04-10 14:34:40 +020066 }
Jakub Josef83379312017-03-29 18:12:34 +020067 } else if(defaultGitRef && defaultGitUrl) {
Jakub Josefe1407ac2017-03-30 14:10:19 +020068 checkouted = gerrit.gerritPatchsetCheckout(defaultGitUrl, defaultGitRef, "HEAD", CREDENTIALS_ID)
Jakub Josefbcd2e902017-06-13 14:40:41 +020069 } else {
70 throw new Exception("Cannot checkout gerrit patchset, GERRIT_REFSPEC and DEFAULT_GIT_REF is null")
Filip Pytloun19376a82017-03-07 12:29:00 +010071 }
Jakub Josefbcd2e902017-06-13 14:40:41 +020072 if(checkouted) {
Jakub Josef83379312017-03-29 18:12:34 +020073 if (fileExists('classes/system')) {
74 ssh.prepareSshAgentKey(CREDENTIALS_ID)
Filip Pytloun19376a82017-03-07 12:29:00 +010075 dir('classes/system') {
Jakub Josef83379312017-03-29 18:12:34 +020076 remoteUrl = git.getGitRemote()
77 ssh.ensureKnownHosts(remoteUrl)
Filip Pytloun19376a82017-03-07 12:29:00 +010078 }
Jakub Josef83379312017-03-29 18:12:34 +020079 ssh.agentSh("git submodule init; git submodule sync; git submodule update --recursive")
Filip Pytloun19376a82017-03-07 12:29:00 +010080 }
Filip Pytloun38005aa2017-03-06 10:26:38 +010081 }
Jakub Josefc5a223a2017-03-01 14:40:08 +010082 }
chnydaa9c88702017-05-09 16:51:07 +020083
chnyda14e44292017-05-13 19:00:06 +020084 stage("test-nodes") {
Jakub Josefbcd2e902017-06-13 14:40:41 +020085 if(checkouted) {
Jakub Josef740a7882017-06-01 14:54:01 +020086 def workspace = common.getWorkspace()
87 def nodes = sh(script: "find ./nodes -type f -name 'cfg*.yml'", returnStdout: true).tokenize()
88 def buildSteps = [:]
chnydafd513922017-06-01 16:17:20 +020089 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 Josef740a7882017-06-01 14:54:01 +020098 }
chnydafd513922017-06-01 16:17:20 +020099 common.serial(buildSteps)
Jakub Josefc5a223a2017-03-01 14:40:08 +0100100 }
chnyda51b03142017-05-10 17:15:27 +0200101 }
Jakub Josefc5a223a2017-03-01 14:40:08 +0100102 } 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ál500c0182017-05-11 13:46:19 +0200109}
Jakub Josef8d024772017-05-15 18:07:45 +0200110
Jakub Josef27424bc2017-05-22 16:56:27 +0200111@NonCPS
112def _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 Josefbcd2e902017-06-13 14:40:41 +0200117 for (int i=0; i<triggeredBuilds.size(); i++) {
Jakub Josef27424bc2017-05-22 16:56:27 +0200118 buildNums.add(triggeredBuilds[i].number)
119 }
120 return buildNums
Jakub Josef8d024772017-05-15 18:07:45 +0200121}