blob: c6f1a71684a21c6b81446abe5eab4d8ee56f009b [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
47 def gerritChange = gerrit.getGerritChange(GERRIT_NAME, GERRIT_HOST, GERRIT_CHANGE_NUMBER, CREDENTIALS_ID)
Jakub Josefbcd2e902017-06-13 14:40:41 +020048 // test WIP contains in commit message
49 if (gerritChange.commitMessage.contains("WIP")) {
50 common.successMsg("Commit message contains WIP, skipping tests") // do nothing
51 } else {
52 def merged = gerritChange.status == "MERGED"
53 if(!merged){
54 checkouted = gerrit.gerritPatchsetCheckout ([
55 credentialsId : CREDENTIALS_ID
56 ])
57 } else{
58 common.successMsg("Change ${GERRIT_CHANGE_NUMBER} is already merged, no need to test them")
59 }
Jakub Joseffcb615e2017-04-10 14:34:40 +020060 }
Jakub Josef83379312017-03-29 18:12:34 +020061 } else if(defaultGitRef && defaultGitUrl) {
Jakub Josefe1407ac2017-03-30 14:10:19 +020062 checkouted = gerrit.gerritPatchsetCheckout(defaultGitUrl, defaultGitRef, "HEAD", CREDENTIALS_ID)
Jakub Josefbcd2e902017-06-13 14:40:41 +020063 } else {
64 throw new Exception("Cannot checkout gerrit patchset, GERRIT_REFSPEC and DEFAULT_GIT_REF is null")
Filip Pytloun19376a82017-03-07 12:29:00 +010065 }
Jakub Josefbcd2e902017-06-13 14:40:41 +020066 if(checkouted) {
Jakub Josef83379312017-03-29 18:12:34 +020067 if (fileExists('classes/system')) {
68 ssh.prepareSshAgentKey(CREDENTIALS_ID)
Filip Pytloun19376a82017-03-07 12:29:00 +010069 dir('classes/system') {
Jakub Josef83379312017-03-29 18:12:34 +020070 remoteUrl = git.getGitRemote()
71 ssh.ensureKnownHosts(remoteUrl)
Filip Pytloun19376a82017-03-07 12:29:00 +010072 }
Jakub Josef83379312017-03-29 18:12:34 +020073 ssh.agentSh("git submodule init; git submodule sync; git submodule update --recursive")
Filip Pytloun19376a82017-03-07 12:29:00 +010074 }
Filip Pytloun38005aa2017-03-06 10:26:38 +010075 }
Jakub Josefc5a223a2017-03-01 14:40:08 +010076 }
chnydaa9c88702017-05-09 16:51:07 +020077
chnyda14e44292017-05-13 19:00:06 +020078 stage("test-nodes") {
Jakub Josefbcd2e902017-06-13 14:40:41 +020079 if(checkouted) {
Jakub Josef740a7882017-06-01 14:54:01 +020080 def workspace = common.getWorkspace()
81 def nodes = sh(script: "find ./nodes -type f -name 'cfg*.yml'", returnStdout: true).tokenize()
82 def buildSteps = [:]
chnydafd513922017-06-01 16:17:20 +020083 def partitionSize = (nodes.size() <= PARALLEL_NODE_GROUP_SIZE.toInteger()) ? nodes.size() : PARALLEL_NODE_GROUP_SIZE.toInteger()
84 def partitions = common.partitionList(nodes, partitionSize)
85 for (int i =0; i < partitions.size();i++) {
86 def partition = partitions[i]
87 buildSteps.put("partition-${i}", new HashMap<String,org.jenkinsci.plugins.workflow.cps.CpsClosure2>())
88 for(int k=0; k < partition.size;k++){
89 def basename = sh(script: "basename ${partition[k]} .yml", returnStdout: true).trim()
90 buildSteps.get("partition-${i}").put(basename, { saltModelTesting.setupAndTestNode(basename, EXTRA_FORMULAS, workspace) })
91 }
Jakub Josef740a7882017-06-01 14:54:01 +020092 }
chnydafd513922017-06-01 16:17:20 +020093 common.serial(buildSteps)
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) {
97 // If there was an error or exception thrown, the build failed
98 currentBuild.result = "FAILURE"
99 throw e
100 } finally {
101 common.sendNotification(currentBuild.result,"",["slack"])
102 }
Tomáš Kukrál500c0182017-05-11 13:46:19 +0200103}
Jakub Josef8d024772017-05-15 18:07:45 +0200104
Jakub Josef27424bc2017-05-22 16:56:27 +0200105@NonCPS
106def _getRunningTriggeredTestsBuildNumbers(jobName, gerritChangeNumber, excludePatchsetNumber){
107 def gerrit = new com.mirantis.mk.Gerrit()
108 def jenkinsUtils = new com.mirantis.mk.JenkinsUtils()
109 def triggeredBuilds= gerrit.getGerritTriggeredBuilds(jenkinsUtils.getJobRunningBuilds(jobName), gerritChangeNumber, excludePatchsetNumber)
110 def buildNums =[]
Jakub Josefbcd2e902017-06-13 14:40:41 +0200111 for (int i=0; i<triggeredBuilds.size(); i++) {
Jakub Josef27424bc2017-05-22 16:56:27 +0200112 buildNums.add(triggeredBuilds[i].number)
113 }
114 return buildNums
Jakub Josef8d024772017-05-15 18:07:45 +0200115}