blob: 79a6662becdda7182e38e71606016e098eb1aa6f [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
chnyda64b73582017-07-19 12:01:32 +020017def config_node_name_pattern
18try {
19 config_node_name_pattern = CONFIG_NODE_NAME_PATTERN
20} catch (MissingPropertyException e) {
21 config_node_name_pattern = "cfg01"
22}
23
Filip Pytloun19376a82017-03-07 12:29:00 +010024def gerritRef
25try {
26 gerritRef = GERRIT_REFSPEC
27} catch (MissingPropertyException e) {
28 gerritRef = null
29}
30
Mykyta Karpinc0758f32017-06-23 18:10:24 +030031def formulasSource
32try {
33 formulasSource = FORMULAS_SOURCE
34} catch (MissingPropertyException e) {
35 formulasSource = "pkg"
36}
37
Jakub Josef83379312017-03-29 18:12:34 +020038def defaultGitRef, defaultGitUrl
Filip Pytloun19376a82017-03-07 12:29:00 +010039try {
Jakub Josef83379312017-03-29 18:12:34 +020040 defaultGitRef = DEFAULT_GIT_REF
41 defaultGitUrl = DEFAULT_GIT_URL
Filip Pytloun19376a82017-03-07 12:29:00 +010042} catch (MissingPropertyException e) {
Jakub Josef83379312017-03-29 18:12:34 +020043 defaultGitRef = null
44 defaultGitUrl = null
Filip Pytloun19376a82017-03-07 12:29:00 +010045}
Jakub Josef83379312017-03-29 18:12:34 +020046def checkouted = false
chnyda3b1d2a62017-06-13 10:08:12 +020047node("python") {
Jakub Josefc5a223a2017-03-01 14:40:08 +010048 try{
Jakub Josef27424bc2017-05-22 16:56:27 +020049 stage("stop old tests"){
50 if (gerritRef) {
51 def runningTestBuildNums = _getRunningTriggeredTestsBuildNumbers(env["JOB_NAME"], GERRIT_CHANGE_NUMBER, GERRIT_PATCHSET_NUMBER)
52 for(int i=0; i<runningTestBuildNums.size(); i++){
53 common.infoMsg("Old test with run number ${runningTestBuildNums[i]} found, stopping")
54 Jenkins.instance.getItemByFullName(env["JOB_NAME"]).getBuildByNumber(runningTestBuildNums[i]).finish(hudson.model.Result.ABORTED, new java.io.IOException("Aborting build"));
55 }
56 }
57 }
Jakub Josefc5a223a2017-03-01 14:40:08 +010058 stage("checkout") {
Filip Pytloun19376a82017-03-07 12:29:00 +010059 if (gerritRef) {
Jakub Josef83379312017-03-29 18:12:34 +020060 // job is triggered by Gerrit
Jakub Joseffcb615e2017-04-10 14:34:40 +020061 // test if change aren't already merged
Jakub Josefb2235902017-06-16 12:49:16 +020062 def gerritChange = gerrit.getGerritChange(GERRIT_NAME, GERRIT_HOST, GERRIT_CHANGE_NUMBER, CREDENTIALS_ID, true)
63 // test if gerrit change is already Verified
Jakub Josef2ed50812017-06-16 14:56:58 +020064 if(gerrit.patchsetHasApproval(gerritChange.currentPatchSet,"Verified", "+")){
Jakub Josef894f1e32017-06-19 16:24:19 +000065 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 +020066 // test WIP contains in commit message
Jakub Josefb2235902017-06-16 12:49:16 +020067 }else if (gerritChange.commitMessage.contains("WIP")) {
Jakub Josef894f1e32017-06-19 16:24:19 +000068 common.successMsg("Commit message contains WIP, skipping tests") // do nothing
Jakub Josefbcd2e902017-06-13 14:40:41 +020069 } else {
70 def merged = gerritChange.status == "MERGED"
71 if(!merged){
72 checkouted = gerrit.gerritPatchsetCheckout ([
73 credentialsId : CREDENTIALS_ID
74 ])
75 } else{
76 common.successMsg("Change ${GERRIT_CHANGE_NUMBER} is already merged, no need to test them")
77 }
Jakub Joseffcb615e2017-04-10 14:34:40 +020078 }
chnydad66d6fa2017-06-22 09:34:43 +020079 // defaultGitUrl is passed to the triggered job
80 defaultGitUrl = "${GERRIT_SCHEME}://${GERRIT_NAME}@${GERRIT_HOST}:${GERRIT_PORT}/${GERRIT_PROJECT}"
81 defaultGitRef = GERRIT_REFSPEC
Jakub Josef83379312017-03-29 18:12:34 +020082 } else if(defaultGitRef && defaultGitUrl) {
Jakub Josefe1407ac2017-03-30 14:10:19 +020083 checkouted = gerrit.gerritPatchsetCheckout(defaultGitUrl, defaultGitRef, "HEAD", CREDENTIALS_ID)
Jakub Josefbcd2e902017-06-13 14:40:41 +020084 } else {
85 throw new Exception("Cannot checkout gerrit patchset, GERRIT_REFSPEC and DEFAULT_GIT_REF is null")
Filip Pytloun19376a82017-03-07 12:29:00 +010086 }
Jakub Josefc5a223a2017-03-01 14:40:08 +010087 }
chnydaa9c88702017-05-09 16:51:07 +020088
chnyda14e44292017-05-13 19:00:06 +020089 stage("test-nodes") {
Jakub Josefbcd2e902017-06-13 14:40:41 +020090 if(checkouted) {
chnyda64b73582017-07-19 12:01:32 +020091 def nodes = sh(script: "find ./nodes -type f -name '${config_node_name_pattern}*.yml'", returnStdout: true).tokenize()
chnydad66d6fa2017-06-22 09:34:43 +020092 def branches = [:]
chnyda81ae93d2017-06-22 15:49:52 +020093 def acc = 0
chnydad66d6fa2017-06-22 09:34:43 +020094 for (int i = 0; i < nodes.size(); i++) {
chnyda1b606482017-06-22 15:17:20 +020095 def testTarget = sh(script: "basename ${nodes[i]} .yml", returnStdout: true).trim()
chnyda1186a9c2017-06-26 13:13:32 +020096 if (acc >= PARALLEL_NODE_GROUP_SIZE.toInteger()) {
chnyda81ae93d2017-06-22 15:49:52 +020097 parallel branches
chnyda81ae93d2017-06-22 15:49:52 +020098 branches = [:]
chnyda1186a9c2017-06-26 13:13:32 +020099 acc = 0
chnyda81ae93d2017-06-22 15:49:52 +0200100 }
chnyda1186a9c2017-06-26 13:13:32 +0200101
102 branches[testTarget] = {
103 build job: "test-salt-model-node", parameters: [
104 [$class: 'StringParameterValue', name: 'DEFAULT_GIT_URL', value: defaultGitUrl],
105 [$class: 'StringParameterValue', name: 'DEFAULT_GIT_REF', value: defaultGitRef],
106 [$class: 'StringParameterValue', name: 'NODE_TARGET', value: testTarget],
chnydaab82fd42017-06-26 13:30:02 +0200107 [$class: 'StringParameterValue', name: 'FORMULAS_SOURCE', value: formulasSource],
chnyda1186a9c2017-06-26 13:13:32 +0200108 [$class: 'StringParameterValue', name: 'EXTRA_FORMULAS', value: EXTRA_FORMULAS],
Jakub Josef09251c92017-06-27 11:49:33 +0200109 [$class: 'StringParameterValue', name: 'FORMULAS_REVISION', value: FORMULAS_REVISION],
chnyda1186a9c2017-06-26 13:13:32 +0200110 [$class: 'StringParameterValue', name: 'CREDENTIALS_ID', value: CREDENTIALS_ID],
111 [$class: 'StringParameterValue', name: 'SYSTEM_GIT_URL', value: SYSTEM_GIT_URL],
112 [$class: 'StringParameterValue', name: 'SYSTEM_GIT_REF', value: SYSTEM_GIT_REF]
113 ]}
114 acc++;
chnyda81ae93d2017-06-22 15:49:52 +0200115 }
chnydaf504ba12017-06-26 13:35:46 +0200116 if (acc != 0) {
117 parallel branches
118 }
Jakub Josefc5a223a2017-03-01 14:40:08 +0100119 }
chnyda51b03142017-05-10 17:15:27 +0200120 }
Jakub Josefc5a223a2017-03-01 14:40:08 +0100121 } catch (Throwable e) {
Jakub Josefc5a223a2017-03-01 14:40:08 +0100122 currentBuild.result = "FAILURE"
123 throw e
124 } finally {
125 common.sendNotification(currentBuild.result,"",["slack"])
126 }
Tomáš Kukrál500c0182017-05-11 13:46:19 +0200127}
Jakub Josef8d024772017-05-15 18:07:45 +0200128
Jakub Josef27424bc2017-05-22 16:56:27 +0200129@NonCPS
130def _getRunningTriggeredTestsBuildNumbers(jobName, gerritChangeNumber, excludePatchsetNumber){
131 def gerrit = new com.mirantis.mk.Gerrit()
132 def jenkinsUtils = new com.mirantis.mk.JenkinsUtils()
133 def triggeredBuilds= gerrit.getGerritTriggeredBuilds(jenkinsUtils.getJobRunningBuilds(jobName), gerritChangeNumber, excludePatchsetNumber)
134 def buildNums =[]
Jakub Josefbcd2e902017-06-13 14:40:41 +0200135 for (int i=0; i<triggeredBuilds.size(); i++) {
Jakub Josef27424bc2017-05-22 16:56:27 +0200136 buildNums.add(triggeredBuilds[i].number)
137 }
138 return buildNums
Jakub Josef8d024772017-05-15 18:07:45 +0200139}