blob: 144f76060d7b2838c1b9ae6945712bb33c2da239 [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}"
Denis Egorenkofeb00a62019-06-04 15:41:10 +040030if (env.CVP_DEV_TAG && env.CVP_DOCKER_IMG) {
31 cvpImageName = "${dockerReviewRegistry}/${env.CVP_DOCKER_IMG}:${env.CVP_DEV_TAG}"
32}
Ivan Berezovskiya066c3b2019-01-10 18:21:35 +040033
34def checkouted = false
Ivan Berezovskiy7b2e3bc2019-01-14 20:34:42 +040035def testReportHTMLFile = 'reports/report.html'
36def testReportXMLFile = 'reports/report.xml'
Ivan Berezovskiya066c3b2019-01-10 18:21:35 +040037def manualTrigger = false
38
39def apiProject = 'operations-api'
40def uiProject = 'operations-ui'
41def apiImage
42def uiImage
Ivan Berezovskiyeb934812019-01-30 13:48:02 +040043def component = ''
Ivan Berezovskiya066c3b2019-01-10 18:21:35 +040044
45timeout(time: 1, unit: 'HOURS') {
46 node(slaveNode) {
47 sh "mkdir -p reports ${apiProject} ${uiProject}"
Ivan Berezovskiy5e8b86a2019-01-11 18:39:28 +040048 def testImage = docker.image(cvpImageName)
Ivan Berezovskiy36ab0722019-01-14 19:53:45 +040049 def testImageOptions = "-u root:root --network=host -v ${env.WORKSPACE}/reports:/var/lib/qa_reports --entrypoint=''"
Ivan Berezovskiya066c3b2019-01-10 18:21:35 +040050 try {
51 stage("checkout") {
52 if (event) {
Ivan Berezovskiyba904d42019-01-14 13:14:05 +040053 dir(env.FLAVOR) {
Ivan Berezovskiya066c3b2019-01-10 18:21:35 +040054 // job is triggered by Gerrit
55 def gerritChange = gerrit.getGerritChange(env.GERRIT_NAME, env.GERRIT_HOST, env.GERRIT_CHANGE_NUMBER, gerritCredentials, true)
56 if (gerritChange.commitMessage.contains("WIP")) {
57 common.successMsg("Commit message contains WIP, skipping tests") // do nothing
58 } else {
59 // test if change aren't already merged
60 def merged = gerritChange.status == "MERGED"
61 if (!merged) {
62 checkouted = gerrit.gerritPatchsetCheckout([
63 credentialsId: gerritCredentials
64 ])
65 } else {
66 common.successMsg("Change ${env.GERRIT_CHANGE_NUMBER} is already merged, no need to test them")
67 }
68 }
69 }
70 } else {
71 common.successMsg('Gerrit variables are not set. Assuming it is manual trigger')
72 manualTrigger = true
73 }
74
75 if (checkouted) {
76 if (env.FLAVOR == apiProject) {
77 // Second project is UI
78 checkout([
79 $class : 'GitSCM',
80 branches : [[name: 'FETCH_HEAD'],],
81 extensions : [[$class: 'RelativeTargetDirectory', relativeTargetDir: uiProject]],
82 userRemoteConfigs: [[url: env.UI_GERRIT_REPO, refspec: uiGerritRef, credentialsId: gerritCredentials],],
83 ])
Ivan Berezovskiy5e8b86a2019-01-11 18:39:28 +040084 apiImage = docker.image("${dockerReviewRegistry}/review/${env.FLAVOR}-${env.GERRIT_CHANGE_NUMBER}:${env.GERRIT_PATCHSET_NUMBER}")
85 uiImage = docker.image("${dockerRegistry}/${env.UI_DOCKER_IMG ?: "mirantis/model-generator/operations-ui"}:${version}")
Ivan Berezovskiyeb934812019-01-30 13:48:02 +040086 component = "-k 'api'"
Ivan Berezovskiya066c3b2019-01-10 18:21:35 +040087 } else if (env.FLAVOR == uiProject) {
88 // Second project is API
89 checkout([
90 $class : 'GitSCM',
91 branches : [[name: 'FETCH_HEAD'],],
92 extensions : [[$class: 'RelativeTargetDirectory', relativeTargetDir: apiProject]],
93 userRemoteConfigs: [[url: env.API_GERRIT_REPO, refspec: apiGerritRef, credentialsId: gerritCredentials],],
94 ])
Ivan Berezovskiy5e8b86a2019-01-11 18:39:28 +040095 apiImage = docker.image("${dockerRegistry}/${env.API_DOCKER_IMG ?: "mirantis/model-generator/operations-api"}:${version}")
96 uiImage = docker.image("${dockerReviewRegistry}/review/${env.FLAVOR}-${env.GERRIT_CHANGE_NUMBER}:${env.GERRIT_PATCHSET_NUMBER}")
Ivan Berezovskiyeb934812019-01-30 13:48:02 +040097 component = "-k 'ui'"
Ivan Berezovskiya066c3b2019-01-10 18:21:35 +040098 }
99 } else if (manualTrigger) {
100 checkout([
101 $class : 'GitSCM',
102 branches : [[name: 'FETCH_HEAD'],],
103 extensions : [[$class: 'RelativeTargetDirectory', relativeTargetDir: apiProject]],
104 userRemoteConfigs: [[url: env.API_GERRIT_REPO, refspec: apiGerritRef, credentialsId: gerritCredentials],],
105 ])
106 checkout([
107 $class : 'GitSCM',
108 branches : [[name: 'FETCH_HEAD'],],
109 extensions : [[$class: 'RelativeTargetDirectory', relativeTargetDir: uiProject]],
110 userRemoteConfigs: [[url: env.UI_GERRIT_REPO, refspec: uiGerritRef, credentialsId: gerritCredentials],],
111 ])
Ivan Berezovskiy5e8b86a2019-01-11 18:39:28 +0400112 apiImage = docker.image("${dockerRegistry}/${env.API_DOCKER_IMG ?: "mirantis/model-generator/operations-api"}:${version}")
113 uiImage = docker.image("${dockerRegistry}/${env.UI_DOCKER_IMG ?: "mirantis/model-generator/operations-ui"}:${version}")
Ivan Berezovskiya066c3b2019-01-10 18:21:35 +0400114 } else {
115 throw new Exception('Cannot checkout gerrit repositories. Please verify that parameters for repositories are properly set')
116 }
117 }
118
119 stage('Pull docker images') {
Ivan Berezovskiy5e8b86a2019-01-11 18:39:28 +0400120 common.retry(3, 5) {
121 apiImage.pull()
122 }
123 common.retry(3, 5) {
124 uiImage.pull()
125 }
126 common.retry(3, 5) {
127 testImage.pull()
128 }
Ivan Berezovskiya066c3b2019-01-10 18:21:35 +0400129 }
130
131 stage('Prepare and run docker compose services') {
Ivan Berezovskiy5e8b86a2019-01-11 18:39:28 +0400132 python.setupVirtualenv("${env.WORKSPACE}/venv", 'python2', ['docker-compose==1.22.0'])
Ivan Berezovskiya066c3b2019-01-10 18:21:35 +0400133
Ivan Berezovskiy36ab0722019-01-14 19:53:45 +0400134 // Make sure cockroach_data is cleaned up
135 if(fileExists("${apiProject}/cockroach_data")) {
136 testImage.inside(testImageOptions) {
137 sh("rm -rf ${env.WORKSPACE}/${apiProject}/cockroach_data")
138 }
139 }
140
Ivan Berezovskiya066c3b2019-01-10 18:21:35 +0400141 dir(apiProject) {
Ivan Berezovskiy5e8b86a2019-01-11 18:39:28 +0400142 python.runVirtualenvCommand("${env.WORKSPACE}/venv",
143 "export IMAGE=${apiImage.id}; ./bootstrap_env.sh up")
Ivan Berezovskiy36ab0722019-01-14 19:53:45 +0400144 common.retry(5, 20) {
145 sh 'curl -v http://127.0.0.1:8001/api/v1 > /dev/null'
146 }
Ivan Berezovskiya066c3b2019-01-10 18:21:35 +0400147 }
148 dir(uiProject) {
Ivan Berezovskiy5e8b86a2019-01-11 18:39:28 +0400149 python.runVirtualenvCommand("${env.WORKSPACE}/venv",
Hanna Arhipova21142d92019-03-27 15:04:36 +0200150 "export IMAGE=${uiImage.id}; docker-compose -f docker-compose-test.yml up -d")
Ivan Berezovskiy36ab0722019-01-14 19:53:45 +0400151 common.retry(5, 20) {
152 sh 'curl -v http://127.0.0.1:3000 > /dev/null'
153 }
Ivan Berezovskiya066c3b2019-01-10 18:21:35 +0400154 }
155 }
156
Ivan Berezovskiy5e8b86a2019-01-11 18:39:28 +0400157 stage('Test') {
Ivan Berezovskiy36ab0722019-01-14 19:53:45 +0400158 testImage.inside(testImageOptions) {
159 sh """
160 export TEST_LOGIN=test
161 export TEST_PASSWORD=default
162 export TEST_MODELD_URL=127.0.0.1
163 export TEST_MODELD_PORT=3000
Hanna Arhipovaeb7a7472019-04-15 17:53:59 +0300164 export TEST_TIMEOUT=30
Ivan Berezovskiy36ab0722019-01-14 19:53:45 +0400165 cd /var/lib/trymcp-tests
Hanna Arhipovac5358e82019-04-22 11:00:42 +0300166 pytest ${component}
Ivan Berezovskiy36ab0722019-01-14 19:53:45 +0400167 """
Ivan Berezovskiya066c3b2019-01-10 18:21:35 +0400168 }
169 }
170 } catch (Throwable e) {
171 // If there was an error or exception thrown, the build failed
172 currentBuild.result = "FAILURE"
173 throw e
174 } finally {
Hanna Arhipovac80058e2019-04-03 18:34:47 +0300175 sh (script: """map=\$(docker ps --format '{{.Names}}:{{.ID}}' --filter name=operations);\
176 for cont in \$map ; do NAME="\${cont%%:*}";ID="\${cont##*:}"; docker logs \$ID > \$NAME.log 2>&1 ; done""")
177 archiveArtifacts "*.log"
178
Ivan Berezovskiy7b2e3bc2019-01-14 20:34:42 +0400179 if (fileExists(testReportHTMLFile)) {
180 archiveArtifacts artifacts: testReportHTMLFile
181 }
182 if (fileExists(testReportXMLFile)) {
183 archiveArtifacts artifacts: testReportXMLFile
Ivan Berezovskiya066c3b2019-01-10 18:21:35 +0400184 }
185 stage("Cleanup") {
186 if (fileExists("${env.WORKSPACE}/venv")) {
187 dir(apiProject) {
Ivan Berezovskiy5e8b86a2019-01-11 18:39:28 +0400188 python.runVirtualenvCommand("${env.WORKSPACE}/venv", "./bootstrap_env.sh down || true")
Ivan Berezovskiya066c3b2019-01-10 18:21:35 +0400189 }
190 dir(uiProject) {
Ivan Berezovskiy5e8b86a2019-01-11 18:39:28 +0400191 python.runVirtualenvCommand("${env.WORKSPACE}/venv", "docker-compose down || true")
Ivan Berezovskiya066c3b2019-01-10 18:21:35 +0400192 }
193 sh "rm -rf ${env.WORKSPACE}/venv/"
194 }
Hanna Arhipovaeb7a7472019-04-15 17:53:59 +0300195 try {
196 // to avoid issue PROD-29393 "pipeline freezes in `docker rmi` action"
197 timeout(time: 4, unit: 'MINUTES') {
198 if (apiImage && apiImage.id) {
199 sh "docker rmi ${apiImage.id}"
200 }
201 if (uiImage && uiImage.id) {
202 sh "docker rmi ${uiImage.id}"
203 }
204 }
205 } catch (Exception e) {
206 echo "Failed while cleaning docker images: ${e.toString()}"
Ivan Berezovskiya066c3b2019-01-10 18:21:35 +0400207 }
208 // Remove everything what is owned by root
Ivan Berezovskiy36ab0722019-01-14 19:53:45 +0400209 testImage.inside(testImageOptions) {
Hanna Arhipovac80058e2019-04-03 18:34:47 +0300210 sh("rm -rf /var/lib/qa_reports/* ${env.WORKSPACE}/${apiProject} ${env.WORKSPACE}/${uiProject} ${env.WORKSPACE}/*.log")
Ivan Berezovskiya066c3b2019-01-10 18:21:35 +0400211 }
212 }
213 }
214 }
215}