blob: 22dcf81283d90426bf10e4025fbe4ada29c22fe2 [file] [log] [blame]
asledzinskiy69307da2017-01-19 16:37:16 +02001package com.mirantis.mcp_qa
asledzinskiye0948fd2017-01-06 16:23:54 +02002
3/**
4 * Get latest artifacts
5 * @param imageRepoName is the repo name where image is located
6 * @param imageTagName is the name of the image tag to be used
7 */
8
9def getLatestArtifacts(imageRepoName, imageTagName) {
10 def imageRepo = env.getAt(imageRepoName)
11 def imageTag = env.getAt(imageTagName)
12 if ( imageTag != null && (! imageTag || imageTag.equals('latest')) ) {
13 if ( imageRepo ) {
14 def registry = imageRepo.replaceAll(/\/.*/, '')
15 def image = imageRepo.minus(registry + '/')
16 def hyperkubeImageTag = latestImageTagLookup(registry, image)
17 return "${imageTagName}=${hyperkubeImageTag}"
18 } else {
19 echo "${imageRepoName} variable isn't set, can't inspect 'latest' image!"
20 return null
21 }
22 }
23}
24
25def jsonParse(def json) {
26 new groovy.json.JsonSlurperClassic().parseText(json)
27}
28
29/**
30 * Get digest metadata
31 * @param tag is the image tag to be used
32 * @param registry is the url of registry
33 * @param image is the image which info is looked for
34 */
35
36def get_digest(def tag, def registry, def image) {
37 def digest_link = sprintf('https://%1$s/v2/%2$s/manifests/%3$s', [registry, image, tag])
38 def digest_url = new URL(digest_link)
39 def connection = digest_url.openConnection()
40 connection.setRequestProperty('Accept', 'application/vnd.docker.distribution.manifest.v2+json')
41 def digest = connection.getHeaderField("Docker-Content-Digest")
42 return digest
43}
44
45/**
46 * Get latest tag metadata
47 * @param registry is the url of registry
48 * @param image is the image which tags are looked for
49 */
50
51def latestImageTagLookup(registry, image) {
52 def tags_link = sprintf('https://%1$s/v2/%2$s/tags/list', [registry, image])
53 def tags_url = new URL(tags_link)
54 def tags = jsonParse(tags_url.getText())['tags']
55 def latest_digest = get_digest('latest', registry, image)
56 def same_digest_tags = []
57
58 for (tag in tags) {
59 if (tag == 'latest') {
60 continue
61 }
62 if (get_digest(tag, registry, image) == latest_digest) {
63 same_digest_tags<< tag
64 }
65 }
66
67 return same_digest_tags[0] ?: 'latest'
68}
69
70
71/**
72 * Fetch custom refs
73 * @param gerritUrl is url of gerrit
74 * @param project is the name of project in gerrit
75 * @param targetDir is dir where to fetch changes
76 * @param refs is refs that need to be fetched
77 */
78
79def getCustomRefs(gerritUrl, project, targetDir, refs) {
80 def remote = "${gerritUrl}/${project}"
81 dir(targetDir) {
82 for(int i=0; i<refs.size(); i++) {
83 sh "git fetch ${remote} ${refs[i]} && git checkout FETCH_HEAD"
84 }
85 }
86}
87
88/**
89 * Set downstream k8s artifacts
90 * @param jobSetParameters are current job parameters that can be extended with kubernetes tag
91 */
92
93def set_downstream_k8s_artifacts(jobSetParameters) {
94 def k8sTag = getLatestArtifacts('HYPERKUBE_IMAGE_REPO', 'HYPERKUBE_IMAGE_TAG')
95 if (k8sTag) {
96 jobSetParameters.add(k8sTag)
97 }
98 return jobSetParameters
99}
Artem Panchenko6fd07222017-02-13 17:13:23 +0200100
101/**
102 * Upload tests results to TestRail
103 *
104 * @param config LinkedHashMap
105 * config includes next parameters:
106 * - junitXml String, path to XML file with tests results
107 * - testPlanName String, name of test plan in TestRail
108 * - testSuiteName String, name of test suite in TestRail
109 * - testrailMilestone String, milestone name in TestRail
110 * - tesPlanDesc String, description of test plan in TestRail (optional)
111 * - jobURL String, URL of job build with tests (optional)
112 * - testrailURL String, TestRail URL (optional)
113 * - testrailProject String, project name in TestRail (optional)
114 *
115 *
116 * Usage example:
117 *
118 * uploadResultsTestRail([
119 * junitXml: './nosetests.xml',
120 * testPlanName: 'MCP test plan #1',
121 * testSuiteName: 'Calico component tests',
122 * jobURL: 'jenkins.example.com/job/tests.mcp/1',
123 * ])
124 *
125 */
126def uploadResultsTestRail(config) {
127 def venvPath = 'testrail-venv'
128 // TODO: install 'testrail_reporter' pypi when new version with eee508d commit is released
129 def testrailReporterPackage = 'git+git://github.com/gdyuldin/testrail_reporter.git'
130 def testrailReporterVersion = 'eee508d'
131
132 def requiredArgs = ['junitXml', 'testPlanName', 'testSuiteName', 'testrailMilestone']
133 def missingArgs = []
134 for (i in requiredArgs) { if (!config.containsKey(i)) { missingArgs << i }}
135 if (missingArgs) { println "Required arguments are missing for '${funcName}': ${missingArgs.join(', ')}" }
136
137 def junitXml = config.get('junitXml')
138 def testPlanName = config.get('testPlanName')
139 def testSuiteName = config.get('testSuiteName')
140 def testrailMilestone = config.get('testrailMilestone')
141 def testrailURL = config.get('testrailURL', 'https://mirantis.testrail.com')
142 def testrailProject = config.get('testrailProject', 'Mirantis Cloud Platform')
143 def tesPlanDesc = config.get('tesPlanDesc')
144 def jobURL = config.get('jobURL')
145
146 def reporterOptions = [
147 "--verbose",
148 "--testrail-run-update",
149 "--testrail-url '${testrailURL}'",
150 "--testrail-user \"\${TESTRAIL_USER}\"",
151 "--testrail-password \"\${TESTRAIL_PASSWORD}\"",
152 "--testrail-project '${testrailProject}'",
153 "--testrail-plan-name '${testPlanName}'",
154 "--testrail-milestone '${testrailMilestone}'",
155 "--testrail-suite '${testSuiteName}'",
156 "--xunit-name-template '{methodname}'",
157 "--testrail-name-template '{custom_test_group}'",
158 ]
159
160 if (tesPlanDesc) { reporterOptions << "--env-description '${tesPlanDesc}'" }
161 if (jobURL) { reporterOptions << "--test-results-link '${jobURL}'" }
162
163 // Install testrail reporter
164 sh """
165 virtualenv ${venvPath}
166 . ${venvPath}/bin/activate
167 pip install --upgrade ${testrailReporterPackage}@${testrailReporterVersion}
168 """
169
170 def script = """
171 . ${venvPath}/bin/activate
172 report ${reporterOptions.join(' ')} ${junitXml}
173 """
174
175 withCredentials([
176 [$class : 'UsernamePasswordMultiBinding',
177 credentialsId : 'testrail',
178 passwordVariable: 'TESTRAIL_PASSWORD',
179 usernameVariable: 'TESTRAIL_USER']
180 ]) {
181 return sh(script: script, returnStdout: true).trim().split().last()
182 }
183}