Denis Egorenko | 358a4fc | 2018-11-27 17:57:22 +0400 | [diff] [blame] | 1 | /* |
| 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 | --- |
| 17 | RECLASS_SYSTEM_URL: ssh://mcp-jenkins@gerrit.mcp.mirantis.net:29418/salt-models/reclass-system |
| 18 | RECLASS_SYSTEM_GIT_REF: 2018.11.0 |
| 19 | RECLASS_SYSTEM_BRANCH: refs/heads/2018.11.0 |
| 20 | COOKIECUTTER_TEMPLATE_URL: ssh://mcp-jenkins@gerrit.mcp.mirantis.net:29418/mk/cookiecutter-templates |
| 21 | COOKIECUTTER_TEMPLATE_REF: refs/heads/2018.11.0 |
| 22 | COOKIECUTTER_TEMPLATE_BRANCH: 2018.11.0 |
| 23 | DISTRIB_REVISION: 2018.11.0 |
| 24 | TEST_MODELS: '' |
| 25 | |
| 26 | */ |
| 27 | |
| 28 | import groovy.json.JsonOutput |
Denis Egorenko | 6c32511 | 2018-12-05 19:58:34 +0400 | [diff] [blame^] | 29 | gerrit = new com.mirantis.mk.Gerrit() |
Denis Egorenko | 358a4fc | 2018-11-27 17:57:22 +0400 | [diff] [blame] | 30 | |
| 31 | cookiecutterTemplatesRepo='mk/cookiecutter-templates' |
| 32 | reclassSystemRepo='salt-models/reclass-system' |
| 33 | slaveNode = env.getProperty('SLAVE_NODE') ?: 'python&&docker' |
| 34 | |
Denis Egorenko | 76a0be8 | 2018-12-04 13:36:07 +0400 | [diff] [blame] | 35 | voteMatrix = [ |
| 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 Egorenko | 6c32511 | 2018-12-05 19:58:34 +0400 | [diff] [blame^] | 43 | baseGerritConfig = [:] |
| 44 | |
Denis Egorenko | 358a4fc | 2018-11-27 17:57:22 +0400 | [diff] [blame] | 45 | LinkedHashMap 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 Egorenko | 6c32511 | 2018-12-05 19:58:34 +0400 | [diff] [blame^] | 65 | def 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 Egorenko | 76a0be8 | 2018-12-04 13:36:07 +0400 | [diff] [blame] | 72 | } |
| 73 | |
| 74 | def runTests(String jobName, String extraVars) { |
| 75 | def propagateStatus = voteMatrix.get(jobName, true) |
Denis Egorenko | 358a4fc | 2018-11-27 17:57:22 +0400 | [diff] [blame] | 76 | return { |
Denis Egorenko | 76a0be8 | 2018-12-04 13:36:07 +0400 | [diff] [blame] | 77 | def jobBuild = build job: "${jobName}", propagate: false, parameters: [ |
| 78 | [$class: 'TextParameterValue', name: 'EXTRA_VARIABLES_YAML', value: extraVars ] |
| 79 | ] |
Denis Egorenko | 6c32511 | 2018-12-05 19:58:34 +0400 | [diff] [blame^] | 80 | setGerritReviewComment(jobName, jobBuild.absoluteUrl, jobBuild.result) |
Denis Egorenko | 76a0be8 | 2018-12-04 13:36:07 +0400 | [diff] [blame] | 81 | if (propagateStatus && jobBuild.result == 'FAILURE') { |
| 82 | throw new Exception("Build ${jobName} is failed!") |
Denis Egorenko | 358a4fc | 2018-11-27 17:57:22 +0400 | [diff] [blame] | 83 | } |
| 84 | } |
| 85 | } |
| 86 | |
| 87 | def runTestSaltModelReclass(String cluster, String defaultGitUrl, String clusterGitUrl, String refSpec) { |
Denis Egorenko | 76a0be8 | 2018-12-04 13:36:07 +0400 | [diff] [blame] | 88 | def saltModelJob = "test-salt-model-${cluster}" |
| 89 | def propagateStatus = voteMatrix.get(saltModelJob, true) |
Denis Egorenko | 358a4fc | 2018-11-27 17:57:22 +0400 | [diff] [blame] | 90 | return { |
Denis Egorenko | 76a0be8 | 2018-12-04 13:36:07 +0400 | [diff] [blame] | 91 | def jobBuild = build job: saltModelJob, propagate: false, parameters: [ |
Denis Egorenko | 358a4fc | 2018-11-27 17:57:22 +0400 | [diff] [blame] | 92 | [$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 Egorenko | 6c32511 | 2018-12-05 19:58:34 +0400 | [diff] [blame^] | 97 | setGerritReviewComment(saltModelJob, jobBuild.absoluteUrl, jobBuild.result) |
Denis Egorenko | 76a0be8 | 2018-12-04 13:36:07 +0400 | [diff] [blame] | 98 | if (propagateStatus && jobBuild.result == 'FAILURE') { |
| 99 | throw new Exception("Build ${saltModelJob} is failed!") |
| 100 | } |
Denis Egorenko | 358a4fc | 2018-11-27 17:57:22 +0400 | [diff] [blame] | 101 | } |
| 102 | } |
| 103 | |
Denis Egorenko | 6c32511 | 2018-12-05 19:58:34 +0400 | [diff] [blame^] | 104 | def checkReclassSystemDocumentationCommit(gerritCredentials) { |
| 105 | gerrit.gerritPatchsetCheckout([ |
Denis Egorenko | 358a4fc | 2018-11-27 17:57:22 +0400 | [diff] [blame] | 106 | 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 | |
| 115 | timeout(time: 12, unit: 'HOURS') { |
| 116 | node(slaveNode) { |
| 117 | def common = new com.mirantis.mk.Common() |
Denis Egorenko | 358a4fc | 2018-11-27 17:57:22 +0400 | [diff] [blame] | 118 | 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 Egorenko | 76a0be8 | 2018-12-04 13:36:07 +0400 | [diff] [blame] | 123 | def buildTestParams = [:] |
| 124 | def buildTestParamsYaml = env.getProperty('TEST_PARAMETERS_YAML') |
| 125 | if (buildTestParamsYaml) { |
| 126 | common.mergeEnv(env, buildTestParamsYaml) |
| 127 | buildTestParams = readYaml text: buildTestParamsYaml |
Denis Egorenko | 358a4fc | 2018-11-27 17:57:22 +0400 | [diff] [blame] | 128 | } |
| 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 Egorenko | ff770c6 | 2018-12-03 17:45:34 +0400 | [diff] [blame] | 145 | String distribRevision = job_env.get('DISTRIB_REVISION', 'nightly') |
Denis Egorenko | 358a4fc | 2018-11-27 17:57:22 +0400 | [diff] [blame] | 146 | 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 Egorenko | 6c32511 | 2018-12-05 19:58:34 +0400 | [diff] [blame^] | 158 | gerritPatchSetNumber = job_env.get('GERRIT_PATCHSET_NUMBER') |
Denis Egorenko | 358a4fc | 2018-11-27 17:57:22 +0400 | [diff] [blame] | 159 | 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 Egorenko | 76a0be8 | 2018-12-04 13:36:07 +0400 | [diff] [blame] | 175 | buildTestParams << job_env.findAll { k,v -> k ==~ /GERRIT_.+/ } |
Denis Egorenko | 6c32511 | 2018-12-05 19:58:34 +0400 | [diff] [blame^] | 176 | baseGerritConfig = [ |
| 177 | 'gerritName': gerritName, |
| 178 | 'gerritHost': gerritHost, |
| 179 | 'gerritChangeNumber': gerritChangeNumber, |
| 180 | 'credentialsId': gerritCredentials, |
| 181 | 'gerritPatchSetNumber': gerritPatchSetNumber, |
| 182 | ] |
Denis Egorenko | 358a4fc | 2018-11-27 17:57:22 +0400 | [diff] [blame] | 183 | } 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 Egorenko | 7a806bb | 2018-12-03 14:37:05 +0400 | [diff] [blame] | 192 | descriptionMsgs.add("Ref for ${project} => ${projectsMap[project]['ref']}") |
| 193 | descriptionMsgs.add("Branch for ${project} => ${projectsMap[project]['branch']}") |
Denis Egorenko | 358a4fc | 2018-11-27 17:57:22 +0400 | [diff] [blame] | 194 | } |
| 195 | descriptionMsgs.add("Distrib revision => ${distribRevision}") |
Denis Egorenko | 76a0be8 | 2018-12-04 13:36:07 +0400 | [diff] [blame] | 196 | currentBuild.description = descriptionMsgs.join('<br/>') |
Denis Egorenko | 358a4fc | 2018-11-27 17:57:22 +0400 | [diff] [blame] | 197 | } |
| 198 | |
| 199 | stage("Run tests") { |
| 200 | def branches = [:] |
Denis Egorenko | 358a4fc | 2018-11-27 17:57:22 +0400 | [diff] [blame] | 201 | |
| 202 | if (projectsMap.containsKey(reclassSystemRepo)) { |
Denis Egorenko | 6c32511 | 2018-12-05 19:58:34 +0400 | [diff] [blame^] | 203 | def documentationOnly = checkReclassSystemDocumentationCommit(gerritCredentials) |
Denis Egorenko | 358a4fc | 2018-11-27 17:57:22 +0400 | [diff] [blame] | 204 | if (['master'].contains(gerritBranch) && !documentationOnly) { |
| 205 | for (int i = 0; i < testModels.size(); i++) { |
| 206 | def cluster = testModels[i] |
Denis Egorenko | 76a0be8 | 2018-12-04 13:36:07 +0400 | [diff] [blame] | 207 | def clusterGitUrl = projectsMap[reclassSystemRepo]['url'].substring(0, projectsMap[reclassSystemRepo]['url'].lastIndexOf("/") + 1) + cluster |
Denis Egorenko | 7a806bb | 2018-12-03 14:37:05 +0400 | [diff] [blame] | 208 | branches["reclass-system-${cluster}"] = runTestSaltModelReclass(cluster, projectsMap[reclassSystemRepo]['url'], clusterGitUrl, projectsMap[reclassSystemRepo]['ref']) |
Denis Egorenko | 358a4fc | 2018-11-27 17:57:22 +0400 | [diff] [blame] | 209 | } |
| 210 | } else { |
| 211 | common.warningMsg("Tests for ${testModels} skipped!") |
| 212 | } |
| 213 | } |
| 214 | if (projectsMap.containsKey(reclassSystemRepo) || projectsMap.containsKey(cookiecutterTemplatesRepo)) { |
Denis Egorenko | 76a0be8 | 2018-12-04 13:36:07 +0400 | [diff] [blame] | 215 | branches['cookiecutter-templates'] = runTests('test-mk-cookiecutter-templates', JsonOutput.toJson(buildTestParams)) |
Denis Egorenko | 358a4fc | 2018-11-27 17:57:22 +0400 | [diff] [blame] | 216 | } |
| 217 | if (projectsMap.containsKey(cookiecutterTemplatesRepo)) { |
Denis Egorenko | 76a0be8 | 2018-12-04 13:36:07 +0400 | [diff] [blame] | 218 | 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 Egorenko | 358a4fc | 2018-11-27 17:57:22 +0400 | [diff] [blame] | 221 | } |
| 222 | |
Denis Egorenko | 76a0be8 | 2018-12-04 13:36:07 +0400 | [diff] [blame] | 223 | 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 Egorenko | 358a4fc | 2018-11-27 17:57:22 +0400 | [diff] [blame] | 229 | } |
| 230 | } |
| 231 | } |