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() |
| 18 | def dockerLib = new com.mirantis.mk.Docker() |
| 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' |
| 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}" |
| 43 | def img = dockerLib.getImage("${env.CVP_DOCKER_IMG}:${version}", "${dockerRegistry}/mirantis/cvp/cvp-trymcp-tests:${version}") |
| 44 | try { |
| 45 | stage("checkout") { |
| 46 | if (event) { |
| 47 | dir(gerritProject) { |
| 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 | ]) |
| 78 | apiImage = image("${dockerReviewRegistry}/review/${env.FLAVOR}-${env.GERRIT_CHANGE_NUMBER}:${env.GERRIT_PATCHSET_NUMBER}") |
| 79 | uiImage = image("${dockerRegistry}/${env.UI_DOCKER_IMG ?: "mirantis/model-generator/operations-ui"}:${version}") |
| 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 | ]) |
| 88 | apiImage = image("${dockerRegistry}/${env.API_DOCKER_IMG ?: "mirantis/model-generator/operations-api"}:${version}") |
| 89 | uiImage = image("${dockerReviewRegistry}/review/${env.FLAVOR}-${env.GERRIT_CHANGE_NUMBER}:${env.GERRIT_PATCHSET_NUMBER}") |
| 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 | ]) |
| 104 | apiImage = image("${dockerRegistry}/${env.API_DOCKER_IMG ?: "mirantis/model-generator/operations-api"}:${version}") |
| 105 | uiImage = image("${dockerRegistry}/${env.UI_DOCKER_IMG ?: "mirantis/model-generator/operations-ui"}:${version}") |
| 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') { |
| 112 | apiImage.pull() |
| 113 | uiImage.pull() |
| 114 | } |
| 115 | |
| 116 | stage('Prepare and run docker compose services') { |
| 117 | sh """ |
| 118 | virtualenv ${env.WORKSPACE}/venv |
| 119 | source ${env.WORKSPACE}/venv/bin/activate |
| 120 | pip install docker-compose==1.22.0 |
| 121 | """ |
| 122 | |
| 123 | dir(apiProject) { |
| 124 | sh """ |
| 125 | export IMAGE=${apiImage} |
| 126 | source ${env.WORKSPACE}/venv/bin/activate && ./bootstrap_env.sh up |
| 127 | """ |
| 128 | } |
| 129 | dir(uiProject) { |
| 130 | sh """ |
| 131 | export IMAGE=${uiImage} |
| 132 | source ${env.WORKSPACE}/venv/bin/activate && docker-compose up -d |
| 133 | """ |
| 134 | } |
| 135 | } |
| 136 | |
| 137 | stage("test") { |
| 138 | img.inside("-u root:root -v ${env.WORKSPACE}/reports:/var/lib/qa_reports") { |
| 139 | sh "pytest -m 'not trymcp'" |
| 140 | } |
| 141 | } |
| 142 | } catch (Throwable e) { |
| 143 | // If there was an error or exception thrown, the build failed |
| 144 | currentBuild.result = "FAILURE" |
| 145 | throw e |
| 146 | } finally { |
| 147 | if (fileExists(testReportFile)) { |
| 148 | archiveArtifacts artifacts: testReportFile |
| 149 | } |
| 150 | stage("Cleanup") { |
| 151 | if (fileExists("${env.WORKSPACE}/venv")) { |
| 152 | dir(apiProject) { |
| 153 | sh "source ${env.WORKSPACE}/venv/bin/activate && ./bootstrap_env.sh down || true" |
| 154 | } |
| 155 | dir(uiProject) { |
| 156 | sh "source ${env.WORKSPACE}/venv/bin/activate && docker-compose down || true" |
| 157 | } |
| 158 | sh "rm -rf ${env.WORKSPACE}/venv/" |
| 159 | } |
| 160 | if (apiImage && apiImage.id) { |
| 161 | sh "docker rm -f ${apiImage.id}" |
| 162 | } |
| 163 | if (uiImage && uiImage.id) { |
| 164 | sh "docker rm -f ${uiImage.id}" |
| 165 | } |
| 166 | // Remove everything what is owned by root |
| 167 | img.inside("-u root:root -v ${env.WORKSPACE}:/temp") { |
| 168 | sh('rm -rf /temp/reports/* /temp/cockroach_data') |
| 169 | } |
| 170 | } |
| 171 | } |
| 172 | } |
| 173 | } |