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 | a066c3b | 2019-01-10 18:21:35 +0400 | [diff] [blame] | 26 | def version = env.MCP_VERSION ?: 'testing' |
| 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}" |
Ivan Berezovskiy | a066c3b | 2019-01-10 18:21:35 +0400 | [diff] [blame] | 30 | |
| 31 | def checkouted = false |
| 32 | def testReportFile = "${env.WORKSPACE}/reports/report.html" |
| 33 | def manualTrigger = false |
| 34 | |
| 35 | def apiProject = 'operations-api' |
| 36 | def uiProject = 'operations-ui' |
| 37 | def apiImage |
| 38 | def uiImage |
| 39 | |
| 40 | timeout(time: 1, unit: 'HOURS') { |
| 41 | node(slaveNode) { |
| 42 | sh "mkdir -p reports ${apiProject} ${uiProject}" |
Ivan Berezovskiy | 5e8b86a | 2019-01-11 18:39:28 +0400 | [diff] [blame] | 43 | def testImage = docker.image(cvpImageName) |
Ivan Berezovskiy | a066c3b | 2019-01-10 18:21:35 +0400 | [diff] [blame] | 44 | try { |
| 45 | stage("checkout") { |
| 46 | if (event) { |
Ivan Berezovskiy | ba904d4 | 2019-01-14 13:14:05 +0400 | [diff] [blame^] | 47 | dir(env.FLAVOR) { |
Ivan Berezovskiy | a066c3b | 2019-01-10 18:21:35 +0400 | [diff] [blame] | 48 | // 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 Berezovskiy | 5e8b86a | 2019-01-11 18:39:28 +0400 | [diff] [blame] | 78 | 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 Berezovskiy | a066c3b | 2019-01-10 18:21:35 +0400 | [diff] [blame] | 80 | } 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 Berezovskiy | 5e8b86a | 2019-01-11 18:39:28 +0400 | [diff] [blame] | 88 | 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 Berezovskiy | a066c3b | 2019-01-10 18:21:35 +0400 | [diff] [blame] | 90 | } |
| 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 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("${dockerRegistry}/${env.UI_DOCKER_IMG ?: "mirantis/model-generator/operations-ui"}:${version}") |
Ivan Berezovskiy | a066c3b | 2019-01-10 18:21:35 +0400 | [diff] [blame] | 106 | } 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 Berezovskiy | 5e8b86a | 2019-01-11 18:39:28 +0400 | [diff] [blame] | 112 | 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 Berezovskiy | a066c3b | 2019-01-10 18:21:35 +0400 | [diff] [blame] | 121 | } |
| 122 | |
| 123 | stage('Prepare and run docker compose services') { |
Ivan Berezovskiy | 5e8b86a | 2019-01-11 18:39:28 +0400 | [diff] [blame] | 124 | python.setupVirtualenv("${env.WORKSPACE}/venv", 'python2', ['docker-compose==1.22.0']) |
Ivan Berezovskiy | a066c3b | 2019-01-10 18:21:35 +0400 | [diff] [blame] | 125 | |
| 126 | dir(apiProject) { |
Ivan Berezovskiy | 5e8b86a | 2019-01-11 18:39:28 +0400 | [diff] [blame] | 127 | python.runVirtualenvCommand("${env.WORKSPACE}/venv", |
| 128 | "export IMAGE=${apiImage.id}; ./bootstrap_env.sh up") |
Ivan Berezovskiy | a066c3b | 2019-01-10 18:21:35 +0400 | [diff] [blame] | 129 | } |
| 130 | dir(uiProject) { |
Ivan Berezovskiy | 5e8b86a | 2019-01-11 18:39:28 +0400 | [diff] [blame] | 131 | python.runVirtualenvCommand("${env.WORKSPACE}/venv", |
| 132 | "export IMAGE=${uiImage.id}; docker-compose up -d") |
Ivan Berezovskiy | a066c3b | 2019-01-10 18:21:35 +0400 | [diff] [blame] | 133 | } |
| 134 | } |
| 135 | |
Ivan Berezovskiy | 5e8b86a | 2019-01-11 18:39:28 +0400 | [diff] [blame] | 136 | stage('Test') { |
| 137 | testImage.inside("-u root:root" + |
| 138 | " -v ${env.WORKSPACE}/reports:/var/lib/qa_reports" + |
| 139 | "--entrypoint=/bin/bash") { |
Ivan Berezovskiy | a066c3b | 2019-01-10 18:21:35 +0400 | [diff] [blame] | 140 | 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 Berezovskiy | 5e8b86a | 2019-01-11 18:39:28 +0400 | [diff] [blame] | 154 | python.runVirtualenvCommand("${env.WORKSPACE}/venv", "./bootstrap_env.sh down || true") |
Ivan Berezovskiy | a066c3b | 2019-01-10 18:21:35 +0400 | [diff] [blame] | 155 | } |
| 156 | dir(uiProject) { |
Ivan Berezovskiy | 5e8b86a | 2019-01-11 18:39:28 +0400 | [diff] [blame] | 157 | python.runVirtualenvCommand("${env.WORKSPACE}/venv", "docker-compose down || true") |
Ivan Berezovskiy | a066c3b | 2019-01-10 18:21:35 +0400 | [diff] [blame] | 158 | } |
| 159 | sh "rm -rf ${env.WORKSPACE}/venv/" |
| 160 | } |
| 161 | if (apiImage && apiImage.id) { |
Ivan Berezovskiy | 5e8b86a | 2019-01-11 18:39:28 +0400 | [diff] [blame] | 162 | sh "docker rmi ${apiImage.id}" |
Ivan Berezovskiy | a066c3b | 2019-01-10 18:21:35 +0400 | [diff] [blame] | 163 | } |
| 164 | if (uiImage && uiImage.id) { |
Ivan Berezovskiy | 5e8b86a | 2019-01-11 18:39:28 +0400 | [diff] [blame] | 165 | sh "docker rmi ${uiImage.id}" |
Ivan Berezovskiy | a066c3b | 2019-01-10 18:21:35 +0400 | [diff] [blame] | 166 | } |
| 167 | // Remove everything what is owned by root |
Ivan Berezovskiy | 5e8b86a | 2019-01-11 18:39:28 +0400 | [diff] [blame] | 168 | 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 Berezovskiy | a066c3b | 2019-01-10 18:21:35 +0400 | [diff] [blame] | 172 | } |
| 173 | } |
| 174 | } |
| 175 | } |
| 176 | } |