Jakub Josef | 8337931 | 2017-03-29 18:12:34 +0200 | [diff] [blame] | 1 | |
| 2 | /** |
| 3 | * Test salt models pipeline |
| 4 | * DEFAULT_GIT_REF |
| 5 | * DEFAULT_GIT_URL |
| 6 | * CREDENTIALS_ID |
Jakub Josef | 27424bc | 2017-05-22 16:56:27 +0200 | [diff] [blame] | 7 | * EXTRA_FORMULAS |
chnyda | d66d6fa | 2017-06-22 09:34:43 +0200 | [diff] [blame] | 8 | * SYSTEM_GIT_URL |
| 9 | * SYSTEM_GIT_REF |
chnyda | bb6d2a6 | 2017-07-31 14:09:16 +0200 | [diff] [blame] | 10 | * MAX_CPU_PER_JOB |
Jakub Josef | 0566454 | 2017-10-06 14:35:37 +0200 | [diff] [blame] | 11 | * LEGACY_TEST_MODE |
| 12 | * RECLASS_IGNORE_CLASS_NOTFOUND |
Dmitry Ukov | b2aa6db | 2017-10-23 12:30:31 +0400 | [diff] [blame] | 13 | * APT_REPOSITORY |
| 14 | * APT_REPOSITORY_GPG |
Jakub Josef | 8337931 | 2017-03-29 18:12:34 +0200 | [diff] [blame] | 15 | */ |
| 16 | |
Jakub Josef | c5a223a | 2017-03-01 14:40:08 +0100 | [diff] [blame] | 17 | def gerrit = new com.mirantis.mk.Gerrit() |
Filip Pytloun | 38005aa | 2017-03-06 10:26:38 +0100 | [diff] [blame] | 18 | def ssh = new com.mirantis.mk.Ssh() |
| 19 | def git = new com.mirantis.mk.Git() |
Jakub Josef | c5a223a | 2017-03-01 14:40:08 +0100 | [diff] [blame] | 20 | |
chnyda | 3dcf0bd | 2017-11-30 11:49:24 +0100 | [diff] [blame] | 21 | def config_node_name_pattern |
chnyda | 64b7358 | 2017-07-19 12:01:32 +0200 | [diff] [blame] | 22 | try { |
| 23 | config_node_name_pattern = CONFIG_NODE_NAME_PATTERN |
| 24 | } catch (MissingPropertyException e) { |
| 25 | config_node_name_pattern = "cfg01" |
| 26 | } |
| 27 | |
Filip Pytloun | 19376a8 | 2017-03-07 12:29:00 +0100 | [diff] [blame] | 28 | def gerritRef |
| 29 | try { |
| 30 | gerritRef = GERRIT_REFSPEC |
| 31 | } catch (MissingPropertyException e) { |
| 32 | gerritRef = null |
| 33 | } |
| 34 | |
Mykyta Karpin | c0758f3 | 2017-06-23 18:10:24 +0300 | [diff] [blame] | 35 | def formulasSource |
| 36 | try { |
| 37 | formulasSource = FORMULAS_SOURCE |
| 38 | } catch (MissingPropertyException e) { |
| 39 | formulasSource = "pkg" |
| 40 | } |
| 41 | |
Jakub Josef | 8337931 | 2017-03-29 18:12:34 +0200 | [diff] [blame] | 42 | def defaultGitRef, defaultGitUrl |
Filip Pytloun | 19376a8 | 2017-03-07 12:29:00 +0100 | [diff] [blame] | 43 | try { |
Jakub Josef | 8337931 | 2017-03-29 18:12:34 +0200 | [diff] [blame] | 44 | defaultGitRef = DEFAULT_GIT_REF |
| 45 | defaultGitUrl = DEFAULT_GIT_URL |
Filip Pytloun | 19376a8 | 2017-03-07 12:29:00 +0100 | [diff] [blame] | 46 | } catch (MissingPropertyException e) { |
Jakub Josef | 8337931 | 2017-03-29 18:12:34 +0200 | [diff] [blame] | 47 | defaultGitRef = null |
| 48 | defaultGitUrl = null |
Filip Pytloun | 19376a8 | 2017-03-07 12:29:00 +0100 | [diff] [blame] | 49 | } |
chnyda | 400babe | 2017-10-23 10:35:45 +0200 | [diff] [blame] | 50 | |
Jakub Josef | 8337931 | 2017-03-29 18:12:34 +0200 | [diff] [blame] | 51 | def checkouted = false |
chnyda | 400babe | 2017-10-23 10:35:45 +0200 | [diff] [blame] | 52 | futureNodes = [] |
chnyda | 3dcf0bd | 2017-11-30 11:49:24 +0100 | [diff] [blame] | 53 | failedNodes = false |
chnyda | 400babe | 2017-10-23 10:35:45 +0200 | [diff] [blame] | 54 | common = new com.mirantis.mk.Common() |
chnyda | fa674a0 | 2017-10-19 11:49:22 +0200 | [diff] [blame] | 55 | |
chnyda | 400babe | 2017-10-23 10:35:45 +0200 | [diff] [blame] | 56 | def setupRunner() { |
chnyda | fa674a0 | 2017-10-19 11:49:22 +0200 | [diff] [blame] | 57 | |
chnyda | 400babe | 2017-10-23 10:35:45 +0200 | [diff] [blame] | 58 | def branches = [:] |
| 59 | for (int i = 0; i < PARALLEL_NODE_GROUP_SIZE.toInteger() && i < futureNodes.size(); i++) { |
| 60 | branches["Runner ${i}"] = { |
chnyda | 3dcf0bd | 2017-11-30 11:49:24 +0100 | [diff] [blame] | 61 | while (futureNodes && !failedNodes) { |
chnyda | 400babe | 2017-10-23 10:35:45 +0200 | [diff] [blame] | 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) { |
chnyda | 3dcf0bd | 2017-11-30 11:49:24 +0100 | [diff] [blame] | 72 | if (e.getMessage().contains("completed with status ABORTED")) { |
| 73 | common.warningMsg("Test of ${clusterName} failed because the test was aborted : ${e}") |
| 74 | futureNodes << currentNode |
| 75 | } else { |
| 76 | common.warningMsg("Test of ${clusterName} failed : ${e}") |
| 77 | failedNodes = true |
| 78 | } |
chnyda | 400babe | 2017-10-23 10:35:45 +0200 | [diff] [blame] | 79 | } |
| 80 | } |
| 81 | } |
| 82 | } |
chnyda | 3dcf0bd | 2017-11-30 11:49:24 +0100 | [diff] [blame] | 83 | |
chnyda | 400babe | 2017-10-23 10:35:45 +0200 | [diff] [blame] | 84 | if (branches) { |
| 85 | parallel branches |
| 86 | } |
| 87 | } |
chnyda | fa674a0 | 2017-10-19 11:49:22 +0200 | [diff] [blame] | 88 | |
| 89 | def triggerTestNodeJob(defaultGitUrl, defaultGitRef, clusterName, testTarget, formulasSource) { |
chnyda | 400babe | 2017-10-23 10:35:45 +0200 | [diff] [blame] | 90 | common.infoMsg("Test of ${clusterName} starts") |
chnyda | fa674a0 | 2017-10-19 11:49:22 +0200 | [diff] [blame] | 91 | build job: "test-salt-model-node", parameters: [ |
| 92 | [$class: 'StringParameterValue', name: 'DEFAULT_GIT_URL', value: defaultGitUrl], |
| 93 | [$class: 'StringParameterValue', name: 'DEFAULT_GIT_REF', value: defaultGitRef], |
| 94 | [$class: 'StringParameterValue', name: 'CLUSTER_NAME', value: clusterName], |
| 95 | [$class: 'StringParameterValue', name: 'NODE_TARGET', value: testTarget], |
| 96 | [$class: 'StringParameterValue', name: 'FORMULAS_SOURCE', value: formulasSource], |
| 97 | [$class: 'StringParameterValue', name: 'EXTRA_FORMULAS', value: EXTRA_FORMULAS], |
| 98 | [$class: 'StringParameterValue', name: 'FORMULAS_REVISION', value: FORMULAS_REVISION], |
| 99 | [$class: 'StringParameterValue', name: 'CREDENTIALS_ID', value: CREDENTIALS_ID], |
| 100 | [$class: 'StringParameterValue', name: 'SYSTEM_GIT_URL', value: SYSTEM_GIT_URL], |
| 101 | [$class: 'StringParameterValue', name: 'MAX_CPU_PER_JOB', value: MAX_CPU_PER_JOB], |
| 102 | [$class: 'StringParameterValue', name: 'SYSTEM_GIT_REF', value: SYSTEM_GIT_REF], |
| 103 | [$class: 'BooleanParameterValue', name: 'LEGACY_TEST_MODE', value: LEGACY_TEST_MODE.toBoolean()], |
Dmitry Ukov | b2aa6db | 2017-10-23 12:30:31 +0400 | [diff] [blame] | 104 | [$class: 'BooleanParameterValue', name: 'RECLASS_IGNORE_CLASS_NOTFOUND', value: RECLASS_IGNORE_CLASS_NOTFOUND.toBoolean()], |
| 105 | [$class: 'StringParameterValue', name: 'APT_REPOSITORY', value: APT_REPOSITORY], |
| 106 | [$class: 'StringParameterValue', name: 'APT_REPOSITORY_GPG', value: APT_REPOSITORY_GPG] |
chnyda | fa674a0 | 2017-10-19 11:49:22 +0200 | [diff] [blame] | 107 | ] |
| 108 | } |
| 109 | |
Jakub Josef | 3e77eb7 | 2018-01-15 14:21:53 +0100 | [diff] [blame^] | 110 | def _clusterTestEnabled(infraYMLConfig){ |
| 111 | if(infraYMLConfig["parameters"].containsKey("_jenkins")){ |
| 112 | if(infraYMLConfig["parameters"]["_jenkins"].containsKey("tests_enabled")){ |
| 113 | return infraYMLConfig["parameters"]["_jenkins"]["tests_enabled"]; |
| 114 | } |
| 115 | } |
| 116 | // ci tests are enabled by default |
| 117 | return true; |
| 118 | } |
| 119 | |
Jakub Josef | a63f986 | 2018-01-11 17:58:38 +0100 | [diff] [blame] | 120 | timeout(time: 12, unit: 'HOURS') { |
| 121 | node("python") { |
| 122 | try{ |
| 123 | stage("checkout") { |
chnyda | 32c6d9d | 2017-09-27 10:18:09 +0200 | [diff] [blame] | 124 | if (gerritRef) { |
Jakub Josef | a63f986 | 2018-01-11 17:58:38 +0100 | [diff] [blame] | 125 | // job is triggered by Gerrit |
| 126 | // test if change aren't already merged |
| 127 | def gerritChange = gerrit.getGerritChange(GERRIT_NAME, GERRIT_HOST, GERRIT_CHANGE_NUMBER, CREDENTIALS_ID, true) |
| 128 | // test if gerrit change is already Verified |
| 129 | if(gerrit.patchsetHasApproval(gerritChange.currentPatchSet,"Verified", "+")){ |
| 130 | common.successMsg("Gerrit change ${GERRIT_CHANGE_NUMBER} patchset ${GERRIT_PATCHSET_NUMBER} already has Verified, skipping tests") // do nothing |
| 131 | // test WIP contains in commit message |
| 132 | }else if (gerritChange.commitMessage.contains("WIP")) { |
| 133 | common.successMsg("Commit message contains WIP, skipping tests") // do nothing |
| 134 | } else { |
| 135 | def merged = gerritChange.status == "MERGED" |
| 136 | if(!merged){ |
| 137 | checkouted = gerrit.gerritPatchsetCheckout ([ |
| 138 | credentialsId : CREDENTIALS_ID |
| 139 | ]) |
| 140 | } else{ |
| 141 | common.successMsg("Change ${GERRIT_CHANGE_NUMBER} is already merged, no need to test them") |
| 142 | } |
chnyda | 32c6d9d | 2017-09-27 10:18:09 +0200 | [diff] [blame] | 143 | } |
Jakub Josef | a63f986 | 2018-01-11 17:58:38 +0100 | [diff] [blame] | 144 | // defaultGitUrl is passed to the triggered job |
| 145 | defaultGitUrl = "${GERRIT_SCHEME}://${GERRIT_NAME}@${GERRIT_HOST}:${GERRIT_PORT}/${GERRIT_PROJECT}" |
| 146 | defaultGitRef = GERRIT_REFSPEC |
| 147 | } else if(defaultGitRef && defaultGitUrl) { |
| 148 | checkouted = gerrit.gerritPatchsetCheckout(defaultGitUrl, defaultGitRef, "HEAD", CREDENTIALS_ID) |
| 149 | } else { |
| 150 | throw new Exception("Cannot checkout gerrit patchset, GERRIT_REFSPEC and DEFAULT_GIT_REF is null") |
chnyda | 3bf82d8 | 2017-10-19 14:01:53 +0200 | [diff] [blame] | 151 | } |
Jakub Josef | c5a223a | 2017-03-01 14:40:08 +0100 | [diff] [blame] | 152 | } |
Jakub Josef | a63f986 | 2018-01-11 17:58:38 +0100 | [diff] [blame] | 153 | |
| 154 | stage("Check YAML") { |
| 155 | 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'))\" \\;") |
| 156 | } |
| 157 | |
| 158 | stage("test-nodes") { |
| 159 | if(checkouted) { |
| 160 | def modifiedClusters = null |
| 161 | |
| 162 | if (gerritRef) { |
| 163 | checkChange = sh(script: "git diff-tree --no-commit-id --name-only -r HEAD | grep -v classes/cluster", returnStatus: true) |
| 164 | if (checkChange == 1) { |
| 165 | modifiedClusters = sh(script: "git diff-tree --no-commit-id --name-only -r HEAD | grep classes/cluster/ | awk -F/ '{print \$3}' | uniq", returnStdout: true).tokenize() |
| 166 | } |
| 167 | } |
| 168 | |
| 169 | def infraYMLs = sh(script: "find ./classes/ -regex '.*cluster/[-_a-zA-Z0-9]*/[infra/]*init\\.yml' -exec grep -il 'cluster_name' {} \\;", returnStdout: true).tokenize() |
| 170 | def clusterDirectories = sh(script: "ls -d ./classes/cluster/*/ | awk -F/ '{print \$4}'", returnStdout: true).tokenize() |
| 171 | |
| 172 | // create a list of cluster names present in cluster folder |
| 173 | def infraList = [] |
| 174 | for (elt in infraYMLs) { |
| 175 | infraList << elt.tokenize('/')[3] |
| 176 | } |
| 177 | |
| 178 | // verify we have all valid clusters loaded |
| 179 | def commonList = infraList.intersect(clusterDirectories) |
| 180 | def differenceList = infraList.plus(clusterDirectories) |
| 181 | differenceList.removeAll(commonList) |
| 182 | |
| 183 | if(!differenceList.isEmpty()){ |
| 184 | common.warningMsg("The following clusters are not valid : ${differenceList} - That means we cannot found cluster_name in init.yml or infra/init.yml") |
| 185 | } |
| 186 | if (modifiedClusters) { |
| 187 | infraYMLs.removeAll { !modifiedClusters.contains(it.tokenize('/')[3]) } |
| 188 | common.infoMsg("Testing only modified clusters: ${infraYMLs}") |
| 189 | } |
| 190 | |
| 191 | for (int i = 0; i < infraYMLs.size(); i++) { |
| 192 | def infraYMLConfig = readYaml(file: infraYMLs[i]) |
Jakub Josef | 3e77eb7 | 2018-01-15 14:21:53 +0100 | [diff] [blame^] | 193 | if(_clusterTestEnabled(infraYMLConfig)){ |
| 194 | if(!infraYMLConfig["parameters"].containsKey("_param")){ |
| 195 | common.warningMsg("ERROR: Cannot find soft params (_param) in file " + infraYMLs[i] + " for obtain a cluster info. Skipping test.") |
| 196 | continue |
| 197 | } |
| 198 | def infraParams = infraYMLConfig["parameters"]["_param"]; |
| 199 | if(!infraParams.containsKey("infra_config_hostname") || !infraParams.containsKey("cluster_name") || !infraParams.containsKey("cluster_domain")){ |
| 200 | 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.") |
| 201 | continue |
| 202 | } |
| 203 | def clusterName = infraParams["cluster_name"] |
| 204 | def clusterDomain = infraParams["cluster_domain"] |
| 205 | def configHostname = infraParams["infra_config_hostname"] |
| 206 | def testTarget = String.format("%s.%s", configHostname, clusterDomain) |
Jakub Josef | a63f986 | 2018-01-11 17:58:38 +0100 | [diff] [blame] | 207 | |
Jakub Josef | 3e77eb7 | 2018-01-15 14:21:53 +0100 | [diff] [blame^] | 208 | futureNodes << [defaultGitUrl, defaultGitRef, clusterName, testTarget, formulasSource] |
| 209 | } |
Jakub Josef | a63f986 | 2018-01-11 17:58:38 +0100 | [diff] [blame] | 210 | } |
| 211 | |
| 212 | setupRunner() |
| 213 | |
| 214 | if (failedNodes) { |
| 215 | currentBuild.result = "FAILURE" |
| 216 | } |
| 217 | } |
| 218 | } |
| 219 | } catch (Throwable e) { |
| 220 | currentBuild.result = "FAILURE" |
| 221 | currentBuild.description = currentBuild.description ? e.message + " " + currentBuild.description : e.message |
| 222 | throw e |
| 223 | } finally { |
| 224 | common.sendNotification(currentBuild.result,"",["slack"]) |
chnyda | 51b0314 | 2017-05-10 17:15:27 +0200 | [diff] [blame] | 225 | } |
Jakub Josef | c5a223a | 2017-03-01 14:40:08 +0100 | [diff] [blame] | 226 | } |
Tomáš Kukrál | 500c018 | 2017-05-11 13:46:19 +0200 | [diff] [blame] | 227 | } |