blob: 1df659edd600fea71c7cedd3ccbf93b93f114761 [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
Jakub Joseffcb615e2017-04-10 14:34:40 +020032def merged = false
chnydafa4b0ec2017-05-05 15:49:27 +020033node("python&&docker") {
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
48 def gerritChange = gerrit.getGerritChange(GERRIT_NAME, GERRIT_HOST, GERRIT_CHANGE_NUMBER, CREDENTIALS_ID)
49 merged = gerritChange.status == "MERGED"
50 if(!merged){
51 checkouted = gerrit.gerritPatchsetCheckout ([
52 credentialsId : CREDENTIALS_ID
53 ])
chnydaa9c88702017-05-09 16:51:07 +020054 } else{
Jakub Josef740a7882017-06-01 14:54:01 +020055 common.successMsg("Change ${GERRIT_CHANGE_NUMBER} is already merged, no need to test them")
Jakub Joseffcb615e2017-04-10 14:34:40 +020056 }
Jakub Josef83379312017-03-29 18:12:34 +020057 } else if(defaultGitRef && defaultGitUrl) {
Jakub Josefe1407ac2017-03-30 14:10:19 +020058 checkouted = gerrit.gerritPatchsetCheckout(defaultGitUrl, defaultGitRef, "HEAD", CREDENTIALS_ID)
Filip Pytloun19376a82017-03-07 12:29:00 +010059 }
Jakub Josef83379312017-03-29 18:12:34 +020060 if(checkouted){
61 if (fileExists('classes/system')) {
62 ssh.prepareSshAgentKey(CREDENTIALS_ID)
Filip Pytloun19376a82017-03-07 12:29:00 +010063 dir('classes/system') {
Jakub Josef83379312017-03-29 18:12:34 +020064 remoteUrl = git.getGitRemote()
65 ssh.ensureKnownHosts(remoteUrl)
Filip Pytloun19376a82017-03-07 12:29:00 +010066 }
Jakub Josef83379312017-03-29 18:12:34 +020067 ssh.agentSh("git submodule init; git submodule sync; git submodule update --recursive")
Filip Pytloun19376a82017-03-07 12:29:00 +010068 }
Jakub Joseffcb615e2017-04-10 14:34:40 +020069 }else if(!merged){
Jakub Josef5ce6a362017-03-31 13:41:17 +020070 throw new Exception("Cannot checkout gerrit patchset, GERRIT_REFSPEC and DEFAULT_GIT_REF is null")
Filip Pytloun38005aa2017-03-06 10:26:38 +010071 }
Jakub Josefc5a223a2017-03-01 14:40:08 +010072 }
chnydaa9c88702017-05-09 16:51:07 +020073
chnyda14e44292017-05-13 19:00:06 +020074 stage("test-nodes") {
Jakub Josef740a7882017-06-01 14:54:01 +020075 if(!merged){
76 def workspace = common.getWorkspace()
77 def nodes = sh(script: "find ./nodes -type f -name 'cfg*.yml'", returnStdout: true).tokenize()
78 def buildSteps = [:]
79 if(nodes.size() > 1){
80 if(nodes.size() <= 3 && PARALLEL_NODE_GROUP_SIZE.toInteger() != 1) {
81 common.infoMsg("Found <=3 cfg nodes, running parallel test")
82 for(int i=0; i < nodes.size();i++){
83 def basename = sh(script: "basename ${partition[k]} .yml", returnStdout: true).trim()
84 buildSteps.put("node-${basename}", { saltModelTesting.setupAndTestNode(basename, EXTRA_FORMULAS, workspace) })
85 }
86 parallel buildSteps
87 }else{
88 common.infoMsg("Found more than 3 cfg nodes or debug enabled, running parallel group test with ${PARALLEL_NODE_GROUP_SIZE} nodes")
89 def partitions = common.partitionList(nodes, PARALLEL_NODE_GROUP_SIZE.toInteger())
90 for (int i=0; i < partitions.size();i++) {
91 def partition = partitions[i]
92 buildSteps.put("partition-${i}", new HashMap<String,org.jenkinsci.plugins.workflow.cps.CpsClosure2>())
93 for(int k=0; k < partition.size;k++){
94 def basename = sh(script: "basename ${partition[k]} .yml", returnStdout: true).trim()
95 buildSteps.get("partition-${i}").put(basename, { saltModelTesting.setupAndTestNode(basename, EXTRA_FORMULAS, workspace) })
96 }
Jakub Josef35dd8c02017-05-17 14:25:42 +020097 }
Jakub Josef740a7882017-06-01 14:54:01 +020098 common.serial(buildSteps)
Jakub Josef35dd8c02017-05-17 14:25:42 +020099 }
Jakub Josef740a7882017-06-01 14:54:01 +0200100 }else{
101 common.infoMsg("Found one cfg node, running single test")
102 def basename = sh(script: "basename ${nodes[0]} .yml", returnStdout: true).trim()
103 saltModelTesting.setupAndTestNode(basename, EXTRA_FORMULAS, workspace)
104 }
Jakub Josefc5a223a2017-03-01 14:40:08 +0100105 }
chnyda51b03142017-05-10 17:15:27 +0200106 }
Jakub Josefc5a223a2017-03-01 14:40:08 +0100107 } catch (Throwable e) {
108 // If there was an error or exception thrown, the build failed
109 currentBuild.result = "FAILURE"
110 throw e
111 } finally {
112 common.sendNotification(currentBuild.result,"",["slack"])
113 }
Tomáš Kukrál500c0182017-05-11 13:46:19 +0200114}
Jakub Josef8d024772017-05-15 18:07:45 +0200115
Jakub Josef27424bc2017-05-22 16:56:27 +0200116@NonCPS
117def _getRunningTriggeredTestsBuildNumbers(jobName, gerritChangeNumber, excludePatchsetNumber){
118 def gerrit = new com.mirantis.mk.Gerrit()
119 def jenkinsUtils = new com.mirantis.mk.JenkinsUtils()
120 def triggeredBuilds= gerrit.getGerritTriggeredBuilds(jenkinsUtils.getJobRunningBuilds(jobName), gerritChangeNumber, excludePatchsetNumber)
121 def buildNums =[]
122 for(int i=0;i<triggeredBuilds.size();i++){
123 buildNums.add(triggeredBuilds[i].number)
124 }
125 return buildNums
Jakub Josef8d024772017-05-15 18:07:45 +0200126}