blob: 1daa9ecdeaac69571f9464f9a5f7c443a1d9b251 [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
Ivan Berezovskiy36ab0722019-01-14 19:53:45 +040026def version = env.MCP_VERSION ?: 'nightly'
Ivan Berezovskiya066c3b2019-01-10 18:21:35 +040027def dockerRegistry = env.DOCKER_REGISTRY ?: 'docker-prod-local.docker.mirantis.net'
28def dockerReviewRegistry = env.DOCKER_REVIEW_REGISTRY ?: 'docker-dev-local.docker.mirantis.net'
Ivan Berezovskiy5e8b86a2019-01-11 18:39:28 +040029def 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 +040030
31def checkouted = false
Ivan Berezovskiy7b2e3bc2019-01-14 20:34:42 +040032def testReportHTMLFile = 'reports/report.html'
33def testReportXMLFile = 'reports/report.xml'
Ivan Berezovskiya066c3b2019-01-10 18:21:35 +040034def 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 Berezovskiy36ab0722019-01-14 19:53:45 +040045 def testImageOptions = "-u root:root --network=host -v ${env.WORKSPACE}/reports:/var/lib/qa_reports --entrypoint=''"
Ivan Berezovskiya066c3b2019-01-10 18:21:35 +040046 try {
47 stage("checkout") {
48 if (event) {
Ivan Berezovskiyba904d42019-01-14 13:14:05 +040049 dir(env.FLAVOR) {
Ivan Berezovskiya066c3b2019-01-10 18:21:35 +040050 // job is triggered by Gerrit
51 def gerritChange = gerrit.getGerritChange(env.GERRIT_NAME, env.GERRIT_HOST, env.GERRIT_CHANGE_NUMBER, gerritCredentials, true)
52 if (gerritChange.commitMessage.contains("WIP")) {
53 common.successMsg("Commit message contains WIP, skipping tests") // do nothing
54 } else {
55 // test if change aren't already merged
56 def merged = gerritChange.status == "MERGED"
57 if (!merged) {
58 checkouted = gerrit.gerritPatchsetCheckout([
59 credentialsId: gerritCredentials
60 ])
61 } else {
62 common.successMsg("Change ${env.GERRIT_CHANGE_NUMBER} is already merged, no need to test them")
63 }
64 }
65 }
66 } else {
67 common.successMsg('Gerrit variables are not set. Assuming it is manual trigger')
68 manualTrigger = true
69 }
70
71 if (checkouted) {
72 if (env.FLAVOR == apiProject) {
73 // Second project is UI
74 checkout([
75 $class : 'GitSCM',
76 branches : [[name: 'FETCH_HEAD'],],
77 extensions : [[$class: 'RelativeTargetDirectory', relativeTargetDir: uiProject]],
78 userRemoteConfigs: [[url: env.UI_GERRIT_REPO, refspec: uiGerritRef, credentialsId: gerritCredentials],],
79 ])
Ivan Berezovskiy5e8b86a2019-01-11 18:39:28 +040080 apiImage = docker.image("${dockerReviewRegistry}/review/${env.FLAVOR}-${env.GERRIT_CHANGE_NUMBER}:${env.GERRIT_PATCHSET_NUMBER}")
81 uiImage = docker.image("${dockerRegistry}/${env.UI_DOCKER_IMG ?: "mirantis/model-generator/operations-ui"}:${version}")
Ivan Berezovskiya066c3b2019-01-10 18:21:35 +040082 } else if (env.FLAVOR == uiProject) {
83 // Second project is API
84 checkout([
85 $class : 'GitSCM',
86 branches : [[name: 'FETCH_HEAD'],],
87 extensions : [[$class: 'RelativeTargetDirectory', relativeTargetDir: apiProject]],
88 userRemoteConfigs: [[url: env.API_GERRIT_REPO, refspec: apiGerritRef, credentialsId: gerritCredentials],],
89 ])
Ivan Berezovskiy5e8b86a2019-01-11 18:39:28 +040090 apiImage = docker.image("${dockerRegistry}/${env.API_DOCKER_IMG ?: "mirantis/model-generator/operations-api"}:${version}")
91 uiImage = docker.image("${dockerReviewRegistry}/review/${env.FLAVOR}-${env.GERRIT_CHANGE_NUMBER}:${env.GERRIT_PATCHSET_NUMBER}")
Ivan Berezovskiya066c3b2019-01-10 18:21:35 +040092 }
93 } else if (manualTrigger) {
94 checkout([
95 $class : 'GitSCM',
96 branches : [[name: 'FETCH_HEAD'],],
97 extensions : [[$class: 'RelativeTargetDirectory', relativeTargetDir: apiProject]],
98 userRemoteConfigs: [[url: env.API_GERRIT_REPO, refspec: apiGerritRef, credentialsId: gerritCredentials],],
99 ])
100 checkout([
101 $class : 'GitSCM',
102 branches : [[name: 'FETCH_HEAD'],],
103 extensions : [[$class: 'RelativeTargetDirectory', relativeTargetDir: uiProject]],
104 userRemoteConfigs: [[url: env.UI_GERRIT_REPO, refspec: uiGerritRef, credentialsId: gerritCredentials],],
105 ])
Ivan Berezovskiy5e8b86a2019-01-11 18:39:28 +0400106 apiImage = docker.image("${dockerRegistry}/${env.API_DOCKER_IMG ?: "mirantis/model-generator/operations-api"}:${version}")
107 uiImage = docker.image("${dockerRegistry}/${env.UI_DOCKER_IMG ?: "mirantis/model-generator/operations-ui"}:${version}")
Ivan Berezovskiya066c3b2019-01-10 18:21:35 +0400108 } else {
109 throw new Exception('Cannot checkout gerrit repositories. Please verify that parameters for repositories are properly set')
110 }
111 }
112
113 stage('Pull docker images') {
Ivan Berezovskiy5e8b86a2019-01-11 18:39:28 +0400114 common.retry(3, 5) {
115 apiImage.pull()
116 }
117 common.retry(3, 5) {
118 uiImage.pull()
119 }
120 common.retry(3, 5) {
121 testImage.pull()
122 }
Ivan Berezovskiya066c3b2019-01-10 18:21:35 +0400123 }
124
125 stage('Prepare and run docker compose services') {
Ivan Berezovskiy5e8b86a2019-01-11 18:39:28 +0400126 python.setupVirtualenv("${env.WORKSPACE}/venv", 'python2', ['docker-compose==1.22.0'])
Ivan Berezovskiya066c3b2019-01-10 18:21:35 +0400127
Ivan Berezovskiy36ab0722019-01-14 19:53:45 +0400128 // Make sure cockroach_data is cleaned up
129 if(fileExists("${apiProject}/cockroach_data")) {
130 testImage.inside(testImageOptions) {
131 sh("rm -rf ${env.WORKSPACE}/${apiProject}/cockroach_data")
132 }
133 }
134
Ivan Berezovskiya066c3b2019-01-10 18:21:35 +0400135 dir(apiProject) {
Ivan Berezovskiy5e8b86a2019-01-11 18:39:28 +0400136 python.runVirtualenvCommand("${env.WORKSPACE}/venv",
137 "export IMAGE=${apiImage.id}; ./bootstrap_env.sh up")
Ivan Berezovskiy36ab0722019-01-14 19:53:45 +0400138 common.retry(5, 20) {
139 sh 'curl -v http://127.0.0.1:8001/api/v1 > /dev/null'
140 }
Ivan Berezovskiya066c3b2019-01-10 18:21:35 +0400141 }
142 dir(uiProject) {
Ivan Berezovskiy5e8b86a2019-01-11 18:39:28 +0400143 python.runVirtualenvCommand("${env.WORKSPACE}/venv",
144 "export IMAGE=${uiImage.id}; docker-compose up -d")
Ivan Berezovskiy36ab0722019-01-14 19:53:45 +0400145 common.retry(5, 20) {
146 sh 'curl -v http://127.0.0.1:3000 > /dev/null'
147 }
Ivan Berezovskiya066c3b2019-01-10 18:21:35 +0400148 }
149 }
150
Ivan Berezovskiy5e8b86a2019-01-11 18:39:28 +0400151 stage('Test') {
Ivan Berezovskiy36ab0722019-01-14 19:53:45 +0400152 testImage.inside(testImageOptions) {
153 sh """
154 export TEST_LOGIN=test
155 export TEST_PASSWORD=default
156 export TEST_MODELD_URL=127.0.0.1
157 export TEST_MODELD_PORT=3000
158 cd /var/lib/trymcp-tests
159 pytest -m 'not trymcp'
160 """
Ivan Berezovskiya066c3b2019-01-10 18:21:35 +0400161 }
162 }
163 } catch (Throwable e) {
164 // If there was an error or exception thrown, the build failed
165 currentBuild.result = "FAILURE"
166 throw e
167 } finally {
Ivan Berezovskiy7b2e3bc2019-01-14 20:34:42 +0400168 if (fileExists(testReportHTMLFile)) {
169 archiveArtifacts artifacts: testReportHTMLFile
170 }
171 if (fileExists(testReportXMLFile)) {
172 archiveArtifacts artifacts: testReportXMLFile
Ivan Berezovskiya066c3b2019-01-10 18:21:35 +0400173 }
174 stage("Cleanup") {
175 if (fileExists("${env.WORKSPACE}/venv")) {
176 dir(apiProject) {
Ivan Berezovskiy5e8b86a2019-01-11 18:39:28 +0400177 python.runVirtualenvCommand("${env.WORKSPACE}/venv", "./bootstrap_env.sh down || true")
Ivan Berezovskiya066c3b2019-01-10 18:21:35 +0400178 }
179 dir(uiProject) {
Ivan Berezovskiy5e8b86a2019-01-11 18:39:28 +0400180 python.runVirtualenvCommand("${env.WORKSPACE}/venv", "docker-compose down || true")
Ivan Berezovskiya066c3b2019-01-10 18:21:35 +0400181 }
182 sh "rm -rf ${env.WORKSPACE}/venv/"
183 }
184 if (apiImage && apiImage.id) {
Ivan Berezovskiy5e8b86a2019-01-11 18:39:28 +0400185 sh "docker rmi ${apiImage.id}"
Ivan Berezovskiya066c3b2019-01-10 18:21:35 +0400186 }
187 if (uiImage && uiImage.id) {
Ivan Berezovskiy5e8b86a2019-01-11 18:39:28 +0400188 sh "docker rmi ${uiImage.id}"
Ivan Berezovskiya066c3b2019-01-10 18:21:35 +0400189 }
190 // Remove everything what is owned by root
Ivan Berezovskiy36ab0722019-01-14 19:53:45 +0400191 testImage.inside(testImageOptions) {
Ivan Berezovskiy534ebab2019-01-17 15:41:38 +0400192 sh("rm -rf /var/lib/qa_reports/* ${env.WORKSPACE}/${apiProject} ${env.WORKSPACE}/${uiProject}")
Ivan Berezovskiya066c3b2019-01-10 18:21:35 +0400193 }
194 }
195 }
196 }
197}