blob: 0936113ef1370bba023206f9722b4f6c05cf135f [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()
Jakub Josefc5a223a2017-03-01 14:40:08 +010014
Filip Pytloun19376a82017-03-07 12:29:00 +010015def gerritRef
16try {
17 gerritRef = GERRIT_REFSPEC
18} catch (MissingPropertyException e) {
19 gerritRef = null
20}
21
Jakub Josef83379312017-03-29 18:12:34 +020022def defaultGitRef, defaultGitUrl
Filip Pytloun19376a82017-03-07 12:29:00 +010023try {
Jakub Josef83379312017-03-29 18:12:34 +020024 defaultGitRef = DEFAULT_GIT_REF
25 defaultGitUrl = DEFAULT_GIT_URL
Filip Pytloun19376a82017-03-07 12:29:00 +010026} catch (MissingPropertyException e) {
Jakub Josef83379312017-03-29 18:12:34 +020027 defaultGitRef = null
28 defaultGitUrl = null
Filip Pytloun19376a82017-03-07 12:29:00 +010029}
Jakub Josef83379312017-03-29 18:12:34 +020030def checkouted = false
Jakub Joseffcb615e2017-04-10 14:34:40 +020031def merged = false
chnydafa4b0ec2017-05-05 15:49:27 +020032node("python&&docker") {
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)
48 merged = gerritChange.status == "MERGED"
49 if(!merged){
50 checkouted = gerrit.gerritPatchsetCheckout ([
51 credentialsId : CREDENTIALS_ID
52 ])
chnydaa9c88702017-05-09 16:51:07 +020053 } else{
54 common.successMsg("Change ${GERRIT_CHANGE_NUMBER} is already merged, no need to gate them")
Jakub Joseffcb615e2017-04-10 14:34:40 +020055 }
Jakub Josef83379312017-03-29 18:12:34 +020056 } else if(defaultGitRef && defaultGitUrl) {
Jakub Josefe1407ac2017-03-30 14:10:19 +020057 checkouted = gerrit.gerritPatchsetCheckout(defaultGitUrl, defaultGitRef, "HEAD", CREDENTIALS_ID)
Filip Pytloun19376a82017-03-07 12:29:00 +010058 }
Jakub Josef83379312017-03-29 18:12:34 +020059 if(checkouted){
60 if (fileExists('classes/system')) {
61 ssh.prepareSshAgentKey(CREDENTIALS_ID)
Filip Pytloun19376a82017-03-07 12:29:00 +010062 dir('classes/system') {
Jakub Josef83379312017-03-29 18:12:34 +020063 remoteUrl = git.getGitRemote()
64 ssh.ensureKnownHosts(remoteUrl)
Filip Pytloun19376a82017-03-07 12:29:00 +010065 }
Jakub Josef83379312017-03-29 18:12:34 +020066 ssh.agentSh("git submodule init; git submodule sync; git submodule update --recursive")
Filip Pytloun19376a82017-03-07 12:29:00 +010067 }
Jakub Joseffcb615e2017-04-10 14:34:40 +020068 }else if(!merged){
Jakub Josef5ce6a362017-03-31 13:41:17 +020069 throw new Exception("Cannot checkout gerrit patchset, GERRIT_REFSPEC and DEFAULT_GIT_REF is null")
Filip Pytloun38005aa2017-03-06 10:26:38 +010070 }
Jakub Josefc5a223a2017-03-01 14:40:08 +010071 }
chnydaa9c88702017-05-09 16:51:07 +020072
chnyda14e44292017-05-13 19:00:06 +020073 stage("test-nodes") {
Jakub Josef35dd8c02017-05-17 14:25:42 +020074 def nodes = sh(script: "find ./nodes -type f -name 'cfg*.yml'", returnStdout: true).tokenize()
chnydab098e562017-05-16 10:31:26 +020075 def buildSteps = [:]
Jakub Josef35dd8c02017-05-17 14:25:42 +020076 if(nodes.size() > 1){
chnydad1f7c392017-05-19 10:37:11 +020077 if(nodes.size() <= 3 && PARALLEL_NODE_GROUP_SIZE.toInteger() != 1) {
Jakub Josef35dd8c02017-05-17 14:25:42 +020078 common.infoMsg("Found <=3 cfg nodes, running parallel test")
79 for(int i=0; i < nodes.size();i++){
80 def basename = sh(script: "basename ${partition[k]} .yml", returnStdout: true).trim()
81 buildSteps.put("node-${basename}", { setupAndTestNode(basename) })
82 }
83 parallel buildSteps
84 }else{
chnyda90b15a02017-05-19 10:45:12 +020085 common.infoMsg("Found more than 3 cfg nodes or debug enabled, running parallel group test with ${PARALLEL_NODE_GROUP_SIZE} nodes")
chnydad1f7c392017-05-19 10:37:11 +020086 def partitions = common.partitionList(nodes, PARALLEL_NODE_GROUP_SIZE.toInteger())
Jakub Josef35dd8c02017-05-17 14:25:42 +020087 for (int i=0; i < partitions.size();i++) {
88 def partition = partitions[i]
89 buildSteps.put("partition-${i}", new HashMap<String,org.jenkinsci.plugins.workflow.cps.CpsClosure2>())
90 for(int k=0; k < partition.size;k++){
91 def basename = sh(script: "basename ${partition[k]} .yml", returnStdout: true).trim()
92 buildSteps.get("partition-${i}").put(basename, { setupAndTestNode(basename) })
93 }
94 }
95 common.serial(buildSteps)
96 }
97 }else{
98 common.infoMsg("Found one cfg node, running single test")
99 def basename = sh(script: "basename ${nodes[0]} .yml", returnStdout: true).trim()
100 setupAndTestNode(basename)
Jakub Josefc5a223a2017-03-01 14:40:08 +0100101 }
chnyda51b03142017-05-10 17:15:27 +0200102 }
chnyda14e44292017-05-13 19:00:06 +0200103
Jakub Josefc5a223a2017-03-01 14:40:08 +0100104 } catch (Throwable e) {
105 // If there was an error or exception thrown, the build failed
106 currentBuild.result = "FAILURE"
107 throw e
108 } finally {
109 common.sendNotification(currentBuild.result,"",["slack"])
110 }
Tomáš Kukrál500c0182017-05-11 13:46:19 +0200111}
Jakub Josef8d024772017-05-15 18:07:45 +0200112
113def setupAndTestNode(masterName) {
Jakub Josef8d024772017-05-15 18:07:45 +0200114 def saltOpts = "--retcode-passthrough --force-color"
115 def common = new com.mirantis.mk.Common()
116 def workspace = common.getWorkspace()
chnydaaa869512017-05-25 13:30:32 +0200117 def imageFound = true
118 def img
119 try {
120 img = docker.image("tcpcloud/salt-models-testing")
121 img.pull()
122 } catch (Throwable e) {
123 img = docker.image("ubuntu:latest")
124 imageFound = false
125 }
Jakub Josef8d024772017-05-15 18:07:45 +0200126
127 img.inside("-u root:root --hostname=${masterName}") {
128 wrap([$class: 'AnsiColorBuildWrapper']) {
chnydaaa869512017-05-25 13:30:32 +0200129 if (!imageFound) {
130 sh("apt-get update && apt-get install -y curl subversion git python-pip sudo python-pip python-dev zlib1g-dev git")
131 sh("pip install git+https://github.com/epcim/reclass.git@pr/fix/fix_raise_UndefinedVariableError")
132 }
Jakub Josef8d024772017-05-15 18:07:45 +0200133 sh("mkdir -p /srv/salt/ || true")
134 sh("cp -r ${workspace} /srv/salt/reclass")
Jakub Josef8d024772017-05-15 18:07:45 +0200135 sh("svn export --force https://github.com/salt-formulas/salt-formulas/trunk/deploy/scripts /srv/salt/scripts")
136 sh("git config --global user.email || git config --global user.email 'ci@ci.local'")
137 sh("git config --global user.name || git config --global user.name 'CI'")
Jakub Josef8d024772017-05-15 18:07:45 +0200138
139 // setup iniot and verify salt master and minions
Jakub Josef27424bc2017-05-22 16:56:27 +0200140 withEnv(["FORMULAS_SOURCE=pkg", "EXTRA_FORMULAS=${EXTRA_FORMULAS}", "DEBUG=1", "MASTER_HOSTNAME=${masterName}", "MINION_ID=${masterName}", "HOSTNAME=cfg01", "DOMAIN=mk-ci.local"]){
Jakub Josef8d024772017-05-15 18:07:45 +0200141 sh("bash -c 'echo $MASTER_HOSTNAME'")
142 sh("bash -c 'source /srv/salt/scripts/salt-master-init.sh; cd /srv/salt/scripts && system_config'")
143 sh("bash -c 'source /srv/salt/scripts/salt-master-init.sh; cd /srv/salt/scripts && saltmaster_bootstrap'")
144 sh("bash -c 'source /srv/salt/scripts/salt-master-init.sh; cd /srv/salt/scripts && saltmaster_init'")
145 }
Jakub Josef8d024772017-05-15 18:07:45 +0200146
147 def nodes
148 if (DEFAULT_GIT_URL.contains("mk-ci")) {
149 nodes = sh script: "find /srv/salt/reclass/nodes -name '*.yml' | grep -v 'cfg*.yml'", returnStdout: true
150 } else {
151 nodes = sh script:"find /srv/salt/reclass/nodes/_generated -name '*.yml' | grep -v 'cfg*.yml'", returnStdout: true
152 }
153 for (minion in nodes.tokenize()) {
154 def basename = sh script: "basename ${minion} .yml", returnStdout: true
155 if (!basename.trim().contains(masterName)) {
156 testMinion(basename.trim())
157 }
158 }
159 }
160 }
161}
162
163def testMinion(minionName)
164{
165 sh("service salt-master restart && service salt-minion restart && sleep 5 && bash -c 'source /srv/salt/scripts/salt-master-init.sh; cd /srv/salt/scripts && verify_salt_minion ${minionName}'")
Jakub Josef27424bc2017-05-22 16:56:27 +0200166}
167
168@NonCPS
169def _getRunningTriggeredTestsBuildNumbers(jobName, gerritChangeNumber, excludePatchsetNumber){
170 def gerrit = new com.mirantis.mk.Gerrit()
171 def jenkinsUtils = new com.mirantis.mk.JenkinsUtils()
172 def triggeredBuilds= gerrit.getGerritTriggeredBuilds(jenkinsUtils.getJobRunningBuilds(jobName), gerritChangeNumber, excludePatchsetNumber)
173 def buildNums =[]
174 for(int i=0;i<triggeredBuilds.size();i++){
175 buildNums.add(triggeredBuilds[i].number)
176 }
177 return buildNums
Jakub Josef8d024772017-05-15 18:07:45 +0200178}