blob: 060656cbddadc3b4bbee133a573e7632f024c95c [file] [log] [blame]
Jakub Josef83379312017-03-29 18:12:34 +02001
2/**
3 * Test salt models pipeline
4 * DEFAULT_GIT_REF
5 * DEFAULT_GIT_URL
6 * CREDENTIALS_ID
Jakub Josef27424bc2017-05-22 16:56:27 +02007 * EXTRA_FORMULAS
chnydad66d6fa2017-06-22 09:34:43 +02008 * SYSTEM_GIT_URL
9 * SYSTEM_GIT_REF
chnydabb6d2a62017-07-31 14:09:16 +020010 * MAX_CPU_PER_JOB
Jakub Josef05664542017-10-06 14:35:37 +020011 * LEGACY_TEST_MODE
12 * RECLASS_IGNORE_CLASS_NOTFOUND
Jakub Josef83379312017-03-29 18:12:34 +020013 */
14
Jakub Josefc5a223a2017-03-01 14:40:08 +010015def common = new com.mirantis.mk.Common()
16def gerrit = new com.mirantis.mk.Gerrit()
Filip Pytloun38005aa2017-03-06 10:26:38 +010017def ssh = new com.mirantis.mk.Ssh()
18def git = new com.mirantis.mk.Git()
Jakub Josefc5a223a2017-03-01 14:40:08 +010019
chnyda64b73582017-07-19 12:01:32 +020020def config_node_name_pattern
21try {
22 config_node_name_pattern = CONFIG_NODE_NAME_PATTERN
23} catch (MissingPropertyException e) {
24 config_node_name_pattern = "cfg01"
25}
26
Filip Pytloun19376a82017-03-07 12:29:00 +010027def gerritRef
28try {
29 gerritRef = GERRIT_REFSPEC
30} catch (MissingPropertyException e) {
31 gerritRef = null
32}
33
Mykyta Karpinc0758f32017-06-23 18:10:24 +030034def formulasSource
35try {
36 formulasSource = FORMULAS_SOURCE
37} catch (MissingPropertyException e) {
38 formulasSource = "pkg"
39}
40
Jakub Josef83379312017-03-29 18:12:34 +020041def defaultGitRef, defaultGitUrl
Filip Pytloun19376a82017-03-07 12:29:00 +010042try {
Jakub Josef83379312017-03-29 18:12:34 +020043 defaultGitRef = DEFAULT_GIT_REF
44 defaultGitUrl = DEFAULT_GIT_URL
Filip Pytloun19376a82017-03-07 12:29:00 +010045} catch (MissingPropertyException e) {
Jakub Josef83379312017-03-29 18:12:34 +020046 defaultGitRef = null
47 defaultGitUrl = null
Filip Pytloun19376a82017-03-07 12:29:00 +010048}
Jakub Josef83379312017-03-29 18:12:34 +020049def checkouted = false
chnydafa674a02017-10-19 11:49:22 +020050
51
52
53def triggerTestNodeJob(defaultGitUrl, defaultGitRef, clusterName, testTarget, formulasSource) {
54 build job: "test-salt-model-node", parameters: [
55 [$class: 'StringParameterValue', name: 'DEFAULT_GIT_URL', value: defaultGitUrl],
56 [$class: 'StringParameterValue', name: 'DEFAULT_GIT_REF', value: defaultGitRef],
57 [$class: 'StringParameterValue', name: 'CLUSTER_NAME', value: clusterName],
58 [$class: 'StringParameterValue', name: 'NODE_TARGET', value: testTarget],
59 [$class: 'StringParameterValue', name: 'FORMULAS_SOURCE', value: formulasSource],
60 [$class: 'StringParameterValue', name: 'EXTRA_FORMULAS', value: EXTRA_FORMULAS],
61 [$class: 'StringParameterValue', name: 'FORMULAS_REVISION', value: FORMULAS_REVISION],
62 [$class: 'StringParameterValue', name: 'CREDENTIALS_ID', value: CREDENTIALS_ID],
63 [$class: 'StringParameterValue', name: 'SYSTEM_GIT_URL', value: SYSTEM_GIT_URL],
64 [$class: 'StringParameterValue', name: 'MAX_CPU_PER_JOB', value: MAX_CPU_PER_JOB],
65 [$class: 'StringParameterValue', name: 'SYSTEM_GIT_REF', value: SYSTEM_GIT_REF],
66 [$class: 'BooleanParameterValue', name: 'LEGACY_TEST_MODE', value: LEGACY_TEST_MODE.toBoolean()],
67 [$class: 'BooleanParameterValue', name: 'RECLASS_IGNORE_CLASS_NOTFOUND', value: RECLASS_IGNORE_CLASS_NOTFOUND.toBoolean()]
68 ]
69}
70
71
chnyda3b1d2a62017-06-13 10:08:12 +020072node("python") {
Jakub Josefc5a223a2017-03-01 14:40:08 +010073 try{
74 stage("checkout") {
Filip Pytloun19376a82017-03-07 12:29:00 +010075 if (gerritRef) {
Jakub Josef83379312017-03-29 18:12:34 +020076 // job is triggered by Gerrit
Jakub Joseffcb615e2017-04-10 14:34:40 +020077 // test if change aren't already merged
Jakub Josefb2235902017-06-16 12:49:16 +020078 def gerritChange = gerrit.getGerritChange(GERRIT_NAME, GERRIT_HOST, GERRIT_CHANGE_NUMBER, CREDENTIALS_ID, true)
79 // test if gerrit change is already Verified
Jakub Josef2ed50812017-06-16 14:56:58 +020080 if(gerrit.patchsetHasApproval(gerritChange.currentPatchSet,"Verified", "+")){
Jakub Josef894f1e32017-06-19 16:24:19 +000081 common.successMsg("Gerrit change ${GERRIT_CHANGE_NUMBER} patchset ${GERRIT_PATCHSET_NUMBER} already has Verified, skipping tests") // do nothing
Jakub Josefbcd2e902017-06-13 14:40:41 +020082 // test WIP contains in commit message
Jakub Josefb2235902017-06-16 12:49:16 +020083 }else if (gerritChange.commitMessage.contains("WIP")) {
Jakub Josef894f1e32017-06-19 16:24:19 +000084 common.successMsg("Commit message contains WIP, skipping tests") // do nothing
Jakub Josefbcd2e902017-06-13 14:40:41 +020085 } else {
86 def merged = gerritChange.status == "MERGED"
87 if(!merged){
88 checkouted = gerrit.gerritPatchsetCheckout ([
89 credentialsId : CREDENTIALS_ID
90 ])
91 } else{
92 common.successMsg("Change ${GERRIT_CHANGE_NUMBER} is already merged, no need to test them")
93 }
Jakub Joseffcb615e2017-04-10 14:34:40 +020094 }
chnydad66d6fa2017-06-22 09:34:43 +020095 // defaultGitUrl is passed to the triggered job
96 defaultGitUrl = "${GERRIT_SCHEME}://${GERRIT_NAME}@${GERRIT_HOST}:${GERRIT_PORT}/${GERRIT_PROJECT}"
97 defaultGitRef = GERRIT_REFSPEC
Jakub Josef83379312017-03-29 18:12:34 +020098 } else if(defaultGitRef && defaultGitUrl) {
Jakub Josefe1407ac2017-03-30 14:10:19 +020099 checkouted = gerrit.gerritPatchsetCheckout(defaultGitUrl, defaultGitRef, "HEAD", CREDENTIALS_ID)
Jakub Josefbcd2e902017-06-13 14:40:41 +0200100 } else {
101 throw new Exception("Cannot checkout gerrit patchset, GERRIT_REFSPEC and DEFAULT_GIT_REF is null")
Filip Pytloun19376a82017-03-07 12:29:00 +0100102 }
Jakub Josefc5a223a2017-03-01 14:40:08 +0100103 }
chnydaa9c88702017-05-09 16:51:07 +0200104
chnyda14e44292017-05-13 19:00:06 +0200105 stage("test-nodes") {
Jakub Josefbcd2e902017-06-13 14:40:41 +0200106 if(checkouted) {
chnyda32c6d9d2017-09-27 10:18:09 +0200107 def modifiedClusters = null
chnydae6572452017-09-27 14:40:15 +0200108
chnyda32c6d9d2017-09-27 10:18:09 +0200109 if (gerritRef) {
110 checkChange = sh(script: "git diff-tree --no-commit-id --name-only -r HEAD | grep -v classes/cluster", returnStatus: true)
111 if (checkChange == 1) {
112 modifiedClusters = sh(script: "git diff-tree --no-commit-id --name-only -r HEAD | grep classes/cluster/ | awk -F/ '{print \$3}' | uniq", returnStdout: true).tokenize()
113 }
114 }
115
chnydae6572452017-09-27 14:40:15 +0200116 def infraYMLs = sh(script: "find ./classes/ -regex '.*cluster/[-_a-zA-Z0-9]*/[infra/]*init\\.yml' -exec grep -il 'cluster_name' {} \\;", returnStdout: true).tokenize()
chnydac06539e2017-10-02 12:36:50 +0200117 def clusterDirectories = sh(script: "ls -d ./classes/cluster/*/ | awk -F/ '{print \$4}'", returnStdout: true).tokenize()
chnyda0b077cb2017-09-27 16:51:46 +0200118
Jakub Josef6fb10432017-09-27 17:46:01 +0200119 // create a list of cluster names present in cluster folder
chnyda0b077cb2017-09-27 16:51:46 +0200120 def infraList = []
121 for (elt in infraYMLs) {
122 infraList << elt.tokenize('/')[3]
123 }
124
Jakub Josef6fb10432017-09-27 17:46:01 +0200125 // verify we have all valid clusters loaded
chnyda0b077cb2017-09-27 16:51:46 +0200126 def commonList = infraList.intersect(clusterDirectories)
127 def differenceList = infraList.plus(clusterDirectories)
128 differenceList.removeAll(commonList)
chnydac06539e2017-10-02 12:36:50 +0200129
Jakub Josef6fb10432017-09-27 17:46:01 +0200130 if(!differenceList.isEmpty()){
131 common.warningMsg("The following clusters are not valid : ${differenceList} - That means we cannot found cluster_name in init.yml or infra/init.yml")
132 }
chnydae6572452017-09-27 14:40:15 +0200133 if (modifiedClusters) {
134 infraYMLs.removeAll { !modifiedClusters.contains(it.tokenize('/')[3]) }
135 common.infoMsg("Testing only modified clusters: ${infraYMLs}")
136 }
137
138 def branches = [:]
chnydafa674a02017-10-19 11:49:22 +0200139 def failedNodes = []
chnydae6572452017-09-27 14:40:15 +0200140 def acc = 0
141
Jakub Josef990013b2017-09-22 00:42:59 +0200142 for (int i = 0; i < infraYMLs.size(); i++) {
143 def infraYMLConfig = readYaml(file: infraYMLs[i])
144 if(!infraYMLConfig["parameters"].containsKey("_param")){
145 common.warningMsg("ERROR: Cannot find soft params (_param) in file " + infraYMLs[i] + " for obtain a cluster info. Skipping test.")
146 continue
147 }
148 def infraParams = infraYMLConfig["parameters"]["_param"];
149 if(!infraParams.containsKey("infra_config_hostname") || !infraParams.containsKey("cluster_name") || !infraParams.containsKey("cluster_domain")){
150 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.")
151 continue
152 }
153 def clusterName = infraParams["cluster_name"]
154 def clusterDomain = infraParams["cluster_domain"]
155 def configHostname = infraParams["infra_config_hostname"]
156 def testTarget = String.format("%s.%s", configHostname, clusterDomain)
Cedric Hnydac87fc6c2017-09-22 13:12:36 +0000157 if (acc >= PARALLEL_NODE_GROUP_SIZE.toInteger()) {
158 parallel branches
159 branches = [:]
160 acc = 0
161 }
chnyda1186a9c2017-06-26 13:13:32 +0200162
Jakub Josef3b52adc2017-09-28 20:11:12 +0200163 branches[clusterName] = {
chnydafa674a02017-10-19 11:49:22 +0200164 try {
165 triggerTestNodeJob(defaultGitUrl, defaultGitRef, clusterName, testTarget, formulasSource)
166 } catch (Exception e) {
167 failedNodes << [defaultGitUrl, defaultGitRef, clusterName, testTarget, formulasSource]
chnyda3bf82d82017-10-19 14:01:53 +0200168 common.warningMsg("Test of ${clusterName} failed : ${e}")
chnydafa674a02017-10-19 11:49:22 +0200169 }
170 }
Cedric Hnydac87fc6c2017-09-22 13:12:36 +0000171 acc++;
chnyda81ae93d2017-06-22 15:49:52 +0200172 }
Cedric Hnydac87fc6c2017-09-22 13:12:36 +0000173 if (acc != 0) {
174 parallel branches
175 }
chnydafa674a02017-10-19 11:49:22 +0200176
chnyda3bf82d82017-10-19 14:01:53 +0200177 def nbRetry = 1
178 def maxNbRetry = infraYMLs.size() > 10 ? infraYMLs.size() / 2 : 10
179 for (int i = 0; i < nbRetry && failedNodes && failedNodes.size() <= maxNbRetry; ++i) {
chnydafa674a02017-10-19 11:49:22 +0200180 branches = [:]
181 acc = 0
182 retryNodes = failedNodes
183 failedNodes = []
184 for (retryNode in retryNodes) {
185 if (acc >= PARALLEL_NODE_GROUP_SIZE.toInteger()) {
186 parallel branches
187 branches = [:]
188 acc = 0
189 }
190
191 common.infoMsg("Test of ${retryNode[2]} failed, retrigger it to make sure")
192 branches[retryNode[2]] = {
193 try {
194 triggerTestNodeJob(retryNode[0], retryNode[1], retryNode[2], retryNode[3], retryNode[4])
195 } catch (Exception e) {
196 failedNodes << retryNode
197 common.warningMsg("Test of ${retryNode[2]} failed : ${e}")
198 }
199 }
200 acc++
201 }
202 if (acc != 0) {
203 parallel branches
204 }
205 }
chnyda3bf82d82017-10-19 14:01:53 +0200206 if (failedNodes) {
207 currentBuild.result = "FAILURE"
208 }
Jakub Josefc5a223a2017-03-01 14:40:08 +0100209 }
chnyda51b03142017-05-10 17:15:27 +0200210 }
Jakub Josefc5a223a2017-03-01 14:40:08 +0100211 } catch (Throwable e) {
Jakub Josefc5a223a2017-03-01 14:40:08 +0100212 currentBuild.result = "FAILURE"
Jakub Josefd2efd7d2017-08-22 17:49:57 +0200213 currentBuild.description = currentBuild.description ? e.message + " " + currentBuild.description : e.message
Jakub Josefc5a223a2017-03-01 14:40:08 +0100214 throw e
215 } finally {
216 common.sendNotification(currentBuild.result,"",["slack"])
217 }
Tomáš Kukrál500c0182017-05-11 13:46:19 +0200218}