blob: 401628bb1a84639e40321da522a445bd01d0a23c [file] [log] [blame]
Jakub Josef83379312017-03-29 18:12:34 +02001/**
2 * Test salt formulas pipeline
3 * DEFAULT_GIT_REF
4 * DEFAULT_GIT_URL
5 * CREDENTIALS_ID
Cedric Hnydac87fc6c2017-09-22 13:12:36 +00006 * KITCHEN_TESTS_PARALLEL
Sergey Otpuschennikove5a14742018-05-08 11:42:26 +04007 * RUN_TEST_IN_DOCKER If true, run test stage in docker
8 * SMOKE_TEST_DOCKER_IMG Docker image for run test (default "ubuntu:16.04")
Jakub Josef83379312017-03-29 18:12:34 +02009 */
chnyda85ea22d2017-10-23 13:05:16 +020010common = new com.mirantis.mk.Common()
Jakub Josefc5a223a2017-03-01 14:40:08 +010011def gerrit = new com.mirantis.mk.Gerrit()
Jakub Joseff64c6392017-05-03 13:18:25 +020012def ruby = new com.mirantis.mk.Ruby()
Jakub Josef83379312017-03-29 18:12:34 +020013
14def gerritRef
15try {
16 gerritRef = GERRIT_REFSPEC
17} catch (MissingPropertyException e) {
18 gerritRef = null
19}
20
21def defaultGitRef, defaultGitUrl
22try {
Martin Polreicha3e30122017-08-22 12:43:55 +020023 defaultGitRef = DEFAULT_GIT_REF
24 defaultGitUrl = DEFAULT_GIT_URL
Jakub Josef83379312017-03-29 18:12:34 +020025} catch (MissingPropertyException e) {
Martin Polreicha3e30122017-08-22 12:43:55 +020026 defaultGitRef = null
27 defaultGitUrl = null
Jakub Josef83379312017-03-29 18:12:34 +020028}
29
Jakub Josefbcd2e902017-06-13 14:40:41 +020030def checkouted = false
Jakub Josef83379312017-03-29 18:12:34 +020031
chnyda85ea22d2017-10-23 13:05:16 +020032futureFormulas = []
33failedFormulas = []
34
35def setupRunner(defaultGitRef, defaultGitUrl) {
36 def branches = [:]
37 for (int i = 0; i < PARALLEL_GROUP_SIZE.toInteger() && i < futureFormulas.size(); i++) {
38 branches["Runner ${i}"] = {
39 while (futureFormulas && !failedFormulas) {
40 def currentFormula = futureFormulas[0] ? futureFormulas[0] : null
41 if (!currentFormula) {
42 continue
43 }
44 futureFormulas.remove(currentFormula)
45 try {
46 triggerTestFormulaJob(currentFormula, defaultGitRef, defaultGitUrl)
47 } catch (Exception e) {
chnyda0513b572018-01-23 13:40:57 +010048 if (e.getMessage().contains("completed with status ABORTED")) {
49 common.warningMsg("Test of ${currentFormula} was aborted and will be retriggered")
50 futureFormulas << currentFormula
51 } else {
52 failedFormulas << currentFormula
53 common.warningMsg("Test of ${currentFormula} failed : ${e}")
54 }
chnyda85ea22d2017-10-23 13:05:16 +020055 }
56 }
57 }
58 }
59 parallel branches
60}
61
62def triggerTestFormulaJob(testEnv, defaultGitRef, defaultGitUrl) {
63 common.infoMsg("Test of ${testEnv} starts")
64 build job: "test-salt-formulas-env", parameters: [
65 [$class: 'StringParameterValue', name: 'CREDENTIALS_ID', value: CREDENTIALS_ID],
66 [$class: 'StringParameterValue', name: 'KITCHEN_ENV', value: testEnv],
67 [$class: 'StringParameterValue', name: 'DEFAULT_GIT_REF', value: defaultGitRef],
68 [$class: 'StringParameterValue', name: 'DEFAULT_GIT_URL', value: defaultGitUrl],
69 [$class: 'StringParameterValue', name: 'SALT_OPTS', value: SALT_OPTS],
70 [$class: 'StringParameterValue', name: 'SALT_VERSION', value: SALT_VERSION]
71 ]
72}
Jakub Josefa63f9862018-01-11 17:58:38 +010073timeout(time: 12, unit: 'HOURS') {
74 node("python") {
75 try {
76 stage("checkout") {
77 if (gerritRef) {
78 // job is triggered by Gerrit
79 def gerritChange = gerrit.getGerritChange(GERRIT_NAME, GERRIT_HOST, GERRIT_CHANGE_NUMBER, CREDENTIALS_ID, true)
80 // test if gerrit change is already Verified
81 if (gerrit.patchsetHasApproval(gerritChange.currentPatchSet, "Verified", "+")) {
82 common.successMsg("Gerrit change ${GERRIT_CHANGE_NUMBER} patchset ${GERRIT_PATCHSET_NUMBER} already has Verified, skipping tests") // do nothing
83 // test WIP contains in commit message
84 } else if (gerritChange.commitMessage.contains("WIP")) {
85 common.successMsg("Commit message contains WIP, skipping tests") // do nothing
Martin Polreicha3e30122017-08-22 12:43:55 +020086 } else {
Jakub Josefa63f9862018-01-11 17:58:38 +010087 // test if change aren't already merged
88 def merged = gerritChange.status == "MERGED"
89 if (!merged) {
90 checkouted = gerrit.gerritPatchsetCheckout([
91 credentialsId: CREDENTIALS_ID
92 ])
93 } else {
94 common.successMsg("Change ${GERRIT_CHANGE_NUMBER} is already merged, no need to test them")
95 }
Jakub Josefbcd2e902017-06-13 14:40:41 +020096 }
Jakub Josefa63f9862018-01-11 17:58:38 +010097 defaultGitUrl = "${GERRIT_SCHEME}://${GERRIT_NAME}@${GERRIT_HOST}:${GERRIT_PORT}/${GERRIT_PROJECT}"
98 defaultGitRef = GERRIT_REFSPEC
99 } else if (defaultGitRef && defaultGitUrl) {
100 checkouted = gerrit.gerritPatchsetCheckout(defaultGitUrl, defaultGitRef, "HEAD", CREDENTIALS_ID)
101 } else {
102 throw new Exception("Cannot checkout gerrit patchset, GERRIT_REFSPEC and DEFAULT_GIT_REF is null")
Jakub Josef740a7882017-06-01 14:54:01 +0200103 }
Jakub Josefc5a223a2017-03-01 14:40:08 +0100104 }
105 stage("test") {
Martin Polreicha3e30122017-08-22 12:43:55 +0200106 if (checkouted) {
azvyagintsev3a2e0352018-01-10 12:41:29 +0200107 try {
108 saltVersion = SALT_VERSION
Jakub Josefa63f9862018-01-11 17:58:38 +0100109 } catch (MissingPropertyException e) {
Jakub Josef566dc4e2018-02-07 11:29:30 +0100110 saltVersion = "" // default value is empty string, means latest
azvyagintsev3a2e0352018-01-10 12:41:29 +0200111 }
112 withEnv(["SALT_VERSION=${saltVersion}"]) {
Sergey Otpuschennikovb570b4f2018-05-28 13:18:16 +0400113 boolean run_test_in_docker = (env.RUN_TEST_IN_DOCKER ?: false).toBoolean()
Sergey Otpuschennikove5a14742018-05-08 11:42:26 +0400114 if (run_test_in_docker) {
115 def dockerLib = new com.mirantis.mk.Docker()
116 def img = dockerLib.getImage(env.SMOKE_TEST_DOCKER_IMG, "ubuntu:16.04")
117 def workspace = common.getWorkspace()
118 img.inside("-u root:root -v ${workspace}/:/formula/") {
119 sh("""cd /etc/apt/ && echo > sources.list \
120 && echo "deb [arch=amd64] http://cz.archive.ubuntu.com/ubuntu xenial main restricted universe multiverse" >> sources.list \
121 && echo "deb [arch=amd64] http://cz.archive.ubuntu.com/ubuntu xenial-updates main restricted universe multiverse" >> sources.list \
122 && echo "deb [arch=amd64] http://cz.archive.ubuntu.com/ubuntu xenial-backports main restricted universe multiverse" >> sources.list \
123 && echo 'Acquire::Languages "none";' > apt.conf.d/docker-no-languages \
124 && echo 'Acquire::GzipIndexes "true"; Acquire::CompressionTypes::Order:: "gz";' > apt.conf.d/docker-gzip-indexes \
125 && echo 'APT::Get::Install-Recommends "false"; APT::Get::Install-Suggests "false";' > apt.conf.d/docker-recommends \
126 && apt-get update \
127 && apt-get install -y git-core wget curl apt-transport-https \
128 && apt-get install -y python-pip python3-pip python-virtualenv python3-virtualenv python-yaml autoconf build-essential""")
129 sh("cd /formula/ && make clean && make test")
130 }
131 } else {
132 common.warningMsg("Those tests should be always be run in clean env! Recommends to use docker env!")
133 sh("make clean && make test")
134 }
azvyagintsev3a2e0352018-01-10 12:41:29 +0200135 }
Jakub Josefc5a223a2017-03-01 14:40:08 +0100136 }
137 }
Jakub Joseff64c6392017-05-03 13:18:25 +0200138 stage("kitchen") {
Jakub Josefa63f9862018-01-11 17:58:38 +0100139 if (checkouted) {
140 if (fileExists(".kitchen.yml")) {
141 common.infoMsg(".kitchen.yml found, running kitchen tests")
142 def kitchenEnvs = []
143 def filteredEnvs = []
144 if (fileExists(".travis.yml")) {
145 common.infoMsg(".travis.yml file found.")
146 def kitchenConfigYML = readYaml(file: ".travis.yml")
147 if (kitchenConfigYML.containsKey("env")) {
148 kitchenEnvs = kitchenConfigYML["env"]
149 }
150 } else {
151 common.warningMsg(".travis.yml file not found, suites must be passed via CUSTOM_KITCHEN_ENVS parameter.")
Ruslan Kamaldinov310ffc02017-08-08 16:07:42 +0400152 }
Jakub Josefa63f9862018-01-11 17:58:38 +0100153 common.infoMsg("Running kitchen testing in parallel mode")
154 if (CUSTOM_KITCHEN_ENVS != null && CUSTOM_KITCHEN_ENVS != '') {
155 kitchenEnvs = CUSTOM_KITCHEN_ENVS.tokenize('\n')
156 common.infoMsg("CUSTOM_KITCHEN_ENVS not empty. Running with custom enviroments: ${kitchenEnvs}")
Martin Polreicha3e30122017-08-22 12:43:55 +0200157 }
Jakub Josefa63f9862018-01-11 17:58:38 +0100158 if (kitchenEnvs != null && kitchenEnvs != '') {
159 def acc = 0
160 common.infoMsg("Found " + kitchenEnvs.size() + " environment(s)")
161 for (int i = 0; i < kitchenEnvs.size(); i++) {
162 futureFormulas << kitchenEnvs[i]
163 }
164 setupRunner(defaultGitRef, defaultGitUrl)
165 } else {
166 common.warningMsg(".kitchen.yml file not found, no kitchen tests triggered.")
167 }
Martin Polreicha3e30122017-08-22 12:43:55 +0200168 }
Jakub Joseff64c6392017-05-03 13:18:25 +0200169 }
Jakub Joseff64c6392017-05-03 13:18:25 +0200170 }
Jakub Josefa63f9862018-01-11 17:58:38 +0100171 if (failedFormulas) {
172 currentBuild.result = "FAILURE"
173 common.warningMsg("The following tests failed: ${failedFormulas}")
174 }
175 } catch (Throwable e) {
176 // If there was an error or exception thrown, the build failed
chnyda85ea22d2017-10-23 13:05:16 +0200177 currentBuild.result = "FAILURE"
Jakub Josefa63f9862018-01-11 17:58:38 +0100178 currentBuild.description = currentBuild.description ? e.message + " " + currentBuild.description : e.message
179 throw e
180 } finally {
181 if (currentBuild.result == "FAILURE" && fileExists(".kitchen/logs/kitchen.log")) {
182 common.errorMsg("----------------KITCHEN LOG:---------------")
183 println readFile(".kitchen/logs/kitchen.log")
184 }
Kirill Mashchenko4ccd6e02018-05-22 14:32:22 +0300185 def slack = new com.mirantis.mcp.SlackNotification()
186 slack.jobResultNotification("success", "#test_reclass_notify")
Jakub Josefa63f9862018-01-11 17:58:38 +0100187 common.sendNotification(currentBuild.result, "", ["slack"])
chnyda85ea22d2017-10-23 13:05:16 +0200188 }
Jakub Josefc5a223a2017-03-01 14:40:08 +0100189 }
azvyagintsev3a2e0352018-01-10 12:41:29 +0200190}