blob: ee7c55937f08b6a6cc3ed6c4d75a49b821dcf6dd [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",
147 "export IMAGE=${uiImage.id}; docker-compose 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
161 cd /var/lib/trymcp-tests
Ivan Berezovskiyeb934812019-01-30 13:48:02 +0400162 pytest -m 'not trymcp' ${component}
Ivan Berezovskiy36ab0722019-01-14 19:53:45 +0400163 """
Ivan Berezovskiya066c3b2019-01-10 18:21:35 +0400164 }
165 }
166 } catch (Throwable e) {
167 // If there was an error or exception thrown, the build failed
168 currentBuild.result = "FAILURE"
169 throw e
170 } finally {
Ivan Berezovskiy7b2e3bc2019-01-14 20:34:42 +0400171 if (fileExists(testReportHTMLFile)) {
172 archiveArtifacts artifacts: testReportHTMLFile
173 }
174 if (fileExists(testReportXMLFile)) {
175 archiveArtifacts artifacts: testReportXMLFile
Ivan Berezovskiya066c3b2019-01-10 18:21:35 +0400176 }
177 stage("Cleanup") {
178 if (fileExists("${env.WORKSPACE}/venv")) {
179 dir(apiProject) {
Ivan Berezovskiy5e8b86a2019-01-11 18:39:28 +0400180 python.runVirtualenvCommand("${env.WORKSPACE}/venv", "./bootstrap_env.sh down || true")
Ivan Berezovskiya066c3b2019-01-10 18:21:35 +0400181 }
182 dir(uiProject) {
Ivan Berezovskiy5e8b86a2019-01-11 18:39:28 +0400183 python.runVirtualenvCommand("${env.WORKSPACE}/venv", "docker-compose down || true")
Ivan Berezovskiya066c3b2019-01-10 18:21:35 +0400184 }
185 sh "rm -rf ${env.WORKSPACE}/venv/"
186 }
187 if (apiImage && apiImage.id) {
Ivan Berezovskiy5e8b86a2019-01-11 18:39:28 +0400188 sh "docker rmi ${apiImage.id}"
Ivan Berezovskiya066c3b2019-01-10 18:21:35 +0400189 }
190 if (uiImage && uiImage.id) {
Ivan Berezovskiy5e8b86a2019-01-11 18:39:28 +0400191 sh "docker rmi ${uiImage.id}"
Ivan Berezovskiya066c3b2019-01-10 18:21:35 +0400192 }
193 // Remove everything what is owned by root
Ivan Berezovskiy36ab0722019-01-14 19:53:45 +0400194 testImage.inside(testImageOptions) {
Ivan Berezovskiy534ebab2019-01-17 15:41:38 +0400195 sh("rm -rf /var/lib/qa_reports/* ${env.WORKSPACE}/${apiProject} ${env.WORKSPACE}/${uiProject}")
Ivan Berezovskiya066c3b2019-01-10 18:21:35 +0400196 }
197 }
198 }
199 }
200}