blob: e6df832cfe3e65f544c1da13964b4c71bee16cab [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 Berezovskiya066c3b2019-01-10 18:21:35 +040026def version = env.MCP_VERSION ?: 'testing'
27def 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
32def testReportFile = "${env.WORKSPACE}/reports/report.html"
33def manualTrigger = false
34
35def apiProject = 'operations-api'
36def uiProject = 'operations-ui'
37def apiImage
38def uiImage
39
40timeout(time: 1, unit: 'HOURS') {
41 node(slaveNode) {
42 sh "mkdir -p reports ${apiProject} ${uiProject}"
Ivan Berezovskiy5e8b86a2019-01-11 18:39:28 +040043 def testImage = docker.image(cvpImageName)
Ivan Berezovskiya066c3b2019-01-10 18:21:35 +040044 try {
45 stage("checkout") {
46 if (event) {
Ivan Berezovskiyba904d42019-01-14 13:14:05 +040047 dir(env.FLAVOR) {
Ivan Berezovskiya066c3b2019-01-10 18:21:35 +040048 // job is triggered by Gerrit
49 def gerritChange = gerrit.getGerritChange(env.GERRIT_NAME, env.GERRIT_HOST, env.GERRIT_CHANGE_NUMBER, gerritCredentials, true)
50 if (gerritChange.commitMessage.contains("WIP")) {
51 common.successMsg("Commit message contains WIP, skipping tests") // do nothing
52 } else {
53 // test if change aren't already merged
54 def merged = gerritChange.status == "MERGED"
55 if (!merged) {
56 checkouted = gerrit.gerritPatchsetCheckout([
57 credentialsId: gerritCredentials
58 ])
59 } else {
60 common.successMsg("Change ${env.GERRIT_CHANGE_NUMBER} is already merged, no need to test them")
61 }
62 }
63 }
64 } else {
65 common.successMsg('Gerrit variables are not set. Assuming it is manual trigger')
66 manualTrigger = true
67 }
68
69 if (checkouted) {
70 if (env.FLAVOR == apiProject) {
71 // Second project is UI
72 checkout([
73 $class : 'GitSCM',
74 branches : [[name: 'FETCH_HEAD'],],
75 extensions : [[$class: 'RelativeTargetDirectory', relativeTargetDir: uiProject]],
76 userRemoteConfigs: [[url: env.UI_GERRIT_REPO, refspec: uiGerritRef, credentialsId: gerritCredentials],],
77 ])
Ivan Berezovskiy5e8b86a2019-01-11 18:39:28 +040078 apiImage = docker.image("${dockerReviewRegistry}/review/${env.FLAVOR}-${env.GERRIT_CHANGE_NUMBER}:${env.GERRIT_PATCHSET_NUMBER}")
79 uiImage = docker.image("${dockerRegistry}/${env.UI_DOCKER_IMG ?: "mirantis/model-generator/operations-ui"}:${version}")
Ivan Berezovskiya066c3b2019-01-10 18:21:35 +040080 } else if (env.FLAVOR == uiProject) {
81 // Second project is API
82 checkout([
83 $class : 'GitSCM',
84 branches : [[name: 'FETCH_HEAD'],],
85 extensions : [[$class: 'RelativeTargetDirectory', relativeTargetDir: apiProject]],
86 userRemoteConfigs: [[url: env.API_GERRIT_REPO, refspec: apiGerritRef, credentialsId: gerritCredentials],],
87 ])
Ivan Berezovskiy5e8b86a2019-01-11 18:39:28 +040088 apiImage = docker.image("${dockerRegistry}/${env.API_DOCKER_IMG ?: "mirantis/model-generator/operations-api"}:${version}")
89 uiImage = docker.image("${dockerReviewRegistry}/review/${env.FLAVOR}-${env.GERRIT_CHANGE_NUMBER}:${env.GERRIT_PATCHSET_NUMBER}")
Ivan Berezovskiya066c3b2019-01-10 18:21:35 +040090 }
91 } else if (manualTrigger) {
92 checkout([
93 $class : 'GitSCM',
94 branches : [[name: 'FETCH_HEAD'],],
95 extensions : [[$class: 'RelativeTargetDirectory', relativeTargetDir: apiProject]],
96 userRemoteConfigs: [[url: env.API_GERRIT_REPO, refspec: apiGerritRef, credentialsId: gerritCredentials],],
97 ])
98 checkout([
99 $class : 'GitSCM',
100 branches : [[name: 'FETCH_HEAD'],],
101 extensions : [[$class: 'RelativeTargetDirectory', relativeTargetDir: uiProject]],
102 userRemoteConfigs: [[url: env.UI_GERRIT_REPO, refspec: uiGerritRef, 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("${dockerRegistry}/${env.UI_DOCKER_IMG ?: "mirantis/model-generator/operations-ui"}:${version}")
Ivan Berezovskiya066c3b2019-01-10 18:21:35 +0400106 } else {
107 throw new Exception('Cannot checkout gerrit repositories. Please verify that parameters for repositories are properly set')
108 }
109 }
110
111 stage('Pull docker images') {
Ivan Berezovskiy5e8b86a2019-01-11 18:39:28 +0400112 common.retry(3, 5) {
113 apiImage.pull()
114 }
115 common.retry(3, 5) {
116 uiImage.pull()
117 }
118 common.retry(3, 5) {
119 testImage.pull()
120 }
Ivan Berezovskiya066c3b2019-01-10 18:21:35 +0400121 }
122
123 stage('Prepare and run docker compose services') {
Ivan Berezovskiy5e8b86a2019-01-11 18:39:28 +0400124 python.setupVirtualenv("${env.WORKSPACE}/venv", 'python2', ['docker-compose==1.22.0'])
Ivan Berezovskiya066c3b2019-01-10 18:21:35 +0400125
126 dir(apiProject) {
Ivan Berezovskiy5e8b86a2019-01-11 18:39:28 +0400127 python.runVirtualenvCommand("${env.WORKSPACE}/venv",
128 "export IMAGE=${apiImage.id}; ./bootstrap_env.sh up")
Ivan Berezovskiya066c3b2019-01-10 18:21:35 +0400129 }
130 dir(uiProject) {
Ivan Berezovskiy5e8b86a2019-01-11 18:39:28 +0400131 python.runVirtualenvCommand("${env.WORKSPACE}/venv",
132 "export IMAGE=${uiImage.id}; docker-compose up -d")
Ivan Berezovskiya066c3b2019-01-10 18:21:35 +0400133 }
134 }
135
Ivan Berezovskiy5e8b86a2019-01-11 18:39:28 +0400136 stage('Test') {
137 testImage.inside("-u root:root" +
138 " -v ${env.WORKSPACE}/reports:/var/lib/qa_reports" +
139 "--entrypoint=/bin/bash") {
Ivan Berezovskiya066c3b2019-01-10 18:21:35 +0400140 sh "pytest -m 'not trymcp'"
141 }
142 }
143 } catch (Throwable e) {
144 // If there was an error or exception thrown, the build failed
145 currentBuild.result = "FAILURE"
146 throw e
147 } finally {
148 if (fileExists(testReportFile)) {
149 archiveArtifacts artifacts: testReportFile
150 }
151 stage("Cleanup") {
152 if (fileExists("${env.WORKSPACE}/venv")) {
153 dir(apiProject) {
Ivan Berezovskiy5e8b86a2019-01-11 18:39:28 +0400154 python.runVirtualenvCommand("${env.WORKSPACE}/venv", "./bootstrap_env.sh down || true")
Ivan Berezovskiya066c3b2019-01-10 18:21:35 +0400155 }
156 dir(uiProject) {
Ivan Berezovskiy5e8b86a2019-01-11 18:39:28 +0400157 python.runVirtualenvCommand("${env.WORKSPACE}/venv", "docker-compose down || true")
Ivan Berezovskiya066c3b2019-01-10 18:21:35 +0400158 }
159 sh "rm -rf ${env.WORKSPACE}/venv/"
160 }
161 if (apiImage && apiImage.id) {
Ivan Berezovskiy5e8b86a2019-01-11 18:39:28 +0400162 sh "docker rmi ${apiImage.id}"
Ivan Berezovskiya066c3b2019-01-10 18:21:35 +0400163 }
164 if (uiImage && uiImage.id) {
Ivan Berezovskiy5e8b86a2019-01-11 18:39:28 +0400165 sh "docker rmi ${uiImage.id}"
Ivan Berezovskiya066c3b2019-01-10 18:21:35 +0400166 }
167 // Remove everything what is owned by root
Ivan Berezovskiy5e8b86a2019-01-11 18:39:28 +0400168 testImage.inside("-u root:root" +
169 " -v ${env.WORKSPACE}/reports:/var/lib/qa_reports" +
170 "--entrypoint=/bin/bash") {
171 sh("rm -rf /var/lib/qa_reports/* /${env.WORKSPACE}/cockroach_data")
Ivan Berezovskiya066c3b2019-01-10 18:21:35 +0400172 }
173 }
174 }
175 }
176}