blob: 5039661e37f8ca86a578b48cc3e9462ae3141425 [file] [log] [blame]
Denis Egorenko358a4fc2018-11-27 17:57:22 +04001/*
2 Global wrapper for testing next projects:
3 - salt-models/reclass-system
4 - mk/cookiecutter-templates
5
6 Can be triggered manually or by gerrit trigger:
7 1) gerrit trigger
8 Automatically switches if GERRIT_PROJECT variable detected
9 Always test GERRIT_REFSPEC VS GERRIT_BRANCH-master version of opposite project
10
11 2) manual run via job-build , possible to pass refspecs
12 - for CC
13 - Reclass
14
15 Example of TEST_PARAMETERS_YAML manual config:
16---
17RECLASS_SYSTEM_URL: ssh://mcp-jenkins@gerrit.mcp.mirantis.net:29418/salt-models/reclass-system
18RECLASS_SYSTEM_GIT_REF: 2018.11.0
19RECLASS_SYSTEM_BRANCH: refs/heads/2018.11.0
20COOKIECUTTER_TEMPLATE_URL: ssh://mcp-jenkins@gerrit.mcp.mirantis.net:29418/mk/cookiecutter-templates
21COOKIECUTTER_TEMPLATE_REF: refs/heads/2018.11.0
22COOKIECUTTER_TEMPLATE_BRANCH: 2018.11.0
23DISTRIB_REVISION: 2018.11.0
24TEST_MODELS: ''
25
26 */
27
28import groovy.json.JsonOutput
Denis Egorenko6c325112018-12-05 19:58:34 +040029gerrit = new com.mirantis.mk.Gerrit()
Denis Egorenko358a4fc2018-11-27 17:57:22 +040030
31cookiecutterTemplatesRepo='mk/cookiecutter-templates'
32reclassSystemRepo='salt-models/reclass-system'
33slaveNode = env.getProperty('SLAVE_NODE') ?: 'python&&docker'
34
Denis Egorenko76a0be82018-12-04 13:36:07 +040035voteMatrix = [
36 'test-mk-cookiecutter-templates': true,
37 'test-drivetrain': true,
38 'oscore-test-cookiecutter-models': false,
39 'test-salt-model-infra': true,
40 'test-salt-model-mcp-virtual-lab': true,
41]
42
Denis Egorenko6c325112018-12-05 19:58:34 +040043baseGerritConfig = [:]
44
Denis Egorenko358a4fc2018-11-27 17:57:22 +040045LinkedHashMap getManualRefParams(LinkedHashMap map) {
46 LinkedHashMap manualParams = [:]
47 String defaultGitRef = 'HEAD'
48 if (map.containsKey('RECLASS_SYSTEM_GIT_REF') && map.containsKey('RECLASS_SYSTEM_URL')) {
49 manualParams[reclassSystemRepo] = [
50 'url': map.get('RECLASS_SYSTEM_URL'),
51 'ref': map.get('RECLASS_SYSTEM_GIT_REF'),
52 'branch': map.get('RECLASS_SYSTEM_BRANCH', 'master'),
53 ]
54 }
55 if (map.containsKey('COOKIECUTTER_TEMPLATE_REF') && map.containsKey('COOKIECUTTER_TEMPLATE_URL')) {
56 manualParams[cookiecutterTemplatesRepo] = [
57 'url': map.get('COOKIECUTTER_TEMPLATE_URL'),
58 'ref': map.get('COOKIECUTTER_TEMPLATE_REF'),
59 'branch': map.get('COOKIECUTTER_TEMPLATE_BRANCH', 'master'),
60 ]
61 }
62 return manualParams
63}
64
Denis Egorenko6c325112018-12-05 19:58:34 +040065def setGerritReviewComment(String jobName, String jobBuildURL, String jobStatus) {
66 if (baseGerritConfig) {
67 String skipped = voteMatrix.get(jobName, 'true') ? '' : '(skipped)'
68 LinkedHashMap config = baseGerritConfig.clone()
69 config['message'] = "- ${jobName} ${jobBuildURL}console : ${jobStatus} ${skipped}".trim()
70 gerrit.postGerritComment(config)
71 }
Denis Egorenko76a0be82018-12-04 13:36:07 +040072}
73
74def runTests(String jobName, String extraVars) {
75 def propagateStatus = voteMatrix.get(jobName, true)
Denis Egorenko358a4fc2018-11-27 17:57:22 +040076 return {
Denis Egorenko76a0be82018-12-04 13:36:07 +040077 def jobBuild = build job: "${jobName}", propagate: false, parameters: [
78 [$class: 'TextParameterValue', name: 'EXTRA_VARIABLES_YAML', value: extraVars ]
79 ]
Denis Egorenko6c325112018-12-05 19:58:34 +040080 setGerritReviewComment(jobName, jobBuild.absoluteUrl, jobBuild.result)
Denis Egorenko76a0be82018-12-04 13:36:07 +040081 if (propagateStatus && jobBuild.result == 'FAILURE') {
82 throw new Exception("Build ${jobName} is failed!")
Denis Egorenko358a4fc2018-11-27 17:57:22 +040083 }
84 }
85}
86
87def runTestSaltModelReclass(String cluster, String defaultGitUrl, String clusterGitUrl, String refSpec) {
Denis Egorenko76a0be82018-12-04 13:36:07 +040088 def saltModelJob = "test-salt-model-${cluster}"
89 def propagateStatus = voteMatrix.get(saltModelJob, true)
Denis Egorenko358a4fc2018-11-27 17:57:22 +040090 return {
Denis Egorenko76a0be82018-12-04 13:36:07 +040091 def jobBuild = build job: saltModelJob, propagate: false, parameters: [
Denis Egorenko358a4fc2018-11-27 17:57:22 +040092 [$class: 'StringParameterValue', name: 'DEFAULT_GIT_URL', value: clusterGitUrl],
93 [$class: 'StringParameterValue', name: 'DEFAULT_GIT_REF', value: "HEAD"],
94 [$class: 'StringParameterValue', name: 'SYSTEM_GIT_URL', value: defaultGitUrl],
95 [$class: 'StringParameterValue', name: 'SYSTEM_GIT_REF', value: refSpec ],
96 ]
Denis Egorenko6c325112018-12-05 19:58:34 +040097 setGerritReviewComment(saltModelJob, jobBuild.absoluteUrl, jobBuild.result)
Denis Egorenko76a0be82018-12-04 13:36:07 +040098 if (propagateStatus && jobBuild.result == 'FAILURE') {
99 throw new Exception("Build ${saltModelJob} is failed!")
100 }
Denis Egorenko358a4fc2018-11-27 17:57:22 +0400101 }
102}
103
Denis Egorenko6c325112018-12-05 19:58:34 +0400104def checkReclassSystemDocumentationCommit(gerritCredentials) {
105 gerrit.gerritPatchsetCheckout([
Denis Egorenko358a4fc2018-11-27 17:57:22 +0400106 credentialsId: gerritCredentials
107 ])
108
109 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'))\" \\;")
110
111 return sh(script: "git diff-tree --no-commit-id --name-only -r HEAD | grep -v .releasenotes", returnStatus: true) == 1
112
113}
114
115timeout(time: 12, unit: 'HOURS') {
116 node(slaveNode) {
117 def common = new com.mirantis.mk.Common()
Denis Egorenko358a4fc2018-11-27 17:57:22 +0400118 def git = new com.mirantis.mk.Git()
119 def python = new com.mirantis.mk.Python()
120
121 // Var TEST_PARAMETERS_YAML contains any additional parameters for tests,
122 // like manually specified Gerrit Refs/URLs, additional parameters and so on
Denis Egorenko76a0be82018-12-04 13:36:07 +0400123 def buildTestParams = [:]
124 def buildTestParamsYaml = env.getProperty('TEST_PARAMETERS_YAML')
125 if (buildTestParamsYaml) {
126 common.mergeEnv(env, buildTestParamsYaml)
127 buildTestParams = readYaml text: buildTestParamsYaml
Denis Egorenko358a4fc2018-11-27 17:57:22 +0400128 }
129
130 // init required job variables
131 LinkedHashMap job_env = env.getEnvironment().findAll { k, v -> v }
132
133 // Gerrit parameters
134 String gerritCredentials = job_env.get('CREDENTIALS_ID', 'gerrit')
135 String gerritRef = job_env.get('GERRIT_REFSPEC', '')
136 String gerritProject = ''
137 String gerritName = ''
138 String gerritScheme = ''
139 String gerritHost = ''
140 String gerritPort = ''
141 String gerritChangeNumber = ''
142
143 // Common and manual build parameters
144 LinkedHashMap projectsMap = [:]
Denis Egorenkoff770c62018-12-03 17:45:34 +0400145 String distribRevision = job_env.get('DISTRIB_REVISION', 'nightly')
Denis Egorenko358a4fc2018-11-27 17:57:22 +0400146 ArrayList testModels = job_env.get('TEST_MODELS', 'mcp-virtual-lab,infra').split(',')
147
148 stage('Check build mode') {
149 def buildType = ''
150 if (gerritRef) {
151 // job is triggered by Gerrit, get all required Gerrit vars
152 gerritProject = job_env.get('GERRIT_PROJECT')
153 gerritName = job_env.get('GERRIT_NAME')
154 gerritScheme = job_env.get('GERRIT_SCHEME')
155 gerritHost = job_env.get('GERRIT_HOST')
156 gerritPort = job_env.get('GERRIT_PORT')
157 gerritChangeNumber = job_env.get('GERRIT_CHANGE_NUMBER')
Denis Egorenko6c325112018-12-05 19:58:34 +0400158 gerritPatchSetNumber = job_env.get('GERRIT_PATCHSET_NUMBER')
Denis Egorenko358a4fc2018-11-27 17:57:22 +0400159 gerritBranch = job_env.get('GERRIT_BRANCH')
160
161 // check if change aren't already merged
162 def gerritChange = gerrit.getGerritChange(gerritName, gerritHost, gerritChangeNumber, gerritCredentials)
163 if (gerritChange.status == "MERGED") {
164 common.successMsg('Patch set is alredy merged, no need to test it')
165 currentBuild.result = 'SUCCESS'
166 return
167 }
168
169 projectsMap[gerritProject] = [
170 'url': "${gerritScheme}://${gerritName}@${gerritHost}:${gerritPort}/${gerritProject}",
171 'ref': gerritRef,
172 'branch': gerritBranch,
173 ]
174 buildType = 'Gerrit Trigger'
Denis Egorenko76a0be82018-12-04 13:36:07 +0400175 buildTestParams << job_env.findAll { k,v -> k ==~ /GERRIT_.+/ }
Denis Egorenko6c325112018-12-05 19:58:34 +0400176 baseGerritConfig = [
177 'gerritName': gerritName,
178 'gerritHost': gerritHost,
179 'gerritChangeNumber': gerritChangeNumber,
180 'credentialsId': gerritCredentials,
181 'gerritPatchSetNumber': gerritPatchSetNumber,
182 ]
Denis Egorenko358a4fc2018-11-27 17:57:22 +0400183 } else {
184 projectsMap = getManualRefParams(job_env)
185 if (!projectsMap) {
186 error('Manual build detected and no valid Git refs provided!')
187 }
188 buildType = 'Manual build'
189 }
190 ArrayList descriptionMsgs = [ "<font color='red'>${buildType} detected!</font> Running with next parameters:" ]
191 for(String project in projectsMap.keySet()) {
Denis Egorenko7a806bb2018-12-03 14:37:05 +0400192 descriptionMsgs.add("Ref for ${project} => ${projectsMap[project]['ref']}")
193 descriptionMsgs.add("Branch for ${project} => ${projectsMap[project]['branch']}")
Denis Egorenko358a4fc2018-11-27 17:57:22 +0400194 }
195 descriptionMsgs.add("Distrib revision => ${distribRevision}")
Denis Egorenko76a0be82018-12-04 13:36:07 +0400196 currentBuild.description = descriptionMsgs.join('<br/>')
Denis Egorenko358a4fc2018-11-27 17:57:22 +0400197 }
198
199 stage("Run tests") {
200 def branches = [:]
Denis Egorenko358a4fc2018-11-27 17:57:22 +0400201
202 if (projectsMap.containsKey(reclassSystemRepo)) {
Denis Egorenko6c325112018-12-05 19:58:34 +0400203 def documentationOnly = checkReclassSystemDocumentationCommit(gerritCredentials)
Denis Egorenko358a4fc2018-11-27 17:57:22 +0400204 if (['master'].contains(gerritBranch) && !documentationOnly) {
205 for (int i = 0; i < testModels.size(); i++) {
206 def cluster = testModels[i]
Denis Egorenko76a0be82018-12-04 13:36:07 +0400207 def clusterGitUrl = projectsMap[reclassSystemRepo]['url'].substring(0, projectsMap[reclassSystemRepo]['url'].lastIndexOf("/") + 1) + cluster
Denis Egorenko7a806bb2018-12-03 14:37:05 +0400208 branches["reclass-system-${cluster}"] = runTestSaltModelReclass(cluster, projectsMap[reclassSystemRepo]['url'], clusterGitUrl, projectsMap[reclassSystemRepo]['ref'])
Denis Egorenko358a4fc2018-11-27 17:57:22 +0400209 }
210 } else {
211 common.warningMsg("Tests for ${testModels} skipped!")
212 }
213 }
214 if (projectsMap.containsKey(reclassSystemRepo) || projectsMap.containsKey(cookiecutterTemplatesRepo)) {
Denis Egorenko76a0be82018-12-04 13:36:07 +0400215 branches['cookiecutter-templates'] = runTests('test-mk-cookiecutter-templates', JsonOutput.toJson(buildTestParams))
Denis Egorenko358a4fc2018-11-27 17:57:22 +0400216 }
217 if (projectsMap.containsKey(cookiecutterTemplatesRepo)) {
Denis Egorenko76a0be82018-12-04 13:36:07 +0400218 branches['test-drivetrain'] = runTests('test-drivetrain', JsonOutput.toJson(buildTestParams))
219 // TODO: enable oscore-test job once it's ready to consume EXTRA_VARIABLES_YAML
220 //branches['oscore-test-cookiecutter-models'] = runTests('oscore-test-cookiecutter-models', JsonOutput.toJson(buildTestParams))
Denis Egorenko358a4fc2018-11-27 17:57:22 +0400221 }
222
Denis Egorenko76a0be82018-12-04 13:36:07 +0400223 try {
224 parallel branches
225 } catch (Exception e) {
226 println e
227 println 'Job is in non-voting mode for now. Skipping fails.'
228 }
Denis Egorenko358a4fc2018-11-27 17:57:22 +0400229 }
230 }
231}