blob: de631bf0787927c9906592dd2a9ff1502e7f72cc [file] [log] [blame]
Martin Polreicha3e30122017-08-22 12:43:55 +02001/**
2 * Test salt formulas pipeline
3 * DEFAULT_GIT_REF
4 * DEFAULT_GIT_URL
5 * CREDENTIALS_ID
6 */
7def common = new com.mirantis.mk.Common()
8def ruby = new com.mirantis.mk.Ruby()
Martin Polreich9a1d16b2017-08-29 17:16:12 +02009def gerrit = new com.mirantis.mk.Gerrit()
Martin Polreicha3e30122017-08-22 12:43:55 +020010
11def defaultGitRef, defaultGitUrl
12try {
13 defaultGitRef = DEFAULT_GIT_REF
14 defaultGitUrl = DEFAULT_GIT_URL
15} catch (MissingPropertyException e) {
16 defaultGitRef = null
17 defaultGitUrl = null
18}
19
Martin Polreichc39edae2018-11-07 08:42:11 +010020def openstack_credentials_id = ''
21if (env.OPENSTACK_API_CREDENTIALS) {
22 openstack_credentials_id = OPENSTACK_API_CREDENTIALS
23}
24
Martin Polreich4dedbcf2019-04-09 09:28:46 +020025env.GERRIT_BRANCH = 'master'
Martin Polreich37da1312019-04-09 15:41:46 +020026if (common.validInputParam('GERRIT_PARENT_BRANCH')) {
Martin Polreich4dedbcf2019-04-09 09:28:46 +020027 env.GERRIT_BRANCH = GERRIT_PARENT_BRANCH
28}
29
Martin Polreicha3e30122017-08-22 12:43:55 +020030def checkouted = false
Martin Polreichc0995dc2018-11-05 14:40:02 +010031def openstackTest = false
Martin Polreich32b82222018-11-21 14:01:39 +010032def travisLess = false /** TODO: Remove once formulas are witched to new config */
33def cleanEnv = '' /** TODO: Remove once formulas are witched to new config */
34def testSuite = ''
Martin Polreich5be29e32019-02-26 11:11:51 +010035envOverrides = []
36kitchenFileName = ''
Martin Polreicha3e30122017-08-22 12:43:55 +020037
chnyda03f3ad42017-09-19 14:41:07 +020038throttle(['test-formula']) {
Jakub Josefdb3ddd82018-01-23 13:30:47 +010039 timeout(time: 1, unit: 'HOURS') {
Vladimir Khlyunev2140cbb2020-06-17 12:34:23 +000040 node("python&&docker") {
Jakub Josefa63f9862018-01-11 17:58:38 +010041 try {
42 stage("checkout") {
43 if (defaultGitRef && defaultGitUrl) {
44 checkouted = gerrit.gerritPatchsetCheckout(defaultGitUrl, defaultGitRef, "HEAD", CREDENTIALS_ID)
chnyda03f3ad42017-09-19 14:41:07 +020045 } else {
Jakub Josefa63f9862018-01-11 17:58:38 +010046 throw new Exception("Cannot checkout gerrit patchset, DEFAULT_GIT_REF is null")
chnyda03f3ad42017-09-19 14:41:07 +020047 }
48 }
Jakub Josefa63f9862018-01-11 17:58:38 +010049 stage("cleanup") {
50 if (checkouted) {
51 sh("make clean")
52 }
53 }
54 stage("kitchen") {
Martin Polreich32b82222018-11-21 14:01:39 +010055 if (fileExists(".travis.yml")) {/** TODO: Remove this legacy block once formulas are witched to new config */
56 if (checkouted) {
57 if (fileExists(".kitchen.yml") || fileExists(".kitchen.openstack.yml")) {
58 if (fileExists(".kitchen.openstack.yml")) {
59 common.infoMsg("Openstack Kitchen test configuration found, running Openstack kitchen tests.")
60 if (fileExists(".kitchen.yml")) {
61 common.infoMsg("Ignoring the docker Kitchen test configuration file.")
62 }
63 openstackTest = true
64 } else {
65 common.infoMsg("Docker Kitchen test configuration found, running Docker kitchen tests.")
Martin Polreichc0995dc2018-11-05 14:40:02 +010066 }
Martin Polreich32b82222018-11-21 14:01:39 +010067 ruby.ensureRubyEnv()
68 if (fileExists(".travis.yml")) {
69 common.infoMsg(".travis.yml found, running custom kitchen init")
70 def kitchenConfigYML = readYaml(file: ".travis.yml")
71 def kitchenInit = kitchenConfigYML["install"]
72 def kitchenInstalled = false
73 if (kitchenInit && !kitchenInit.isEmpty()) {
74 for (int i = 0; i < kitchenInit.size(); i++) {
75 if (kitchenInit[i].trim().startsWith("if [ ! -e Gemfile ]")) { //found Gemfile config
76 common.infoMsg("Custom Gemfile configuration found, using them")
77 if (openstackTest) {
78 withCredentials([[$class: 'UsernamePasswordMultiBinding', credentialsId: openstack_credentials_id,
79 usernameVariable: 'OS_USERNAME', passwordVariable: 'OS_PASSWORD'], ]) {
80 env.OS_USERNAME = OS_USERNAME
81 env.OS_PASSWORD = OS_PASSWORD
82 env.OS_AUTH_URL = OS_AUTH_URL
83 env.OS_PROJECT_NAME = OS_PROJECT_NAME
84 env.OS_DOMAIN_NAME = OS_DOMAIN_NAME
85 env.OS_AZ = OS_AZ
86 }
Martin Polreichc39edae2018-11-07 08:42:11 +010087 }
Martin Polreich32b82222018-11-21 14:01:39 +010088 ruby.installKitchen(kitchenInit[i].trim())
89 kitchenInstalled = true
Martin Polreichc0995dc2018-11-05 14:40:02 +010090 }
Jakub Josefa63f9862018-01-11 17:58:38 +010091 }
92 }
Martin Polreich32b82222018-11-21 14:01:39 +010093 if (!kitchenInstalled) {
94 ruby.installKitchen()
95 }
96 } else {
97 common.infoMsg(".travis.yml not found, running default kitchen init")
Jakub Josefa63f9862018-01-11 17:58:38 +010098 ruby.installKitchen()
99 }
Martin Polreich32b82222018-11-21 14:01:39 +0100100 common.infoMsg("Running part of kitchen test")
101 if (KITCHEN_ENV != null && !KITCHEN_ENV.isEmpty() && KITCHEN_ENV != "") {
102 cleanEnv = KITCHEN_ENV.replaceAll("\\s?SUITE=[^\\s]*", "")
103 if (openstackTest) { cleanEnv = "KITCHEN_YAML=.kitchen.openstack.yml " + cleanEnv }
104 sh("grep apt.mirantis.com -Ril | xargs -I{} bash -c \"echo {}; sed -i 's/apt.mirantis.com/apt.mcp.mirantis.net/g' {}\"")
105 sh("grep apt-mk.mirantis.com -Ril | xargs -I{} bash -c \"echo {}; sed -i 's/apt-mk.mirantis.com/apt.mcp.mirantis.net/g' {}\"")
106 def suite = ruby.getSuiteName(KITCHEN_ENV)
107 if (suite && suite != "") {
108 common.infoMsg("Running kitchen test with environment:" + KITCHEN_ENV.trim())
109 ruby.runKitchenTests(cleanEnv, suite)
110 } else {
111 common.warningMsg("No SUITE was found. Running with all suites.")
112 ruby.runKitchenTests(cleanEnv, "")
113 }
Jakub Josefa63f9862018-01-11 17:58:38 +0100114 } else {
Martin Polreich32b82222018-11-21 14:01:39 +0100115 throw new Exception("KITCHEN_ENV parameter is empty or invalid. This may indicate wrong env settings of initial test job or .travis.yml file.")
Jakub Josefa63f9862018-01-11 17:58:38 +0100116 }
117 } else {
Martin Polreich32b82222018-11-21 14:01:39 +0100118 throw new Exception(".kitchen.yml file not found, no kitchen tests triggered.")
Jakub Josefa63f9862018-01-11 17:58:38 +0100119 }
Martin Polreich32b82222018-11-21 14:01:39 +0100120 }/** TODO: End of block for removal */
121 } else {
122 if (checkouted) {
123 travisLess = true
Martin Polreich5be29e32019-02-26 11:11:51 +0100124 if (fileExists(".kitchen.openstack.yml")) {
125 common.infoMsg("Openstack Kitchen test configuration found, running Openstack kitchen tests.")
126 kitchenFileName = ".kitchen.openstack.yml"
127 envOverrides.add("KITCHEN_YAML=${kitchenFileName}")
Martin Polreich73c8e512019-02-26 15:16:33 +0100128 rubyVersion = '2.5.0'
Martin Polreich5be29e32019-02-26 11:11:51 +0100129 withCredentials([[$class: 'UsernamePasswordMultiBinding', credentialsId: openstack_credentials_id,
130 usernameVariable: 'OS_USERNAME', passwordVariable: 'OS_PASSWORD'], ]) {
131 env.OS_USERNAME = OS_USERNAME
132 env.OS_PASSWORD = OS_PASSWORD
133 env.OS_AUTH_URL = OS_AUTH_URL
134 env.OS_PROJECT_NAME = OS_PROJECT_NAME
135 env.OS_DOMAIN_NAME = OS_DOMAIN_NAME
136 env.OS_AZ = OS_AZ
Martin Polreich32b82222018-11-21 14:01:39 +0100137 }
Martin Polreich5be29e32019-02-26 11:11:51 +0100138 } else if (fileExists(".kitchen.yml")) {
139 common.infoMsg("Docker Kitchen test configuration found, running Docker kitchen tests.")
140 kitchenFileName = ".kitchen.yml"
Martin Polreich73c8e512019-02-26 15:16:33 +0100141 rubyVersion = '2.4.1'
Martin Polreich5be29e32019-02-26 11:11:51 +0100142 }
143 if (kitchenFileName) {
Martin Polreich5be29e32019-02-26 11:11:51 +0100144 ruby.ensureRubyEnv(rubyVersion)
Martin Polreich32b82222018-11-21 14:01:39 +0100145 if (!fileExists("Gemfile")) {
146 sh("curl -s -o ./Gemfile 'https://gerrit.mcp.mirantis.com/gitweb?p=salt-formulas/salt-formulas-scripts.git;a=blob_plain;f=Gemfile;hb=refs/heads/master'")
147 ruby.installKitchen()
148 } else {
149 common.infoMsg("Override Gemfile found in the kitchen directory, using it.")
150 ruby.installKitchen()
151 }
152 common.infoMsg("Running part of kitchen test")
153 if (KITCHEN_ENV != null && !KITCHEN_ENV.isEmpty() && KITCHEN_ENV != "") {
154 testSuite = KITCHEN_ENV.replaceAll("_", "-").trim()
Martin Polreich32b82222018-11-21 14:01:39 +0100155 sh("grep apt.mirantis.com -Ril | xargs -I{} bash -c \"echo {}; sed -i 's/apt.mirantis.com/apt.mcp.mirantis.net/g' {}\"")
156 sh("grep apt-mk.mirantis.com -Ril | xargs -I{} bash -c \"echo {}; sed -i 's/apt-mk.mirantis.com/apt.mcp.mirantis.net/g' {}\"")
157 common.infoMsg("Running kitchen test with environment:" + testSuite)
Martin Polreich5be29e32019-02-26 11:11:51 +0100158 ruby.runKitchenTests(envOverrides.join(' '), testSuite)
Martin Polreich32b82222018-11-21 14:01:39 +0100159 } else {
160 throw new Exception("KITCHEN_ENV parameter is empty or invalid. This may indicate wrong env settings of initial test job or .travis.yml file.")
161 }
162 } else {
Martin Polreich5be29e32019-02-26 11:11:51 +0100163 throw new Exception(".kitchen.yml nor .kitchen.openstack.yml file not found, no kitchen tests triggered.")
Martin Polreich32b82222018-11-21 14:01:39 +0100164 }
Jakub Josefa63f9862018-01-11 17:58:38 +0100165 }
166 }
167 }
168 } catch (Throwable e) {
169 // If there was an error or exception thrown, the build failed
170 currentBuild.result = "FAILURE"
Dzmitry Stremkouskica76bfa2018-11-07 14:19:58 +0100171 sh(script: 'find .kitchen/logs/ -type f -iname "*.log" | xargs -I{} bash -c "echo {}; cat {}"')
Martin Polreich32b82222018-11-21 14:01:39 +0100172 if (travisLess) {
173 ruby.runKitchenCommand("destroy", testSuite)
174 } else {
175 ruby.runKitchenCommand("destroy", cleanEnv) /** TODO: Remove once formulas are witched to new config */
176 }
Jakub Josefa63f9862018-01-11 17:58:38 +0100177 throw e
178 } finally {
179 if (currentBuild.result == "FAILURE" && fileExists(".kitchen/logs/kitchen.log")) {
180 common.errorMsg("----------------KITCHEN LOG:---------------")
181 println readFile(".kitchen/logs/kitchen.log")
182 }
chnyda03f3ad42017-09-19 14:41:07 +0200183 }
Martin Polreicha3e30122017-08-22 12:43:55 +0200184 }
Martin Polreicha3e30122017-08-22 12:43:55 +0200185 }
Martin Polreichb33d23e2017-09-01 15:22:16 +0000186}