Jakub Josef | 8337931 | 2017-03-29 18:12:34 +0200 | [diff] [blame] | 1 | |
| 2 | /** |
| 3 | * Test salt models pipeline |
| 4 | * DEFAULT_GIT_REF |
| 5 | * DEFAULT_GIT_URL |
| 6 | * CREDENTIALS_ID |
Jakub Josef | 27424bc | 2017-05-22 16:56:27 +0200 | [diff] [blame] | 7 | * EXTRA_FORMULAS |
Jakub Josef | 8337931 | 2017-03-29 18:12:34 +0200 | [diff] [blame] | 8 | */ |
| 9 | |
Jakub Josef | c5a223a | 2017-03-01 14:40:08 +0100 | [diff] [blame] | 10 | def common = new com.mirantis.mk.Common() |
| 11 | def gerrit = new com.mirantis.mk.Gerrit() |
Filip Pytloun | 38005aa | 2017-03-06 10:26:38 +0100 | [diff] [blame] | 12 | def ssh = new com.mirantis.mk.Ssh() |
| 13 | def git = new com.mirantis.mk.Git() |
Jakub Josef | c5a223a | 2017-03-01 14:40:08 +0100 | [diff] [blame] | 14 | |
Filip Pytloun | 19376a8 | 2017-03-07 12:29:00 +0100 | [diff] [blame] | 15 | def gerritRef |
| 16 | try { |
| 17 | gerritRef = GERRIT_REFSPEC |
| 18 | } catch (MissingPropertyException e) { |
| 19 | gerritRef = null |
| 20 | } |
| 21 | |
Jakub Josef | 8337931 | 2017-03-29 18:12:34 +0200 | [diff] [blame] | 22 | def defaultGitRef, defaultGitUrl |
Filip Pytloun | 19376a8 | 2017-03-07 12:29:00 +0100 | [diff] [blame] | 23 | try { |
Jakub Josef | 8337931 | 2017-03-29 18:12:34 +0200 | [diff] [blame] | 24 | defaultGitRef = DEFAULT_GIT_REF |
| 25 | defaultGitUrl = DEFAULT_GIT_URL |
Filip Pytloun | 19376a8 | 2017-03-07 12:29:00 +0100 | [diff] [blame] | 26 | } catch (MissingPropertyException e) { |
Jakub Josef | 8337931 | 2017-03-29 18:12:34 +0200 | [diff] [blame] | 27 | defaultGitRef = null |
| 28 | defaultGitUrl = null |
Filip Pytloun | 19376a8 | 2017-03-07 12:29:00 +0100 | [diff] [blame] | 29 | } |
Jakub Josef | 8337931 | 2017-03-29 18:12:34 +0200 | [diff] [blame] | 30 | def checkouted = false |
Jakub Josef | fcb615e | 2017-04-10 14:34:40 +0200 | [diff] [blame] | 31 | def merged = false |
chnyda | fa4b0ec | 2017-05-05 15:49:27 +0200 | [diff] [blame] | 32 | node("python&&docker") { |
Jakub Josef | c5a223a | 2017-03-01 14:40:08 +0100 | [diff] [blame] | 33 | try{ |
Jakub Josef | 27424bc | 2017-05-22 16:56:27 +0200 | [diff] [blame] | 34 | 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 Josef | c5a223a | 2017-03-01 14:40:08 +0100 | [diff] [blame] | 43 | stage("checkout") { |
Filip Pytloun | 19376a8 | 2017-03-07 12:29:00 +0100 | [diff] [blame] | 44 | if (gerritRef) { |
Jakub Josef | 8337931 | 2017-03-29 18:12:34 +0200 | [diff] [blame] | 45 | // job is triggered by Gerrit |
Jakub Josef | fcb615e | 2017-04-10 14:34:40 +0200 | [diff] [blame] | 46 | // 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 | ]) |
chnyda | a9c8870 | 2017-05-09 16:51:07 +0200 | [diff] [blame] | 53 | } else{ |
| 54 | common.successMsg("Change ${GERRIT_CHANGE_NUMBER} is already merged, no need to gate them") |
Jakub Josef | fcb615e | 2017-04-10 14:34:40 +0200 | [diff] [blame] | 55 | } |
Jakub Josef | 8337931 | 2017-03-29 18:12:34 +0200 | [diff] [blame] | 56 | } else if(defaultGitRef && defaultGitUrl) { |
Jakub Josef | e1407ac | 2017-03-30 14:10:19 +0200 | [diff] [blame] | 57 | checkouted = gerrit.gerritPatchsetCheckout(defaultGitUrl, defaultGitRef, "HEAD", CREDENTIALS_ID) |
Filip Pytloun | 19376a8 | 2017-03-07 12:29:00 +0100 | [diff] [blame] | 58 | } |
Jakub Josef | 8337931 | 2017-03-29 18:12:34 +0200 | [diff] [blame] | 59 | if(checkouted){ |
| 60 | if (fileExists('classes/system')) { |
| 61 | ssh.prepareSshAgentKey(CREDENTIALS_ID) |
Filip Pytloun | 19376a8 | 2017-03-07 12:29:00 +0100 | [diff] [blame] | 62 | dir('classes/system') { |
Jakub Josef | 8337931 | 2017-03-29 18:12:34 +0200 | [diff] [blame] | 63 | remoteUrl = git.getGitRemote() |
| 64 | ssh.ensureKnownHosts(remoteUrl) |
Filip Pytloun | 19376a8 | 2017-03-07 12:29:00 +0100 | [diff] [blame] | 65 | } |
Jakub Josef | 8337931 | 2017-03-29 18:12:34 +0200 | [diff] [blame] | 66 | ssh.agentSh("git submodule init; git submodule sync; git submodule update --recursive") |
Filip Pytloun | 19376a8 | 2017-03-07 12:29:00 +0100 | [diff] [blame] | 67 | } |
Jakub Josef | fcb615e | 2017-04-10 14:34:40 +0200 | [diff] [blame] | 68 | }else if(!merged){ |
Jakub Josef | 5ce6a36 | 2017-03-31 13:41:17 +0200 | [diff] [blame] | 69 | throw new Exception("Cannot checkout gerrit patchset, GERRIT_REFSPEC and DEFAULT_GIT_REF is null") |
Filip Pytloun | 38005aa | 2017-03-06 10:26:38 +0100 | [diff] [blame] | 70 | } |
Jakub Josef | c5a223a | 2017-03-01 14:40:08 +0100 | [diff] [blame] | 71 | } |
chnyda | a9c8870 | 2017-05-09 16:51:07 +0200 | [diff] [blame] | 72 | |
chnyda | 14e4429 | 2017-05-13 19:00:06 +0200 | [diff] [blame] | 73 | stage("test-nodes") { |
Jakub Josef | 35dd8c0 | 2017-05-17 14:25:42 +0200 | [diff] [blame] | 74 | def nodes = sh(script: "find ./nodes -type f -name 'cfg*.yml'", returnStdout: true).tokenize() |
chnyda | b098e56 | 2017-05-16 10:31:26 +0200 | [diff] [blame] | 75 | def buildSteps = [:] |
Jakub Josef | 35dd8c0 | 2017-05-17 14:25:42 +0200 | [diff] [blame] | 76 | if(nodes.size() > 1){ |
chnyda | d1f7c39 | 2017-05-19 10:37:11 +0200 | [diff] [blame] | 77 | if(nodes.size() <= 3 && PARALLEL_NODE_GROUP_SIZE.toInteger() != 1) { |
Jakub Josef | 35dd8c0 | 2017-05-17 14:25:42 +0200 | [diff] [blame] | 78 | 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{ |
chnyda | 90b15a0 | 2017-05-19 10:45:12 +0200 | [diff] [blame] | 85 | common.infoMsg("Found more than 3 cfg nodes or debug enabled, running parallel group test with ${PARALLEL_NODE_GROUP_SIZE} nodes") |
chnyda | d1f7c39 | 2017-05-19 10:37:11 +0200 | [diff] [blame] | 86 | def partitions = common.partitionList(nodes, PARALLEL_NODE_GROUP_SIZE.toInteger()) |
Jakub Josef | 35dd8c0 | 2017-05-17 14:25:42 +0200 | [diff] [blame] | 87 | 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 Josef | c5a223a | 2017-03-01 14:40:08 +0100 | [diff] [blame] | 101 | } |
chnyda | 51b0314 | 2017-05-10 17:15:27 +0200 | [diff] [blame] | 102 | } |
chnyda | 14e4429 | 2017-05-13 19:00:06 +0200 | [diff] [blame] | 103 | |
Jakub Josef | c5a223a | 2017-03-01 14:40:08 +0100 | [diff] [blame] | 104 | } 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ál | 500c018 | 2017-05-11 13:46:19 +0200 | [diff] [blame] | 111 | } |
Jakub Josef | 8d02477 | 2017-05-15 18:07:45 +0200 | [diff] [blame] | 112 | |
| 113 | def setupAndTestNode(masterName) { |
| 114 | def img = docker.image("ubuntu:latest") |
| 115 | def saltOpts = "--retcode-passthrough --force-color" |
| 116 | def common = new com.mirantis.mk.Common() |
| 117 | def workspace = common.getWorkspace() |
| 118 | |
| 119 | img.inside("-u root:root --hostname=${masterName}") { |
| 120 | wrap([$class: 'AnsiColorBuildWrapper']) { |
| 121 | sh("mkdir -p /srv/salt/ || true") |
| 122 | sh("cp -r ${workspace} /srv/salt/reclass") |
| 123 | sh("apt-get update && apt-get install -y curl subversion git python-pip sudo python-pip python-dev zlib1g-dev git") |
| 124 | sh("svn export --force https://github.com/salt-formulas/salt-formulas/trunk/deploy/scripts /srv/salt/scripts") |
| 125 | sh("git config --global user.email || git config --global user.email 'ci@ci.local'") |
| 126 | sh("git config --global user.name || git config --global user.name 'CI'") |
| 127 | sh("pip install git+https://github.com/epcim/reclass.git@pr/fix/fix_raise_UndefinedVariableError") |
| 128 | sh("ls -lRa /srv/salt/reclass") |
| 129 | |
| 130 | // setup iniot and verify salt master and minions |
Jakub Josef | 27424bc | 2017-05-22 16:56:27 +0200 | [diff] [blame] | 131 | withEnv(["FORMULAS_SOURCE=pkg", "EXTRA_FORMULAS=${EXTRA_FORMULAS}", "DEBUG=1", "MASTER_HOSTNAME=${masterName}", "MINION_ID=${masterName}", "HOSTNAME=cfg01", "DOMAIN=mk-ci.local"]){ |
Jakub Josef | 8d02477 | 2017-05-15 18:07:45 +0200 | [diff] [blame] | 132 | sh("bash -c 'echo $MASTER_HOSTNAME'") |
| 133 | sh("bash -c 'source /srv/salt/scripts/salt-master-init.sh; cd /srv/salt/scripts && system_config'") |
| 134 | sh("bash -c 'source /srv/salt/scripts/salt-master-init.sh; cd /srv/salt/scripts && saltmaster_bootstrap'") |
| 135 | sh("bash -c 'source /srv/salt/scripts/salt-master-init.sh; cd /srv/salt/scripts && saltmaster_init'") |
| 136 | } |
| 137 | sh("ls -lRa /srv/salt/reclass/classes/service/") |
| 138 | |
| 139 | def nodes |
| 140 | if (DEFAULT_GIT_URL.contains("mk-ci")) { |
| 141 | nodes = sh script: "find /srv/salt/reclass/nodes -name '*.yml' | grep -v 'cfg*.yml'", returnStdout: true |
| 142 | } else { |
| 143 | nodes = sh script:"find /srv/salt/reclass/nodes/_generated -name '*.yml' | grep -v 'cfg*.yml'", returnStdout: true |
| 144 | } |
| 145 | for (minion in nodes.tokenize()) { |
| 146 | def basename = sh script: "basename ${minion} .yml", returnStdout: true |
| 147 | if (!basename.trim().contains(masterName)) { |
| 148 | testMinion(basename.trim()) |
| 149 | } |
| 150 | } |
| 151 | } |
| 152 | } |
| 153 | } |
| 154 | |
| 155 | def testMinion(minionName) |
| 156 | { |
| 157 | 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 Josef | 27424bc | 2017-05-22 16:56:27 +0200 | [diff] [blame] | 158 | } |
| 159 | |
| 160 | @NonCPS |
| 161 | def _getRunningTriggeredTestsBuildNumbers(jobName, gerritChangeNumber, excludePatchsetNumber){ |
| 162 | def gerrit = new com.mirantis.mk.Gerrit() |
| 163 | def jenkinsUtils = new com.mirantis.mk.JenkinsUtils() |
| 164 | def triggeredBuilds= gerrit.getGerritTriggeredBuilds(jenkinsUtils.getJobRunningBuilds(jobName), gerritChangeNumber, excludePatchsetNumber) |
| 165 | def buildNums =[] |
| 166 | for(int i=0;i<triggeredBuilds.size();i++){ |
| 167 | buildNums.add(triggeredBuilds[i].number) |
| 168 | } |
| 169 | return buildNums |
Jakub Josef | 8d02477 | 2017-05-15 18:07:45 +0200 | [diff] [blame] | 170 | } |