blob: 9c41a9442f72ba97605c6390ce1182e56cb04cbc [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)
Martin Polreicha4546112019-04-08 17:39:19 +020040// env.GERRIT_BRANCH = GERRIT_BRANCH
chnyda03f3ad42017-09-19 14:41:07 +020041 } else {
Jakub Josefa63f9862018-01-11 17:58:38 +010042 throw new Exception("Cannot checkout gerrit patchset, DEFAULT_GIT_REF is null")
chnyda03f3ad42017-09-19 14:41:07 +020043 }
44 }
Jakub Josefa63f9862018-01-11 17:58:38 +010045 stage("cleanup") {
46 if (checkouted) {
47 sh("make clean")
48 }
49 }
50 stage("kitchen") {
Martin Polreich32b82222018-11-21 14:01:39 +010051 if (fileExists(".travis.yml")) {/** TODO: Remove this legacy block once formulas are witched to new config */
52 if (checkouted) {
53 if (fileExists(".kitchen.yml") || fileExists(".kitchen.openstack.yml")) {
54 if (fileExists(".kitchen.openstack.yml")) {
55 common.infoMsg("Openstack Kitchen test configuration found, running Openstack kitchen tests.")
56 if (fileExists(".kitchen.yml")) {
57 common.infoMsg("Ignoring the docker Kitchen test configuration file.")
58 }
59 openstackTest = true
60 } else {
61 common.infoMsg("Docker Kitchen test configuration found, running Docker kitchen tests.")
Martin Polreichc0995dc2018-11-05 14:40:02 +010062 }
Martin Polreich32b82222018-11-21 14:01:39 +010063 ruby.ensureRubyEnv()
64 if (fileExists(".travis.yml")) {
65 common.infoMsg(".travis.yml found, running custom kitchen init")
66 def kitchenConfigYML = readYaml(file: ".travis.yml")
67 def kitchenInit = kitchenConfigYML["install"]
68 def kitchenInstalled = false
69 if (kitchenInit && !kitchenInit.isEmpty()) {
70 for (int i = 0; i < kitchenInit.size(); i++) {
71 if (kitchenInit[i].trim().startsWith("if [ ! -e Gemfile ]")) { //found Gemfile config
72 common.infoMsg("Custom Gemfile configuration found, using them")
73 if (openstackTest) {
74 withCredentials([[$class: 'UsernamePasswordMultiBinding', credentialsId: openstack_credentials_id,
75 usernameVariable: 'OS_USERNAME', passwordVariable: 'OS_PASSWORD'], ]) {
76 env.OS_USERNAME = OS_USERNAME
77 env.OS_PASSWORD = OS_PASSWORD
78 env.OS_AUTH_URL = OS_AUTH_URL
79 env.OS_PROJECT_NAME = OS_PROJECT_NAME
80 env.OS_DOMAIN_NAME = OS_DOMAIN_NAME
81 env.OS_AZ = OS_AZ
82 }
Martin Polreichc39edae2018-11-07 08:42:11 +010083 }
Martin Polreich32b82222018-11-21 14:01:39 +010084 ruby.installKitchen(kitchenInit[i].trim())
85 kitchenInstalled = true
Martin Polreichc0995dc2018-11-05 14:40:02 +010086 }
Jakub Josefa63f9862018-01-11 17:58:38 +010087 }
88 }
Martin Polreich32b82222018-11-21 14:01:39 +010089 if (!kitchenInstalled) {
90 ruby.installKitchen()
91 }
92 } else {
93 common.infoMsg(".travis.yml not found, running default kitchen init")
Jakub Josefa63f9862018-01-11 17:58:38 +010094 ruby.installKitchen()
95 }
Martin Polreich32b82222018-11-21 14:01:39 +010096 common.infoMsg("Running part of kitchen test")
97 if (KITCHEN_ENV != null && !KITCHEN_ENV.isEmpty() && KITCHEN_ENV != "") {
98 cleanEnv = KITCHEN_ENV.replaceAll("\\s?SUITE=[^\\s]*", "")
99 if (openstackTest) { cleanEnv = "KITCHEN_YAML=.kitchen.openstack.yml " + cleanEnv }
100 sh("grep apt.mirantis.com -Ril | xargs -I{} bash -c \"echo {}; sed -i 's/apt.mirantis.com/apt.mcp.mirantis.net/g' {}\"")
101 sh("grep apt-mk.mirantis.com -Ril | xargs -I{} bash -c \"echo {}; sed -i 's/apt-mk.mirantis.com/apt.mcp.mirantis.net/g' {}\"")
102 def suite = ruby.getSuiteName(KITCHEN_ENV)
103 if (suite && suite != "") {
104 common.infoMsg("Running kitchen test with environment:" + KITCHEN_ENV.trim())
105 ruby.runKitchenTests(cleanEnv, suite)
106 } else {
107 common.warningMsg("No SUITE was found. Running with all suites.")
108 ruby.runKitchenTests(cleanEnv, "")
109 }
Jakub Josefa63f9862018-01-11 17:58:38 +0100110 } else {
Martin Polreich32b82222018-11-21 14:01:39 +0100111 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 +0100112 }
113 } else {
Martin Polreich32b82222018-11-21 14:01:39 +0100114 throw new Exception(".kitchen.yml file not found, no kitchen tests triggered.")
Jakub Josefa63f9862018-01-11 17:58:38 +0100115 }
Martin Polreich32b82222018-11-21 14:01:39 +0100116 }/** TODO: End of block for removal */
117 } else {
118 if (checkouted) {
119 travisLess = true
Martin Polreich5be29e32019-02-26 11:11:51 +0100120 if (fileExists(".kitchen.openstack.yml")) {
121 common.infoMsg("Openstack Kitchen test configuration found, running Openstack kitchen tests.")
122 kitchenFileName = ".kitchen.openstack.yml"
123 envOverrides.add("KITCHEN_YAML=${kitchenFileName}")
Martin Polreich73c8e512019-02-26 15:16:33 +0100124 rubyVersion = '2.5.0'
Martin Polreich5be29e32019-02-26 11:11:51 +0100125 withCredentials([[$class: 'UsernamePasswordMultiBinding', credentialsId: openstack_credentials_id,
126 usernameVariable: 'OS_USERNAME', passwordVariable: 'OS_PASSWORD'], ]) {
127 env.OS_USERNAME = OS_USERNAME
128 env.OS_PASSWORD = OS_PASSWORD
129 env.OS_AUTH_URL = OS_AUTH_URL
130 env.OS_PROJECT_NAME = OS_PROJECT_NAME
131 env.OS_DOMAIN_NAME = OS_DOMAIN_NAME
132 env.OS_AZ = OS_AZ
Martin Polreich32b82222018-11-21 14:01:39 +0100133 }
Martin Polreich5be29e32019-02-26 11:11:51 +0100134 } else if (fileExists(".kitchen.yml")) {
135 common.infoMsg("Docker Kitchen test configuration found, running Docker kitchen tests.")
136 kitchenFileName = ".kitchen.yml"
Martin Polreich73c8e512019-02-26 15:16:33 +0100137 rubyVersion = '2.4.1'
Martin Polreich5be29e32019-02-26 11:11:51 +0100138 }
139 if (kitchenFileName) {
Martin Polreich5be29e32019-02-26 11:11:51 +0100140 ruby.ensureRubyEnv(rubyVersion)
Martin Polreich32b82222018-11-21 14:01:39 +0100141 if (!fileExists("Gemfile")) {
142 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'")
143 ruby.installKitchen()
144 } else {
145 common.infoMsg("Override Gemfile found in the kitchen directory, using it.")
146 ruby.installKitchen()
147 }
148 common.infoMsg("Running part of kitchen test")
149 if (KITCHEN_ENV != null && !KITCHEN_ENV.isEmpty() && KITCHEN_ENV != "") {
150 testSuite = KITCHEN_ENV.replaceAll("_", "-").trim()
Martin Polreich32b82222018-11-21 14:01:39 +0100151 sh("grep apt.mirantis.com -Ril | xargs -I{} bash -c \"echo {}; sed -i 's/apt.mirantis.com/apt.mcp.mirantis.net/g' {}\"")
152 sh("grep apt-mk.mirantis.com -Ril | xargs -I{} bash -c \"echo {}; sed -i 's/apt-mk.mirantis.com/apt.mcp.mirantis.net/g' {}\"")
153 common.infoMsg("Running kitchen test with environment:" + testSuite)
Martin Polreich5be29e32019-02-26 11:11:51 +0100154 ruby.runKitchenTests(envOverrides.join(' '), testSuite)
Martin Polreich32b82222018-11-21 14:01:39 +0100155 } else {
156 throw new Exception("KITCHEN_ENV parameter is empty or invalid. This may indicate wrong env settings of initial test job or .travis.yml file.")
157 }
158 } else {
Martin Polreich5be29e32019-02-26 11:11:51 +0100159 throw new Exception(".kitchen.yml nor .kitchen.openstack.yml file not found, no kitchen tests triggered.")
Martin Polreich32b82222018-11-21 14:01:39 +0100160 }
Jakub Josefa63f9862018-01-11 17:58:38 +0100161 }
162 }
163 }
164 } catch (Throwable e) {
165 // If there was an error or exception thrown, the build failed
166 currentBuild.result = "FAILURE"
Dzmitry Stremkouskica76bfa2018-11-07 14:19:58 +0100167 sh(script: 'find .kitchen/logs/ -type f -iname "*.log" | xargs -I{} bash -c "echo {}; cat {}"')
Martin Polreich32b82222018-11-21 14:01:39 +0100168 if (travisLess) {
169 ruby.runKitchenCommand("destroy", testSuite)
170 } else {
171 ruby.runKitchenCommand("destroy", cleanEnv) /** TODO: Remove once formulas are witched to new config */
172 }
Jakub Josefa63f9862018-01-11 17:58:38 +0100173 throw e
174 } finally {
175 if (currentBuild.result == "FAILURE" && fileExists(".kitchen/logs/kitchen.log")) {
176 common.errorMsg("----------------KITCHEN LOG:---------------")
177 println readFile(".kitchen/logs/kitchen.log")
178 }
chnyda03f3ad42017-09-19 14:41:07 +0200179 }
Martin Polreicha3e30122017-08-22 12:43:55 +0200180 }
Martin Polreicha3e30122017-08-22 12:43:55 +0200181 }
Martin Polreichb33d23e2017-09-01 15:22:16 +0000182}