blob: c119e9c8b62926f58a7accbb3f1d3c0f2a9ff351 [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
Jakub Josefcb4f1dc2018-02-19 15:27:29 +01006 * MAX_CPU_PER_JOB max cpu count for one docket test instance
7 * SYSTEM_GIT_URL reclass system git URL (optional)
8 * SYSTEM_GIT_REF reclass system git URL (optional)
9 * TEST_CLUSTER_NAMES list of comma separated cluster names to test (optional, default all cluster levels)
10 * LEGACY_TEST_MODE legacy test mode flag
11 * RECLASS_IGNORE_CLASS_NOTFOUND ignore missing class flag for reclass config
azvyagintsev1bcadc42018-10-05 20:13:15 +030012 * DISTRIB_REVISION of apt mirrror to be used (http://mirror.mirantis.com/DISTRIB_REVISION/ by default)
Jakub Josefcb4f1dc2018-02-19 15:27:29 +010013 * APT_REPOSITORY extra apt repository url
14 * APT_REPOSITORY_GPG extra apt repository url GPG
Jakub Josef83379312017-03-29 18:12:34 +020015 */
16
Jakub Josefc5a223a2017-03-01 14:40:08 +010017def gerrit = new com.mirantis.mk.Gerrit()
azvyagintsev1bcadc42018-10-05 20:13:15 +030018def common = new com.mirantis.mk.Common()
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
azvyagintsev1bcadc42018-10-05 20:13:15 +030022def config_node_name_pattern = env.CONFIG_NODE_NAME_PATTERN ?: 'cfg01'
23def gerritRef = env.GERRIT_REFSPEC ?: GERRIT_REFSPEC
24def formulasSource = env.FORMULAS_SOURCE ?: 'pkg'
chnyda64b73582017-07-19 12:01:32 +020025
azvyagintsev1bcadc42018-10-05 20:13:15 +030026def testClusterNames = env.TEST_CLUSTER_NAMES ?: ''
27def defaultGitRef = env.DEFAULT_GIT_REF ?: null
28def defaultGitUrl = env.DEFAULT_GIT_URL ?: null
chnyda400babe2017-10-23 10:35:45 +020029
Jakub Josef83379312017-03-29 18:12:34 +020030def checkouted = false
chnyda400babe2017-10-23 10:35:45 +020031futureNodes = []
chnyda3dcf0bd2017-11-30 11:49:24 +010032failedNodes = false
azvyagintsev1bcadc42018-10-05 20:13:15 +030033
chnydafa674a02017-10-19 11:49:22 +020034
chnyda400babe2017-10-23 10:35:45 +020035def setupRunner() {
Denis Egorenkoeff76d32018-10-01 12:09:40 +040036 def branches = [:]
37 branches.failFast = true
38 for(int i = 0; i < futureNodes.size(); i++) {
39 def currentNode = futureNodes[i] ? futureNodes[i] : null
chnyda400babe2017-10-23 10:35:45 +020040 if (!currentNode) {
Denis Egorenkoeff76d32018-10-01 12:09:40 +040041 continue
chnyda400babe2017-10-23 10:35:45 +020042 }
Denis Egorenkoeff76d32018-10-01 12:09:40 +040043 branches["Runner ${i}"] = {
44 try {
45 triggerTestNodeJob(currentNode[0], currentNode[1], currentNode[2], currentNode[3], currentNode[4])
46 } catch (Exception e) {
47 common.warningMsg("Test of ${currentNode[2]} failed : ${e}")
48 throw e
49 }
chnyda400babe2017-10-23 10:35:45 +020050 }
chnyda400babe2017-10-23 10:35:45 +020051 }
chnyda3dcf0bd2017-11-30 11:49:24 +010052
Denis Egorenkoeff76d32018-10-01 12:09:40 +040053 if (branches) {
54 common.runParallel(branches, PARALLEL_NODE_GROUP_SIZE.toInteger())
55 }
chnyda400babe2017-10-23 10:35:45 +020056}
chnydafa674a02017-10-19 11:49:22 +020057
58def triggerTestNodeJob(defaultGitUrl, defaultGitRef, clusterName, testTarget, formulasSource) {
chnyda400babe2017-10-23 10:35:45 +020059 common.infoMsg("Test of ${clusterName} starts")
chnydafa674a02017-10-19 11:49:22 +020060 build job: "test-salt-model-node", parameters: [
61 [$class: 'StringParameterValue', name: 'DEFAULT_GIT_URL', value: defaultGitUrl],
62 [$class: 'StringParameterValue', name: 'DEFAULT_GIT_REF', value: defaultGitRef],
63 [$class: 'StringParameterValue', name: 'CLUSTER_NAME', value: clusterName],
64 [$class: 'StringParameterValue', name: 'NODE_TARGET', value: testTarget],
65 [$class: 'StringParameterValue', name: 'FORMULAS_SOURCE', value: formulasSource],
chnydafa674a02017-10-19 11:49:22 +020066 [$class: 'StringParameterValue', name: 'CREDENTIALS_ID', value: CREDENTIALS_ID],
67 [$class: 'StringParameterValue', name: 'SYSTEM_GIT_URL', value: SYSTEM_GIT_URL],
azvyagintsev1bcadc42018-10-05 20:13:15 +030068 [$class: 'StringParameterValue', name: 'DISTRIB_REVISION', value: distribRevision],
chnydafa674a02017-10-19 11:49:22 +020069 [$class: 'StringParameterValue', name: 'MAX_CPU_PER_JOB', value: MAX_CPU_PER_JOB],
70 [$class: 'StringParameterValue', name: 'SYSTEM_GIT_REF', value: SYSTEM_GIT_REF],
71 [$class: 'BooleanParameterValue', name: 'LEGACY_TEST_MODE', value: LEGACY_TEST_MODE.toBoolean()],
Dmitry Ukovb2aa6db2017-10-23 12:30:31 +040072 [$class: 'BooleanParameterValue', name: 'RECLASS_IGNORE_CLASS_NOTFOUND', value: RECLASS_IGNORE_CLASS_NOTFOUND.toBoolean()],
73 [$class: 'StringParameterValue', name: 'APT_REPOSITORY', value: APT_REPOSITORY],
74 [$class: 'StringParameterValue', name: 'APT_REPOSITORY_GPG', value: APT_REPOSITORY_GPG]
chnydafa674a02017-10-19 11:49:22 +020075 ]
76}
77
Jakub Josef3e77eb72018-01-15 14:21:53 +010078def _clusterTestEnabled(infraYMLConfig){
Jakub Josefcb4f1dc2018-02-19 15:27:29 +010079 if (infraYMLConfig["parameters"].containsKey("_jenkins")) {
80 if (infraYMLConfig["parameters"]["_jenkins"].containsKey("tests_enabled")) {
Jakub Josef3e77eb72018-01-15 14:21:53 +010081 return infraYMLConfig["parameters"]["_jenkins"]["tests_enabled"];
82 }
83 }
84 // ci tests are enabled by default
85 return true;
86}
87
Jakub Josefa63f9862018-01-11 17:58:38 +010088timeout(time: 12, unit: 'HOURS') {
89 node("python") {
90 try{
91 stage("checkout") {
chnyda32c6d9d2017-09-27 10:18:09 +020092 if (gerritRef) {
Jakub Josefa63f9862018-01-11 17:58:38 +010093 // job is triggered by Gerrit
94 // test if change aren't already merged
95 def gerritChange = gerrit.getGerritChange(GERRIT_NAME, GERRIT_HOST, GERRIT_CHANGE_NUMBER, CREDENTIALS_ID, true)
96 // test if gerrit change is already Verified
Jakub Josefcb4f1dc2018-02-19 15:27:29 +010097 if (gerrit.patchsetHasApproval(gerritChange.currentPatchSet,"Verified", "+")) {
Jakub Josefa63f9862018-01-11 17:58:38 +010098 common.successMsg("Gerrit change ${GERRIT_CHANGE_NUMBER} patchset ${GERRIT_PATCHSET_NUMBER} already has Verified, skipping tests") // do nothing
99 // test WIP contains in commit message
100 }else if (gerritChange.commitMessage.contains("WIP")) {
101 common.successMsg("Commit message contains WIP, skipping tests") // do nothing
102 } else {
103 def merged = gerritChange.status == "MERGED"
104 if(!merged){
105 checkouted = gerrit.gerritPatchsetCheckout ([
106 credentialsId : CREDENTIALS_ID
107 ])
108 } else{
109 common.successMsg("Change ${GERRIT_CHANGE_NUMBER} is already merged, no need to test them")
110 }
chnyda32c6d9d2017-09-27 10:18:09 +0200111 }
Jakub Josefa63f9862018-01-11 17:58:38 +0100112 // defaultGitUrl is passed to the triggered job
113 defaultGitUrl = "${GERRIT_SCHEME}://${GERRIT_NAME}@${GERRIT_HOST}:${GERRIT_PORT}/${GERRIT_PROJECT}"
114 defaultGitRef = GERRIT_REFSPEC
115 } else if(defaultGitRef && defaultGitUrl) {
116 checkouted = gerrit.gerritPatchsetCheckout(defaultGitUrl, defaultGitRef, "HEAD", CREDENTIALS_ID)
117 } else {
118 throw new Exception("Cannot checkout gerrit patchset, GERRIT_REFSPEC and DEFAULT_GIT_REF is null")
chnyda3bf82d82017-10-19 14:01:53 +0200119 }
Jakub Josefc5a223a2017-03-01 14:40:08 +0100120 }
Jakub Josefa63f9862018-01-11 17:58:38 +0100121
122 stage("Check YAML") {
Jakub Josefcb4f1dc2018-02-19 15:27:29 +0100123 common.infoMsg("Checking YAML syntax for changed files")
124 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)
125 if(syntaxCheckStatus > 0){
126 common.errorMsg("YAML syntax check failed!")
127 }
Jakub Josefa63f9862018-01-11 17:58:38 +0100128 }
129
130 stage("test-nodes") {
Jakub Josefcb4f1dc2018-02-19 15:27:29 +0100131 if (checkouted) {
Jakub Josefa63f9862018-01-11 17:58:38 +0100132 def modifiedClusters = null
Jakub Josefcb4f1dc2018-02-19 15:27:29 +0100133 // testing modified cluster is used only if test was triggered by gerrit
134 if (gerritRef) {
135 def checkChange = sh(script: "set +x;git diff-tree --no-commit-id --name-only -r HEAD | grep -v classes/cluster", returnStatus: true)
136 if (checkChange == 1) {
137 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 +0100138 }
Jakub Josefa63f9862018-01-11 17:58:38 +0100139 }
140
Jakub Josefcb4f1dc2018-02-19 15:27:29 +0100141 def infraYMLs = []
142 // list of cluster names can be explicitly given
143 if (testClusterNames != null && testClusterNames != "") {
144 common.infoMsg("TEST_CLUSTER_NAMES param found, using explicitly defined cluster names: ${testClusterNames}")
Jakub Josefdbe40d32018-02-19 16:08:12 +0100145 def clusterNameRegex = testClusterNames.tokenize(",").collect{it.trim()}.join("|")
146 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 +0100147 } else {
148 common.infoMsg("TEST_CLUSTER_NAMES param not found, all clusters with enabled tests will be tested")
149 // else we want to test all cluster levels found
150 infraYMLs = sh(script: "set +x;find ./classes/ -regex '.*cluster/[-_a-zA-Z0-9]*/[infra/]*init\\.yml' -exec grep -il 'cluster_name' {} \\;", returnStdout: true).tokenize()
151 def clusterDirectories = sh(script: "set +x;ls -d ./classes/cluster/*/ | awk -F/ '{print \$4}'", returnStdout: true).tokenize()
152
153 // create a list of cluster names present in cluster folder
154 def infraList = []
155 for (elt in infraYMLs) {
156 infraList << elt.tokenize('/')[3]
157 }
158
159 // verify we have all valid clusters loaded
160 def commonList = infraList.intersect(clusterDirectories)
161 def differenceList = infraList.plus(clusterDirectories)
162 differenceList.removeAll(commonList)
163
164 if (!differenceList.isEmpty()) {
165 common.warningMsg("The following clusters are not valid : ${differenceList} - That means we cannot found cluster_name in init.yml or infra/init.yml")
166 }
167 if (modifiedClusters) {
168 infraYMLs.removeAll { !modifiedClusters.contains(it.tokenize('/')[3]) }
169 common.infoMsg("Testing only modified clusters: ${infraYMLs}")
170 }
171 }
172 common.infoMsg("Starting salt models test for these clusters " + infraYMLs.collect{ it.tokenize("/")[3] })
173 if (infraYMLs.size() > 0) {
174 for (int i = 0; i < infraYMLs.size(); i++) {
175 def infraYMLConfig = readYaml(file: infraYMLs[i])
176 if (_clusterTestEnabled(infraYMLConfig)) {
177 if(!infraYMLConfig["parameters"].containsKey("_param")){
178 common.warningMsg("ERROR: Cannot find soft params (_param) in file " + infraYMLs[i] + " for obtain a cluster info. Skipping test.")
179 continue
180 }
181 def infraParams = infraYMLConfig["parameters"]["_param"];
182 if(!infraParams.containsKey("infra_config_hostname") || !infraParams.containsKey("cluster_name") || !infraParams.containsKey("cluster_domain")){
183 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.")
184 continue
185 }
186 def clusterName = infraParams["cluster_name"]
187 def clusterDomain = infraParams["cluster_domain"]
188 def configHostname = infraParams["infra_config_hostname"]
189 def testTarget = String.format("%s.%s", configHostname, clusterDomain)
190
191 futureNodes << [defaultGitUrl, defaultGitRef, clusterName, testTarget, formulasSource]
192 }
193 }
194 } else {
195 common.warningMsg("List of found salt model clusters is empty, no tests will be started!")
196 }
197
Jakub Josefa63f9862018-01-11 17:58:38 +0100198 setupRunner()
199
200 if (failedNodes) {
201 currentBuild.result = "FAILURE"
202 }
203 }
204 }
205 } catch (Throwable e) {
206 currentBuild.result = "FAILURE"
207 currentBuild.description = currentBuild.description ? e.message + " " + currentBuild.description : e.message
208 throw e
209 } finally {
210 common.sendNotification(currentBuild.result,"",["slack"])
chnyda51b03142017-05-10 17:15:27 +0200211 }
Jakub Josefc5a223a2017-03-01 14:40:08 +0100212 }
Tomáš Kukrál500c0182017-05-11 13:46:19 +0200213}