blob: 47cd110285b5b60c09f9b21069735a6e676ce27e [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
Vladimir Khlyunev522d9f92021-02-01 18:51:15 +040025def nodeLabel = 'old16.04'
26
Martin Polreicha3e30122017-08-22 12:43:55 +020027def checkouted = false
Martin Polreichc0995dc2018-11-05 14:40:02 +010028def openstackTest = false
Martin Polreich32b82222018-11-21 14:01:39 +010029def travisLess = false /** TODO: Remove once formulas are witched to new config */
30def cleanEnv = '' /** TODO: Remove once formulas are witched to new config */
31def testSuite = ''
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') {
Vladimir Khlyunev522d9f92021-02-01 18:51:15 +040035 node(nodeLabel) {
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
119 if (fileExists(".kitchen.yml") || fileExists(".kitchen.openstack.yml")) {
120 if (fileExists(".kitchen.openstack.yml")) {
121 common.infoMsg("Openstack Kitchen test configuration found, running Openstack kitchen tests.")
122 if (fileExists(".kitchen.yml")) {
123 common.infoMsg("Ignoring the docker Kitchen test configuration file.")
124 }
125 openstackTest = true
126 withCredentials([[$class: 'UsernamePasswordMultiBinding', credentialsId: openstack_credentials_id,
127 usernameVariable: 'OS_USERNAME', passwordVariable: 'OS_PASSWORD'], ]) {
128 env.OS_USERNAME = OS_USERNAME
129 env.OS_PASSWORD = OS_PASSWORD
130 env.OS_AUTH_URL = OS_AUTH_URL
131 env.OS_PROJECT_NAME = OS_PROJECT_NAME
132 env.OS_DOMAIN_NAME = OS_DOMAIN_NAME
133 env.OS_AZ = OS_AZ
134 }
135 } else {
136 common.infoMsg("Docker Kitchen test configuration found, running Docker kitchen tests.")
137 }
138 ruby.ensureRubyEnv()
139 if (!fileExists("Gemfile")) {
140 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'")
141 ruby.installKitchen()
142 } else {
143 common.infoMsg("Override Gemfile found in the kitchen directory, using it.")
144 ruby.installKitchen()
145 }
146 common.infoMsg("Running part of kitchen test")
147 if (KITCHEN_ENV != null && !KITCHEN_ENV.isEmpty() && KITCHEN_ENV != "") {
148 testSuite = KITCHEN_ENV.replaceAll("_", "-").trim()
149 if (openstackTest) { testSuite = "KITCHEN_YAML=.kitchen.openstack.yml " + testSuite }
150 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)
153 ruby.runKitchenTests("", testSuite)
154 } 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 {
158 throw new Exception(".kitchen.yml file not found, no kitchen tests triggered.")
159 }
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}