blob: 6a37ac7a7ddc0851dbe22084bea889654e991645 [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
Jakub Josefb8cdba92018-04-05 17:10:35 +020013 * RECLASS_VERSION Version of reclass to be used (branch, ...)
Jakub Josefcb4f1dc2018-02-19 15:27:29 +010014 * APT_REPOSITORY extra apt repository url
15 * APT_REPOSITORY_GPG extra apt repository url GPG
Jakub Josef83379312017-03-29 18:12:34 +020016 */
17
Jakub Josefc5a223a2017-03-01 14:40:08 +010018def gerrit = new com.mirantis.mk.Gerrit()
Filip Pytloun38005aa2017-03-06 10:26:38 +010019def ssh = new com.mirantis.mk.Ssh()
20def git = new com.mirantis.mk.Git()
Jakub Josefc5a223a2017-03-01 14:40:08 +010021
chnyda3dcf0bd2017-11-30 11:49:24 +010022def config_node_name_pattern
chnyda64b73582017-07-19 12:01:32 +020023try {
24 config_node_name_pattern = CONFIG_NODE_NAME_PATTERN
25} catch (MissingPropertyException e) {
26 config_node_name_pattern = "cfg01"
27}
28
Filip Pytloun19376a82017-03-07 12:29:00 +010029def gerritRef
30try {
31 gerritRef = GERRIT_REFSPEC
32} catch (MissingPropertyException e) {
33 gerritRef = null
34}
35
Mykyta Karpinc0758f32017-06-23 18:10:24 +030036def formulasSource
37try {
38 formulasSource = FORMULAS_SOURCE
39} catch (MissingPropertyException e) {
40 formulasSource = "pkg"
41}
42
Jakub Josefcb4f1dc2018-02-19 15:27:29 +010043def testClusterNames
44try {
45 testClusterNames = TEST_CLUSTER_NAMES
46} catch (MissingPropertyException e) {
47 testClusterNames = ""
48}
49
Jakub Josef83379312017-03-29 18:12:34 +020050def defaultGitRef, defaultGitUrl
Filip Pytloun19376a82017-03-07 12:29:00 +010051try {
Jakub Josef83379312017-03-29 18:12:34 +020052 defaultGitRef = DEFAULT_GIT_REF
53 defaultGitUrl = DEFAULT_GIT_URL
Filip Pytloun19376a82017-03-07 12:29:00 +010054} catch (MissingPropertyException e) {
Jakub Josef83379312017-03-29 18:12:34 +020055 defaultGitRef = null
56 defaultGitUrl = null
Filip Pytloun19376a82017-03-07 12:29:00 +010057}
chnyda400babe2017-10-23 10:35:45 +020058
Jakub Josef83379312017-03-29 18:12:34 +020059def checkouted = false
chnyda400babe2017-10-23 10:35:45 +020060futureNodes = []
chnyda3dcf0bd2017-11-30 11:49:24 +010061failedNodes = false
chnyda400babe2017-10-23 10:35:45 +020062common = new com.mirantis.mk.Common()
chnydafa674a02017-10-19 11:49:22 +020063
chnyda400babe2017-10-23 10:35:45 +020064def setupRunner() {
chnydafa674a02017-10-19 11:49:22 +020065
chnyda400babe2017-10-23 10:35:45 +020066 def branches = [:]
67 for (int i = 0; i < PARALLEL_NODE_GROUP_SIZE.toInteger() && i < futureNodes.size(); i++) {
68 branches["Runner ${i}"] = {
chnyda3dcf0bd2017-11-30 11:49:24 +010069 while (futureNodes && !failedNodes) {
chnyda400babe2017-10-23 10:35:45 +020070 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) {
chnyda3dcf0bd2017-11-30 11:49:24 +010080 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 }
chnyda400babe2017-10-23 10:35:45 +020087 }
88 }
89 }
90 }
chnyda3dcf0bd2017-11-30 11:49:24 +010091
chnyda400babe2017-10-23 10:35:45 +020092 if (branches) {
93 parallel branches
94 }
95}
chnydafa674a02017-10-19 11:49:22 +020096
97def triggerTestNodeJob(defaultGitUrl, defaultGitRef, clusterName, testTarget, formulasSource) {
chnyda400babe2017-10-23 10:35:45 +020098 common.infoMsg("Test of ${clusterName} starts")
chnydafa674a02017-10-19 11:49:22 +020099 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 Josefe40bbf92018-03-22 15:02:46 +0100109 [$class: 'StringParameterValue', name: 'RECLASS_VERSION', value: RECLASS_VERSION],
chnydafa674a02017-10-19 11:49:22 +0200110 [$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 Ukovb2aa6db2017-10-23 12:30:31 +0400113 [$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]
chnydafa674a02017-10-19 11:49:22 +0200116 ]
117}
118
Jakub Josef3e77eb72018-01-15 14:21:53 +0100119def _clusterTestEnabled(infraYMLConfig){
Jakub Josefcb4f1dc2018-02-19 15:27:29 +0100120 if (infraYMLConfig["parameters"].containsKey("_jenkins")) {
121 if (infraYMLConfig["parameters"]["_jenkins"].containsKey("tests_enabled")) {
Jakub Josef3e77eb72018-01-15 14:21:53 +0100122 return infraYMLConfig["parameters"]["_jenkins"]["tests_enabled"];
123 }
124 }
125 // ci tests are enabled by default
126 return true;
127}
128
Jakub Josefa63f9862018-01-11 17:58:38 +0100129timeout(time: 12, unit: 'HOURS') {
130 node("python") {
131 try{
132 stage("checkout") {
chnyda32c6d9d2017-09-27 10:18:09 +0200133 if (gerritRef) {
Jakub Josefa63f9862018-01-11 17:58:38 +0100134 // 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 Josefcb4f1dc2018-02-19 15:27:29 +0100138 if (gerrit.patchsetHasApproval(gerritChange.currentPatchSet,"Verified", "+")) {
Jakub Josefa63f9862018-01-11 17:58:38 +0100139 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 }
chnyda32c6d9d2017-09-27 10:18:09 +0200152 }
Jakub Josefa63f9862018-01-11 17:58:38 +0100153 // 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")
chnyda3bf82d82017-10-19 14:01:53 +0200160 }
Jakub Josefc5a223a2017-03-01 14:40:08 +0100161 }
Jakub Josefa63f9862018-01-11 17:58:38 +0100162
163 stage("Check YAML") {
Jakub Josefcb4f1dc2018-02-19 15:27:29 +0100164 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 Josefa63f9862018-01-11 17:58:38 +0100169 }
170
171 stage("test-nodes") {
Jakub Josefcb4f1dc2018-02-19 15:27:29 +0100172 if (checkouted) {
Jakub Josefa63f9862018-01-11 17:58:38 +0100173 def modifiedClusters = null
Jakub Josefcb4f1dc2018-02-19 15:27:29 +0100174 // 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 Josef3e77eb72018-01-15 14:21:53 +0100179 }
Jakub Josefa63f9862018-01-11 17:58:38 +0100180 }
181
Jakub Josefcb4f1dc2018-02-19 15:27:29 +0100182 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 Josefdbe40d32018-02-19 16:08:12 +0100186 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 Josefcb4f1dc2018-02-19 15:27:29 +0100188 } 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 Josefa63f9862018-01-11 17:58:38 +0100239 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"])
chnyda51b03142017-05-10 17:15:27 +0200252 }
Jakub Josefc5a223a2017-03-01 14:40:08 +0100253 }
Tomáš Kukrál500c0182017-05-11 13:46:19 +0200254}