blob: 02e178926575564ce576c7dce3c1ae377ca34ca9 [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
Ivan Berezovskiyeb934812019-01-30 13:48:02 +040040def component = ''
Ivan Berezovskiya066c3b2019-01-10 18:21:35 +040041
42timeout(time: 1, unit: 'HOURS') {
43 node(slaveNode) {
44 sh "mkdir -p reports ${apiProject} ${uiProject}"
Ivan Berezovskiy5e8b86a2019-01-11 18:39:28 +040045 def testImage = docker.image(cvpImageName)
Ivan Berezovskiy36ab0722019-01-14 19:53:45 +040046 def testImageOptions = "-u root:root --network=host -v ${env.WORKSPACE}/reports:/var/lib/qa_reports --entrypoint=''"
Ivan Berezovskiya066c3b2019-01-10 18:21:35 +040047 try {
48 stage("checkout") {
49 if (event) {
Ivan Berezovskiyba904d42019-01-14 13:14:05 +040050 dir(env.FLAVOR) {
Ivan Berezovskiya066c3b2019-01-10 18:21:35 +040051 // job is triggered by Gerrit
52 def gerritChange = gerrit.getGerritChange(env.GERRIT_NAME, env.GERRIT_HOST, env.GERRIT_CHANGE_NUMBER, gerritCredentials, true)
53 if (gerritChange.commitMessage.contains("WIP")) {
54 common.successMsg("Commit message contains WIP, skipping tests") // do nothing
55 } else {
56 // test if change aren't already merged
57 def merged = gerritChange.status == "MERGED"
58 if (!merged) {
59 checkouted = gerrit.gerritPatchsetCheckout([
60 credentialsId: gerritCredentials
61 ])
62 } else {
63 common.successMsg("Change ${env.GERRIT_CHANGE_NUMBER} is already merged, no need to test them")
64 }
65 }
66 }
67 } else {
68 common.successMsg('Gerrit variables are not set. Assuming it is manual trigger')
69 manualTrigger = true
70 }
71
72 if (checkouted) {
73 if (env.FLAVOR == apiProject) {
74 // Second project is UI
75 checkout([
76 $class : 'GitSCM',
77 branches : [[name: 'FETCH_HEAD'],],
78 extensions : [[$class: 'RelativeTargetDirectory', relativeTargetDir: uiProject]],
79 userRemoteConfigs: [[url: env.UI_GERRIT_REPO, refspec: uiGerritRef, credentialsId: gerritCredentials],],
80 ])
Ivan Berezovskiy5e8b86a2019-01-11 18:39:28 +040081 apiImage = docker.image("${dockerReviewRegistry}/review/${env.FLAVOR}-${env.GERRIT_CHANGE_NUMBER}:${env.GERRIT_PATCHSET_NUMBER}")
82 uiImage = docker.image("${dockerRegistry}/${env.UI_DOCKER_IMG ?: "mirantis/model-generator/operations-ui"}:${version}")
Ivan Berezovskiyeb934812019-01-30 13:48:02 +040083 component = "-k 'api'"
Ivan Berezovskiya066c3b2019-01-10 18:21:35 +040084 } else if (env.FLAVOR == uiProject) {
85 // Second project is API
86 checkout([
87 $class : 'GitSCM',
88 branches : [[name: 'FETCH_HEAD'],],
89 extensions : [[$class: 'RelativeTargetDirectory', relativeTargetDir: apiProject]],
90 userRemoteConfigs: [[url: env.API_GERRIT_REPO, refspec: apiGerritRef, credentialsId: gerritCredentials],],
91 ])
Ivan Berezovskiy5e8b86a2019-01-11 18:39:28 +040092 apiImage = docker.image("${dockerRegistry}/${env.API_DOCKER_IMG ?: "mirantis/model-generator/operations-api"}:${version}")
93 uiImage = docker.image("${dockerReviewRegistry}/review/${env.FLAVOR}-${env.GERRIT_CHANGE_NUMBER}:${env.GERRIT_PATCHSET_NUMBER}")
Ivan Berezovskiyeb934812019-01-30 13:48:02 +040094 component = "-k 'ui'"
Ivan Berezovskiya066c3b2019-01-10 18:21:35 +040095 }
96 } else if (manualTrigger) {
97 checkout([
98 $class : 'GitSCM',
99 branches : [[name: 'FETCH_HEAD'],],
100 extensions : [[$class: 'RelativeTargetDirectory', relativeTargetDir: apiProject]],
101 userRemoteConfigs: [[url: env.API_GERRIT_REPO, refspec: apiGerritRef, credentialsId: gerritCredentials],],
102 ])
103 checkout([
104 $class : 'GitSCM',
105 branches : [[name: 'FETCH_HEAD'],],
106 extensions : [[$class: 'RelativeTargetDirectory', relativeTargetDir: uiProject]],
107 userRemoteConfigs: [[url: env.UI_GERRIT_REPO, refspec: uiGerritRef, credentialsId: gerritCredentials],],
108 ])
Ivan Berezovskiy5e8b86a2019-01-11 18:39:28 +0400109 apiImage = docker.image("${dockerRegistry}/${env.API_DOCKER_IMG ?: "mirantis/model-generator/operations-api"}:${version}")
110 uiImage = docker.image("${dockerRegistry}/${env.UI_DOCKER_IMG ?: "mirantis/model-generator/operations-ui"}:${version}")
Ivan Berezovskiya066c3b2019-01-10 18:21:35 +0400111 } else {
112 throw new Exception('Cannot checkout gerrit repositories. Please verify that parameters for repositories are properly set')
113 }
114 }
115
116 stage('Pull docker images') {
Ivan Berezovskiy5e8b86a2019-01-11 18:39:28 +0400117 common.retry(3, 5) {
118 apiImage.pull()
119 }
120 common.retry(3, 5) {
121 uiImage.pull()
122 }
123 common.retry(3, 5) {
124 testImage.pull()
125 }
Ivan Berezovskiya066c3b2019-01-10 18:21:35 +0400126 }
127
128 stage('Prepare and run docker compose services') {
Ivan Berezovskiy5e8b86a2019-01-11 18:39:28 +0400129 python.setupVirtualenv("${env.WORKSPACE}/venv", 'python2', ['docker-compose==1.22.0'])
Ivan Berezovskiya066c3b2019-01-10 18:21:35 +0400130
Ivan Berezovskiy36ab0722019-01-14 19:53:45 +0400131 // Make sure cockroach_data is cleaned up
132 if(fileExists("${apiProject}/cockroach_data")) {
133 testImage.inside(testImageOptions) {
134 sh("rm -rf ${env.WORKSPACE}/${apiProject}/cockroach_data")
135 }
136 }
137
Ivan Berezovskiya066c3b2019-01-10 18:21:35 +0400138 dir(apiProject) {
Ivan Berezovskiy5e8b86a2019-01-11 18:39:28 +0400139 python.runVirtualenvCommand("${env.WORKSPACE}/venv",
140 "export IMAGE=${apiImage.id}; ./bootstrap_env.sh up")
Ivan Berezovskiy36ab0722019-01-14 19:53:45 +0400141 common.retry(5, 20) {
142 sh 'curl -v http://127.0.0.1:8001/api/v1 > /dev/null'
143 }
Ivan Berezovskiya066c3b2019-01-10 18:21:35 +0400144 }
145 dir(uiProject) {
Ivan Berezovskiy5e8b86a2019-01-11 18:39:28 +0400146 python.runVirtualenvCommand("${env.WORKSPACE}/venv",
Hanna Arhipova21142d92019-03-27 15:04:36 +0200147 "export IMAGE=${uiImage.id}; docker-compose -f docker-compose-test.yml up -d")
Ivan Berezovskiy36ab0722019-01-14 19:53:45 +0400148 common.retry(5, 20) {
149 sh 'curl -v http://127.0.0.1:3000 > /dev/null'
150 }
Ivan Berezovskiya066c3b2019-01-10 18:21:35 +0400151 }
152 }
153
Ivan Berezovskiy5e8b86a2019-01-11 18:39:28 +0400154 stage('Test') {
Ivan Berezovskiy36ab0722019-01-14 19:53:45 +0400155 testImage.inside(testImageOptions) {
156 sh """
157 export TEST_LOGIN=test
158 export TEST_PASSWORD=default
159 export TEST_MODELD_URL=127.0.0.1
160 export TEST_MODELD_PORT=3000
Hanna Arhipovaeb7a7472019-04-15 17:53:59 +0300161 export TEST_TIMEOUT=30
Ivan Berezovskiy36ab0722019-01-14 19:53:45 +0400162 cd /var/lib/trymcp-tests
Hanna Arhipovac5358e82019-04-22 11:00:42 +0300163 pytest ${component}
Ivan Berezovskiy36ab0722019-01-14 19:53:45 +0400164 """
Ivan Berezovskiya066c3b2019-01-10 18:21:35 +0400165 }
166 }
167 } catch (Throwable e) {
168 // If there was an error or exception thrown, the build failed
169 currentBuild.result = "FAILURE"
170 throw e
171 } finally {
Hanna Arhipovac80058e2019-04-03 18:34:47 +0300172 sh (script: """map=\$(docker ps --format '{{.Names}}:{{.ID}}' --filter name=operations);\
173 for cont in \$map ; do NAME="\${cont%%:*}";ID="\${cont##*:}"; docker logs \$ID > \$NAME.log 2>&1 ; done""")
174 archiveArtifacts "*.log"
175
Ivan Berezovskiy7b2e3bc2019-01-14 20:34:42 +0400176 if (fileExists(testReportHTMLFile)) {
177 archiveArtifacts artifacts: testReportHTMLFile
178 }
179 if (fileExists(testReportXMLFile)) {
180 archiveArtifacts artifacts: testReportXMLFile
Ivan Berezovskiya066c3b2019-01-10 18:21:35 +0400181 }
182 stage("Cleanup") {
183 if (fileExists("${env.WORKSPACE}/venv")) {
184 dir(apiProject) {
Ivan Berezovskiy5e8b86a2019-01-11 18:39:28 +0400185 python.runVirtualenvCommand("${env.WORKSPACE}/venv", "./bootstrap_env.sh down || true")
Ivan Berezovskiya066c3b2019-01-10 18:21:35 +0400186 }
187 dir(uiProject) {
Ivan Berezovskiy5e8b86a2019-01-11 18:39:28 +0400188 python.runVirtualenvCommand("${env.WORKSPACE}/venv", "docker-compose down || true")
Ivan Berezovskiya066c3b2019-01-10 18:21:35 +0400189 }
190 sh "rm -rf ${env.WORKSPACE}/venv/"
191 }
Hanna Arhipovaeb7a7472019-04-15 17:53:59 +0300192 try {
193 // to avoid issue PROD-29393 "pipeline freezes in `docker rmi` action"
194 timeout(time: 4, unit: 'MINUTES') {
195 if (apiImage && apiImage.id) {
196 sh "docker rmi ${apiImage.id}"
197 }
198 if (uiImage && uiImage.id) {
199 sh "docker rmi ${uiImage.id}"
200 }
201 }
202 } catch (Exception e) {
203 echo "Failed while cleaning docker images: ${e.toString()}"
Ivan Berezovskiya066c3b2019-01-10 18:21:35 +0400204 }
205 // Remove everything what is owned by root
Ivan Berezovskiy36ab0722019-01-14 19:53:45 +0400206 testImage.inside(testImageOptions) {
Hanna Arhipovac80058e2019-04-03 18:34:47 +0300207 sh("rm -rf /var/lib/qa_reports/* ${env.WORKSPACE}/${apiProject} ${env.WORKSPACE}/${uiProject} ${env.WORKSPACE}/*.log")
Ivan Berezovskiya066c3b2019-01-10 18:21:35 +0400208 }
209 }
210 }
211 }
212}