blob: fcfba0c80085f34693ecd4f904990089ebb54872 [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
Dmitry Ukovb2aa6db2017-10-23 12:30:31 +040013 * APT_REPOSITORY
14 * APT_REPOSITORY_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
chnyda64b73582017-07-19 12:01:32 +020021def config_node_name_pattern
22try {
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 Josef83379312017-03-29 18:12:34 +020042def defaultGitRef, defaultGitUrl
Filip Pytloun19376a82017-03-07 12:29:00 +010043try {
Jakub Josef83379312017-03-29 18:12:34 +020044 defaultGitRef = DEFAULT_GIT_REF
45 defaultGitUrl = DEFAULT_GIT_URL
Filip Pytloun19376a82017-03-07 12:29:00 +010046} catch (MissingPropertyException e) {
Jakub Josef83379312017-03-29 18:12:34 +020047 defaultGitRef = null
48 defaultGitUrl = null
Filip Pytloun19376a82017-03-07 12:29:00 +010049}
chnyda400babe2017-10-23 10:35:45 +020050
Jakub Josef83379312017-03-29 18:12:34 +020051def checkouted = false
chnyda400babe2017-10-23 10:35:45 +020052futureNodes = []
53failedNodes = []
54common = new com.mirantis.mk.Common()
chnydafa674a02017-10-19 11:49:22 +020055
chnyda400babe2017-10-23 10:35:45 +020056def setupRunner() {
chnydafa674a02017-10-19 11:49:22 +020057
chnyda400babe2017-10-23 10:35:45 +020058 def branches = [:]
59 for (int i = 0; i < PARALLEL_NODE_GROUP_SIZE.toInteger() && i < futureNodes.size(); i++) {
60 branches["Runner ${i}"] = {
61 while (futureNodes) {
62 def currentNode = futureNodes[0] ? futureNodes[0] : null
63 if (!currentNode) {
64 continue
65 }
66
67 def clusterName = currentNode[2]
68 futureNodes.remove(currentNode)
69 try {
70 triggerTestNodeJob(currentNode[0], currentNode[1], currentNode[2], currentNode[3], currentNode[4])
71 } catch (Exception e) {
72 failedNodes << currentNode
73 common.warningMsg("Test of ${clusterName} failed : ${e}")
74 }
75 }
76 }
77 }
78 failedNodes = []
79 if (branches) {
80 parallel branches
81 }
82}
chnydafa674a02017-10-19 11:49:22 +020083
84def triggerTestNodeJob(defaultGitUrl, defaultGitRef, clusterName, testTarget, formulasSource) {
chnyda400babe2017-10-23 10:35:45 +020085 common.infoMsg("Test of ${clusterName} starts")
chnydafa674a02017-10-19 11:49:22 +020086 build job: "test-salt-model-node", parameters: [
87 [$class: 'StringParameterValue', name: 'DEFAULT_GIT_URL', value: defaultGitUrl],
88 [$class: 'StringParameterValue', name: 'DEFAULT_GIT_REF', value: defaultGitRef],
89 [$class: 'StringParameterValue', name: 'CLUSTER_NAME', value: clusterName],
90 [$class: 'StringParameterValue', name: 'NODE_TARGET', value: testTarget],
91 [$class: 'StringParameterValue', name: 'FORMULAS_SOURCE', value: formulasSource],
92 [$class: 'StringParameterValue', name: 'EXTRA_FORMULAS', value: EXTRA_FORMULAS],
93 [$class: 'StringParameterValue', name: 'FORMULAS_REVISION', value: FORMULAS_REVISION],
94 [$class: 'StringParameterValue', name: 'CREDENTIALS_ID', value: CREDENTIALS_ID],
95 [$class: 'StringParameterValue', name: 'SYSTEM_GIT_URL', value: SYSTEM_GIT_URL],
96 [$class: 'StringParameterValue', name: 'MAX_CPU_PER_JOB', value: MAX_CPU_PER_JOB],
97 [$class: 'StringParameterValue', name: 'SYSTEM_GIT_REF', value: SYSTEM_GIT_REF],
98 [$class: 'BooleanParameterValue', name: 'LEGACY_TEST_MODE', value: LEGACY_TEST_MODE.toBoolean()],
Dmitry Ukovb2aa6db2017-10-23 12:30:31 +040099 [$class: 'BooleanParameterValue', name: 'RECLASS_IGNORE_CLASS_NOTFOUND', value: RECLASS_IGNORE_CLASS_NOTFOUND.toBoolean()],
100 [$class: 'StringParameterValue', name: 'APT_REPOSITORY', value: APT_REPOSITORY],
101 [$class: 'StringParameterValue', name: 'APT_REPOSITORY_GPG', value: APT_REPOSITORY_GPG]
chnydafa674a02017-10-19 11:49:22 +0200102 ]
103}
104
105
chnyda3b1d2a62017-06-13 10:08:12 +0200106node("python") {
Jakub Josefc5a223a2017-03-01 14:40:08 +0100107 try{
108 stage("checkout") {
Filip Pytloun19376a82017-03-07 12:29:00 +0100109 if (gerritRef) {
Jakub Josef83379312017-03-29 18:12:34 +0200110 // job is triggered by Gerrit
Jakub Joseffcb615e2017-04-10 14:34:40 +0200111 // test if change aren't already merged
Jakub Josefb2235902017-06-16 12:49:16 +0200112 def gerritChange = gerrit.getGerritChange(GERRIT_NAME, GERRIT_HOST, GERRIT_CHANGE_NUMBER, CREDENTIALS_ID, true)
113 // test if gerrit change is already Verified
Jakub Josef2ed50812017-06-16 14:56:58 +0200114 if(gerrit.patchsetHasApproval(gerritChange.currentPatchSet,"Verified", "+")){
Jakub Josef894f1e32017-06-19 16:24:19 +0000115 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 +0200116 // test WIP contains in commit message
Jakub Josefb2235902017-06-16 12:49:16 +0200117 }else if (gerritChange.commitMessage.contains("WIP")) {
Jakub Josef894f1e32017-06-19 16:24:19 +0000118 common.successMsg("Commit message contains WIP, skipping tests") // do nothing
Jakub Josefbcd2e902017-06-13 14:40:41 +0200119 } else {
120 def merged = gerritChange.status == "MERGED"
121 if(!merged){
122 checkouted = gerrit.gerritPatchsetCheckout ([
123 credentialsId : CREDENTIALS_ID
124 ])
125 } else{
126 common.successMsg("Change ${GERRIT_CHANGE_NUMBER} is already merged, no need to test them")
127 }
Jakub Joseffcb615e2017-04-10 14:34:40 +0200128 }
chnydad66d6fa2017-06-22 09:34:43 +0200129 // defaultGitUrl is passed to the triggered job
130 defaultGitUrl = "${GERRIT_SCHEME}://${GERRIT_NAME}@${GERRIT_HOST}:${GERRIT_PORT}/${GERRIT_PROJECT}"
131 defaultGitRef = GERRIT_REFSPEC
Jakub Josef83379312017-03-29 18:12:34 +0200132 } else if(defaultGitRef && defaultGitUrl) {
Jakub Josefe1407ac2017-03-30 14:10:19 +0200133 checkouted = gerrit.gerritPatchsetCheckout(defaultGitUrl, defaultGitRef, "HEAD", CREDENTIALS_ID)
Jakub Josefbcd2e902017-06-13 14:40:41 +0200134 } else {
135 throw new Exception("Cannot checkout gerrit patchset, GERRIT_REFSPEC and DEFAULT_GIT_REF is null")
Filip Pytloun19376a82017-03-07 12:29:00 +0100136 }
Jakub Josefc5a223a2017-03-01 14:40:08 +0100137 }
chnydaa9c88702017-05-09 16:51:07 +0200138
chnyda9bb38b22017-11-13 13:55:57 +0100139 stage("Check YAML") {
140 sh("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'))\" \\;")
141 }
142
chnyda14e44292017-05-13 19:00:06 +0200143 stage("test-nodes") {
Jakub Josefbcd2e902017-06-13 14:40:41 +0200144 if(checkouted) {
chnyda32c6d9d2017-09-27 10:18:09 +0200145 def modifiedClusters = null
chnydae6572452017-09-27 14:40:15 +0200146
chnyda32c6d9d2017-09-27 10:18:09 +0200147 if (gerritRef) {
148 checkChange = sh(script: "git diff-tree --no-commit-id --name-only -r HEAD | grep -v classes/cluster", returnStatus: true)
149 if (checkChange == 1) {
150 modifiedClusters = sh(script: "git diff-tree --no-commit-id --name-only -r HEAD | grep classes/cluster/ | awk -F/ '{print \$3}' | uniq", returnStdout: true).tokenize()
151 }
152 }
153
chnydae6572452017-09-27 14:40:15 +0200154 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 +0200155 def clusterDirectories = sh(script: "ls -d ./classes/cluster/*/ | awk -F/ '{print \$4}'", returnStdout: true).tokenize()
chnyda0b077cb2017-09-27 16:51:46 +0200156
Jakub Josef6fb10432017-09-27 17:46:01 +0200157 // create a list of cluster names present in cluster folder
chnyda0b077cb2017-09-27 16:51:46 +0200158 def infraList = []
159 for (elt in infraYMLs) {
160 infraList << elt.tokenize('/')[3]
161 }
162
Jakub Josef6fb10432017-09-27 17:46:01 +0200163 // verify we have all valid clusters loaded
chnyda0b077cb2017-09-27 16:51:46 +0200164 def commonList = infraList.intersect(clusterDirectories)
165 def differenceList = infraList.plus(clusterDirectories)
166 differenceList.removeAll(commonList)
chnydac06539e2017-10-02 12:36:50 +0200167
Jakub Josef6fb10432017-09-27 17:46:01 +0200168 if(!differenceList.isEmpty()){
169 common.warningMsg("The following clusters are not valid : ${differenceList} - That means we cannot found cluster_name in init.yml or infra/init.yml")
170 }
chnydae6572452017-09-27 14:40:15 +0200171 if (modifiedClusters) {
172 infraYMLs.removeAll { !modifiedClusters.contains(it.tokenize('/')[3]) }
173 common.infoMsg("Testing only modified clusters: ${infraYMLs}")
174 }
175
Jakub Josef990013b2017-09-22 00:42:59 +0200176 for (int i = 0; i < infraYMLs.size(); i++) {
177 def infraYMLConfig = readYaml(file: infraYMLs[i])
178 if(!infraYMLConfig["parameters"].containsKey("_param")){
179 common.warningMsg("ERROR: Cannot find soft params (_param) in file " + infraYMLs[i] + " for obtain a cluster info. Skipping test.")
180 continue
181 }
182 def infraParams = infraYMLConfig["parameters"]["_param"];
183 if(!infraParams.containsKey("infra_config_hostname") || !infraParams.containsKey("cluster_name") || !infraParams.containsKey("cluster_domain")){
184 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.")
185 continue
186 }
187 def clusterName = infraParams["cluster_name"]
188 def clusterDomain = infraParams["cluster_domain"]
189 def configHostname = infraParams["infra_config_hostname"]
190 def testTarget = String.format("%s.%s", configHostname, clusterDomain)
chnyda1186a9c2017-06-26 13:13:32 +0200191
chnyda400babe2017-10-23 10:35:45 +0200192 futureNodes << [defaultGitUrl, defaultGitRef, clusterName, testTarget, formulasSource]
Cedric Hnydac87fc6c2017-09-22 13:12:36 +0000193 }
chnydafa674a02017-10-19 11:49:22 +0200194
chnyda400babe2017-10-23 10:35:45 +0200195 setupRunner()
chnydafa674a02017-10-19 11:49:22 +0200196
chnyda400babe2017-10-23 10:35:45 +0200197 def maxNodes = infraYMLs.size() > 10 ? infraYMLs.size() / 2 : 5
chnyda5ca17a32017-10-23 16:37:38 +0200198 if (failedNodes && failedNodes.size() <= maxNodes) {
chnyda400babe2017-10-23 10:35:45 +0200199 common.infoMsg("Some tests failed. They will be retriggered to make sure the failure is correct")
200 for (int retry = 0; retry < 2 && failedNodes; retry++) {
201 futureNodes = failedNodes
202 failedNodes = []
203 setupRunner()
chnydafa674a02017-10-19 11:49:22 +0200204 }
205 }
chnyda400babe2017-10-23 10:35:45 +0200206
chnyda3bf82d82017-10-19 14:01:53 +0200207 if (failedNodes) {
208 currentBuild.result = "FAILURE"
209 }
Jakub Josefc5a223a2017-03-01 14:40:08 +0100210 }
chnyda51b03142017-05-10 17:15:27 +0200211 }
Jakub Josefc5a223a2017-03-01 14:40:08 +0100212 } catch (Throwable e) {
Jakub Josefc5a223a2017-03-01 14:40:08 +0100213 currentBuild.result = "FAILURE"
Jakub Josefd2efd7d2017-08-22 17:49:57 +0200214 currentBuild.description = currentBuild.description ? e.message + " " + currentBuild.description : e.message
Jakub Josefc5a223a2017-03-01 14:40:08 +0100215 throw e
216 } finally {
217 common.sendNotification(currentBuild.result,"",["slack"])
218 }
Tomáš Kukrál500c0182017-05-11 13:46:19 +0200219}