Jakub Josef | 8337931 | 2017-03-29 18:12:34 +0200 | [diff] [blame] | 1 | /** |
| 2 | * Test salt models pipeline |
Jakub Josef | cb4f1dc | 2018-02-19 15:27:29 +0100 | [diff] [blame] | 3 | * DEFAULT_GIT_URL default git url (will be used if pipeline run is not triggered by gerrit) |
| 4 | * DEFAULT_GIT_RED default git ref (branch,tag,...) (will be used if pipeline run is not triggered by gerrit) |
| 5 | * CREDENTIALS_ID Jenkins credetials id for git checkout |
| 6 | * EXTRA_FORMULAS extra formulas list for passing to salt bootstrap script |
| 7 | * MAX_CPU_PER_JOB max cpu count for one docket test instance |
| 8 | * SYSTEM_GIT_URL reclass system git URL (optional) |
| 9 | * SYSTEM_GIT_REF reclass system git URL (optional) |
| 10 | * TEST_CLUSTER_NAMES list of comma separated cluster names to test (optional, default all cluster levels) |
| 11 | * LEGACY_TEST_MODE legacy test mode flag |
| 12 | * RECLASS_IGNORE_CLASS_NOTFOUND ignore missing class flag for reclass config |
Jakub Josef | b8cdba9 | 2018-04-05 17:10:35 +0200 | [diff] [blame] | 13 | * RECLASS_VERSION Version of reclass to be used (branch, ...) |
Jakub Josef | cb4f1dc | 2018-02-19 15:27:29 +0100 | [diff] [blame] | 14 | * APT_REPOSITORY extra apt repository url |
| 15 | * APT_REPOSITORY_GPG extra apt repository url GPG |
Jakub Josef | 8337931 | 2017-03-29 18:12:34 +0200 | [diff] [blame] | 16 | */ |
| 17 | |
Jakub Josef | c5a223a | 2017-03-01 14:40:08 +0100 | [diff] [blame] | 18 | def gerrit = new com.mirantis.mk.Gerrit() |
Filip Pytloun | 38005aa | 2017-03-06 10:26:38 +0100 | [diff] [blame] | 19 | def ssh = new com.mirantis.mk.Ssh() |
| 20 | def git = new com.mirantis.mk.Git() |
Jakub Josef | c5a223a | 2017-03-01 14:40:08 +0100 | [diff] [blame] | 21 | |
chnyda | 3dcf0bd | 2017-11-30 11:49:24 +0100 | [diff] [blame] | 22 | def config_node_name_pattern |
chnyda | 64b7358 | 2017-07-19 12:01:32 +0200 | [diff] [blame] | 23 | try { |
| 24 | config_node_name_pattern = CONFIG_NODE_NAME_PATTERN |
| 25 | } catch (MissingPropertyException e) { |
| 26 | config_node_name_pattern = "cfg01" |
| 27 | } |
| 28 | |
Filip Pytloun | 19376a8 | 2017-03-07 12:29:00 +0100 | [diff] [blame] | 29 | def gerritRef |
| 30 | try { |
| 31 | gerritRef = GERRIT_REFSPEC |
| 32 | } catch (MissingPropertyException e) { |
| 33 | gerritRef = null |
| 34 | } |
| 35 | |
Mykyta Karpin | c0758f3 | 2017-06-23 18:10:24 +0300 | [diff] [blame] | 36 | def formulasSource |
| 37 | try { |
| 38 | formulasSource = FORMULAS_SOURCE |
| 39 | } catch (MissingPropertyException e) { |
| 40 | formulasSource = "pkg" |
| 41 | } |
| 42 | |
Jakub Josef | cb4f1dc | 2018-02-19 15:27:29 +0100 | [diff] [blame] | 43 | def testClusterNames |
| 44 | try { |
| 45 | testClusterNames = TEST_CLUSTER_NAMES |
| 46 | } catch (MissingPropertyException e) { |
| 47 | testClusterNames = "" |
| 48 | } |
| 49 | |
Jakub Josef | 8337931 | 2017-03-29 18:12:34 +0200 | [diff] [blame] | 50 | def defaultGitRef, defaultGitUrl |
Filip Pytloun | 19376a8 | 2017-03-07 12:29:00 +0100 | [diff] [blame] | 51 | try { |
Jakub Josef | 8337931 | 2017-03-29 18:12:34 +0200 | [diff] [blame] | 52 | defaultGitRef = DEFAULT_GIT_REF |
| 53 | defaultGitUrl = DEFAULT_GIT_URL |
Filip Pytloun | 19376a8 | 2017-03-07 12:29:00 +0100 | [diff] [blame] | 54 | } catch (MissingPropertyException e) { |
Jakub Josef | 8337931 | 2017-03-29 18:12:34 +0200 | [diff] [blame] | 55 | defaultGitRef = null |
| 56 | defaultGitUrl = null |
Filip Pytloun | 19376a8 | 2017-03-07 12:29:00 +0100 | [diff] [blame] | 57 | } |
chnyda | 400babe | 2017-10-23 10:35:45 +0200 | [diff] [blame] | 58 | |
Jakub Josef | 8337931 | 2017-03-29 18:12:34 +0200 | [diff] [blame] | 59 | def checkouted = false |
chnyda | 400babe | 2017-10-23 10:35:45 +0200 | [diff] [blame] | 60 | futureNodes = [] |
chnyda | 3dcf0bd | 2017-11-30 11:49:24 +0100 | [diff] [blame] | 61 | failedNodes = false |
chnyda | 400babe | 2017-10-23 10:35:45 +0200 | [diff] [blame] | 62 | common = new com.mirantis.mk.Common() |
chnyda | fa674a0 | 2017-10-19 11:49:22 +0200 | [diff] [blame] | 63 | |
chnyda | 400babe | 2017-10-23 10:35:45 +0200 | [diff] [blame] | 64 | def setupRunner() { |
chnyda | fa674a0 | 2017-10-19 11:49:22 +0200 | [diff] [blame] | 65 | |
chnyda | 400babe | 2017-10-23 10:35:45 +0200 | [diff] [blame] | 66 | def branches = [:] |
| 67 | for (int i = 0; i < PARALLEL_NODE_GROUP_SIZE.toInteger() && i < futureNodes.size(); i++) { |
| 68 | branches["Runner ${i}"] = { |
chnyda | 3dcf0bd | 2017-11-30 11:49:24 +0100 | [diff] [blame] | 69 | while (futureNodes && !failedNodes) { |
chnyda | 400babe | 2017-10-23 10:35:45 +0200 | [diff] [blame] | 70 | def currentNode = futureNodes[0] ? futureNodes[0] : null |
| 71 | if (!currentNode) { |
| 72 | continue |
| 73 | } |
| 74 | |
| 75 | def clusterName = currentNode[2] |
| 76 | futureNodes.remove(currentNode) |
| 77 | try { |
| 78 | triggerTestNodeJob(currentNode[0], currentNode[1], currentNode[2], currentNode[3], currentNode[4]) |
| 79 | } catch (Exception e) { |
chnyda | 3dcf0bd | 2017-11-30 11:49:24 +0100 | [diff] [blame] | 80 | if (e.getMessage().contains("completed with status ABORTED")) { |
| 81 | common.warningMsg("Test of ${clusterName} failed because the test was aborted : ${e}") |
| 82 | futureNodes << currentNode |
| 83 | } else { |
| 84 | common.warningMsg("Test of ${clusterName} failed : ${e}") |
| 85 | failedNodes = true |
| 86 | } |
chnyda | 400babe | 2017-10-23 10:35:45 +0200 | [diff] [blame] | 87 | } |
| 88 | } |
| 89 | } |
| 90 | } |
chnyda | 3dcf0bd | 2017-11-30 11:49:24 +0100 | [diff] [blame] | 91 | |
chnyda | 400babe | 2017-10-23 10:35:45 +0200 | [diff] [blame] | 92 | if (branches) { |
| 93 | parallel branches |
| 94 | } |
| 95 | } |
chnyda | fa674a0 | 2017-10-19 11:49:22 +0200 | [diff] [blame] | 96 | |
| 97 | def triggerTestNodeJob(defaultGitUrl, defaultGitRef, clusterName, testTarget, formulasSource) { |
chnyda | 400babe | 2017-10-23 10:35:45 +0200 | [diff] [blame] | 98 | common.infoMsg("Test of ${clusterName} starts") |
chnyda | fa674a0 | 2017-10-19 11:49:22 +0200 | [diff] [blame] | 99 | build job: "test-salt-model-node", parameters: [ |
| 100 | [$class: 'StringParameterValue', name: 'DEFAULT_GIT_URL', value: defaultGitUrl], |
| 101 | [$class: 'StringParameterValue', name: 'DEFAULT_GIT_REF', value: defaultGitRef], |
| 102 | [$class: 'StringParameterValue', name: 'CLUSTER_NAME', value: clusterName], |
| 103 | [$class: 'StringParameterValue', name: 'NODE_TARGET', value: testTarget], |
| 104 | [$class: 'StringParameterValue', name: 'FORMULAS_SOURCE', value: formulasSource], |
| 105 | [$class: 'StringParameterValue', name: 'EXTRA_FORMULAS', value: EXTRA_FORMULAS], |
| 106 | [$class: 'StringParameterValue', name: 'FORMULAS_REVISION', value: FORMULAS_REVISION], |
| 107 | [$class: 'StringParameterValue', name: 'CREDENTIALS_ID', value: CREDENTIALS_ID], |
| 108 | [$class: 'StringParameterValue', name: 'SYSTEM_GIT_URL', value: SYSTEM_GIT_URL], |
Jakub Josef | e40bbf9 | 2018-03-22 15:02:46 +0100 | [diff] [blame] | 109 | [$class: 'StringParameterValue', name: 'RECLASS_VERSION', value: RECLASS_VERSION], |
chnyda | fa674a0 | 2017-10-19 11:49:22 +0200 | [diff] [blame] | 110 | [$class: 'StringParameterValue', name: 'MAX_CPU_PER_JOB', value: MAX_CPU_PER_JOB], |
| 111 | [$class: 'StringParameterValue', name: 'SYSTEM_GIT_REF', value: SYSTEM_GIT_REF], |
| 112 | [$class: 'BooleanParameterValue', name: 'LEGACY_TEST_MODE', value: LEGACY_TEST_MODE.toBoolean()], |
Dmitry Ukov | b2aa6db | 2017-10-23 12:30:31 +0400 | [diff] [blame] | 113 | [$class: 'BooleanParameterValue', name: 'RECLASS_IGNORE_CLASS_NOTFOUND', value: RECLASS_IGNORE_CLASS_NOTFOUND.toBoolean()], |
| 114 | [$class: 'StringParameterValue', name: 'APT_REPOSITORY', value: APT_REPOSITORY], |
| 115 | [$class: 'StringParameterValue', name: 'APT_REPOSITORY_GPG', value: APT_REPOSITORY_GPG] |
chnyda | fa674a0 | 2017-10-19 11:49:22 +0200 | [diff] [blame] | 116 | ] |
| 117 | } |
| 118 | |
Jakub Josef | 3e77eb7 | 2018-01-15 14:21:53 +0100 | [diff] [blame] | 119 | def _clusterTestEnabled(infraYMLConfig){ |
Jakub Josef | cb4f1dc | 2018-02-19 15:27:29 +0100 | [diff] [blame] | 120 | if (infraYMLConfig["parameters"].containsKey("_jenkins")) { |
| 121 | if (infraYMLConfig["parameters"]["_jenkins"].containsKey("tests_enabled")) { |
Jakub Josef | 3e77eb7 | 2018-01-15 14:21:53 +0100 | [diff] [blame] | 122 | return infraYMLConfig["parameters"]["_jenkins"]["tests_enabled"]; |
| 123 | } |
| 124 | } |
| 125 | // ci tests are enabled by default |
| 126 | return true; |
| 127 | } |
| 128 | |
Jakub Josef | a63f986 | 2018-01-11 17:58:38 +0100 | [diff] [blame] | 129 | timeout(time: 12, unit: 'HOURS') { |
| 130 | node("python") { |
| 131 | try{ |
| 132 | stage("checkout") { |
chnyda | 32c6d9d | 2017-09-27 10:18:09 +0200 | [diff] [blame] | 133 | if (gerritRef) { |
Jakub Josef | a63f986 | 2018-01-11 17:58:38 +0100 | [diff] [blame] | 134 | // job is triggered by Gerrit |
| 135 | // test if change aren't already merged |
| 136 | def gerritChange = gerrit.getGerritChange(GERRIT_NAME, GERRIT_HOST, GERRIT_CHANGE_NUMBER, CREDENTIALS_ID, true) |
| 137 | // test if gerrit change is already Verified |
Jakub Josef | cb4f1dc | 2018-02-19 15:27:29 +0100 | [diff] [blame] | 138 | if (gerrit.patchsetHasApproval(gerritChange.currentPatchSet,"Verified", "+")) { |
Jakub Josef | a63f986 | 2018-01-11 17:58:38 +0100 | [diff] [blame] | 139 | common.successMsg("Gerrit change ${GERRIT_CHANGE_NUMBER} patchset ${GERRIT_PATCHSET_NUMBER} already has Verified, skipping tests") // do nothing |
| 140 | // test WIP contains in commit message |
| 141 | }else if (gerritChange.commitMessage.contains("WIP")) { |
| 142 | common.successMsg("Commit message contains WIP, skipping tests") // do nothing |
| 143 | } else { |
| 144 | def merged = gerritChange.status == "MERGED" |
| 145 | if(!merged){ |
| 146 | checkouted = gerrit.gerritPatchsetCheckout ([ |
| 147 | credentialsId : CREDENTIALS_ID |
| 148 | ]) |
| 149 | } else{ |
| 150 | common.successMsg("Change ${GERRIT_CHANGE_NUMBER} is already merged, no need to test them") |
| 151 | } |
chnyda | 32c6d9d | 2017-09-27 10:18:09 +0200 | [diff] [blame] | 152 | } |
Jakub Josef | a63f986 | 2018-01-11 17:58:38 +0100 | [diff] [blame] | 153 | // defaultGitUrl is passed to the triggered job |
| 154 | defaultGitUrl = "${GERRIT_SCHEME}://${GERRIT_NAME}@${GERRIT_HOST}:${GERRIT_PORT}/${GERRIT_PROJECT}" |
| 155 | defaultGitRef = GERRIT_REFSPEC |
| 156 | } else if(defaultGitRef && defaultGitUrl) { |
| 157 | checkouted = gerrit.gerritPatchsetCheckout(defaultGitUrl, defaultGitRef, "HEAD", CREDENTIALS_ID) |
| 158 | } else { |
| 159 | throw new Exception("Cannot checkout gerrit patchset, GERRIT_REFSPEC and DEFAULT_GIT_REF is null") |
chnyda | 3bf82d8 | 2017-10-19 14:01:53 +0200 | [diff] [blame] | 160 | } |
Jakub Josef | c5a223a | 2017-03-01 14:40:08 +0100 | [diff] [blame] | 161 | } |
Jakub Josef | a63f986 | 2018-01-11 17:58:38 +0100 | [diff] [blame] | 162 | |
| 163 | stage("Check YAML") { |
Jakub Josef | cb4f1dc | 2018-02-19 15:27:29 +0100 | [diff] [blame] | 164 | common.infoMsg("Checking YAML syntax for changed files") |
| 165 | def syntaxCheckStatus = sh(script:"set +x;git diff-tree --no-commit-id --diff-filter=d --name-only -r HEAD | grep .yml | xargs -I {} python -c \"import yaml; yaml.load(open('{}', 'r'))\" \\;", returnStatus:true) |
| 166 | if(syntaxCheckStatus > 0){ |
| 167 | common.errorMsg("YAML syntax check failed!") |
| 168 | } |
Jakub Josef | a63f986 | 2018-01-11 17:58:38 +0100 | [diff] [blame] | 169 | } |
| 170 | |
| 171 | stage("test-nodes") { |
Jakub Josef | cb4f1dc | 2018-02-19 15:27:29 +0100 | [diff] [blame] | 172 | if (checkouted) { |
Jakub Josef | a63f986 | 2018-01-11 17:58:38 +0100 | [diff] [blame] | 173 | def modifiedClusters = null |
Jakub Josef | cb4f1dc | 2018-02-19 15:27:29 +0100 | [diff] [blame] | 174 | // testing modified cluster is used only if test was triggered by gerrit |
| 175 | if (gerritRef) { |
| 176 | def checkChange = sh(script: "set +x;git diff-tree --no-commit-id --name-only -r HEAD | grep -v classes/cluster", returnStatus: true) |
| 177 | if (checkChange == 1) { |
| 178 | modifiedClusters = sh(script: "set +x;git diff-tree --no-commit-id --name-only -r HEAD | grep classes/cluster/ | awk -F/ '{print \$3}' | uniq", returnStdout: true).tokenize() |
Jakub Josef | 3e77eb7 | 2018-01-15 14:21:53 +0100 | [diff] [blame] | 179 | } |
Jakub Josef | a63f986 | 2018-01-11 17:58:38 +0100 | [diff] [blame] | 180 | } |
| 181 | |
Jakub Josef | cb4f1dc | 2018-02-19 15:27:29 +0100 | [diff] [blame] | 182 | def infraYMLs = [] |
| 183 | // list of cluster names can be explicitly given |
| 184 | if (testClusterNames != null && testClusterNames != "") { |
| 185 | common.infoMsg("TEST_CLUSTER_NAMES param found, using explicitly defined cluster names: ${testClusterNames}") |
Jakub Josef | dbe40d3 | 2018-02-19 16:08:12 +0100 | [diff] [blame] | 186 | def clusterNameRegex = testClusterNames.tokenize(",").collect{it.trim()}.join("|") |
| 187 | infraYMLs = sh(script:"set +x;find ./classes/ -regextype posix-egrep -regex '.*cluster/(${clusterNameRegex}){1}/[infra/]*init\\.yml' -exec grep -il 'cluster_name' {} \\;", returnStdout: true).tokenize() |
Jakub Josef | cb4f1dc | 2018-02-19 15:27:29 +0100 | [diff] [blame] | 188 | } else { |
| 189 | common.infoMsg("TEST_CLUSTER_NAMES param not found, all clusters with enabled tests will be tested") |
| 190 | // else we want to test all cluster levels found |
| 191 | infraYMLs = sh(script: "set +x;find ./classes/ -regex '.*cluster/[-_a-zA-Z0-9]*/[infra/]*init\\.yml' -exec grep -il 'cluster_name' {} \\;", returnStdout: true).tokenize() |
| 192 | def clusterDirectories = sh(script: "set +x;ls -d ./classes/cluster/*/ | awk -F/ '{print \$4}'", returnStdout: true).tokenize() |
| 193 | |
| 194 | // create a list of cluster names present in cluster folder |
| 195 | def infraList = [] |
| 196 | for (elt in infraYMLs) { |
| 197 | infraList << elt.tokenize('/')[3] |
| 198 | } |
| 199 | |
| 200 | // verify we have all valid clusters loaded |
| 201 | def commonList = infraList.intersect(clusterDirectories) |
| 202 | def differenceList = infraList.plus(clusterDirectories) |
| 203 | differenceList.removeAll(commonList) |
| 204 | |
| 205 | if (!differenceList.isEmpty()) { |
| 206 | common.warningMsg("The following clusters are not valid : ${differenceList} - That means we cannot found cluster_name in init.yml or infra/init.yml") |
| 207 | } |
| 208 | if (modifiedClusters) { |
| 209 | infraYMLs.removeAll { !modifiedClusters.contains(it.tokenize('/')[3]) } |
| 210 | common.infoMsg("Testing only modified clusters: ${infraYMLs}") |
| 211 | } |
| 212 | } |
| 213 | common.infoMsg("Starting salt models test for these clusters " + infraYMLs.collect{ it.tokenize("/")[3] }) |
| 214 | if (infraYMLs.size() > 0) { |
| 215 | for (int i = 0; i < infraYMLs.size(); i++) { |
| 216 | def infraYMLConfig = readYaml(file: infraYMLs[i]) |
| 217 | if (_clusterTestEnabled(infraYMLConfig)) { |
| 218 | if(!infraYMLConfig["parameters"].containsKey("_param")){ |
| 219 | common.warningMsg("ERROR: Cannot find soft params (_param) in file " + infraYMLs[i] + " for obtain a cluster info. Skipping test.") |
| 220 | continue |
| 221 | } |
| 222 | def infraParams = infraYMLConfig["parameters"]["_param"]; |
| 223 | if(!infraParams.containsKey("infra_config_hostname") || !infraParams.containsKey("cluster_name") || !infraParams.containsKey("cluster_domain")){ |
| 224 | common.warningMsg("ERROR: Cannot find _param:infra_config_hostname or _param:cluster_name or _param:cluster_domain in file " + infraYMLs[i] + " for obtain a cluster info. Skipping test.") |
| 225 | continue |
| 226 | } |
| 227 | def clusterName = infraParams["cluster_name"] |
| 228 | def clusterDomain = infraParams["cluster_domain"] |
| 229 | def configHostname = infraParams["infra_config_hostname"] |
| 230 | def testTarget = String.format("%s.%s", configHostname, clusterDomain) |
| 231 | |
| 232 | futureNodes << [defaultGitUrl, defaultGitRef, clusterName, testTarget, formulasSource] |
| 233 | } |
| 234 | } |
| 235 | } else { |
| 236 | common.warningMsg("List of found salt model clusters is empty, no tests will be started!") |
| 237 | } |
| 238 | |
Jakub Josef | a63f986 | 2018-01-11 17:58:38 +0100 | [diff] [blame] | 239 | setupRunner() |
| 240 | |
| 241 | if (failedNodes) { |
| 242 | currentBuild.result = "FAILURE" |
| 243 | } |
| 244 | } |
| 245 | } |
| 246 | } catch (Throwable e) { |
| 247 | currentBuild.result = "FAILURE" |
| 248 | currentBuild.description = currentBuild.description ? e.message + " " + currentBuild.description : e.message |
| 249 | throw e |
| 250 | } finally { |
| 251 | common.sendNotification(currentBuild.result,"",["slack"]) |
chnyda | 51b0314 | 2017-05-10 17:15:27 +0200 | [diff] [blame] | 252 | } |
Jakub Josef | c5a223a | 2017-03-01 14:40:08 +0100 | [diff] [blame] | 253 | } |
Tomáš Kukrál | 500c018 | 2017-05-11 13:46:19 +0200 | [diff] [blame] | 254 | } |