blob: baa2983f4dfd9681f7592a08e30580c5d043332c [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
chnydad66d6fa2017-06-22 09:34:43 +02008 * SYSTEM_GIT_URL
9 * SYSTEM_GIT_REF
Jakub Josef83379312017-03-29 18:12:34 +020010 */
11
Jakub Josefc5a223a2017-03-01 14:40:08 +010012def common = new com.mirantis.mk.Common()
13def gerrit = new com.mirantis.mk.Gerrit()
Filip Pytloun38005aa2017-03-06 10:26:38 +010014def ssh = new com.mirantis.mk.Ssh()
15def git = new com.mirantis.mk.Git()
Jakub Josefc5a223a2017-03-01 14:40:08 +010016
Filip Pytloun19376a82017-03-07 12:29:00 +010017def gerritRef
18try {
19 gerritRef = GERRIT_REFSPEC
20} catch (MissingPropertyException e) {
21 gerritRef = null
22}
23
Jakub Josef83379312017-03-29 18:12:34 +020024def defaultGitRef, defaultGitUrl
Filip Pytloun19376a82017-03-07 12:29:00 +010025try {
Jakub Josef83379312017-03-29 18:12:34 +020026 defaultGitRef = DEFAULT_GIT_REF
27 defaultGitUrl = DEFAULT_GIT_URL
Filip Pytloun19376a82017-03-07 12:29:00 +010028} catch (MissingPropertyException e) {
Jakub Josef83379312017-03-29 18:12:34 +020029 defaultGitRef = null
30 defaultGitUrl = null
Filip Pytloun19376a82017-03-07 12:29:00 +010031}
Jakub Josef83379312017-03-29 18:12:34 +020032def checkouted = false
chnyda3b1d2a62017-06-13 10:08:12 +020033node("python") {
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
Jakub Josefb2235902017-06-16 12:49:16 +020048 def gerritChange = gerrit.getGerritChange(GERRIT_NAME, GERRIT_HOST, GERRIT_CHANGE_NUMBER, CREDENTIALS_ID, true)
49 // test if gerrit change is already Verified
Jakub Josef2ed50812017-06-16 14:56:58 +020050 if(gerrit.patchsetHasApproval(gerritChange.currentPatchSet,"Verified", "+")){
Jakub Josef894f1e32017-06-19 16:24:19 +000051 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 +020052 // test WIP contains in commit message
Jakub Josefb2235902017-06-16 12:49:16 +020053 }else if (gerritChange.commitMessage.contains("WIP")) {
Jakub Josef894f1e32017-06-19 16:24:19 +000054 common.successMsg("Commit message contains WIP, skipping tests") // do nothing
Jakub Josefbcd2e902017-06-13 14:40:41 +020055 } else {
56 def merged = gerritChange.status == "MERGED"
57 if(!merged){
58 checkouted = gerrit.gerritPatchsetCheckout ([
59 credentialsId : CREDENTIALS_ID
60 ])
61 } else{
62 common.successMsg("Change ${GERRIT_CHANGE_NUMBER} is already merged, no need to test them")
63 }
Jakub Joseffcb615e2017-04-10 14:34:40 +020064 }
chnydad66d6fa2017-06-22 09:34:43 +020065 // defaultGitUrl is passed to the triggered job
66 defaultGitUrl = "${GERRIT_SCHEME}://${GERRIT_NAME}@${GERRIT_HOST}:${GERRIT_PORT}/${GERRIT_PROJECT}"
67 defaultGitRef = GERRIT_REFSPEC
Jakub Josef83379312017-03-29 18:12:34 +020068 } else if(defaultGitRef && defaultGitUrl) {
Jakub Josefe1407ac2017-03-30 14:10:19 +020069 checkouted = gerrit.gerritPatchsetCheckout(defaultGitUrl, defaultGitRef, "HEAD", CREDENTIALS_ID)
Jakub Josefbcd2e902017-06-13 14:40:41 +020070 } else {
71 throw new Exception("Cannot checkout gerrit patchset, GERRIT_REFSPEC and DEFAULT_GIT_REF is null")
Filip Pytloun19376a82017-03-07 12:29:00 +010072 }
Jakub Josefc5a223a2017-03-01 14:40:08 +010073 }
chnydaa9c88702017-05-09 16:51:07 +020074
chnyda14e44292017-05-13 19:00:06 +020075 stage("test-nodes") {
Jakub Josefbcd2e902017-06-13 14:40:41 +020076 if(checkouted) {
Jakub Josef740a7882017-06-01 14:54:01 +020077 def nodes = sh(script: "find ./nodes -type f -name 'cfg*.yml'", returnStdout: true).tokenize()
chnydad66d6fa2017-06-22 09:34:43 +020078 def branches = [:]
79 for (int i = 0; i < nodes.size(); i++) {
80
chnyda1b606482017-06-22 15:17:20 +020081 def testTarget = sh(script: "basename ${nodes[i]} .yml", returnStdout: true).trim()
chnydac6092a22017-06-22 14:47:10 +020082 branches[testTarget] = {
chnydad66d6fa2017-06-22 09:34:43 +020083 build job: "test-salt-model-node", parameters: [
84 [$class: 'StringParameterValue', name: 'DEFAULT_GIT_URL', value: defaultGitUrl],
85 [$class: 'StringParameterValue', name: 'DEFAULT_GIT_REF', value: defaultGitRef],
86 [$class: 'StringParameterValue', name: 'NODE_TARGET', value: testTarget],
87 [$class: 'StringParameterValue', name: 'EXTRA_FORMULAS', value: EXTRA_FORMULAS],
88 [$class: 'StringParameterValue', name: 'CREDENTIALS_ID', value: CREDENTIALS_ID],
89 [$class: 'StringParameterValue', name: 'SYSTEM_GIT_URL', value: SYSTEM_GIT_URL],
90 [$class: 'StringParameterValue', name: 'SYSTEM_GIT_REF', value: SYSTEM_GIT_REF]
91 ]}
Jakub Josef740a7882017-06-01 14:54:01 +020092 }
chnyda1b606482017-06-22 15:17:20 +020093 parallel branches
Jakub Josefc5a223a2017-03-01 14:40:08 +010094 }
chnyda51b03142017-05-10 17:15:27 +020095 }
Jakub Josefc5a223a2017-03-01 14:40:08 +010096 } catch (Throwable e) {
Jakub Josefc5a223a2017-03-01 14:40:08 +010097 currentBuild.result = "FAILURE"
98 throw e
99 } finally {
100 common.sendNotification(currentBuild.result,"",["slack"])
101 }
Tomáš Kukrál500c0182017-05-11 13:46:19 +0200102}
Jakub Josef8d024772017-05-15 18:07:45 +0200103
Jakub Josef27424bc2017-05-22 16:56:27 +0200104@NonCPS
105def _getRunningTriggeredTestsBuildNumbers(jobName, gerritChangeNumber, excludePatchsetNumber){
106 def gerrit = new com.mirantis.mk.Gerrit()
107 def jenkinsUtils = new com.mirantis.mk.JenkinsUtils()
108 def triggeredBuilds= gerrit.getGerritTriggeredBuilds(jenkinsUtils.getJobRunningBuilds(jobName), gerritChangeNumber, excludePatchsetNumber)
109 def buildNums =[]
Jakub Josefbcd2e902017-06-13 14:40:41 +0200110 for (int i=0; i<triggeredBuilds.size(); i++) {
Jakub Josef27424bc2017-05-22 16:56:27 +0200111 buildNums.add(triggeredBuilds[i].number)
112 }
113 return buildNums
Jakub Josef8d024772017-05-15 18:07:45 +0200114}