blob: 8c0849395975e5c3084064c0c78789ab639297da [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=''"
Hanna Arhipovad3d01d02019-07-09 13:18:59 +030050 withCredentials([
51 [$class : 'UsernamePasswordMultiBinding',
52 credentialsId : 'scale-ci',
53 passwordVariable: 'JENKINS_PASSWORD',
54 usernameVariable: 'JENKINS_USER']
55 ]) {
56 env.JENKINS_USER = JENKINS_USER
57 env.JENKINS_PASSWORD = JENKINS_PASSWORD
58 }
Ivan Berezovskiya066c3b2019-01-10 18:21:35 +040059 try {
60 stage("checkout") {
61 if (event) {
Ivan Berezovskiyba904d42019-01-14 13:14:05 +040062 dir(env.FLAVOR) {
Ivan Berezovskiya066c3b2019-01-10 18:21:35 +040063 // job is triggered by Gerrit
64 def gerritChange = gerrit.getGerritChange(env.GERRIT_NAME, env.GERRIT_HOST, env.GERRIT_CHANGE_NUMBER, gerritCredentials, true)
65 if (gerritChange.commitMessage.contains("WIP")) {
66 common.successMsg("Commit message contains WIP, skipping tests") // do nothing
67 } else {
68 // test if change aren't already merged
69 def merged = gerritChange.status == "MERGED"
70 if (!merged) {
71 checkouted = gerrit.gerritPatchsetCheckout([
72 credentialsId: gerritCredentials
73 ])
74 } else {
75 common.successMsg("Change ${env.GERRIT_CHANGE_NUMBER} is already merged, no need to test them")
76 }
77 }
78 }
79 } else {
80 common.successMsg('Gerrit variables are not set. Assuming it is manual trigger')
81 manualTrigger = true
82 }
83
84 if (checkouted) {
85 if (env.FLAVOR == apiProject) {
86 // Second project is UI
87 checkout([
88 $class : 'GitSCM',
89 branches : [[name: 'FETCH_HEAD'],],
90 extensions : [[$class: 'RelativeTargetDirectory', relativeTargetDir: uiProject]],
91 userRemoteConfigs: [[url: env.UI_GERRIT_REPO, refspec: uiGerritRef, credentialsId: gerritCredentials],],
92 ])
Ivan Berezovskiy5e8b86a2019-01-11 18:39:28 +040093 apiImage = docker.image("${dockerReviewRegistry}/review/${env.FLAVOR}-${env.GERRIT_CHANGE_NUMBER}:${env.GERRIT_PATCHSET_NUMBER}")
94 uiImage = docker.image("${dockerRegistry}/${env.UI_DOCKER_IMG ?: "mirantis/model-generator/operations-ui"}:${version}")
Ivan Berezovskiyeb934812019-01-30 13:48:02 +040095 component = "-k 'api'"
Ivan Berezovskiya066c3b2019-01-10 18:21:35 +040096 } else if (env.FLAVOR == uiProject) {
97 // Second project is API
98 checkout([
99 $class : 'GitSCM',
100 branches : [[name: 'FETCH_HEAD'],],
101 extensions : [[$class: 'RelativeTargetDirectory', relativeTargetDir: apiProject]],
102 userRemoteConfigs: [[url: env.API_GERRIT_REPO, refspec: apiGerritRef, credentialsId: gerritCredentials],],
103 ])
Ivan Berezovskiy5e8b86a2019-01-11 18:39:28 +0400104 apiImage = docker.image("${dockerRegistry}/${env.API_DOCKER_IMG ?: "mirantis/model-generator/operations-api"}:${version}")
105 uiImage = docker.image("${dockerReviewRegistry}/review/${env.FLAVOR}-${env.GERRIT_CHANGE_NUMBER}:${env.GERRIT_PATCHSET_NUMBER}")
Ivan Berezovskiyeb934812019-01-30 13:48:02 +0400106 component = "-k 'ui'"
Ivan Berezovskiya066c3b2019-01-10 18:21:35 +0400107 }
108 } else if (manualTrigger) {
109 checkout([
110 $class : 'GitSCM',
111 branches : [[name: 'FETCH_HEAD'],],
112 extensions : [[$class: 'RelativeTargetDirectory', relativeTargetDir: apiProject]],
113 userRemoteConfigs: [[url: env.API_GERRIT_REPO, refspec: apiGerritRef, credentialsId: gerritCredentials],],
114 ])
115 checkout([
116 $class : 'GitSCM',
117 branches : [[name: 'FETCH_HEAD'],],
118 extensions : [[$class: 'RelativeTargetDirectory', relativeTargetDir: uiProject]],
119 userRemoteConfigs: [[url: env.UI_GERRIT_REPO, refspec: uiGerritRef, credentialsId: gerritCredentials],],
120 ])
Ivan Berezovskiy5e8b86a2019-01-11 18:39:28 +0400121 apiImage = docker.image("${dockerRegistry}/${env.API_DOCKER_IMG ?: "mirantis/model-generator/operations-api"}:${version}")
122 uiImage = docker.image("${dockerRegistry}/${env.UI_DOCKER_IMG ?: "mirantis/model-generator/operations-ui"}:${version}")
Ivan Berezovskiya066c3b2019-01-10 18:21:35 +0400123 } else {
124 throw new Exception('Cannot checkout gerrit repositories. Please verify that parameters for repositories are properly set')
125 }
126 }
127
128 stage('Pull docker images') {
Ivan Berezovskiy5e8b86a2019-01-11 18:39:28 +0400129 common.retry(3, 5) {
130 apiImage.pull()
131 }
132 common.retry(3, 5) {
133 uiImage.pull()
134 }
135 common.retry(3, 5) {
136 testImage.pull()
137 }
Ivan Berezovskiya066c3b2019-01-10 18:21:35 +0400138 }
139
140 stage('Prepare and run docker compose services') {
Ivan Berezovskiy5e8b86a2019-01-11 18:39:28 +0400141 python.setupVirtualenv("${env.WORKSPACE}/venv", 'python2', ['docker-compose==1.22.0'])
Ivan Berezovskiya066c3b2019-01-10 18:21:35 +0400142
Ivan Berezovskiy36ab0722019-01-14 19:53:45 +0400143 // Make sure cockroach_data is cleaned up
144 if(fileExists("${apiProject}/cockroach_data")) {
145 testImage.inside(testImageOptions) {
146 sh("rm -rf ${env.WORKSPACE}/${apiProject}/cockroach_data")
147 }
148 }
149
Ivan Berezovskiya066c3b2019-01-10 18:21:35 +0400150 dir(apiProject) {
Ivan Berezovskiy5e8b86a2019-01-11 18:39:28 +0400151 python.runVirtualenvCommand("${env.WORKSPACE}/venv",
Hanna Arhipovad3d01d02019-07-09 13:18:59 +0300152 "export IMAGE=${apiImage.id}; export DOCKER_COMPOSE=docker-compose-test.yml; ./bootstrap_env.sh up")
Ivan Berezovskiy36ab0722019-01-14 19:53:45 +0400153 common.retry(5, 20) {
154 sh 'curl -v http://127.0.0.1:8001/api/v1 > /dev/null'
155 }
Ivan Berezovskiya066c3b2019-01-10 18:21:35 +0400156 }
157 dir(uiProject) {
Ivan Berezovskiy5e8b86a2019-01-11 18:39:28 +0400158 python.runVirtualenvCommand("${env.WORKSPACE}/venv",
Hanna Arhipova21142d92019-03-27 15:04:36 +0200159 "export IMAGE=${uiImage.id}; docker-compose -f docker-compose-test.yml up -d")
Ivan Berezovskiy36ab0722019-01-14 19:53:45 +0400160 common.retry(5, 20) {
161 sh 'curl -v http://127.0.0.1:3000 > /dev/null'
162 }
Ivan Berezovskiya066c3b2019-01-10 18:21:35 +0400163 }
164 }
165
Ivan Berezovskiy5e8b86a2019-01-11 18:39:28 +0400166 stage('Test') {
Ivan Berezovskiy36ab0722019-01-14 19:53:45 +0400167 testImage.inside(testImageOptions) {
168 sh """
169 export TEST_LOGIN=test
170 export TEST_PASSWORD=default
171 export TEST_MODELD_URL=127.0.0.1
172 export TEST_MODELD_PORT=3000
Hanna Arhipovad3d01d02019-07-09 13:18:59 +0300173 export TEST_TIMEOUT=15
Ivan Berezovskiy36ab0722019-01-14 19:53:45 +0400174 cd /var/lib/trymcp-tests
Hanna Arhipovac5358e82019-04-22 11:00:42 +0300175 pytest ${component}
Ivan Berezovskiy36ab0722019-01-14 19:53:45 +0400176 """
Ivan Berezovskiya066c3b2019-01-10 18:21:35 +0400177 }
178 }
179 } catch (Throwable e) {
180 // If there was an error or exception thrown, the build failed
181 currentBuild.result = "FAILURE"
182 throw e
183 } finally {
Hanna Arhipovac80058e2019-04-03 18:34:47 +0300184 sh (script: """map=\$(docker ps --format '{{.Names}}:{{.ID}}' --filter name=operations);\
185 for cont in \$map ; do NAME="\${cont%%:*}";ID="\${cont##*:}"; docker logs \$ID > \$NAME.log 2>&1 ; done""")
186 archiveArtifacts "*.log"
187
Ivan Berezovskiy7b2e3bc2019-01-14 20:34:42 +0400188 if (fileExists(testReportHTMLFile)) {
189 archiveArtifacts artifacts: testReportHTMLFile
190 }
191 if (fileExists(testReportXMLFile)) {
192 archiveArtifacts artifacts: testReportXMLFile
Ivan Berezovskiya066c3b2019-01-10 18:21:35 +0400193 }
194 stage("Cleanup") {
195 if (fileExists("${env.WORKSPACE}/venv")) {
196 dir(apiProject) {
Ivan Berezovskiy5e8b86a2019-01-11 18:39:28 +0400197 python.runVirtualenvCommand("${env.WORKSPACE}/venv", "./bootstrap_env.sh down || true")
Ivan Berezovskiya066c3b2019-01-10 18:21:35 +0400198 }
199 dir(uiProject) {
Ivan Berezovskiy5e8b86a2019-01-11 18:39:28 +0400200 python.runVirtualenvCommand("${env.WORKSPACE}/venv", "docker-compose down || true")
Ivan Berezovskiya066c3b2019-01-10 18:21:35 +0400201 }
202 sh "rm -rf ${env.WORKSPACE}/venv/"
203 }
Hanna Arhipovaeb7a7472019-04-15 17:53:59 +0300204 try {
205 // to avoid issue PROD-29393 "pipeline freezes in `docker rmi` action"
206 timeout(time: 4, unit: 'MINUTES') {
207 if (apiImage && apiImage.id) {
208 sh "docker rmi ${apiImage.id}"
209 }
210 if (uiImage && uiImage.id) {
211 sh "docker rmi ${uiImage.id}"
212 }
213 }
214 } catch (Exception e) {
215 echo "Failed while cleaning docker images: ${e.toString()}"
Ivan Berezovskiya066c3b2019-01-10 18:21:35 +0400216 }
217 // Remove everything what is owned by root
Ivan Berezovskiy36ab0722019-01-14 19:53:45 +0400218 testImage.inside(testImageOptions) {
Hanna Arhipovac80058e2019-04-03 18:34:47 +0300219 sh("rm -rf /var/lib/qa_reports/* ${env.WORKSPACE}/${apiProject} ${env.WORKSPACE}/${uiProject} ${env.WORKSPACE}/*.log")
Ivan Berezovskiya066c3b2019-01-10 18:21:35 +0400220 }
221 }
222 }
223 }
224}