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