blob: a72e6e2f692cd0e63b91ae0e79b319ed770431f3 [file] [log] [blame]
Ivan Berezovskiya066c3b2019-01-10 18:21:35 +04001/**
2 * Tests model manager UI
3 * API_GERRIT_REPO
4 * API_GERRIT_REF
5 * UI_GERRIT_REPO
6 * UI_GERRIT_REF
7 * API_DOCKER_IMG
8 * UI_DOCKER_IMG
9 * CVP_DOCKER_IMG
10 * DOCKER_REGISTRY
11 * DOCKER_REVIEW_REGISTRY
12 * MCP_VERSION
13 * FLAVOR
14 */
15
16def common = new com.mirantis.mk.Common()
17def gerrit = new com.mirantis.mk.Gerrit()
Ivan Berezovskiy5e8b86a2019-01-11 18:39:28 +040018def python = new com.mirantis.mk.Python()
Ivan Berezovskiya066c3b2019-01-10 18:21:35 +040019
20def gerritCredentials = env.CREDENTIALS_ID ?: 'gerrit'
21def slaveNode = env.SLAVE_NODE ?: 'python&&docker'
22def event = env.GERRIT_EVENT_TYPE ?: null
23def defaultRef = 'master'
24def apiGerritRef = env.API_GERRIT_REF ?: defaultRef
25def uiGerritRef = env.UI_GERRIT_REF ?: defaultRef
26def gerritProject = env.GERRIT_PROJECT ?: null
27def version = env.MCP_VERSION ?: 'testing'
28def dockerRegistry = env.DOCKER_REGISTRY ?: 'docker-prod-local.docker.mirantis.net'
29def dockerReviewRegistry = env.DOCKER_REVIEW_REGISTRY ?: 'docker-dev-local.docker.mirantis.net'
Ivan Berezovskiy5e8b86a2019-01-11 18:39:28 +040030def cvpImageName = env.CVP_DOCKER_IMG ? "${dockerRegistry}/${env.CVP_DOCKER_IMG}:${version}" : "${dockerRegistry}/mirantis/cvp/cvp-trymcp-tests:${version}"
Ivan Berezovskiya066c3b2019-01-10 18:21:35 +040031
32def checkouted = false
33def testReportFile = "${env.WORKSPACE}/reports/report.html"
34def manualTrigger = false
35
36def apiProject = 'operations-api'
37def uiProject = 'operations-ui'
38def apiImage
39def uiImage
40
41timeout(time: 1, unit: 'HOURS') {
42 node(slaveNode) {
43 sh "mkdir -p reports ${apiProject} ${uiProject}"
Ivan Berezovskiy5e8b86a2019-01-11 18:39:28 +040044 def testImage = docker.image(cvpImageName)
Ivan Berezovskiya066c3b2019-01-10 18:21:35 +040045 try {
46 stage("checkout") {
47 if (event) {
48 dir(gerritProject) {
49 // job is triggered by Gerrit
50 def gerritChange = gerrit.getGerritChange(env.GERRIT_NAME, env.GERRIT_HOST, env.GERRIT_CHANGE_NUMBER, gerritCredentials, true)
51 if (gerritChange.commitMessage.contains("WIP")) {
52 common.successMsg("Commit message contains WIP, skipping tests") // do nothing
53 } else {
54 // test if change aren't already merged
55 def merged = gerritChange.status == "MERGED"
56 if (!merged) {
57 checkouted = gerrit.gerritPatchsetCheckout([
58 credentialsId: gerritCredentials
59 ])
60 } else {
61 common.successMsg("Change ${env.GERRIT_CHANGE_NUMBER} is already merged, no need to test them")
62 }
63 }
64 }
65 } else {
66 common.successMsg('Gerrit variables are not set. Assuming it is manual trigger')
67 manualTrigger = true
68 }
69
70 if (checkouted) {
71 if (env.FLAVOR == apiProject) {
72 // Second project is UI
73 checkout([
74 $class : 'GitSCM',
75 branches : [[name: 'FETCH_HEAD'],],
76 extensions : [[$class: 'RelativeTargetDirectory', relativeTargetDir: uiProject]],
77 userRemoteConfigs: [[url: env.UI_GERRIT_REPO, refspec: uiGerritRef, credentialsId: gerritCredentials],],
78 ])
Ivan Berezovskiy5e8b86a2019-01-11 18:39:28 +040079 apiImage = docker.image("${dockerReviewRegistry}/review/${env.FLAVOR}-${env.GERRIT_CHANGE_NUMBER}:${env.GERRIT_PATCHSET_NUMBER}")
80 uiImage = docker.image("${dockerRegistry}/${env.UI_DOCKER_IMG ?: "mirantis/model-generator/operations-ui"}:${version}")
Ivan Berezovskiya066c3b2019-01-10 18:21:35 +040081 } else if (env.FLAVOR == uiProject) {
82 // Second project is API
83 checkout([
84 $class : 'GitSCM',
85 branches : [[name: 'FETCH_HEAD'],],
86 extensions : [[$class: 'RelativeTargetDirectory', relativeTargetDir: apiProject]],
87 userRemoteConfigs: [[url: env.API_GERRIT_REPO, refspec: apiGerritRef, credentialsId: gerritCredentials],],
88 ])
Ivan Berezovskiy5e8b86a2019-01-11 18:39:28 +040089 apiImage = docker.image("${dockerRegistry}/${env.API_DOCKER_IMG ?: "mirantis/model-generator/operations-api"}:${version}")
90 uiImage = docker.image("${dockerReviewRegistry}/review/${env.FLAVOR}-${env.GERRIT_CHANGE_NUMBER}:${env.GERRIT_PATCHSET_NUMBER}")
Ivan Berezovskiya066c3b2019-01-10 18:21:35 +040091 }
92 } else if (manualTrigger) {
93 checkout([
94 $class : 'GitSCM',
95 branches : [[name: 'FETCH_HEAD'],],
96 extensions : [[$class: 'RelativeTargetDirectory', relativeTargetDir: apiProject]],
97 userRemoteConfigs: [[url: env.API_GERRIT_REPO, refspec: apiGerritRef, credentialsId: gerritCredentials],],
98 ])
99 checkout([
100 $class : 'GitSCM',
101 branches : [[name: 'FETCH_HEAD'],],
102 extensions : [[$class: 'RelativeTargetDirectory', relativeTargetDir: uiProject]],
103 userRemoteConfigs: [[url: env.UI_GERRIT_REPO, refspec: uiGerritRef, credentialsId: gerritCredentials],],
104 ])
Ivan Berezovskiy5e8b86a2019-01-11 18:39:28 +0400105 apiImage = docker.image("${dockerRegistry}/${env.API_DOCKER_IMG ?: "mirantis/model-generator/operations-api"}:${version}")
106 uiImage = docker.image("${dockerRegistry}/${env.UI_DOCKER_IMG ?: "mirantis/model-generator/operations-ui"}:${version}")
Ivan Berezovskiya066c3b2019-01-10 18:21:35 +0400107 } else {
108 throw new Exception('Cannot checkout gerrit repositories. Please verify that parameters for repositories are properly set')
109 }
110 }
111
112 stage('Pull docker images') {
Ivan Berezovskiy5e8b86a2019-01-11 18:39:28 +0400113 common.retry(3, 5) {
114 apiImage.pull()
115 }
116 common.retry(3, 5) {
117 uiImage.pull()
118 }
119 common.retry(3, 5) {
120 testImage.pull()
121 }
Ivan Berezovskiya066c3b2019-01-10 18:21:35 +0400122 }
123
124 stage('Prepare and run docker compose services') {
Ivan Berezovskiy5e8b86a2019-01-11 18:39:28 +0400125 python.setupVirtualenv("${env.WORKSPACE}/venv", 'python2', ['docker-compose==1.22.0'])
Ivan Berezovskiya066c3b2019-01-10 18:21:35 +0400126
127 dir(apiProject) {
Ivan Berezovskiy5e8b86a2019-01-11 18:39:28 +0400128 python.runVirtualenvCommand("${env.WORKSPACE}/venv",
129 "export IMAGE=${apiImage.id}; ./bootstrap_env.sh up")
Ivan Berezovskiya066c3b2019-01-10 18:21:35 +0400130 }
131 dir(uiProject) {
Ivan Berezovskiy5e8b86a2019-01-11 18:39:28 +0400132 python.runVirtualenvCommand("${env.WORKSPACE}/venv",
133 "export IMAGE=${uiImage.id}; docker-compose up -d")
Ivan Berezovskiya066c3b2019-01-10 18:21:35 +0400134 }
135 }
136
Ivan Berezovskiy5e8b86a2019-01-11 18:39:28 +0400137 stage('Test') {
138 testImage.inside("-u root:root" +
139 " -v ${env.WORKSPACE}/reports:/var/lib/qa_reports" +
140 "--entrypoint=/bin/bash") {
Ivan Berezovskiya066c3b2019-01-10 18:21:35 +0400141 sh "pytest -m 'not trymcp'"
142 }
143 }
144 } catch (Throwable e) {
145 // If there was an error or exception thrown, the build failed
146 currentBuild.result = "FAILURE"
147 throw e
148 } finally {
149 if (fileExists(testReportFile)) {
150 archiveArtifacts artifacts: testReportFile
151 }
152 stage("Cleanup") {
153 if (fileExists("${env.WORKSPACE}/venv")) {
154 dir(apiProject) {
Ivan Berezovskiy5e8b86a2019-01-11 18:39:28 +0400155 python.runVirtualenvCommand("${env.WORKSPACE}/venv", "./bootstrap_env.sh down || true")
Ivan Berezovskiya066c3b2019-01-10 18:21:35 +0400156 }
157 dir(uiProject) {
Ivan Berezovskiy5e8b86a2019-01-11 18:39:28 +0400158 python.runVirtualenvCommand("${env.WORKSPACE}/venv", "docker-compose down || true")
Ivan Berezovskiya066c3b2019-01-10 18:21:35 +0400159 }
160 sh "rm -rf ${env.WORKSPACE}/venv/"
161 }
162 if (apiImage && apiImage.id) {
Ivan Berezovskiy5e8b86a2019-01-11 18:39:28 +0400163 sh "docker rmi ${apiImage.id}"
Ivan Berezovskiya066c3b2019-01-10 18:21:35 +0400164 }
165 if (uiImage && uiImage.id) {
Ivan Berezovskiy5e8b86a2019-01-11 18:39:28 +0400166 sh "docker rmi ${uiImage.id}"
Ivan Berezovskiya066c3b2019-01-10 18:21:35 +0400167 }
168 // Remove everything what is owned by root
Ivan Berezovskiy5e8b86a2019-01-11 18:39:28 +0400169 testImage.inside("-u root:root" +
170 " -v ${env.WORKSPACE}/reports:/var/lib/qa_reports" +
171 "--entrypoint=/bin/bash") {
172 sh("rm -rf /var/lib/qa_reports/* /${env.WORKSPACE}/cockroach_data")
Ivan Berezovskiya066c3b2019-01-10 18:21:35 +0400173 }
174 }
175 }
176 }
177}