blob: d6279f9d3496352ac9ae5cc3750e2a60fed9e727 [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],
Jakub Josefe40bbf92018-03-22 15:02:46 +0100108 [$class: 'StringParameterValue', name: 'RECLASS_VERSION', value: RECLASS_VERSION],
chnydafa674a02017-10-19 11:49:22 +0200109 [$class: 'StringParameterValue', name: 'MAX_CPU_PER_JOB', value: MAX_CPU_PER_JOB],
110 [$class: 'StringParameterValue', name: 'SYSTEM_GIT_REF', value: SYSTEM_GIT_REF],
111 [$class: 'BooleanParameterValue', name: 'LEGACY_TEST_MODE', value: LEGACY_TEST_MODE.toBoolean()],
Dmitry Ukovb2aa6db2017-10-23 12:30:31 +0400112 [$class: 'BooleanParameterValue', name: 'RECLASS_IGNORE_CLASS_NOTFOUND', value: RECLASS_IGNORE_CLASS_NOTFOUND.toBoolean()],
113 [$class: 'StringParameterValue', name: 'APT_REPOSITORY', value: APT_REPOSITORY],
114 [$class: 'StringParameterValue', name: 'APT_REPOSITORY_GPG', value: APT_REPOSITORY_GPG]
chnydafa674a02017-10-19 11:49:22 +0200115 ]
116}
117
Jakub Josef3e77eb72018-01-15 14:21:53 +0100118def _clusterTestEnabled(infraYMLConfig){
Jakub Josefcb4f1dc2018-02-19 15:27:29 +0100119 if (infraYMLConfig["parameters"].containsKey("_jenkins")) {
120 if (infraYMLConfig["parameters"]["_jenkins"].containsKey("tests_enabled")) {
Jakub Josef3e77eb72018-01-15 14:21:53 +0100121 return infraYMLConfig["parameters"]["_jenkins"]["tests_enabled"];
122 }
123 }
124 // ci tests are enabled by default
125 return true;
126}
127
Jakub Josefa63f9862018-01-11 17:58:38 +0100128timeout(time: 12, unit: 'HOURS') {
129 node("python") {
130 try{
131 stage("checkout") {
chnyda32c6d9d2017-09-27 10:18:09 +0200132 if (gerritRef) {
Jakub Josefa63f9862018-01-11 17:58:38 +0100133 // job is triggered by Gerrit
134 // test if change aren't already merged
135 def gerritChange = gerrit.getGerritChange(GERRIT_NAME, GERRIT_HOST, GERRIT_CHANGE_NUMBER, CREDENTIALS_ID, true)
136 // test if gerrit change is already Verified
Jakub Josefcb4f1dc2018-02-19 15:27:29 +0100137 if (gerrit.patchsetHasApproval(gerritChange.currentPatchSet,"Verified", "+")) {
Jakub Josefa63f9862018-01-11 17:58:38 +0100138 common.successMsg("Gerrit change ${GERRIT_CHANGE_NUMBER} patchset ${GERRIT_PATCHSET_NUMBER} already has Verified, skipping tests") // do nothing
139 // test WIP contains in commit message
140 }else if (gerritChange.commitMessage.contains("WIP")) {
141 common.successMsg("Commit message contains WIP, skipping tests") // do nothing
142 } else {
143 def merged = gerritChange.status == "MERGED"
144 if(!merged){
145 checkouted = gerrit.gerritPatchsetCheckout ([
146 credentialsId : CREDENTIALS_ID
147 ])
148 } else{
149 common.successMsg("Change ${GERRIT_CHANGE_NUMBER} is already merged, no need to test them")
150 }
chnyda32c6d9d2017-09-27 10:18:09 +0200151 }
Jakub Josefa63f9862018-01-11 17:58:38 +0100152 // defaultGitUrl is passed to the triggered job
153 defaultGitUrl = "${GERRIT_SCHEME}://${GERRIT_NAME}@${GERRIT_HOST}:${GERRIT_PORT}/${GERRIT_PROJECT}"
154 defaultGitRef = GERRIT_REFSPEC
155 } else if(defaultGitRef && defaultGitUrl) {
156 checkouted = gerrit.gerritPatchsetCheckout(defaultGitUrl, defaultGitRef, "HEAD", CREDENTIALS_ID)
157 } else {
158 throw new Exception("Cannot checkout gerrit patchset, GERRIT_REFSPEC and DEFAULT_GIT_REF is null")
chnyda3bf82d82017-10-19 14:01:53 +0200159 }
Jakub Josefc5a223a2017-03-01 14:40:08 +0100160 }
Jakub Josefa63f9862018-01-11 17:58:38 +0100161
162 stage("Check YAML") {
Jakub Josefcb4f1dc2018-02-19 15:27:29 +0100163 common.infoMsg("Checking YAML syntax for changed files")
164 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)
165 if(syntaxCheckStatus > 0){
166 common.errorMsg("YAML syntax check failed!")
167 }
Jakub Josefa63f9862018-01-11 17:58:38 +0100168 }
169
170 stage("test-nodes") {
Jakub Josefcb4f1dc2018-02-19 15:27:29 +0100171 if (checkouted) {
Jakub Josefa63f9862018-01-11 17:58:38 +0100172 def modifiedClusters = null
Jakub Josefcb4f1dc2018-02-19 15:27:29 +0100173 // testing modified cluster is used only if test was triggered by gerrit
174 if (gerritRef) {
175 def checkChange = sh(script: "set +x;git diff-tree --no-commit-id --name-only -r HEAD | grep -v classes/cluster", returnStatus: true)
176 if (checkChange == 1) {
177 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 +0100178 }
Jakub Josefa63f9862018-01-11 17:58:38 +0100179 }
180
Jakub Josefcb4f1dc2018-02-19 15:27:29 +0100181 def infraYMLs = []
182 // list of cluster names can be explicitly given
183 if (testClusterNames != null && testClusterNames != "") {
184 common.infoMsg("TEST_CLUSTER_NAMES param found, using explicitly defined cluster names: ${testClusterNames}")
Jakub Josefdbe40d32018-02-19 16:08:12 +0100185 def clusterNameRegex = testClusterNames.tokenize(",").collect{it.trim()}.join("|")
186 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 +0100187 } else {
188 common.infoMsg("TEST_CLUSTER_NAMES param not found, all clusters with enabled tests will be tested")
189 // else we want to test all cluster levels found
190 infraYMLs = sh(script: "set +x;find ./classes/ -regex '.*cluster/[-_a-zA-Z0-9]*/[infra/]*init\\.yml' -exec grep -il 'cluster_name' {} \\;", returnStdout: true).tokenize()
191 def clusterDirectories = sh(script: "set +x;ls -d ./classes/cluster/*/ | awk -F/ '{print \$4}'", returnStdout: true).tokenize()
192
193 // create a list of cluster names present in cluster folder
194 def infraList = []
195 for (elt in infraYMLs) {
196 infraList << elt.tokenize('/')[3]
197 }
198
199 // verify we have all valid clusters loaded
200 def commonList = infraList.intersect(clusterDirectories)
201 def differenceList = infraList.plus(clusterDirectories)
202 differenceList.removeAll(commonList)
203
204 if (!differenceList.isEmpty()) {
205 common.warningMsg("The following clusters are not valid : ${differenceList} - That means we cannot found cluster_name in init.yml or infra/init.yml")
206 }
207 if (modifiedClusters) {
208 infraYMLs.removeAll { !modifiedClusters.contains(it.tokenize('/')[3]) }
209 common.infoMsg("Testing only modified clusters: ${infraYMLs}")
210 }
211 }
212 common.infoMsg("Starting salt models test for these clusters " + infraYMLs.collect{ it.tokenize("/")[3] })
213 if (infraYMLs.size() > 0) {
214 for (int i = 0; i < infraYMLs.size(); i++) {
215 def infraYMLConfig = readYaml(file: infraYMLs[i])
216 if (_clusterTestEnabled(infraYMLConfig)) {
217 if(!infraYMLConfig["parameters"].containsKey("_param")){
218 common.warningMsg("ERROR: Cannot find soft params (_param) in file " + infraYMLs[i] + " for obtain a cluster info. Skipping test.")
219 continue
220 }
221 def infraParams = infraYMLConfig["parameters"]["_param"];
222 if(!infraParams.containsKey("infra_config_hostname") || !infraParams.containsKey("cluster_name") || !infraParams.containsKey("cluster_domain")){
223 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.")
224 continue
225 }
226 def clusterName = infraParams["cluster_name"]
227 def clusterDomain = infraParams["cluster_domain"]
228 def configHostname = infraParams["infra_config_hostname"]
229 def testTarget = String.format("%s.%s", configHostname, clusterDomain)
230
231 futureNodes << [defaultGitUrl, defaultGitRef, clusterName, testTarget, formulasSource]
232 }
233 }
234 } else {
235 common.warningMsg("List of found salt model clusters is empty, no tests will be started!")
236 }
237
Jakub Josefa63f9862018-01-11 17:58:38 +0100238 setupRunner()
239
240 if (failedNodes) {
241 currentBuild.result = "FAILURE"
242 }
243 }
244 }
245 } catch (Throwable e) {
246 currentBuild.result = "FAILURE"
247 currentBuild.description = currentBuild.description ? e.message + " " + currentBuild.description : e.message
248 throw e
249 } finally {
250 common.sendNotification(currentBuild.result,"",["slack"])
chnyda51b03142017-05-10 17:15:27 +0200251 }
Jakub Josefc5a223a2017-03-01 14:40:08 +0100252 }
Tomáš Kukrál500c0182017-05-11 13:46:19 +0200253}