blob: e8941ea1b60b0bb3b2902c987bf5ecd2cafbdf03 [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 Egorenko358a4fc2018-11-27 17:57:22 +04005 */
6
7import groovy.json.JsonOutput
Denis Egorenko6c325112018-12-05 19:58:34 +04008gerrit = new com.mirantis.mk.Gerrit()
Denis Egorenko358a4fc2018-11-27 17:57:22 +04009
10cookiecutterTemplatesRepo='mk/cookiecutter-templates'
11reclassSystemRepo='salt-models/reclass-system'
12slaveNode = env.getProperty('SLAVE_NODE') ?: 'python&&docker'
13
Denis Egorenko76a0be82018-12-04 13:36:07 +040014voteMatrix = [
15 'test-mk-cookiecutter-templates': true,
16 'test-drivetrain': true,
17 'oscore-test-cookiecutter-models': false,
18 'test-salt-model-infra': true,
19 'test-salt-model-mcp-virtual-lab': true,
20]
21
Denis Egorenko6c325112018-12-05 19:58:34 +040022baseGerritConfig = [:]
Denis Egorenkof4d3f522018-12-06 16:33:11 +040023jobResultComments = [:]
24commentLock = false
Denis Egorenko6c325112018-12-05 19:58:34 +040025
Denis Egorenko86164872018-12-10 17:34:46 +040026// post Gerrit review comment to patch
27def setGerritReviewComment() {
Denis Egorenko6c325112018-12-05 19:58:34 +040028 if (baseGerritConfig) {
Denis Egorenkof4d3f522018-12-06 16:33:11 +040029 while(commentLock) {
30 sleep 5
31 }
32 commentLock = true
Denis Egorenko6c325112018-12-05 19:58:34 +040033 LinkedHashMap config = baseGerritConfig.clone()
Denis Egorenkof4d3f522018-12-06 16:33:11 +040034 String jobResultComment = ''
35 jobResultComments.each { job, info ->
Denis Egorenko86164872018-12-10 17:34:46 +040036 String skipped = voteMatrix.get(job, 'true') ? '' : '(non-voting)'
Denis Egorenkof4d3f522018-12-06 16:33:11 +040037 jobResultComment += "- ${job} ${info.url}console : ${info.status} ${skipped}".trim() + '\n'
38 }
39 config['message'] = sh(script: "echo '${jobResultComment}'", returnStdout: true).trim()
Denis Egorenko6c325112018-12-05 19:58:34 +040040 gerrit.postGerritComment(config)
Denis Egorenkof4d3f522018-12-06 16:33:11 +040041 commentLock = false
Denis Egorenko6c325112018-12-05 19:58:34 +040042 }
Denis Egorenko76a0be82018-12-04 13:36:07 +040043}
44
Denis Egorenko86164872018-12-10 17:34:46 +040045// get job parameters for YAML-based job parametrization
46def yamlJobParameters(LinkedHashMap jobParams) {
47 return [
48 [$class: 'TextParameterValue', name: 'EXTRA_VARIABLES_YAML', value: JsonOutput.toJson(jobParams) ]
49 ]
50}
51
52// run needed job with params
53def runTests(String jobName, ArrayList jobParams) {
Denis Egorenko76a0be82018-12-04 13:36:07 +040054 def propagateStatus = voteMatrix.get(jobName, true)
Denis Egorenko358a4fc2018-11-27 17:57:22 +040055 return {
Denis Egorenko86164872018-12-10 17:34:46 +040056 def jobBuild = build job: jobName, propagate: false, parameters: jobParams
Denis Egorenkof4d3f522018-12-06 16:33:11 +040057 jobResultComments[jobName] = [ 'url': jobBuild.absoluteUrl, 'status': jobBuild.result ]
58 setGerritReviewComment()
Denis Egorenko76a0be82018-12-04 13:36:07 +040059 if (propagateStatus && jobBuild.result == 'FAILURE') {
60 throw new Exception("Build ${jobName} is failed!")
Denis Egorenko358a4fc2018-11-27 17:57:22 +040061 }
62 }
63}
64
Denis Egorenko358a4fc2018-11-27 17:57:22 +040065timeout(time: 12, unit: 'HOURS') {
66 node(slaveNode) {
67 def common = new com.mirantis.mk.Common()
Denis Egorenko358a4fc2018-11-27 17:57:22 +040068 def git = new com.mirantis.mk.Git()
69 def python = new com.mirantis.mk.Python()
70
Denis Egorenko28f8c812018-12-10 16:06:37 +040071 // Var EXTRA_VARIABLES_YAML contains any additional parameters for tests,
Denis Egorenko358a4fc2018-11-27 17:57:22 +040072 // like manually specified Gerrit Refs/URLs, additional parameters and so on
Denis Egorenko76a0be82018-12-04 13:36:07 +040073 def buildTestParams = [:]
Denis Egorenko28f8c812018-12-10 16:06:37 +040074 def buildTestParamsYaml = env.getProperty('EXTRA_VARIABLES_YAML')
Denis Egorenko76a0be82018-12-04 13:36:07 +040075 if (buildTestParamsYaml) {
76 common.mergeEnv(env, buildTestParamsYaml)
77 buildTestParams = readYaml text: buildTestParamsYaml
Denis Egorenko358a4fc2018-11-27 17:57:22 +040078 }
79
80 // init required job variables
81 LinkedHashMap job_env = env.getEnvironment().findAll { k, v -> v }
82
83 // Gerrit parameters
84 String gerritCredentials = job_env.get('CREDENTIALS_ID', 'gerrit')
Denis Egorenko86164872018-12-10 17:34:46 +040085 String gerritRef = job_env.get('GERRIT_REFSPEC')
86 String gerritProject = job_env.get('GERRIT_PROJECT')
87 String gerritName = job_env.get('GERRIT_NAME')
88 String gerritScheme = job_env.get('GERRIT_SCHEME')
89 String gerritHost = job_env.get('GERRIT_HOST')
90 String gerritPort = job_env.get('GERRIT_PORT')
91 String gerritChangeNumber = job_env.get('GERRIT_CHANGE_NUMBER')
92 String gerritPatchSetNumber = job_env.get('GERRIT_PATCHSET_NUMBER')
93 String gerritBranch = job_env.get('GERRIT_BRANCH')
Denis Egorenkoe581f9e2018-12-11 18:56:14 +040094 Boolean gateMode = job_env.get('GERRIT_CI_MERGE_TRIGGER', false).toBoolean()
Denis Egorenko358a4fc2018-11-27 17:57:22 +040095
96 // Common and manual build parameters
97 LinkedHashMap projectsMap = [:]
Denis Egorenko86164872018-12-10 17:34:46 +040098 String distribRevision = 'nightly'
99 //checking if the branch is from release
100 if (gerritBranch.startsWith('release')) {
101 def distribRevisionRelease = gerritBranch.tokenize('/')[-1]
102 if (!common.checkRemoteBinary([apt_mk_version: distribRevisionRelease]).linux_system_repo_url) {
103 common.infoMsg("Binary release ${distribRevisionRelease} does not exist on http://mirror.mirantis.com. Falling back to 'proposed'.")
104 distribRevision = 'proposed'
105 }
106 distribRevision = distribRevisionRelease
107 }
Denis Egorenko358a4fc2018-11-27 17:57:22 +0400108 ArrayList testModels = job_env.get('TEST_MODELS', 'mcp-virtual-lab,infra').split(',')
109
Denis Egorenko86164872018-12-10 17:34:46 +0400110 stage('Gerrit prepare') {
111 // check if change aren't already merged
112 def gerritChange = gerrit.getGerritChange(gerritName, gerritHost, gerritChangeNumber, gerritCredentials)
113 if (gerritChange.status == "MERGED") {
114 common.successMsg('Patch set is alredy merged, no need to test it')
115 currentBuild.result = 'SUCCESS'
116 return
Denis Egorenko358a4fc2018-11-27 17:57:22 +0400117 }
Denis Egorenko86164872018-12-10 17:34:46 +0400118 def defaultURL = "${gerritScheme}://${gerritName}@${gerritHost}:${gerritPort}"
119 projectsMap[gerritProject] = [
120 'url': "${defaultURL}/${gerritProject}",
121 'ref': gerritRef,
122 'branch': gerritBranch,
123 ]
124 buildType = 'Gerrit Trigger'
125 buildTestParams << job_env.findAll { k,v -> k ==~ /GERRIT_.+/ }
126 baseGerritConfig = [
127 'gerritName': gerritName,
128 'gerritHost': gerritHost,
129 'gerritChangeNumber': gerritChangeNumber,
130 'credentialsId': gerritCredentials,
131 'gerritPatchSetNumber': gerritPatchSetNumber,
132 ]
133 ArrayList descriptionMsgs = [ "Running with next parameters:" ]
Denis Egorenko358a4fc2018-11-27 17:57:22 +0400134 for(String project in projectsMap.keySet()) {
Denis Egorenko7a806bb2018-12-03 14:37:05 +0400135 descriptionMsgs.add("Ref for ${project} => ${projectsMap[project]['ref']}")
136 descriptionMsgs.add("Branch for ${project} => ${projectsMap[project]['branch']}")
Denis Egorenko358a4fc2018-11-27 17:57:22 +0400137 }
138 descriptionMsgs.add("Distrib revision => ${distribRevision}")
Denis Egorenko76a0be82018-12-04 13:36:07 +0400139 currentBuild.description = descriptionMsgs.join('<br/>')
Denis Egorenko86164872018-12-10 17:34:46 +0400140
141 gerrit.gerritPatchsetCheckout([
142 credentialsId: gerritCredentials
143 ])
144 }
145
Denis Egorenko358a4fc2018-11-27 17:57:22 +0400146 stage("Run tests") {
Denis Egorenko86164872018-12-10 17:34:46 +0400147 def documentationOnly = sh(script: "git diff-tree --no-commit-id --name-only -r HEAD | grep -v .releasenotes", returnStatus: true) == 1
148 if (documentationOnly) {
149 common.infoMsg("Tests skipped, documenation only changed!")
150 currentBuild.result = 'SUCCESS'
151 return
152 }
153
Denis Egorenko358a4fc2018-11-27 17:57:22 +0400154 def branches = [:]
Denis Egorenkof4d3f522018-12-06 16:33:11 +0400155 String branchJobName = ''
Denis Egorenko358a4fc2018-11-27 17:57:22 +0400156
Denis Egorenko86164872018-12-10 17:34:46 +0400157 if (gerritProject == reclassSystemRepo && gerritBranch == 'master') {
Denis Egorenkobbc12682018-12-11 18:38:43 +0400158 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 Egorenko86164872018-12-10 17:34:46 +0400159 for (int i = 0; i < testModels.size(); i++) {
160 def cluster = testModels[i]
161 def clusterGitUrl = projectsMap[reclassSystemRepo]['url'].substring(0, projectsMap[reclassSystemRepo]['url'].lastIndexOf("/") + 1) + cluster
162 branchJobName = "test-salt-model-${cluster}"
163 def jobParams = [
164 [$class: 'StringParameterValue', name: 'DEFAULT_GIT_URL', value: clusterGitUrl],
165 [$class: 'StringParameterValue', name: 'DEFAULT_GIT_REF', value: "HEAD"],
166 [$class: 'StringParameterValue', name: 'SYSTEM_GIT_URL', value: projectsMap[reclassSystemRepo]['url']],
167 [$class: 'StringParameterValue', name: 'SYSTEM_GIT_REF', value: projectsMap[reclassSystemRepo]['ref'] ],
168 ]
169 branches[branchJobName] = runTests(branchJobName, jobParams)
Denis Egorenko358a4fc2018-11-27 17:57:22 +0400170 }
171 }
Denis Egorenko86164872018-12-10 17:34:46 +0400172 if (gerritProject == reclassSystemRepo || gerritProject == cookiecutterTemplatesRepo) {
Denis Egorenkof4d3f522018-12-06 16:33:11 +0400173 branchJobName = 'test-mk-cookiecutter-templates'
Denis Egorenko86164872018-12-10 17:34:46 +0400174 branches[branchJobName] = runTests(branchJobName, yamlJobParameters(buildTestParams))
Denis Egorenko358a4fc2018-11-27 17:57:22 +0400175 }
Denis Egorenko28f8c812018-12-10 16:06:37 +0400176
177 if (!gateMode) {
178 if (gerritProject == cookiecutterTemplatesRepo) {
179 branchJobName = 'test-drivetrain'
180 branches[branchJobName] = runTests(branchJobName, yamlJobParameters(buildTestParams))
181 branchJobName = 'oscore-test-cookiecutter-models'
182 branches[branchJobName] = runTests(branchJobName, yamlJobParameters(buildTestParams))
183 }
Denis Egorenko358a4fc2018-11-27 17:57:22 +0400184 }
185
Denis Egorenkof4d3f522018-12-06 16:33:11 +0400186 branches.keySet().each { key ->
187 if (branches[key] instanceof Closure) {
188 jobResultComments[key] = [ 'url': job_env.get('BUILD_URL'), 'status': 'WAITING' ]
189 }
190 }
Denis Egorenko86164872018-12-10 17:34:46 +0400191 setGerritReviewComment()
Denis Egorenkod8d13e82018-12-07 16:16:31 +0400192 parallel branches
Denis Egorenko358a4fc2018-11-27 17:57:22 +0400193 }
194 }
195}