blob: e007fe91adf1bf6fc920b51e5e3be06146bee026 [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 Polreicha3e30122017-08-22 12:43:55 +020025def checkouted = false
Martin Polreichc0995dc2018-11-05 14:40:02 +010026def openstackTest = false
Martin Polreich32b82222018-11-21 14:01:39 +010027def travisLess = false /** TODO: Remove once formulas are witched to new config */
28def cleanEnv = '' /** TODO: Remove once formulas are witched to new config */
29def testSuite = ''
Martin Polreich5be29e32019-02-26 11:11:51 +010030envOverrides = []
31kitchenFileName = ''
Martin Polreicha3e30122017-08-22 12:43:55 +020032
chnyda03f3ad42017-09-19 14:41:07 +020033throttle(['test-formula']) {
Jakub Josefdb3ddd82018-01-23 13:30:47 +010034 timeout(time: 1, unit: 'HOURS') {
Jakub Josef22815b02018-01-30 16:05:26 +010035 node("python&&docker") {
Jakub Josefa63f9862018-01-11 17:58:38 +010036 try {
37 stage("checkout") {
38 if (defaultGitRef && defaultGitUrl) {
39 checkouted = gerrit.gerritPatchsetCheckout(defaultGitUrl, defaultGitRef, "HEAD", CREDENTIALS_ID)
chnyda03f3ad42017-09-19 14:41:07 +020040 } else {
Jakub Josefa63f9862018-01-11 17:58:38 +010041 throw new Exception("Cannot checkout gerrit patchset, DEFAULT_GIT_REF is null")
chnyda03f3ad42017-09-19 14:41:07 +020042 }
43 }
Jakub Josefa63f9862018-01-11 17:58:38 +010044 stage("cleanup") {
45 if (checkouted) {
46 sh("make clean")
47 }
48 }
49 stage("kitchen") {
Martin Polreich32b82222018-11-21 14:01:39 +010050 if (fileExists(".travis.yml")) {/** TODO: Remove this legacy block once formulas are witched to new config */
51 if (checkouted) {
52 if (fileExists(".kitchen.yml") || fileExists(".kitchen.openstack.yml")) {
53 if (fileExists(".kitchen.openstack.yml")) {
54 common.infoMsg("Openstack Kitchen test configuration found, running Openstack kitchen tests.")
55 if (fileExists(".kitchen.yml")) {
56 common.infoMsg("Ignoring the docker Kitchen test configuration file.")
57 }
58 openstackTest = true
59 } else {
60 common.infoMsg("Docker Kitchen test configuration found, running Docker kitchen tests.")
Martin Polreichc0995dc2018-11-05 14:40:02 +010061 }
Martin Polreich32b82222018-11-21 14:01:39 +010062 ruby.ensureRubyEnv()
63 if (fileExists(".travis.yml")) {
64 common.infoMsg(".travis.yml found, running custom kitchen init")
65 def kitchenConfigYML = readYaml(file: ".travis.yml")
66 def kitchenInit = kitchenConfigYML["install"]
67 def kitchenInstalled = false
68 if (kitchenInit && !kitchenInit.isEmpty()) {
69 for (int i = 0; i < kitchenInit.size(); i++) {
70 if (kitchenInit[i].trim().startsWith("if [ ! -e Gemfile ]")) { //found Gemfile config
71 common.infoMsg("Custom Gemfile configuration found, using them")
72 if (openstackTest) {
73 withCredentials([[$class: 'UsernamePasswordMultiBinding', credentialsId: openstack_credentials_id,
74 usernameVariable: 'OS_USERNAME', passwordVariable: 'OS_PASSWORD'], ]) {
75 env.OS_USERNAME = OS_USERNAME
76 env.OS_PASSWORD = OS_PASSWORD
77 env.OS_AUTH_URL = OS_AUTH_URL
78 env.OS_PROJECT_NAME = OS_PROJECT_NAME
79 env.OS_DOMAIN_NAME = OS_DOMAIN_NAME
80 env.OS_AZ = OS_AZ
81 }
Martin Polreichc39edae2018-11-07 08:42:11 +010082 }
Martin Polreich32b82222018-11-21 14:01:39 +010083 ruby.installKitchen(kitchenInit[i].trim())
84 kitchenInstalled = true
Martin Polreichc0995dc2018-11-05 14:40:02 +010085 }
Jakub Josefa63f9862018-01-11 17:58:38 +010086 }
87 }
Martin Polreich32b82222018-11-21 14:01:39 +010088 if (!kitchenInstalled) {
89 ruby.installKitchen()
90 }
91 } else {
92 common.infoMsg(".travis.yml not found, running default kitchen init")
Jakub Josefa63f9862018-01-11 17:58:38 +010093 ruby.installKitchen()
94 }
Martin Polreich32b82222018-11-21 14:01:39 +010095 common.infoMsg("Running part of kitchen test")
96 if (KITCHEN_ENV != null && !KITCHEN_ENV.isEmpty() && KITCHEN_ENV != "") {
97 cleanEnv = KITCHEN_ENV.replaceAll("\\s?SUITE=[^\\s]*", "")
98 if (openstackTest) { cleanEnv = "KITCHEN_YAML=.kitchen.openstack.yml " + cleanEnv }
99 sh("grep apt.mirantis.com -Ril | xargs -I{} bash -c \"echo {}; sed -i 's/apt.mirantis.com/apt.mcp.mirantis.net/g' {}\"")
100 sh("grep apt-mk.mirantis.com -Ril | xargs -I{} bash -c \"echo {}; sed -i 's/apt-mk.mirantis.com/apt.mcp.mirantis.net/g' {}\"")
101 def suite = ruby.getSuiteName(KITCHEN_ENV)
102 if (suite && suite != "") {
103 common.infoMsg("Running kitchen test with environment:" + KITCHEN_ENV.trim())
104 ruby.runKitchenTests(cleanEnv, suite)
105 } else {
106 common.warningMsg("No SUITE was found. Running with all suites.")
107 ruby.runKitchenTests(cleanEnv, "")
108 }
Jakub Josefa63f9862018-01-11 17:58:38 +0100109 } else {
Martin Polreich32b82222018-11-21 14:01:39 +0100110 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 +0100111 }
112 } else {
Martin Polreich32b82222018-11-21 14:01:39 +0100113 throw new Exception(".kitchen.yml file not found, no kitchen tests triggered.")
Jakub Josefa63f9862018-01-11 17:58:38 +0100114 }
Martin Polreich32b82222018-11-21 14:01:39 +0100115 }/** TODO: End of block for removal */
116 } else {
117 if (checkouted) {
118 travisLess = true
Martin Polreich5be29e32019-02-26 11:11:51 +0100119 if (fileExists(".kitchen.openstack.yml")) {
120 common.infoMsg("Openstack Kitchen test configuration found, running Openstack kitchen tests.")
121 kitchenFileName = ".kitchen.openstack.yml"
122 envOverrides.add("KITCHEN_YAML=${kitchenFileName}")
Martin Polreich73c8e512019-02-26 15:16:33 +0100123 rubyVersion = '2.5.0'
Martin Polreich5be29e32019-02-26 11:11:51 +0100124 withCredentials([[$class: 'UsernamePasswordMultiBinding', credentialsId: openstack_credentials_id,
125 usernameVariable: 'OS_USERNAME', passwordVariable: 'OS_PASSWORD'], ]) {
126 env.OS_USERNAME = OS_USERNAME
127 env.OS_PASSWORD = OS_PASSWORD
128 env.OS_AUTH_URL = OS_AUTH_URL
129 env.OS_PROJECT_NAME = OS_PROJECT_NAME
130 env.OS_DOMAIN_NAME = OS_DOMAIN_NAME
131 env.OS_AZ = OS_AZ
Martin Polreich32b82222018-11-21 14:01:39 +0100132 }
Martin Polreich5be29e32019-02-26 11:11:51 +0100133 } else if (fileExists(".kitchen.yml")) {
134 common.infoMsg("Docker Kitchen test configuration found, running Docker kitchen tests.")
135 kitchenFileName = ".kitchen.yml"
Martin Polreich73c8e512019-02-26 15:16:33 +0100136 rubyVersion = '2.4.1'
Martin Polreich5be29e32019-02-26 11:11:51 +0100137 }
138 if (kitchenFileName) {
Martin Polreich5be29e32019-02-26 11:11:51 +0100139 ruby.ensureRubyEnv(rubyVersion)
Martin Polreich32b82222018-11-21 14:01:39 +0100140 if (!fileExists("Gemfile")) {
141 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'")
142 ruby.installKitchen()
143 } else {
144 common.infoMsg("Override Gemfile found in the kitchen directory, using it.")
145 ruby.installKitchen()
146 }
147 common.infoMsg("Running part of kitchen test")
148 if (KITCHEN_ENV != null && !KITCHEN_ENV.isEmpty() && KITCHEN_ENV != "") {
149 testSuite = KITCHEN_ENV.replaceAll("_", "-").trim()
Martin Polreich32b82222018-11-21 14:01:39 +0100150 sh("grep apt.mirantis.com -Ril | xargs -I{} bash -c \"echo {}; sed -i 's/apt.mirantis.com/apt.mcp.mirantis.net/g' {}\"")
151 sh("grep apt-mk.mirantis.com -Ril | xargs -I{} bash -c \"echo {}; sed -i 's/apt-mk.mirantis.com/apt.mcp.mirantis.net/g' {}\"")
152 common.infoMsg("Running kitchen test with environment:" + testSuite)
Martin Polreich5be29e32019-02-26 11:11:51 +0100153 ruby.runKitchenTests(envOverrides.join(' '), testSuite)
Martin Polreich32b82222018-11-21 14:01:39 +0100154 } else {
155 throw new Exception("KITCHEN_ENV parameter is empty or invalid. This may indicate wrong env settings of initial test job or .travis.yml file.")
156 }
157 } else {
Martin Polreich5be29e32019-02-26 11:11:51 +0100158 throw new Exception(".kitchen.yml nor .kitchen.openstack.yml file not found, no kitchen tests triggered.")
Martin Polreich32b82222018-11-21 14:01:39 +0100159 }
Jakub Josefa63f9862018-01-11 17:58:38 +0100160 }
161 }
162 }
163 } catch (Throwable e) {
164 // If there was an error or exception thrown, the build failed
165 currentBuild.result = "FAILURE"
Dzmitry Stremkouskica76bfa2018-11-07 14:19:58 +0100166 sh(script: 'find .kitchen/logs/ -type f -iname "*.log" | xargs -I{} bash -c "echo {}; cat {}"')
Martin Polreich32b82222018-11-21 14:01:39 +0100167 if (travisLess) {
168 ruby.runKitchenCommand("destroy", testSuite)
169 } else {
170 ruby.runKitchenCommand("destroy", cleanEnv) /** TODO: Remove once formulas are witched to new config */
171 }
Jakub Josefa63f9862018-01-11 17:58:38 +0100172 throw e
173 } finally {
174 if (currentBuild.result == "FAILURE" && fileExists(".kitchen/logs/kitchen.log")) {
175 common.errorMsg("----------------KITCHEN LOG:---------------")
176 println readFile(".kitchen/logs/kitchen.log")
177 }
chnyda03f3ad42017-09-19 14:41:07 +0200178 }
Martin Polreicha3e30122017-08-22 12:43:55 +0200179 }
Martin Polreicha3e30122017-08-22 12:43:55 +0200180 }
Martin Polreichb33d23e2017-09-01 15:22:16 +0000181}