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 |
| 26 | def gerritProject = env.GERRIT_PROJECT ?: null |
| 27 | def version = env.MCP_VERSION ?: 'testing' |
| 28 | def dockerRegistry = env.DOCKER_REGISTRY ?: 'docker-prod-local.docker.mirantis.net' |
| 29 | def dockerReviewRegistry = env.DOCKER_REVIEW_REGISTRY ?: 'docker-dev-local.docker.mirantis.net' |
Ivan Berezovskiy | 5e8b86a | 2019-01-11 18:39:28 +0400 | [diff] [blame] | 30 | def cvpImageName = env.CVP_DOCKER_IMG ? "${dockerRegistry}/${env.CVP_DOCKER_IMG}:${version}" : "${dockerRegistry}/mirantis/cvp/cvp-trymcp-tests:${version}" |
Ivan Berezovskiy | a066c3b | 2019-01-10 18:21:35 +0400 | [diff] [blame] | 31 | |
| 32 | def checkouted = false |
| 33 | def testReportFile = "${env.WORKSPACE}/reports/report.html" |
| 34 | def manualTrigger = false |
| 35 | |
| 36 | def apiProject = 'operations-api' |
| 37 | def uiProject = 'operations-ui' |
| 38 | def apiImage |
| 39 | def uiImage |
| 40 | |
| 41 | timeout(time: 1, unit: 'HOURS') { |
| 42 | node(slaveNode) { |
| 43 | sh "mkdir -p reports ${apiProject} ${uiProject}" |
Ivan Berezovskiy | 5e8b86a | 2019-01-11 18:39:28 +0400 | [diff] [blame] | 44 | def testImage = docker.image(cvpImageName) |
Ivan Berezovskiy | a066c3b | 2019-01-10 18:21:35 +0400 | [diff] [blame] | 45 | try { |
| 46 | stage("checkout") { |
| 47 | if (event) { |
| 48 | dir(gerritProject) { |
| 49 | // job is triggered by Gerrit |
| 50 | def gerritChange = gerrit.getGerritChange(env.GERRIT_NAME, env.GERRIT_HOST, env.GERRIT_CHANGE_NUMBER, gerritCredentials, true) |
| 51 | if (gerritChange.commitMessage.contains("WIP")) { |
| 52 | common.successMsg("Commit message contains WIP, skipping tests") // do nothing |
| 53 | } else { |
| 54 | // test if change aren't already merged |
| 55 | def merged = gerritChange.status == "MERGED" |
| 56 | if (!merged) { |
| 57 | checkouted = gerrit.gerritPatchsetCheckout([ |
| 58 | credentialsId: gerritCredentials |
| 59 | ]) |
| 60 | } else { |
| 61 | common.successMsg("Change ${env.GERRIT_CHANGE_NUMBER} is already merged, no need to test them") |
| 62 | } |
| 63 | } |
| 64 | } |
| 65 | } else { |
| 66 | common.successMsg('Gerrit variables are not set. Assuming it is manual trigger') |
| 67 | manualTrigger = true |
| 68 | } |
| 69 | |
| 70 | if (checkouted) { |
| 71 | if (env.FLAVOR == apiProject) { |
| 72 | // Second project is UI |
| 73 | checkout([ |
| 74 | $class : 'GitSCM', |
| 75 | branches : [[name: 'FETCH_HEAD'],], |
| 76 | extensions : [[$class: 'RelativeTargetDirectory', relativeTargetDir: uiProject]], |
| 77 | userRemoteConfigs: [[url: env.UI_GERRIT_REPO, refspec: uiGerritRef, credentialsId: gerritCredentials],], |
| 78 | ]) |
Ivan Berezovskiy | 5e8b86a | 2019-01-11 18:39:28 +0400 | [diff] [blame] | 79 | apiImage = docker.image("${dockerReviewRegistry}/review/${env.FLAVOR}-${env.GERRIT_CHANGE_NUMBER}:${env.GERRIT_PATCHSET_NUMBER}") |
| 80 | 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] | 81 | } else if (env.FLAVOR == uiProject) { |
| 82 | // Second project is API |
| 83 | checkout([ |
| 84 | $class : 'GitSCM', |
| 85 | branches : [[name: 'FETCH_HEAD'],], |
| 86 | extensions : [[$class: 'RelativeTargetDirectory', relativeTargetDir: apiProject]], |
| 87 | userRemoteConfigs: [[url: env.API_GERRIT_REPO, refspec: apiGerritRef, credentialsId: gerritCredentials],], |
| 88 | ]) |
Ivan Berezovskiy | 5e8b86a | 2019-01-11 18:39:28 +0400 | [diff] [blame] | 89 | apiImage = docker.image("${dockerRegistry}/${env.API_DOCKER_IMG ?: "mirantis/model-generator/operations-api"}:${version}") |
| 90 | uiImage = docker.image("${dockerReviewRegistry}/review/${env.FLAVOR}-${env.GERRIT_CHANGE_NUMBER}:${env.GERRIT_PATCHSET_NUMBER}") |
Ivan Berezovskiy | a066c3b | 2019-01-10 18:21:35 +0400 | [diff] [blame] | 91 | } |
| 92 | } else if (manualTrigger) { |
| 93 | checkout([ |
| 94 | $class : 'GitSCM', |
| 95 | branches : [[name: 'FETCH_HEAD'],], |
| 96 | extensions : [[$class: 'RelativeTargetDirectory', relativeTargetDir: apiProject]], |
| 97 | userRemoteConfigs: [[url: env.API_GERRIT_REPO, refspec: apiGerritRef, credentialsId: gerritCredentials],], |
| 98 | ]) |
| 99 | checkout([ |
| 100 | $class : 'GitSCM', |
| 101 | branches : [[name: 'FETCH_HEAD'],], |
| 102 | extensions : [[$class: 'RelativeTargetDirectory', relativeTargetDir: uiProject]], |
| 103 | userRemoteConfigs: [[url: env.UI_GERRIT_REPO, refspec: uiGerritRef, credentialsId: gerritCredentials],], |
| 104 | ]) |
Ivan Berezovskiy | 5e8b86a | 2019-01-11 18:39:28 +0400 | [diff] [blame] | 105 | apiImage = docker.image("${dockerRegistry}/${env.API_DOCKER_IMG ?: "mirantis/model-generator/operations-api"}:${version}") |
| 106 | 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] | 107 | } else { |
| 108 | throw new Exception('Cannot checkout gerrit repositories. Please verify that parameters for repositories are properly set') |
| 109 | } |
| 110 | } |
| 111 | |
| 112 | stage('Pull docker images') { |
Ivan Berezovskiy | 5e8b86a | 2019-01-11 18:39:28 +0400 | [diff] [blame] | 113 | common.retry(3, 5) { |
| 114 | apiImage.pull() |
| 115 | } |
| 116 | common.retry(3, 5) { |
| 117 | uiImage.pull() |
| 118 | } |
| 119 | common.retry(3, 5) { |
| 120 | testImage.pull() |
| 121 | } |
Ivan Berezovskiy | a066c3b | 2019-01-10 18:21:35 +0400 | [diff] [blame] | 122 | } |
| 123 | |
| 124 | stage('Prepare and run docker compose services') { |
Ivan Berezovskiy | 5e8b86a | 2019-01-11 18:39:28 +0400 | [diff] [blame] | 125 | python.setupVirtualenv("${env.WORKSPACE}/venv", 'python2', ['docker-compose==1.22.0']) |
Ivan Berezovskiy | a066c3b | 2019-01-10 18:21:35 +0400 | [diff] [blame] | 126 | |
| 127 | dir(apiProject) { |
Ivan Berezovskiy | 5e8b86a | 2019-01-11 18:39:28 +0400 | [diff] [blame] | 128 | python.runVirtualenvCommand("${env.WORKSPACE}/venv", |
| 129 | "export IMAGE=${apiImage.id}; ./bootstrap_env.sh up") |
Ivan Berezovskiy | a066c3b | 2019-01-10 18:21:35 +0400 | [diff] [blame] | 130 | } |
| 131 | dir(uiProject) { |
Ivan Berezovskiy | 5e8b86a | 2019-01-11 18:39:28 +0400 | [diff] [blame] | 132 | python.runVirtualenvCommand("${env.WORKSPACE}/venv", |
| 133 | "export IMAGE=${uiImage.id}; docker-compose up -d") |
Ivan Berezovskiy | a066c3b | 2019-01-10 18:21:35 +0400 | [diff] [blame] | 134 | } |
| 135 | } |
| 136 | |
Ivan Berezovskiy | 5e8b86a | 2019-01-11 18:39:28 +0400 | [diff] [blame] | 137 | stage('Test') { |
| 138 | testImage.inside("-u root:root" + |
| 139 | " -v ${env.WORKSPACE}/reports:/var/lib/qa_reports" + |
| 140 | "--entrypoint=/bin/bash") { |
Ivan Berezovskiy | a066c3b | 2019-01-10 18:21:35 +0400 | [diff] [blame] | 141 | sh "pytest -m 'not trymcp'" |
| 142 | } |
| 143 | } |
| 144 | } catch (Throwable e) { |
| 145 | // If there was an error or exception thrown, the build failed |
| 146 | currentBuild.result = "FAILURE" |
| 147 | throw e |
| 148 | } finally { |
| 149 | if (fileExists(testReportFile)) { |
| 150 | archiveArtifacts artifacts: testReportFile |
| 151 | } |
| 152 | stage("Cleanup") { |
| 153 | if (fileExists("${env.WORKSPACE}/venv")) { |
| 154 | dir(apiProject) { |
Ivan Berezovskiy | 5e8b86a | 2019-01-11 18:39:28 +0400 | [diff] [blame] | 155 | python.runVirtualenvCommand("${env.WORKSPACE}/venv", "./bootstrap_env.sh down || true") |
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", "docker-compose down || true") |
Ivan Berezovskiy | a066c3b | 2019-01-10 18:21:35 +0400 | [diff] [blame] | 159 | } |
| 160 | sh "rm -rf ${env.WORKSPACE}/venv/" |
| 161 | } |
| 162 | if (apiImage && apiImage.id) { |
Ivan Berezovskiy | 5e8b86a | 2019-01-11 18:39:28 +0400 | [diff] [blame] | 163 | sh "docker rmi ${apiImage.id}" |
Ivan Berezovskiy | a066c3b | 2019-01-10 18:21:35 +0400 | [diff] [blame] | 164 | } |
| 165 | if (uiImage && uiImage.id) { |
Ivan Berezovskiy | 5e8b86a | 2019-01-11 18:39:28 +0400 | [diff] [blame] | 166 | sh "docker rmi ${uiImage.id}" |
Ivan Berezovskiy | a066c3b | 2019-01-10 18:21:35 +0400 | [diff] [blame] | 167 | } |
| 168 | // Remove everything what is owned by root |
Ivan Berezovskiy | 5e8b86a | 2019-01-11 18:39:28 +0400 | [diff] [blame] | 169 | testImage.inside("-u root:root" + |
| 170 | " -v ${env.WORKSPACE}/reports:/var/lib/qa_reports" + |
| 171 | "--entrypoint=/bin/bash") { |
| 172 | sh("rm -rf /var/lib/qa_reports/* /${env.WORKSPACE}/cockroach_data") |
Ivan Berezovskiy | a066c3b | 2019-01-10 18:21:35 +0400 | [diff] [blame] | 173 | } |
| 174 | } |
| 175 | } |
| 176 | } |
| 177 | } |