blob: ca07fc16fb8e612922dae07c74183817af8b70d7 [file] [log] [blame]
Jakub Josef83379312017-03-29 18:12:34 +02001/**
2 * Test salt models pipeline
Jakub Josefcb4f1dc2018-02-19 15:27:29 +01003 * 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
13 * APT_REPOSITORY extra apt repository url
14 * APT_REPOSITORY_GPG extra apt repository url GPG
Jakub Josef83379312017-03-29 18:12:34 +020015 */
16
Jakub Josefc5a223a2017-03-01 14:40:08 +010017def gerrit = new com.mirantis.mk.Gerrit()
Filip Pytloun38005aa2017-03-06 10:26:38 +010018def ssh = new com.mirantis.mk.Ssh()
19def git = new com.mirantis.mk.Git()
Jakub Josefc5a223a2017-03-01 14:40:08 +010020
chnyda3dcf0bd2017-11-30 11:49:24 +010021def config_node_name_pattern
chnyda64b73582017-07-19 12:01:32 +020022try {
23 config_node_name_pattern = CONFIG_NODE_NAME_PATTERN
24} catch (MissingPropertyException e) {
25 config_node_name_pattern = "cfg01"
26}
27
Filip Pytloun19376a82017-03-07 12:29:00 +010028def gerritRef
29try {
30 gerritRef = GERRIT_REFSPEC
31} catch (MissingPropertyException e) {
32 gerritRef = null
33}
34
Mykyta Karpinc0758f32017-06-23 18:10:24 +030035def formulasSource
36try {
37 formulasSource = FORMULAS_SOURCE
38} catch (MissingPropertyException e) {
39 formulasSource = "pkg"
40}
41
Jakub Josefcb4f1dc2018-02-19 15:27:29 +010042def testClusterNames
43try {
44 testClusterNames = TEST_CLUSTER_NAMES
45} catch (MissingPropertyException e) {
46 testClusterNames = ""
47}
48
Jakub Josef83379312017-03-29 18:12:34 +020049def defaultGitRef, defaultGitUrl
Filip Pytloun19376a82017-03-07 12:29:00 +010050try {
Jakub Josef83379312017-03-29 18:12:34 +020051 defaultGitRef = DEFAULT_GIT_REF
52 defaultGitUrl = DEFAULT_GIT_URL
Filip Pytloun19376a82017-03-07 12:29:00 +010053} catch (MissingPropertyException e) {
Jakub Josef83379312017-03-29 18:12:34 +020054 defaultGitRef = null
55 defaultGitUrl = null
Filip Pytloun19376a82017-03-07 12:29:00 +010056}
chnyda400babe2017-10-23 10:35:45 +020057
Jakub Josef83379312017-03-29 18:12:34 +020058def checkouted = false
chnyda400babe2017-10-23 10:35:45 +020059futureNodes = []
chnyda3dcf0bd2017-11-30 11:49:24 +010060failedNodes = false
chnyda400babe2017-10-23 10:35:45 +020061common = new com.mirantis.mk.Common()
chnydafa674a02017-10-19 11:49:22 +020062
chnyda400babe2017-10-23 10:35:45 +020063def setupRunner() {
chnydafa674a02017-10-19 11:49:22 +020064
chnyda400babe2017-10-23 10:35:45 +020065 def branches = [:]
66 for (int i = 0; i < PARALLEL_NODE_GROUP_SIZE.toInteger() && i < futureNodes.size(); i++) {
67 branches["Runner ${i}"] = {
chnyda3dcf0bd2017-11-30 11:49:24 +010068 while (futureNodes && !failedNodes) {
chnyda400babe2017-10-23 10:35:45 +020069 def currentNode = futureNodes[0] ? futureNodes[0] : null
70 if (!currentNode) {
71 continue
72 }
73
74 def clusterName = currentNode[2]
75 futureNodes.remove(currentNode)
76 try {
77 triggerTestNodeJob(currentNode[0], currentNode[1], currentNode[2], currentNode[3], currentNode[4])
78 } catch (Exception e) {
chnyda3dcf0bd2017-11-30 11:49:24 +010079 if (e.getMessage().contains("completed with status ABORTED")) {
80 common.warningMsg("Test of ${clusterName} failed because the test was aborted : ${e}")
81 futureNodes << currentNode
82 } else {
83 common.warningMsg("Test of ${clusterName} failed : ${e}")
84 failedNodes = true
85 }
chnyda400babe2017-10-23 10:35:45 +020086 }
87 }
88 }
89 }
chnyda3dcf0bd2017-11-30 11:49:24 +010090
chnyda400babe2017-10-23 10:35:45 +020091 if (branches) {
92 parallel branches
93 }
94}
chnydafa674a02017-10-19 11:49:22 +020095
96def triggerTestNodeJob(defaultGitUrl, defaultGitRef, clusterName, testTarget, formulasSource) {
chnyda400babe2017-10-23 10:35:45 +020097 common.infoMsg("Test of ${clusterName} starts")
chnydafa674a02017-10-19 11:49:22 +020098 build job: "test-salt-model-node", parameters: [
99 [$class: 'StringParameterValue', name: 'DEFAULT_GIT_URL', value: defaultGitUrl],
100 [$class: 'StringParameterValue', name: 'DEFAULT_GIT_REF', value: defaultGitRef],
101 [$class: 'StringParameterValue', name: 'CLUSTER_NAME', value: clusterName],
102 [$class: 'StringParameterValue', name: 'NODE_TARGET', value: testTarget],
103 [$class: 'StringParameterValue', name: 'FORMULAS_SOURCE', value: formulasSource],
104 [$class: 'StringParameterValue', name: 'EXTRA_FORMULAS', value: EXTRA_FORMULAS],
105 [$class: 'StringParameterValue', name: 'FORMULAS_REVISION', value: FORMULAS_REVISION],
106 [$class: 'StringParameterValue', name: 'CREDENTIALS_ID', value: CREDENTIALS_ID],
107 [$class: 'StringParameterValue', name: 'SYSTEM_GIT_URL', value: SYSTEM_GIT_URL],
108 [$class: 'StringParameterValue', name: 'MAX_CPU_PER_JOB', value: MAX_CPU_PER_JOB],
109 [$class: 'StringParameterValue', name: 'SYSTEM_GIT_REF', value: SYSTEM_GIT_REF],
110 [$class: 'BooleanParameterValue', name: 'LEGACY_TEST_MODE', value: LEGACY_TEST_MODE.toBoolean()],
Dmitry Ukovb2aa6db2017-10-23 12:30:31 +0400111 [$class: 'BooleanParameterValue', name: 'RECLASS_IGNORE_CLASS_NOTFOUND', value: RECLASS_IGNORE_CLASS_NOTFOUND.toBoolean()],
112 [$class: 'StringParameterValue', name: 'APT_REPOSITORY', value: APT_REPOSITORY],
113 [$class: 'StringParameterValue', name: 'APT_REPOSITORY_GPG', value: APT_REPOSITORY_GPG]
chnydafa674a02017-10-19 11:49:22 +0200114 ]
115}
116
Jakub Josef3e77eb72018-01-15 14:21:53 +0100117def _clusterTestEnabled(infraYMLConfig){
Jakub Josefcb4f1dc2018-02-19 15:27:29 +0100118 if (infraYMLConfig["parameters"].containsKey("_jenkins")) {
119 if (infraYMLConfig["parameters"]["_jenkins"].containsKey("tests_enabled")) {
Jakub Josef3e77eb72018-01-15 14:21:53 +0100120 return infraYMLConfig["parameters"]["_jenkins"]["tests_enabled"];
121 }
122 }
123 // ci tests are enabled by default
124 return true;
125}
126
Jakub Josefa63f9862018-01-11 17:58:38 +0100127timeout(time: 12, unit: 'HOURS') {
128 node("python") {
129 try{
130 stage("checkout") {
chnyda32c6d9d2017-09-27 10:18:09 +0200131 if (gerritRef) {
Jakub Josefa63f9862018-01-11 17:58:38 +0100132 // job is triggered by Gerrit
133 // test if change aren't already merged
134 def gerritChange = gerrit.getGerritChange(GERRIT_NAME, GERRIT_HOST, GERRIT_CHANGE_NUMBER, CREDENTIALS_ID, true)
135 // test if gerrit change is already Verified
Jakub Josefcb4f1dc2018-02-19 15:27:29 +0100136 if (gerrit.patchsetHasApproval(gerritChange.currentPatchSet,"Verified", "+")) {
Jakub Josefa63f9862018-01-11 17:58:38 +0100137 common.successMsg("Gerrit change ${GERRIT_CHANGE_NUMBER} patchset ${GERRIT_PATCHSET_NUMBER} already has Verified, skipping tests") // do nothing
138 // test WIP contains in commit message
139 }else if (gerritChange.commitMessage.contains("WIP")) {
140 common.successMsg("Commit message contains WIP, skipping tests") // do nothing
141 } else {
142 def merged = gerritChange.status == "MERGED"
143 if(!merged){
144 checkouted = gerrit.gerritPatchsetCheckout ([
145 credentialsId : CREDENTIALS_ID
146 ])
147 } else{
148 common.successMsg("Change ${GERRIT_CHANGE_NUMBER} is already merged, no need to test them")
149 }
chnyda32c6d9d2017-09-27 10:18:09 +0200150 }
Jakub Josefa63f9862018-01-11 17:58:38 +0100151 // defaultGitUrl is passed to the triggered job
152 defaultGitUrl = "${GERRIT_SCHEME}://${GERRIT_NAME}@${GERRIT_HOST}:${GERRIT_PORT}/${GERRIT_PROJECT}"
153 defaultGitRef = GERRIT_REFSPEC
154 } else if(defaultGitRef && defaultGitUrl) {
155 checkouted = gerrit.gerritPatchsetCheckout(defaultGitUrl, defaultGitRef, "HEAD", CREDENTIALS_ID)
156 } else {
157 throw new Exception("Cannot checkout gerrit patchset, GERRIT_REFSPEC and DEFAULT_GIT_REF is null")
chnyda3bf82d82017-10-19 14:01:53 +0200158 }
Jakub Josefc5a223a2017-03-01 14:40:08 +0100159 }
Jakub Josefa63f9862018-01-11 17:58:38 +0100160
161 stage("Check YAML") {
Jakub Josefcb4f1dc2018-02-19 15:27:29 +0100162 common.infoMsg("Checking YAML syntax for changed files")
163 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)
164 if(syntaxCheckStatus > 0){
165 common.errorMsg("YAML syntax check failed!")
166 }
Jakub Josefa63f9862018-01-11 17:58:38 +0100167 }
168
169 stage("test-nodes") {
Jakub Josefcb4f1dc2018-02-19 15:27:29 +0100170 if (checkouted) {
Jakub Josefa63f9862018-01-11 17:58:38 +0100171 def modifiedClusters = null
Jakub Josefcb4f1dc2018-02-19 15:27:29 +0100172 // testing modified cluster is used only if test was triggered by gerrit
173 if (gerritRef) {
174 def checkChange = sh(script: "set +x;git diff-tree --no-commit-id --name-only -r HEAD | grep -v classes/cluster", returnStatus: true)
175 if (checkChange == 1) {
176 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 Josef3e77eb72018-01-15 14:21:53 +0100177 }
Jakub Josefa63f9862018-01-11 17:58:38 +0100178 }
179
Jakub Josefcb4f1dc2018-02-19 15:27:29 +0100180 def infraYMLs = []
181 // list of cluster names can be explicitly given
182 if (testClusterNames != null && testClusterNames != "") {
183 common.infoMsg("TEST_CLUSTER_NAMES param found, using explicitly defined cluster names: ${testClusterNames}")
Jakub Josefdbe40d32018-02-19 16:08:12 +0100184 def clusterNameRegex = testClusterNames.tokenize(",").collect{it.trim()}.join("|")
185 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 Josefcb4f1dc2018-02-19 15:27:29 +0100186 } else {
187 common.infoMsg("TEST_CLUSTER_NAMES param not found, all clusters with enabled tests will be tested")
188 // else we want to test all cluster levels found
189 infraYMLs = sh(script: "set +x;find ./classes/ -regex '.*cluster/[-_a-zA-Z0-9]*/[infra/]*init\\.yml' -exec grep -il 'cluster_name' {} \\;", returnStdout: true).tokenize()
190 def clusterDirectories = sh(script: "set +x;ls -d ./classes/cluster/*/ | awk -F/ '{print \$4}'", returnStdout: true).tokenize()
191
192 // create a list of cluster names present in cluster folder
193 def infraList = []
194 for (elt in infraYMLs) {
195 infraList << elt.tokenize('/')[3]
196 }
197
198 // verify we have all valid clusters loaded
199 def commonList = infraList.intersect(clusterDirectories)
200 def differenceList = infraList.plus(clusterDirectories)
201 differenceList.removeAll(commonList)
202
203 if (!differenceList.isEmpty()) {
204 common.warningMsg("The following clusters are not valid : ${differenceList} - That means we cannot found cluster_name in init.yml or infra/init.yml")
205 }
206 if (modifiedClusters) {
207 infraYMLs.removeAll { !modifiedClusters.contains(it.tokenize('/')[3]) }
208 common.infoMsg("Testing only modified clusters: ${infraYMLs}")
209 }
210 }
211 common.infoMsg("Starting salt models test for these clusters " + infraYMLs.collect{ it.tokenize("/")[3] })
212 if (infraYMLs.size() > 0) {
213 for (int i = 0; i < infraYMLs.size(); i++) {
214 def infraYMLConfig = readYaml(file: infraYMLs[i])
215 if (_clusterTestEnabled(infraYMLConfig)) {
216 if(!infraYMLConfig["parameters"].containsKey("_param")){
217 common.warningMsg("ERROR: Cannot find soft params (_param) in file " + infraYMLs[i] + " for obtain a cluster info. Skipping test.")
218 continue
219 }
220 def infraParams = infraYMLConfig["parameters"]["_param"];
221 if(!infraParams.containsKey("infra_config_hostname") || !infraParams.containsKey("cluster_name") || !infraParams.containsKey("cluster_domain")){
222 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.")
223 continue
224 }
225 def clusterName = infraParams["cluster_name"]
226 def clusterDomain = infraParams["cluster_domain"]
227 def configHostname = infraParams["infra_config_hostname"]
228 def testTarget = String.format("%s.%s", configHostname, clusterDomain)
229
230 futureNodes << [defaultGitUrl, defaultGitRef, clusterName, testTarget, formulasSource]
231 }
232 }
233 } else {
234 common.warningMsg("List of found salt model clusters is empty, no tests will be started!")
235 }
236
Jakub Josefa63f9862018-01-11 17:58:38 +0100237 setupRunner()
238
239 if (failedNodes) {
240 currentBuild.result = "FAILURE"
241 }
242 }
243 }
244 } catch (Throwable e) {
245 currentBuild.result = "FAILURE"
246 currentBuild.description = currentBuild.description ? e.message + " " + currentBuild.description : e.message
247 throw e
248 } finally {
249 common.sendNotification(currentBuild.result,"",["slack"])
chnyda51b03142017-05-10 17:15:27 +0200250 }
Jakub Josefc5a223a2017-03-01 14:40:08 +0100251 }
Tomáš Kukrál500c0182017-05-11 13:46:19 +0200252}