blob: 6adc72050451ce20185097c02f5efde00c30368c [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
29
30cookiecutterTemplatesRepo='mk/cookiecutter-templates'
31reclassSystemRepo='salt-models/reclass-system'
32slaveNode = env.getProperty('SLAVE_NODE') ?: 'python&&docker'
33
34LinkedHashMap getManualRefParams(LinkedHashMap map) {
35 LinkedHashMap manualParams = [:]
36 String defaultGitRef = 'HEAD'
37 if (map.containsKey('RECLASS_SYSTEM_GIT_REF') && map.containsKey('RECLASS_SYSTEM_URL')) {
38 manualParams[reclassSystemRepo] = [
39 'url': map.get('RECLASS_SYSTEM_URL'),
40 'ref': map.get('RECLASS_SYSTEM_GIT_REF'),
41 'branch': map.get('RECLASS_SYSTEM_BRANCH', 'master'),
42 ]
43 }
44 if (map.containsKey('COOKIECUTTER_TEMPLATE_REF') && map.containsKey('COOKIECUTTER_TEMPLATE_URL')) {
45 manualParams[cookiecutterTemplatesRepo] = [
46 'url': map.get('COOKIECUTTER_TEMPLATE_URL'),
47 'ref': map.get('COOKIECUTTER_TEMPLATE_REF'),
48 'branch': map.get('COOKIECUTTER_TEMPLATE_BRANCH', 'master'),
49 ]
50 }
51 return manualParams
52}
53
54def runTests(String jobName, String extraVars, Boolean propagateStatus=true) {
55 return {
56 try {
57 build job: "${jobName}", parameters: [
58 [$class: 'TextParameterValue', name: 'EXTRA_VARIABLES_YAML', value: extraVars ]
59 ]
60 } catch (Exception e) {
61 if (propagateStatus) {
62 throw e
63 }
64 }
65 }
66}
67
68def runTestSaltModelReclass(String cluster, String defaultGitUrl, String clusterGitUrl, String refSpec) {
69 return {
70 build job: "test-salt-model-${cluster}", parameters: [
71 [$class: 'StringParameterValue', name: 'DEFAULT_GIT_URL', value: clusterGitUrl],
72 [$class: 'StringParameterValue', name: 'DEFAULT_GIT_REF', value: "HEAD"],
73 [$class: 'StringParameterValue', name: 'SYSTEM_GIT_URL', value: defaultGitUrl],
74 [$class: 'StringParameterValue', name: 'SYSTEM_GIT_REF', value: refSpec ],
75 ]
76 }
77}
78
79def checkReclassSystemDocumentationCommit(gerritLib, gerritCredentials) {
Denis Egorenko7a806bb2018-12-03 14:37:05 +040080 gerritLib.gerritPatchsetCheckout([
Denis Egorenko358a4fc2018-11-27 17:57:22 +040081 credentialsId: gerritCredentials
82 ])
83
84 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'))\" \\;")
85
86 return sh(script: "git diff-tree --no-commit-id --name-only -r HEAD | grep -v .releasenotes", returnStatus: true) == 1
87
88}
89
90timeout(time: 12, unit: 'HOURS') {
91 node(slaveNode) {
92 def common = new com.mirantis.mk.Common()
93 def gerrit = new com.mirantis.mk.Gerrit()
94 def git = new com.mirantis.mk.Git()
95 def python = new com.mirantis.mk.Python()
96
97 // Var TEST_PARAMETERS_YAML contains any additional parameters for tests,
98 // like manually specified Gerrit Refs/URLs, additional parameters and so on
99 if (env.getProperty('TEST_PARAMETERS_YAML')) {
100 common.mergeEnv(env, env.getProperty('TEST_PARAMETERS_YAML'))
101 }
102
103 // init required job variables
104 LinkedHashMap job_env = env.getEnvironment().findAll { k, v -> v }
105
106 // Gerrit parameters
107 String gerritCredentials = job_env.get('CREDENTIALS_ID', 'gerrit')
108 String gerritRef = job_env.get('GERRIT_REFSPEC', '')
109 String gerritProject = ''
110 String gerritName = ''
111 String gerritScheme = ''
112 String gerritHost = ''
113 String gerritPort = ''
114 String gerritChangeNumber = ''
115
116 // Common and manual build parameters
117 LinkedHashMap projectsMap = [:]
118 String distribRevision = job_env('DISTRIB_REVISION', 'nightly')
119 ArrayList testModels = job_env.get('TEST_MODELS', 'mcp-virtual-lab,infra').split(',')
120
121 stage('Check build mode') {
122 def buildType = ''
123 if (gerritRef) {
124 // job is triggered by Gerrit, get all required Gerrit vars
125 gerritProject = job_env.get('GERRIT_PROJECT')
126 gerritName = job_env.get('GERRIT_NAME')
127 gerritScheme = job_env.get('GERRIT_SCHEME')
128 gerritHost = job_env.get('GERRIT_HOST')
129 gerritPort = job_env.get('GERRIT_PORT')
130 gerritChangeNumber = job_env.get('GERRIT_CHANGE_NUMBER')
131 gerritBranch = job_env.get('GERRIT_BRANCH')
132
133 // check if change aren't already merged
134 def gerritChange = gerrit.getGerritChange(gerritName, gerritHost, gerritChangeNumber, gerritCredentials)
135 if (gerritChange.status == "MERGED") {
136 common.successMsg('Patch set is alredy merged, no need to test it')
137 currentBuild.result = 'SUCCESS'
138 return
139 }
140
141 projectsMap[gerritProject] = [
142 'url': "${gerritScheme}://${gerritName}@${gerritHost}:${gerritPort}/${gerritProject}",
143 'ref': gerritRef,
144 'branch': gerritBranch,
145 ]
146 buildType = 'Gerrit Trigger'
147 } else {
148 projectsMap = getManualRefParams(job_env)
149 if (!projectsMap) {
150 error('Manual build detected and no valid Git refs provided!')
151 }
152 buildType = 'Manual build'
153 }
154 ArrayList descriptionMsgs = [ "<font color='red'>${buildType} detected!</font> Running with next parameters:" ]
155 for(String project in projectsMap.keySet()) {
Denis Egorenko7a806bb2018-12-03 14:37:05 +0400156 descriptionMsgs.add("Ref for ${project} => ${projectsMap[project]['ref']}")
157 descriptionMsgs.add("Branch for ${project} => ${projectsMap[project]['branch']}")
Denis Egorenko358a4fc2018-11-27 17:57:22 +0400158 }
159 descriptionMsgs.add("Distrib revision => ${distribRevision}")
160 currentBuild.description = descriptionMsgs.join('\n')
161 }
162
163 stage("Run tests") {
164 def branches = [:]
165 branches.failFast = true
166
167 if (projectsMap.containsKey(reclassSystemRepo)) {
168 def documentationOnly = checkReclassSystemDocumentationCommit(gerrit, gerritCredentials)
169 if (['master'].contains(gerritBranch) && !documentationOnly) {
170 for (int i = 0; i < testModels.size(); i++) {
171 def cluster = testModels[i]
Denis Egorenko7a806bb2018-12-03 14:37:05 +0400172 //def clusterGitUrl = projectsMap[reclassSystemRepo]['url'].substring(0, defaultGitUrl.lastIndexOf("/") + 1) + cluster
173 def clusterGitUrl = ''
174 branches["reclass-system-${cluster}"] = runTestSaltModelReclass(cluster, projectsMap[reclassSystemRepo]['url'], clusterGitUrl, projectsMap[reclassSystemRepo]['ref'])
Denis Egorenko358a4fc2018-11-27 17:57:22 +0400175 }
176 } else {
177 common.warningMsg("Tests for ${testModels} skipped!")
178 }
179 }
180 if (projectsMap.containsKey(reclassSystemRepo) || projectsMap.containsKey(cookiecutterTemplatesRepo)) {
181 branches['cookiecutter-templates'] = runTests('test-mk-cookiecutter-templates', JsonOutput.toJson(job_env))
182 }
183 if (projectsMap.containsKey(cookiecutterTemplatesRepo)) {
184 branches['test-drivetrain'] = runTests('test-drivetrain', JsonOutput.toJson(job_env))
185 branches['oscore-test-cookiecutter-models'] = runTests('oscore-test-cookiecutter-models', JsonOutput.toJson(job_env))
186 }
187
188 // temp block to disable test run until job is stable
189 print branches.keySet()
190 currentBuild.result = 'SUCCESS'
191 return
192 // ----
193
194 parallel branches
195 }
196 }
197}