Ivan Berezovskiy | a066c3b | 2019-01-10 18:21:35 +0400 | [diff] [blame] | 1 | /** |
| 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 | |
| 16 | def common = new com.mirantis.mk.Common() |
| 17 | def gerrit = new com.mirantis.mk.Gerrit() |
Ivan Berezovskiy | 5e8b86a | 2019-01-11 18:39:28 +0400 | [diff] [blame] | 18 | def python = new com.mirantis.mk.Python() |
Ivan Berezovskiy | a066c3b | 2019-01-10 18:21:35 +0400 | [diff] [blame] | 19 | |
| 20 | def gerritCredentials = env.CREDENTIALS_ID ?: 'gerrit' |
| 21 | def slaveNode = env.SLAVE_NODE ?: 'python&&docker' |
| 22 | def event = env.GERRIT_EVENT_TYPE ?: null |
| 23 | def defaultRef = 'master' |
| 24 | def apiGerritRef = env.API_GERRIT_REF ?: defaultRef |
| 25 | def uiGerritRef = env.UI_GERRIT_REF ?: defaultRef |
Ivan Berezovskiy | 36ab072 | 2019-01-14 19:53:45 +0400 | [diff] [blame] | 26 | def version = env.MCP_VERSION ?: 'nightly' |
Ivan Berezovskiy | a066c3b | 2019-01-10 18:21:35 +0400 | [diff] [blame] | 27 | def dockerRegistry = env.DOCKER_REGISTRY ?: 'docker-prod-local.docker.mirantis.net' |
| 28 | def dockerReviewRegistry = env.DOCKER_REVIEW_REGISTRY ?: 'docker-dev-local.docker.mirantis.net' |
Ivan Berezovskiy | 5e8b86a | 2019-01-11 18:39:28 +0400 | [diff] [blame] | 29 | def cvpImageName = env.CVP_DOCKER_IMG ? "${dockerRegistry}/${env.CVP_DOCKER_IMG}:${version}" : "${dockerRegistry}/mirantis/cvp/cvp-trymcp-tests:${version}" |
Denis Egorenko | feb00a6 | 2019-06-04 15:41:10 +0400 | [diff] [blame] | 30 | if (env.CVP_DEV_TAG && env.CVP_DOCKER_IMG) { |
| 31 | cvpImageName = "${dockerReviewRegistry}/${env.CVP_DOCKER_IMG}:${env.CVP_DEV_TAG}" |
| 32 | } |
Ivan Berezovskiy | a066c3b | 2019-01-10 18:21:35 +0400 | [diff] [blame] | 33 | |
| 34 | def checkouted = false |
Ivan Berezovskiy | 7b2e3bc | 2019-01-14 20:34:42 +0400 | [diff] [blame] | 35 | def testReportHTMLFile = 'reports/report.html' |
| 36 | def testReportXMLFile = 'reports/report.xml' |
Ivan Berezovskiy | a066c3b | 2019-01-10 18:21:35 +0400 | [diff] [blame] | 37 | def manualTrigger = false |
| 38 | |
| 39 | def apiProject = 'operations-api' |
| 40 | def uiProject = 'operations-ui' |
| 41 | def apiImage |
| 42 | def uiImage |
Ivan Berezovskiy | eb93481 | 2019-01-30 13:48:02 +0400 | [diff] [blame] | 43 | def component = '' |
Ivan Berezovskiy | a066c3b | 2019-01-10 18:21:35 +0400 | [diff] [blame] | 44 | |
| 45 | timeout(time: 1, unit: 'HOURS') { |
| 46 | node(slaveNode) { |
| 47 | sh "mkdir -p reports ${apiProject} ${uiProject}" |
Ivan Berezovskiy | 5e8b86a | 2019-01-11 18:39:28 +0400 | [diff] [blame] | 48 | def testImage = docker.image(cvpImageName) |
Ivan Berezovskiy | 36ab072 | 2019-01-14 19:53:45 +0400 | [diff] [blame] | 49 | def testImageOptions = "-u root:root --network=host -v ${env.WORKSPACE}/reports:/var/lib/qa_reports --entrypoint=''" |
Hanna Arhipova | d3d01d0 | 2019-07-09 13:18:59 +0300 | [diff] [blame] | 50 | 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 Berezovskiy | a066c3b | 2019-01-10 18:21:35 +0400 | [diff] [blame] | 59 | try { |
| 60 | stage("checkout") { |
| 61 | if (event) { |
Ivan Berezovskiy | ba904d4 | 2019-01-14 13:14:05 +0400 | [diff] [blame] | 62 | dir(env.FLAVOR) { |
Ivan Berezovskiy | a066c3b | 2019-01-10 18:21:35 +0400 | [diff] [blame] | 63 | // 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 Berezovskiy | 5e8b86a | 2019-01-11 18:39:28 +0400 | [diff] [blame] | 93 | 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 Berezovskiy | eb93481 | 2019-01-30 13:48:02 +0400 | [diff] [blame] | 95 | component = "-k 'api'" |
Ivan Berezovskiy | a066c3b | 2019-01-10 18:21:35 +0400 | [diff] [blame] | 96 | } 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 Berezovskiy | 5e8b86a | 2019-01-11 18:39:28 +0400 | [diff] [blame] | 104 | 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 Berezovskiy | eb93481 | 2019-01-30 13:48:02 +0400 | [diff] [blame] | 106 | component = "-k 'ui'" |
Ivan Berezovskiy | a066c3b | 2019-01-10 18:21:35 +0400 | [diff] [blame] | 107 | } |
| 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 Berezovskiy | 5e8b86a | 2019-01-11 18:39:28 +0400 | [diff] [blame] | 121 | 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 Berezovskiy | a066c3b | 2019-01-10 18:21:35 +0400 | [diff] [blame] | 123 | } 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 Berezovskiy | 5e8b86a | 2019-01-11 18:39:28 +0400 | [diff] [blame] | 129 | 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 Berezovskiy | a066c3b | 2019-01-10 18:21:35 +0400 | [diff] [blame] | 138 | } |
| 139 | |
| 140 | stage('Prepare and run docker compose services') { |
Ivan Berezovskiy | 5e8b86a | 2019-01-11 18:39:28 +0400 | [diff] [blame] | 141 | python.setupVirtualenv("${env.WORKSPACE}/venv", 'python2', ['docker-compose==1.22.0']) |
Ivan Berezovskiy | a066c3b | 2019-01-10 18:21:35 +0400 | [diff] [blame] | 142 | |
Ivan Berezovskiy | 36ab072 | 2019-01-14 19:53:45 +0400 | [diff] [blame] | 143 | // 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 Berezovskiy | a066c3b | 2019-01-10 18:21:35 +0400 | [diff] [blame] | 150 | dir(apiProject) { |
Ivan Berezovskiy | 5e8b86a | 2019-01-11 18:39:28 +0400 | [diff] [blame] | 151 | python.runVirtualenvCommand("${env.WORKSPACE}/venv", |
Hanna Arhipova | d3d01d0 | 2019-07-09 13:18:59 +0300 | [diff] [blame] | 152 | "export IMAGE=${apiImage.id}; export DOCKER_COMPOSE=docker-compose-test.yml; ./bootstrap_env.sh up") |
Ivan Berezovskiy | 36ab072 | 2019-01-14 19:53:45 +0400 | [diff] [blame] | 153 | common.retry(5, 20) { |
| 154 | sh 'curl -v http://127.0.0.1:8001/api/v1 > /dev/null' |
| 155 | } |
Ivan Berezovskiy | a066c3b | 2019-01-10 18:21:35 +0400 | [diff] [blame] | 156 | } |
| 157 | dir(uiProject) { |
Ivan Berezovskiy | 5e8b86a | 2019-01-11 18:39:28 +0400 | [diff] [blame] | 158 | python.runVirtualenvCommand("${env.WORKSPACE}/venv", |
Hanna Arhipova | 21142d9 | 2019-03-27 15:04:36 +0200 | [diff] [blame] | 159 | "export IMAGE=${uiImage.id}; docker-compose -f docker-compose-test.yml up -d") |
Ivan Berezovskiy | 36ab072 | 2019-01-14 19:53:45 +0400 | [diff] [blame] | 160 | common.retry(5, 20) { |
| 161 | sh 'curl -v http://127.0.0.1:3000 > /dev/null' |
| 162 | } |
Ivan Berezovskiy | a066c3b | 2019-01-10 18:21:35 +0400 | [diff] [blame] | 163 | } |
| 164 | } |
| 165 | |
Ivan Berezovskiy | 5e8b86a | 2019-01-11 18:39:28 +0400 | [diff] [blame] | 166 | stage('Test') { |
Ivan Berezovskiy | 36ab072 | 2019-01-14 19:53:45 +0400 | [diff] [blame] | 167 | 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 Arhipova | d3d01d0 | 2019-07-09 13:18:59 +0300 | [diff] [blame] | 173 | export TEST_TIMEOUT=15 |
Ivan Berezovskiy | 36ab072 | 2019-01-14 19:53:45 +0400 | [diff] [blame] | 174 | cd /var/lib/trymcp-tests |
Hanna Arhipova | c5358e8 | 2019-04-22 11:00:42 +0300 | [diff] [blame] | 175 | pytest ${component} |
Ivan Berezovskiy | 36ab072 | 2019-01-14 19:53:45 +0400 | [diff] [blame] | 176 | """ |
Ivan Berezovskiy | a066c3b | 2019-01-10 18:21:35 +0400 | [diff] [blame] | 177 | } |
| 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 Arhipova | c80058e | 2019-04-03 18:34:47 +0300 | [diff] [blame] | 184 | 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 Berezovskiy | 7b2e3bc | 2019-01-14 20:34:42 +0400 | [diff] [blame] | 188 | if (fileExists(testReportHTMLFile)) { |
| 189 | archiveArtifacts artifacts: testReportHTMLFile |
| 190 | } |
| 191 | if (fileExists(testReportXMLFile)) { |
| 192 | archiveArtifacts artifacts: testReportXMLFile |
Ivan Berezovskiy | a066c3b | 2019-01-10 18:21:35 +0400 | [diff] [blame] | 193 | } |
| 194 | stage("Cleanup") { |
| 195 | if (fileExists("${env.WORKSPACE}/venv")) { |
| 196 | dir(apiProject) { |
Ivan Berezovskiy | 5e8b86a | 2019-01-11 18:39:28 +0400 | [diff] [blame] | 197 | python.runVirtualenvCommand("${env.WORKSPACE}/venv", "./bootstrap_env.sh down || true") |
Ivan Berezovskiy | a066c3b | 2019-01-10 18:21:35 +0400 | [diff] [blame] | 198 | } |
| 199 | dir(uiProject) { |
Ivan Berezovskiy | 5e8b86a | 2019-01-11 18:39:28 +0400 | [diff] [blame] | 200 | python.runVirtualenvCommand("${env.WORKSPACE}/venv", "docker-compose down || true") |
Ivan Berezovskiy | a066c3b | 2019-01-10 18:21:35 +0400 | [diff] [blame] | 201 | } |
| 202 | sh "rm -rf ${env.WORKSPACE}/venv/" |
| 203 | } |
Hanna Arhipova | eb7a747 | 2019-04-15 17:53:59 +0300 | [diff] [blame] | 204 | 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 Berezovskiy | a066c3b | 2019-01-10 18:21:35 +0400 | [diff] [blame] | 216 | } |
| 217 | // Remove everything what is owned by root |
Ivan Berezovskiy | 36ab072 | 2019-01-14 19:53:45 +0400 | [diff] [blame] | 218 | testImage.inside(testImageOptions) { |
Hanna Arhipova | c80058e | 2019-04-03 18:34:47 +0300 | [diff] [blame] | 219 | sh("rm -rf /var/lib/qa_reports/* ${env.WORKSPACE}/${apiProject} ${env.WORKSPACE}/${uiProject} ${env.WORKSPACE}/*.log") |
Ivan Berezovskiy | a066c3b | 2019-01-10 18:21:35 +0400 | [diff] [blame] | 220 | } |
| 221 | } |
| 222 | } |
| 223 | } |
| 224 | } |