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