blob: 69104b2cb5ecebc54b9d2b2a9f9d7ac87594cc8f [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 gerrit = new com.mirantis.mk.Gerrit()
Filip Pytloun38005aa2017-03-06 10:26:38 +010016def ssh = new com.mirantis.mk.Ssh()
17def git = new com.mirantis.mk.Git()
Jakub Josefc5a223a2017-03-01 14:40:08 +010018
chnyda64b73582017-07-19 12:01:32 +020019def config_node_name_pattern
20try {
21 config_node_name_pattern = CONFIG_NODE_NAME_PATTERN
22} catch (MissingPropertyException e) {
23 config_node_name_pattern = "cfg01"
24}
25
Filip Pytloun19376a82017-03-07 12:29:00 +010026def gerritRef
27try {
28 gerritRef = GERRIT_REFSPEC
29} catch (MissingPropertyException e) {
30 gerritRef = null
31}
32
Mykyta Karpinc0758f32017-06-23 18:10:24 +030033def formulasSource
34try {
35 formulasSource = FORMULAS_SOURCE
36} catch (MissingPropertyException e) {
37 formulasSource = "pkg"
38}
39
Jakub Josef83379312017-03-29 18:12:34 +020040def defaultGitRef, defaultGitUrl
Filip Pytloun19376a82017-03-07 12:29:00 +010041try {
Jakub Josef83379312017-03-29 18:12:34 +020042 defaultGitRef = DEFAULT_GIT_REF
43 defaultGitUrl = DEFAULT_GIT_URL
Filip Pytloun19376a82017-03-07 12:29:00 +010044} catch (MissingPropertyException e) {
Jakub Josef83379312017-03-29 18:12:34 +020045 defaultGitRef = null
46 defaultGitUrl = null
Filip Pytloun19376a82017-03-07 12:29:00 +010047}
chnyda400babe2017-10-23 10:35:45 +020048
Jakub Josef83379312017-03-29 18:12:34 +020049def checkouted = false
chnyda400babe2017-10-23 10:35:45 +020050futureNodes = []
51failedNodes = []
52common = new com.mirantis.mk.Common()
chnydafa674a02017-10-19 11:49:22 +020053
chnyda400babe2017-10-23 10:35:45 +020054def setupRunner() {
chnydafa674a02017-10-19 11:49:22 +020055
chnyda400babe2017-10-23 10:35:45 +020056 def branches = [:]
57 for (int i = 0; i < PARALLEL_NODE_GROUP_SIZE.toInteger() && i < futureNodes.size(); i++) {
58 branches["Runner ${i}"] = {
59 while (futureNodes) {
60 def currentNode = futureNodes[0] ? futureNodes[0] : null
61 if (!currentNode) {
62 continue
63 }
64
65 def clusterName = currentNode[2]
66 futureNodes.remove(currentNode)
67 try {
68 triggerTestNodeJob(currentNode[0], currentNode[1], currentNode[2], currentNode[3], currentNode[4])
69 } catch (Exception e) {
70 failedNodes << currentNode
71 common.warningMsg("Test of ${clusterName} failed : ${e}")
72 }
73 }
74 }
75 }
76 failedNodes = []
77 if (branches) {
78 parallel branches
79 }
80}
chnydafa674a02017-10-19 11:49:22 +020081
82def triggerTestNodeJob(defaultGitUrl, defaultGitRef, clusterName, testTarget, formulasSource) {
chnyda400babe2017-10-23 10:35:45 +020083 common.infoMsg("Test of ${clusterName} starts")
chnydafa674a02017-10-19 11:49:22 +020084 build job: "test-salt-model-node", parameters: [
85 [$class: 'StringParameterValue', name: 'DEFAULT_GIT_URL', value: defaultGitUrl],
86 [$class: 'StringParameterValue', name: 'DEFAULT_GIT_REF', value: defaultGitRef],
87 [$class: 'StringParameterValue', name: 'CLUSTER_NAME', value: clusterName],
88 [$class: 'StringParameterValue', name: 'NODE_TARGET', value: testTarget],
89 [$class: 'StringParameterValue', name: 'FORMULAS_SOURCE', value: formulasSource],
90 [$class: 'StringParameterValue', name: 'EXTRA_FORMULAS', value: EXTRA_FORMULAS],
91 [$class: 'StringParameterValue', name: 'FORMULAS_REVISION', value: FORMULAS_REVISION],
92 [$class: 'StringParameterValue', name: 'CREDENTIALS_ID', value: CREDENTIALS_ID],
93 [$class: 'StringParameterValue', name: 'SYSTEM_GIT_URL', value: SYSTEM_GIT_URL],
94 [$class: 'StringParameterValue', name: 'MAX_CPU_PER_JOB', value: MAX_CPU_PER_JOB],
95 [$class: 'StringParameterValue', name: 'SYSTEM_GIT_REF', value: SYSTEM_GIT_REF],
96 [$class: 'BooleanParameterValue', name: 'LEGACY_TEST_MODE', value: LEGACY_TEST_MODE.toBoolean()],
97 [$class: 'BooleanParameterValue', name: 'RECLASS_IGNORE_CLASS_NOTFOUND', value: RECLASS_IGNORE_CLASS_NOTFOUND.toBoolean()]
98 ]
99}
100
101
chnyda3b1d2a62017-06-13 10:08:12 +0200102node("python") {
Jakub Josefc5a223a2017-03-01 14:40:08 +0100103 try{
104 stage("checkout") {
Filip Pytloun19376a82017-03-07 12:29:00 +0100105 if (gerritRef) {
Jakub Josef83379312017-03-29 18:12:34 +0200106 // job is triggered by Gerrit
Jakub Joseffcb615e2017-04-10 14:34:40 +0200107 // test if change aren't already merged
Jakub Josefb2235902017-06-16 12:49:16 +0200108 def gerritChange = gerrit.getGerritChange(GERRIT_NAME, GERRIT_HOST, GERRIT_CHANGE_NUMBER, CREDENTIALS_ID, true)
109 // test if gerrit change is already Verified
Jakub Josef2ed50812017-06-16 14:56:58 +0200110 if(gerrit.patchsetHasApproval(gerritChange.currentPatchSet,"Verified", "+")){
Jakub Josef894f1e32017-06-19 16:24:19 +0000111 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 +0200112 // test WIP contains in commit message
Jakub Josefb2235902017-06-16 12:49:16 +0200113 }else if (gerritChange.commitMessage.contains("WIP")) {
Jakub Josef894f1e32017-06-19 16:24:19 +0000114 common.successMsg("Commit message contains WIP, skipping tests") // do nothing
Jakub Josefbcd2e902017-06-13 14:40:41 +0200115 } else {
116 def merged = gerritChange.status == "MERGED"
117 if(!merged){
118 checkouted = gerrit.gerritPatchsetCheckout ([
119 credentialsId : CREDENTIALS_ID
120 ])
121 } else{
122 common.successMsg("Change ${GERRIT_CHANGE_NUMBER} is already merged, no need to test them")
123 }
Jakub Joseffcb615e2017-04-10 14:34:40 +0200124 }
chnydad66d6fa2017-06-22 09:34:43 +0200125 // defaultGitUrl is passed to the triggered job
126 defaultGitUrl = "${GERRIT_SCHEME}://${GERRIT_NAME}@${GERRIT_HOST}:${GERRIT_PORT}/${GERRIT_PROJECT}"
127 defaultGitRef = GERRIT_REFSPEC
Jakub Josef83379312017-03-29 18:12:34 +0200128 } else if(defaultGitRef && defaultGitUrl) {
Jakub Josefe1407ac2017-03-30 14:10:19 +0200129 checkouted = gerrit.gerritPatchsetCheckout(defaultGitUrl, defaultGitRef, "HEAD", CREDENTIALS_ID)
Jakub Josefbcd2e902017-06-13 14:40:41 +0200130 } else {
131 throw new Exception("Cannot checkout gerrit patchset, GERRIT_REFSPEC and DEFAULT_GIT_REF is null")
Filip Pytloun19376a82017-03-07 12:29:00 +0100132 }
Jakub Josefc5a223a2017-03-01 14:40:08 +0100133 }
chnydaa9c88702017-05-09 16:51:07 +0200134
chnyda14e44292017-05-13 19:00:06 +0200135 stage("test-nodes") {
Jakub Josefbcd2e902017-06-13 14:40:41 +0200136 if(checkouted) {
chnyda32c6d9d2017-09-27 10:18:09 +0200137 def modifiedClusters = null
chnydae6572452017-09-27 14:40:15 +0200138
chnyda32c6d9d2017-09-27 10:18:09 +0200139 if (gerritRef) {
140 checkChange = sh(script: "git diff-tree --no-commit-id --name-only -r HEAD | grep -v classes/cluster", returnStatus: true)
141 if (checkChange == 1) {
142 modifiedClusters = sh(script: "git diff-tree --no-commit-id --name-only -r HEAD | grep classes/cluster/ | awk -F/ '{print \$3}' | uniq", returnStdout: true).tokenize()
143 }
144 }
145
chnydae6572452017-09-27 14:40:15 +0200146 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 +0200147 def clusterDirectories = sh(script: "ls -d ./classes/cluster/*/ | awk -F/ '{print \$4}'", returnStdout: true).tokenize()
chnyda0b077cb2017-09-27 16:51:46 +0200148
Jakub Josef6fb10432017-09-27 17:46:01 +0200149 // create a list of cluster names present in cluster folder
chnyda0b077cb2017-09-27 16:51:46 +0200150 def infraList = []
151 for (elt in infraYMLs) {
152 infraList << elt.tokenize('/')[3]
153 }
154
Jakub Josef6fb10432017-09-27 17:46:01 +0200155 // verify we have all valid clusters loaded
chnyda0b077cb2017-09-27 16:51:46 +0200156 def commonList = infraList.intersect(clusterDirectories)
157 def differenceList = infraList.plus(clusterDirectories)
158 differenceList.removeAll(commonList)
chnydac06539e2017-10-02 12:36:50 +0200159
Jakub Josef6fb10432017-09-27 17:46:01 +0200160 if(!differenceList.isEmpty()){
161 common.warningMsg("The following clusters are not valid : ${differenceList} - That means we cannot found cluster_name in init.yml or infra/init.yml")
162 }
chnydae6572452017-09-27 14:40:15 +0200163 if (modifiedClusters) {
164 infraYMLs.removeAll { !modifiedClusters.contains(it.tokenize('/')[3]) }
165 common.infoMsg("Testing only modified clusters: ${infraYMLs}")
166 }
167
Jakub Josef990013b2017-09-22 00:42:59 +0200168 for (int i = 0; i < infraYMLs.size(); i++) {
169 def infraYMLConfig = readYaml(file: infraYMLs[i])
170 if(!infraYMLConfig["parameters"].containsKey("_param")){
171 common.warningMsg("ERROR: Cannot find soft params (_param) in file " + infraYMLs[i] + " for obtain a cluster info. Skipping test.")
172 continue
173 }
174 def infraParams = infraYMLConfig["parameters"]["_param"];
175 if(!infraParams.containsKey("infra_config_hostname") || !infraParams.containsKey("cluster_name") || !infraParams.containsKey("cluster_domain")){
176 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.")
177 continue
178 }
179 def clusterName = infraParams["cluster_name"]
180 def clusterDomain = infraParams["cluster_domain"]
181 def configHostname = infraParams["infra_config_hostname"]
182 def testTarget = String.format("%s.%s", configHostname, clusterDomain)
chnyda1186a9c2017-06-26 13:13:32 +0200183
chnyda400babe2017-10-23 10:35:45 +0200184 futureNodes << [defaultGitUrl, defaultGitRef, clusterName, testTarget, formulasSource]
Cedric Hnydac87fc6c2017-09-22 13:12:36 +0000185 }
chnydafa674a02017-10-19 11:49:22 +0200186
chnyda400babe2017-10-23 10:35:45 +0200187 setupRunner()
chnydafa674a02017-10-19 11:49:22 +0200188
chnyda400babe2017-10-23 10:35:45 +0200189 def maxNodes = infraYMLs.size() > 10 ? infraYMLs.size() / 2 : 5
chnyda5ca17a32017-10-23 16:37:38 +0200190 if (failedNodes && failedNodes.size() <= maxNodes) {
chnyda400babe2017-10-23 10:35:45 +0200191 common.infoMsg("Some tests failed. They will be retriggered to make sure the failure is correct")
192 for (int retry = 0; retry < 2 && failedNodes; retry++) {
193 futureNodes = failedNodes
194 failedNodes = []
195 setupRunner()
chnydafa674a02017-10-19 11:49:22 +0200196 }
197 }
chnyda400babe2017-10-23 10:35:45 +0200198
chnyda3bf82d82017-10-19 14:01:53 +0200199 if (failedNodes) {
200 currentBuild.result = "FAILURE"
201 }
Jakub Josefc5a223a2017-03-01 14:40:08 +0100202 }
chnyda51b03142017-05-10 17:15:27 +0200203 }
Jakub Josefc5a223a2017-03-01 14:40:08 +0100204 } catch (Throwable e) {
Jakub Josefc5a223a2017-03-01 14:40:08 +0100205 currentBuild.result = "FAILURE"
Jakub Josefd2efd7d2017-08-22 17:49:57 +0200206 currentBuild.description = currentBuild.description ? e.message + " " + currentBuild.description : e.message
Jakub Josefc5a223a2017-03-01 14:40:08 +0100207 throw e
208 } finally {
209 common.sendNotification(currentBuild.result,"",["slack"])
210 }
Tomáš Kukrál500c0182017-05-11 13:46:19 +0200211}