blob: de6a61278775e33c0022e6f5d503a647035a32d1 [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 Josef894f1e32017-06-19 16:24:19 +000050 common.successMsg("Gerrit change ${GERRIT_CHANGE_NUMBER} patchset ${GERRIT_PATCHSET_NUMBER} already has Verified, skipping tests") // do nothing
Jakub Josefbcd2e902017-06-13 14:40:41 +020051 // test WIP contains in commit message
Jakub Josefb2235902017-06-16 12:49:16 +020052 }else if (gerritChange.commitMessage.contains("WIP")) {
Jakub Josef894f1e32017-06-19 16:24:19 +000053 common.successMsg("Commit message contains WIP, skipping tests") // do nothing
Jakub Josefbcd2e902017-06-13 14:40:41 +020054 } else {
55 def merged = gerritChange.status == "MERGED"
56 if(!merged){
57 checkouted = gerrit.gerritPatchsetCheckout ([
58 credentialsId : CREDENTIALS_ID
59 ])
60 } else{
61 common.successMsg("Change ${GERRIT_CHANGE_NUMBER} is already merged, no need to test them")
62 }
Jakub Joseffcb615e2017-04-10 14:34:40 +020063 }
Jakub Josef83379312017-03-29 18:12:34 +020064 } else if(defaultGitRef && defaultGitUrl) {
Jakub Josefe1407ac2017-03-30 14:10:19 +020065 checkouted = gerrit.gerritPatchsetCheckout(defaultGitUrl, defaultGitRef, "HEAD", CREDENTIALS_ID)
Jakub Josefbcd2e902017-06-13 14:40:41 +020066 } else {
67 throw new Exception("Cannot checkout gerrit patchset, GERRIT_REFSPEC and DEFAULT_GIT_REF is null")
Filip Pytloun19376a82017-03-07 12:29:00 +010068 }
Jakub Josefbcd2e902017-06-13 14:40:41 +020069 if(checkouted) {
Jakub Josef83379312017-03-29 18:12:34 +020070 if (fileExists('classes/system')) {
71 ssh.prepareSshAgentKey(CREDENTIALS_ID)
Filip Pytloun19376a82017-03-07 12:29:00 +010072 dir('classes/system') {
Jakub Josef83379312017-03-29 18:12:34 +020073 remoteUrl = git.getGitRemote()
74 ssh.ensureKnownHosts(remoteUrl)
Filip Pytloun19376a82017-03-07 12:29:00 +010075 }
Jakub Josef83379312017-03-29 18:12:34 +020076 ssh.agentSh("git submodule init; git submodule sync; git submodule update --recursive")
Filip Pytloun19376a82017-03-07 12:29:00 +010077 }
Filip Pytloun38005aa2017-03-06 10:26:38 +010078 }
Jakub Josefc5a223a2017-03-01 14:40:08 +010079 }
chnydaa9c88702017-05-09 16:51:07 +020080
chnyda14e44292017-05-13 19:00:06 +020081 stage("test-nodes") {
Jakub Josefbcd2e902017-06-13 14:40:41 +020082 if(checkouted) {
Jakub Josef740a7882017-06-01 14:54:01 +020083 def workspace = common.getWorkspace()
84 def nodes = sh(script: "find ./nodes -type f -name 'cfg*.yml'", returnStdout: true).tokenize()
85 def buildSteps = [:]
chnydafd513922017-06-01 16:17:20 +020086 def partitionSize = (nodes.size() <= PARALLEL_NODE_GROUP_SIZE.toInteger()) ? nodes.size() : PARALLEL_NODE_GROUP_SIZE.toInteger()
87 def partitions = common.partitionList(nodes, partitionSize)
88 for (int i =0; i < partitions.size();i++) {
89 def partition = partitions[i]
90 buildSteps.put("partition-${i}", new HashMap<String,org.jenkinsci.plugins.workflow.cps.CpsClosure2>())
91 for(int k=0; k < partition.size;k++){
92 def basename = sh(script: "basename ${partition[k]} .yml", returnStdout: true).trim()
93 buildSteps.get("partition-${i}").put(basename, { saltModelTesting.setupAndTestNode(basename, EXTRA_FORMULAS, workspace) })
94 }
Jakub Josef740a7882017-06-01 14:54:01 +020095 }
chnydafd513922017-06-01 16:17:20 +020096 common.serial(buildSteps)
Jakub Josefc5a223a2017-03-01 14:40:08 +010097 }
chnyda51b03142017-05-10 17:15:27 +020098 }
Jakub Josefc5a223a2017-03-01 14:40:08 +010099 } catch (Throwable e) {
100 // If there was an error or exception thrown, the build failed
101 currentBuild.result = "FAILURE"
102 throw e
103 } finally {
104 common.sendNotification(currentBuild.result,"",["slack"])
105 }
Tomáš Kukrál500c0182017-05-11 13:46:19 +0200106}
Jakub Josef8d024772017-05-15 18:07:45 +0200107
Jakub Josef27424bc2017-05-22 16:56:27 +0200108@NonCPS
109def _getRunningTriggeredTestsBuildNumbers(jobName, gerritChangeNumber, excludePatchsetNumber){
110 def gerrit = new com.mirantis.mk.Gerrit()
111 def jenkinsUtils = new com.mirantis.mk.JenkinsUtils()
112 def triggeredBuilds= gerrit.getGerritTriggeredBuilds(jenkinsUtils.getJobRunningBuilds(jobName), gerritChangeNumber, excludePatchsetNumber)
113 def buildNums =[]
Jakub Josefbcd2e902017-06-13 14:40:41 +0200114 for (int i=0; i<triggeredBuilds.size(); i++) {
Jakub Josef27424bc2017-05-22 16:56:27 +0200115 buildNums.add(triggeredBuilds[i].number)
116 }
117 return buildNums
Jakub Josef8d024772017-05-15 18:07:45 +0200118}