blob: cb16cf34026f5fab8bf7b1cc91edd0b783c05275 [file] [log] [blame]
Denis Egorenko358a4fc2018-11-27 17:57:22 +04001/*
Denis Egorenko86164872018-12-10 17:34:46 +04002 Global CI wrapper for testing next projects:
Denis Egorenko358a4fc2018-11-27 17:57:22 +04003 - salt-models/reclass-system
4 - mk/cookiecutter-templates
Denis Egorenkod71aaa02018-12-11 16:03:37 +04005
6 Wrapper allows to test cross-project patches, based on
7 'Depends-On: http://<gerrit_address>/<change_number>' key phrase
Denis Egorenko358a4fc2018-11-27 17:57:22 +04008 */
9
10import groovy.json.JsonOutput
Denis Egorenko6c325112018-12-05 19:58:34 +040011gerrit = new com.mirantis.mk.Gerrit()
Denis Egorenko358a4fc2018-11-27 17:57:22 +040012
13cookiecutterTemplatesRepo='mk/cookiecutter-templates'
14reclassSystemRepo='salt-models/reclass-system'
Denis Egorenko6c6b08e2018-12-13 13:23:43 +040015slaveNode = env.getProperty('SLAVE_NODE') ?: 'virtual'
Denis Egorenko358a4fc2018-11-27 17:57:22 +040016
Denis Egorenko76a0be82018-12-04 13:36:07 +040017voteMatrix = [
18 'test-mk-cookiecutter-templates': true,
19 'test-drivetrain': true,
20 'oscore-test-cookiecutter-models': false,
21 'test-salt-model-infra': true,
22 'test-salt-model-mcp-virtual-lab': true,
23]
24
Denis Egorenko6c325112018-12-05 19:58:34 +040025baseGerritConfig = [:]
Denis Egorenkod71aaa02018-12-11 16:03:37 +040026buildTestParams = [:]
Denis Egorenkof4d3f522018-12-06 16:33:11 +040027jobResultComments = [:]
28commentLock = false
Denis Egorenko6c325112018-12-05 19:58:34 +040029
Denis Egorenko86164872018-12-10 17:34:46 +040030// post Gerrit review comment to patch
31def setGerritReviewComment() {
Denis Egorenko6c325112018-12-05 19:58:34 +040032 if (baseGerritConfig) {
Denis Egorenkof4d3f522018-12-06 16:33:11 +040033 while(commentLock) {
34 sleep 5
35 }
36 commentLock = true
Denis Egorenko6c325112018-12-05 19:58:34 +040037 LinkedHashMap config = baseGerritConfig.clone()
Denis Egorenkof4d3f522018-12-06 16:33:11 +040038 String jobResultComment = ''
39 jobResultComments.each { job, info ->
Denis Egorenko86164872018-12-10 17:34:46 +040040 String skipped = voteMatrix.get(job, 'true') ? '' : '(non-voting)'
Denis Egorenkof4d3f522018-12-06 16:33:11 +040041 jobResultComment += "- ${job} ${info.url}console : ${info.status} ${skipped}".trim() + '\n'
42 }
43 config['message'] = sh(script: "echo '${jobResultComment}'", returnStdout: true).trim()
Denis Egorenko6c325112018-12-05 19:58:34 +040044 gerrit.postGerritComment(config)
Denis Egorenkof4d3f522018-12-06 16:33:11 +040045 commentLock = false
Denis Egorenko6c325112018-12-05 19:58:34 +040046 }
Denis Egorenko76a0be82018-12-04 13:36:07 +040047}
48
Denis Egorenko86164872018-12-10 17:34:46 +040049// get job parameters for YAML-based job parametrization
50def yamlJobParameters(LinkedHashMap jobParams) {
51 return [
52 [$class: 'TextParameterValue', name: 'EXTRA_VARIABLES_YAML', value: JsonOutput.toJson(jobParams) ]
53 ]
54}
55
56// run needed job with params
57def runTests(String jobName, ArrayList jobParams) {
Denis Egorenko76a0be82018-12-04 13:36:07 +040058 def propagateStatus = voteMatrix.get(jobName, true)
Denis Egorenko358a4fc2018-11-27 17:57:22 +040059 return {
Denis Egorenko86164872018-12-10 17:34:46 +040060 def jobBuild = build job: jobName, propagate: false, parameters: jobParams
Denis Egorenkof4d3f522018-12-06 16:33:11 +040061 jobResultComments[jobName] = [ 'url': jobBuild.absoluteUrl, 'status': jobBuild.result ]
62 setGerritReviewComment()
Denis Egorenko76a0be82018-12-04 13:36:07 +040063 if (propagateStatus && jobBuild.result == 'FAILURE') {
64 throw new Exception("Build ${jobName} is failed!")
Denis Egorenko358a4fc2018-11-27 17:57:22 +040065 }
66 }
67}
68
Denis Egorenkod71aaa02018-12-11 16:03:37 +040069// set params based on depending patches
70def setupDependingVars(LinkedHashMap dependingProjects) {
71 if (dependingProjects) {
72 if (dependingProjects.containsKey(reclassSystemRepo)) {
73 buildTestParams['RECLASS_SYSTEM_GIT_REF'] = dependingProjects[reclassSystemRepo].ref
74 buildTestParams['RECLASS_SYSTEM_BRANCH'] = dependingProjects[reclassSystemRepo].branch
75 }
76 if (dependingProjects.containsKey(cookiecutterTemplatesRepo)) {
77 buildTestParams['COOKIECUTTER_TEMPLATE_REF'] = dependingProjects[cookiecutterTemplatesRepo].ref
78 buildTestParams['COOKIECUTTER_TEMPLATE_BRANCH'] = dependingProjects[cookiecutterTemplatesRepo].branch
79 }
80 }
81}
82
Denis Egorenko358a4fc2018-11-27 17:57:22 +040083timeout(time: 12, unit: 'HOURS') {
84 node(slaveNode) {
85 def common = new com.mirantis.mk.Common()
Denis Egorenko358a4fc2018-11-27 17:57:22 +040086
Denis Egorenko28f8c812018-12-10 16:06:37 +040087 // Var EXTRA_VARIABLES_YAML contains any additional parameters for tests,
Denis Egorenko358a4fc2018-11-27 17:57:22 +040088 // like manually specified Gerrit Refs/URLs, additional parameters and so on
Denis Egorenko28f8c812018-12-10 16:06:37 +040089 def buildTestParamsYaml = env.getProperty('EXTRA_VARIABLES_YAML')
Denis Egorenko76a0be82018-12-04 13:36:07 +040090 if (buildTestParamsYaml) {
91 common.mergeEnv(env, buildTestParamsYaml)
92 buildTestParams = readYaml text: buildTestParamsYaml
Denis Egorenko358a4fc2018-11-27 17:57:22 +040093 }
94
95 // init required job variables
96 LinkedHashMap job_env = env.getEnvironment().findAll { k, v -> v }
97
98 // Gerrit parameters
99 String gerritCredentials = job_env.get('CREDENTIALS_ID', 'gerrit')
Denis Egorenko86164872018-12-10 17:34:46 +0400100 String gerritRef = job_env.get('GERRIT_REFSPEC')
101 String gerritProject = job_env.get('GERRIT_PROJECT')
102 String gerritName = job_env.get('GERRIT_NAME')
103 String gerritScheme = job_env.get('GERRIT_SCHEME')
104 String gerritHost = job_env.get('GERRIT_HOST')
105 String gerritPort = job_env.get('GERRIT_PORT')
106 String gerritChangeNumber = job_env.get('GERRIT_CHANGE_NUMBER')
107 String gerritPatchSetNumber = job_env.get('GERRIT_PATCHSET_NUMBER')
108 String gerritBranch = job_env.get('GERRIT_BRANCH')
Denis Egorenkoe581f9e2018-12-11 18:56:14 +0400109 Boolean gateMode = job_env.get('GERRIT_CI_MERGE_TRIGGER', false).toBoolean()
Denis Egorenko358a4fc2018-11-27 17:57:22 +0400110
111 // Common and manual build parameters
112 LinkedHashMap projectsMap = [:]
Denis Egorenko86164872018-12-10 17:34:46 +0400113 String distribRevision = 'nightly'
114 //checking if the branch is from release
115 if (gerritBranch.startsWith('release')) {
116 def distribRevisionRelease = gerritBranch.tokenize('/')[-1]
117 if (!common.checkRemoteBinary([apt_mk_version: distribRevisionRelease]).linux_system_repo_url) {
118 common.infoMsg("Binary release ${distribRevisionRelease} does not exist on http://mirror.mirantis.com. Falling back to 'proposed'.")
119 distribRevision = 'proposed'
120 }
121 distribRevision = distribRevisionRelease
122 }
Denis Egorenko358a4fc2018-11-27 17:57:22 +0400123 ArrayList testModels = job_env.get('TEST_MODELS', 'mcp-virtual-lab,infra').split(',')
124
Denis Egorenko86164872018-12-10 17:34:46 +0400125 stage('Gerrit prepare') {
126 // check if change aren't already merged
127 def gerritChange = gerrit.getGerritChange(gerritName, gerritHost, gerritChangeNumber, gerritCredentials)
128 if (gerritChange.status == "MERGED") {
129 common.successMsg('Patch set is alredy merged, no need to test it')
130 currentBuild.result = 'SUCCESS'
131 return
Denis Egorenko358a4fc2018-11-27 17:57:22 +0400132 }
Denis Egorenko86164872018-12-10 17:34:46 +0400133 buildTestParams << job_env.findAll { k,v -> k ==~ /GERRIT_.+/ }
134 baseGerritConfig = [
135 'gerritName': gerritName,
136 'gerritHost': gerritHost,
137 'gerritChangeNumber': gerritChangeNumber,
138 'credentialsId': gerritCredentials,
139 'gerritPatchSetNumber': gerritPatchSetNumber,
140 ]
Denis Egorenkod71aaa02018-12-11 16:03:37 +0400141 LinkedHashMap gerritDependingProjects = gerrit.getDependentPatches(baseGerritConfig)
142 setupDependingVars(gerritDependingProjects)
143 ArrayList descriptionMsgs = [
144 "Running with next parameters:",
145 "Ref for ${gerritProject} => ${gerritRef}",
146 "Branch for ${gerritProject} => ${gerritBranch}"
147 ]
Denis Egorenko358a4fc2018-11-27 17:57:22 +0400148 descriptionMsgs.add("Distrib revision => ${distribRevision}")
Denis Egorenkod71aaa02018-12-11 16:03:37 +0400149 for(String project in gerritDependingProjects.keySet()) {
150 descriptionMsgs.add("---")
151 descriptionMsgs.add("Depending patch to ${project} found:")
152 descriptionMsgs.add("Ref for ${project} => ${gerritDependingProjects[project]['ref']}")
153 descriptionMsgs.add("Branch for ${project} => ${gerritDependingProjects[project]['branch']}")
154 }
Denis Egorenko76a0be82018-12-04 13:36:07 +0400155 currentBuild.description = descriptionMsgs.join('<br/>')
Denis Egorenko86164872018-12-10 17:34:46 +0400156 gerrit.gerritPatchsetCheckout([
157 credentialsId: gerritCredentials
158 ])
159 }
160
Denis Egorenko358a4fc2018-11-27 17:57:22 +0400161 stage("Run tests") {
Denis Egorenko86164872018-12-10 17:34:46 +0400162 def documentationOnly = sh(script: "git diff-tree --no-commit-id --name-only -r HEAD | grep -v .releasenotes", returnStatus: true) == 1
163 if (documentationOnly) {
164 common.infoMsg("Tests skipped, documenation only changed!")
165 currentBuild.result = 'SUCCESS'
166 return
167 }
168
Denis Egorenko358a4fc2018-11-27 17:57:22 +0400169 def branches = [:]
Vasyl Saienkob121eb82018-12-27 17:34:28 +0200170 branches.failFast = false
Denis Egorenkof4d3f522018-12-06 16:33:11 +0400171 String branchJobName = ''
Denis Egorenko358a4fc2018-11-27 17:57:22 +0400172
Denis Egorenko86164872018-12-10 17:34:46 +0400173 if (gerritProject == reclassSystemRepo && gerritBranch == 'master') {
Denis Egorenkobbc12682018-12-11 18:38:43 +0400174 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'))\" \\;")
Denis Egorenkod71aaa02018-12-11 16:03:37 +0400175 def defaultSystemURL = "${gerritScheme}://${gerritName}@${gerritHost}:${gerritPort}/${gerritProject}"
Denis Egorenko86164872018-12-10 17:34:46 +0400176 for (int i = 0; i < testModels.size(); i++) {
177 def cluster = testModels[i]
Denis Egorenkod71aaa02018-12-11 16:03:37 +0400178 def clusterGitUrl = defaultSystemURL.substring(0, defaultSystemURL.lastIndexOf("/") + 1) + cluster
Denis Egorenko86164872018-12-10 17:34:46 +0400179 branchJobName = "test-salt-model-${cluster}"
180 def jobParams = [
181 [$class: 'StringParameterValue', name: 'DEFAULT_GIT_URL', value: clusterGitUrl],
182 [$class: 'StringParameterValue', name: 'DEFAULT_GIT_REF', value: "HEAD"],
Denis Egorenkod71aaa02018-12-11 16:03:37 +0400183 [$class: 'StringParameterValue', name: 'SYSTEM_GIT_URL', value: defaultSystemURL ],
184 [$class: 'StringParameterValue', name: 'SYSTEM_GIT_REF', value: gerritRef ],
Denis Egorenko86164872018-12-10 17:34:46 +0400185 ]
186 branches[branchJobName] = runTests(branchJobName, jobParams)
Denis Egorenko358a4fc2018-11-27 17:57:22 +0400187 }
188 }
Denis Egorenko86164872018-12-10 17:34:46 +0400189 if (gerritProject == reclassSystemRepo || gerritProject == cookiecutterTemplatesRepo) {
Denis Egorenkof4d3f522018-12-06 16:33:11 +0400190 branchJobName = 'test-mk-cookiecutter-templates'
Denis Egorenko86164872018-12-10 17:34:46 +0400191 branches[branchJobName] = runTests(branchJobName, yamlJobParameters(buildTestParams))
Denis Egorenko358a4fc2018-11-27 17:57:22 +0400192 }
Denis Egorenko28f8c812018-12-10 16:06:37 +0400193
194 if (!gateMode) {
195 if (gerritProject == cookiecutterTemplatesRepo) {
196 branchJobName = 'test-drivetrain'
197 branches[branchJobName] = runTests(branchJobName, yamlJobParameters(buildTestParams))
198 branchJobName = 'oscore-test-cookiecutter-models'
199 branches[branchJobName] = runTests(branchJobName, yamlJobParameters(buildTestParams))
200 }
Denis Egorenko358a4fc2018-11-27 17:57:22 +0400201 }
202
Denis Egorenkof4d3f522018-12-06 16:33:11 +0400203 branches.keySet().each { key ->
204 if (branches[key] instanceof Closure) {
205 jobResultComments[key] = [ 'url': job_env.get('BUILD_URL'), 'status': 'WAITING' ]
206 }
207 }
Denis Egorenko86164872018-12-10 17:34:46 +0400208 setGerritReviewComment()
Denis Egorenkod8d13e82018-12-07 16:16:31 +0400209 parallel branches
Denis Egorenko358a4fc2018-11-27 17:57:22 +0400210 }
211 }
212}