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=''" |
Ivan Berezovskiy | a066c3b | 2019-01-10 18:21:35 +0400 | [diff] [blame] | 50 | try { |
| 51 | stage("checkout") { |
| 52 | if (event) { |
Ivan Berezovskiy | ba904d4 | 2019-01-14 13:14:05 +0400 | [diff] [blame] | 53 | dir(env.FLAVOR) { |
Ivan Berezovskiy | a066c3b | 2019-01-10 18:21:35 +0400 | [diff] [blame] | 54 | // job is triggered by Gerrit |
| 55 | def gerritChange = gerrit.getGerritChange(env.GERRIT_NAME, env.GERRIT_HOST, env.GERRIT_CHANGE_NUMBER, gerritCredentials, true) |
| 56 | if (gerritChange.commitMessage.contains("WIP")) { |
| 57 | common.successMsg("Commit message contains WIP, skipping tests") // do nothing |
| 58 | } else { |
| 59 | // test if change aren't already merged |
| 60 | def merged = gerritChange.status == "MERGED" |
| 61 | if (!merged) { |
| 62 | checkouted = gerrit.gerritPatchsetCheckout([ |
| 63 | credentialsId: gerritCredentials |
| 64 | ]) |
| 65 | } else { |
| 66 | common.successMsg("Change ${env.GERRIT_CHANGE_NUMBER} is already merged, no need to test them") |
| 67 | } |
| 68 | } |
| 69 | } |
| 70 | } else { |
| 71 | common.successMsg('Gerrit variables are not set. Assuming it is manual trigger') |
| 72 | manualTrigger = true |
| 73 | } |
| 74 | |
| 75 | if (checkouted) { |
| 76 | if (env.FLAVOR == apiProject) { |
| 77 | // Second project is UI |
| 78 | checkout([ |
| 79 | $class : 'GitSCM', |
| 80 | branches : [[name: 'FETCH_HEAD'],], |
| 81 | extensions : [[$class: 'RelativeTargetDirectory', relativeTargetDir: uiProject]], |
| 82 | userRemoteConfigs: [[url: env.UI_GERRIT_REPO, refspec: uiGerritRef, credentialsId: gerritCredentials],], |
| 83 | ]) |
Ivan Berezovskiy | 5e8b86a | 2019-01-11 18:39:28 +0400 | [diff] [blame] | 84 | apiImage = docker.image("${dockerReviewRegistry}/review/${env.FLAVOR}-${env.GERRIT_CHANGE_NUMBER}:${env.GERRIT_PATCHSET_NUMBER}") |
| 85 | 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] | 86 | component = "-k 'api'" |
Ivan Berezovskiy | a066c3b | 2019-01-10 18:21:35 +0400 | [diff] [blame] | 87 | } else if (env.FLAVOR == uiProject) { |
| 88 | // Second project is API |
| 89 | checkout([ |
| 90 | $class : 'GitSCM', |
| 91 | branches : [[name: 'FETCH_HEAD'],], |
| 92 | extensions : [[$class: 'RelativeTargetDirectory', relativeTargetDir: apiProject]], |
| 93 | userRemoteConfigs: [[url: env.API_GERRIT_REPO, refspec: apiGerritRef, credentialsId: gerritCredentials],], |
| 94 | ]) |
Ivan Berezovskiy | 5e8b86a | 2019-01-11 18:39:28 +0400 | [diff] [blame] | 95 | apiImage = docker.image("${dockerRegistry}/${env.API_DOCKER_IMG ?: "mirantis/model-generator/operations-api"}:${version}") |
| 96 | 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] | 97 | component = "-k 'ui'" |
Ivan Berezovskiy | a066c3b | 2019-01-10 18:21:35 +0400 | [diff] [blame] | 98 | } |
| 99 | } else if (manualTrigger) { |
| 100 | checkout([ |
| 101 | $class : 'GitSCM', |
| 102 | branches : [[name: 'FETCH_HEAD'],], |
| 103 | extensions : [[$class: 'RelativeTargetDirectory', relativeTargetDir: apiProject]], |
| 104 | userRemoteConfigs: [[url: env.API_GERRIT_REPO, refspec: apiGerritRef, credentialsId: gerritCredentials],], |
| 105 | ]) |
| 106 | checkout([ |
| 107 | $class : 'GitSCM', |
| 108 | branches : [[name: 'FETCH_HEAD'],], |
| 109 | extensions : [[$class: 'RelativeTargetDirectory', relativeTargetDir: uiProject]], |
| 110 | userRemoteConfigs: [[url: env.UI_GERRIT_REPO, refspec: uiGerritRef, credentialsId: gerritCredentials],], |
| 111 | ]) |
Ivan Berezovskiy | 5e8b86a | 2019-01-11 18:39:28 +0400 | [diff] [blame] | 112 | apiImage = docker.image("${dockerRegistry}/${env.API_DOCKER_IMG ?: "mirantis/model-generator/operations-api"}:${version}") |
| 113 | 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] | 114 | } else { |
| 115 | throw new Exception('Cannot checkout gerrit repositories. Please verify that parameters for repositories are properly set') |
| 116 | } |
| 117 | } |
| 118 | |
| 119 | stage('Pull docker images') { |
Ivan Berezovskiy | 5e8b86a | 2019-01-11 18:39:28 +0400 | [diff] [blame] | 120 | common.retry(3, 5) { |
| 121 | apiImage.pull() |
| 122 | } |
| 123 | common.retry(3, 5) { |
| 124 | uiImage.pull() |
| 125 | } |
| 126 | common.retry(3, 5) { |
| 127 | testImage.pull() |
| 128 | } |
Ivan Berezovskiy | a066c3b | 2019-01-10 18:21:35 +0400 | [diff] [blame] | 129 | } |
| 130 | |
| 131 | stage('Prepare and run docker compose services') { |
Ivan Berezovskiy | 5e8b86a | 2019-01-11 18:39:28 +0400 | [diff] [blame] | 132 | python.setupVirtualenv("${env.WORKSPACE}/venv", 'python2', ['docker-compose==1.22.0']) |
Ivan Berezovskiy | a066c3b | 2019-01-10 18:21:35 +0400 | [diff] [blame] | 133 | |
Ivan Berezovskiy | 36ab072 | 2019-01-14 19:53:45 +0400 | [diff] [blame] | 134 | // Make sure cockroach_data is cleaned up |
| 135 | if(fileExists("${apiProject}/cockroach_data")) { |
| 136 | testImage.inside(testImageOptions) { |
| 137 | sh("rm -rf ${env.WORKSPACE}/${apiProject}/cockroach_data") |
| 138 | } |
| 139 | } |
| 140 | |
Ivan Berezovskiy | a066c3b | 2019-01-10 18:21:35 +0400 | [diff] [blame] | 141 | dir(apiProject) { |
Ivan Berezovskiy | 5e8b86a | 2019-01-11 18:39:28 +0400 | [diff] [blame] | 142 | python.runVirtualenvCommand("${env.WORKSPACE}/venv", |
| 143 | "export IMAGE=${apiImage.id}; ./bootstrap_env.sh up") |
Ivan Berezovskiy | 36ab072 | 2019-01-14 19:53:45 +0400 | [diff] [blame] | 144 | common.retry(5, 20) { |
| 145 | sh 'curl -v http://127.0.0.1:8001/api/v1 > /dev/null' |
| 146 | } |
Ivan Berezovskiy | a066c3b | 2019-01-10 18:21:35 +0400 | [diff] [blame] | 147 | } |
| 148 | dir(uiProject) { |
Ivan Berezovskiy | 5e8b86a | 2019-01-11 18:39:28 +0400 | [diff] [blame] | 149 | python.runVirtualenvCommand("${env.WORKSPACE}/venv", |
Hanna Arhipova | 21142d9 | 2019-03-27 15:04:36 +0200 | [diff] [blame] | 150 | "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] | 151 | common.retry(5, 20) { |
| 152 | sh 'curl -v http://127.0.0.1:3000 > /dev/null' |
| 153 | } |
Ivan Berezovskiy | a066c3b | 2019-01-10 18:21:35 +0400 | [diff] [blame] | 154 | } |
| 155 | } |
| 156 | |
Ivan Berezovskiy | 5e8b86a | 2019-01-11 18:39:28 +0400 | [diff] [blame] | 157 | stage('Test') { |
Ivan Berezovskiy | 36ab072 | 2019-01-14 19:53:45 +0400 | [diff] [blame] | 158 | testImage.inside(testImageOptions) { |
| 159 | sh """ |
| 160 | export TEST_LOGIN=test |
| 161 | export TEST_PASSWORD=default |
| 162 | export TEST_MODELD_URL=127.0.0.1 |
| 163 | export TEST_MODELD_PORT=3000 |
Hanna Arhipova | eb7a747 | 2019-04-15 17:53:59 +0300 | [diff] [blame] | 164 | export TEST_TIMEOUT=30 |
Ivan Berezovskiy | 36ab072 | 2019-01-14 19:53:45 +0400 | [diff] [blame] | 165 | cd /var/lib/trymcp-tests |
Hanna Arhipova | c5358e8 | 2019-04-22 11:00:42 +0300 | [diff] [blame] | 166 | pytest ${component} |
Ivan Berezovskiy | 36ab072 | 2019-01-14 19:53:45 +0400 | [diff] [blame] | 167 | """ |
Ivan Berezovskiy | a066c3b | 2019-01-10 18:21:35 +0400 | [diff] [blame] | 168 | } |
| 169 | } |
| 170 | } catch (Throwable e) { |
| 171 | // If there was an error or exception thrown, the build failed |
| 172 | currentBuild.result = "FAILURE" |
| 173 | throw e |
| 174 | } finally { |
Hanna Arhipova | c80058e | 2019-04-03 18:34:47 +0300 | [diff] [blame] | 175 | sh (script: """map=\$(docker ps --format '{{.Names}}:{{.ID}}' --filter name=operations);\ |
| 176 | for cont in \$map ; do NAME="\${cont%%:*}";ID="\${cont##*:}"; docker logs \$ID > \$NAME.log 2>&1 ; done""") |
| 177 | archiveArtifacts "*.log" |
| 178 | |
Ivan Berezovskiy | 7b2e3bc | 2019-01-14 20:34:42 +0400 | [diff] [blame] | 179 | if (fileExists(testReportHTMLFile)) { |
| 180 | archiveArtifacts artifacts: testReportHTMLFile |
| 181 | } |
| 182 | if (fileExists(testReportXMLFile)) { |
| 183 | archiveArtifacts artifacts: testReportXMLFile |
Ivan Berezovskiy | a066c3b | 2019-01-10 18:21:35 +0400 | [diff] [blame] | 184 | } |
| 185 | stage("Cleanup") { |
| 186 | if (fileExists("${env.WORKSPACE}/venv")) { |
| 187 | dir(apiProject) { |
Ivan Berezovskiy | 5e8b86a | 2019-01-11 18:39:28 +0400 | [diff] [blame] | 188 | python.runVirtualenvCommand("${env.WORKSPACE}/venv", "./bootstrap_env.sh down || true") |
Ivan Berezovskiy | a066c3b | 2019-01-10 18:21:35 +0400 | [diff] [blame] | 189 | } |
| 190 | dir(uiProject) { |
Ivan Berezovskiy | 5e8b86a | 2019-01-11 18:39:28 +0400 | [diff] [blame] | 191 | python.runVirtualenvCommand("${env.WORKSPACE}/venv", "docker-compose down || true") |
Ivan Berezovskiy | a066c3b | 2019-01-10 18:21:35 +0400 | [diff] [blame] | 192 | } |
| 193 | sh "rm -rf ${env.WORKSPACE}/venv/" |
| 194 | } |
Hanna Arhipova | eb7a747 | 2019-04-15 17:53:59 +0300 | [diff] [blame] | 195 | try { |
| 196 | // to avoid issue PROD-29393 "pipeline freezes in `docker rmi` action" |
| 197 | timeout(time: 4, unit: 'MINUTES') { |
| 198 | if (apiImage && apiImage.id) { |
| 199 | sh "docker rmi ${apiImage.id}" |
| 200 | } |
| 201 | if (uiImage && uiImage.id) { |
| 202 | sh "docker rmi ${uiImage.id}" |
| 203 | } |
| 204 | } |
| 205 | } catch (Exception e) { |
| 206 | echo "Failed while cleaning docker images: ${e.toString()}" |
Ivan Berezovskiy | a066c3b | 2019-01-10 18:21:35 +0400 | [diff] [blame] | 207 | } |
| 208 | // Remove everything what is owned by root |
Ivan Berezovskiy | 36ab072 | 2019-01-14 19:53:45 +0400 | [diff] [blame] | 209 | testImage.inside(testImageOptions) { |
Hanna Arhipova | c80058e | 2019-04-03 18:34:47 +0300 | [diff] [blame] | 210 | 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] | 211 | } |
| 212 | } |
| 213 | } |
| 214 | } |
| 215 | } |