blob: e2b164c4511beaf617887c47d241c4911a0e5d7d [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
Vladimir Khlyunevd77c1882022-01-31 15:57:35 +040030def nodeLabel = 'docker'
Vladimir Khlyunev81a3c902021-02-01 18:51:15 +040031
Martin Polreicha3e30122017-08-22 12:43:55 +020032def checkouted = false
Martin Polreichc0995dc2018-11-05 14:40:02 +010033def openstackTest = false
Martin Polreich32b82222018-11-21 14:01:39 +010034def travisLess = false /** TODO: Remove once formulas are witched to new config */
35def cleanEnv = '' /** TODO: Remove once formulas are witched to new config */
36def testSuite = ''
Martin Polreich5be29e32019-02-26 11:11:51 +010037envOverrides = []
38kitchenFileName = ''
Martin Polreicha3e30122017-08-22 12:43:55 +020039
chnyda03f3ad42017-09-19 14:41:07 +020040throttle(['test-formula']) {
Jakub Josefdb3ddd82018-01-23 13:30:47 +010041 timeout(time: 1, unit: 'HOURS') {
Vladimir Khlyunev81a3c902021-02-01 18:51:15 +040042 node(nodeLabel) {
Jakub Josefa63f9862018-01-11 17:58:38 +010043 try {
44 stage("checkout") {
45 if (defaultGitRef && defaultGitUrl) {
46 checkouted = gerrit.gerritPatchsetCheckout(defaultGitUrl, defaultGitRef, "HEAD", CREDENTIALS_ID)
chnyda03f3ad42017-09-19 14:41:07 +020047 } else {
Jakub Josefa63f9862018-01-11 17:58:38 +010048 throw new Exception("Cannot checkout gerrit patchset, DEFAULT_GIT_REF is null")
chnyda03f3ad42017-09-19 14:41:07 +020049 }
50 }
Jakub Josefa63f9862018-01-11 17:58:38 +010051 stage("cleanup") {
52 if (checkouted) {
53 sh("make clean")
54 }
55 }
56 stage("kitchen") {
Martin Polreich32b82222018-11-21 14:01:39 +010057 if (fileExists(".travis.yml")) {/** TODO: Remove this legacy block once formulas are witched to new config */
58 if (checkouted) {
59 if (fileExists(".kitchen.yml") || fileExists(".kitchen.openstack.yml")) {
60 if (fileExists(".kitchen.openstack.yml")) {
61 common.infoMsg("Openstack Kitchen test configuration found, running Openstack kitchen tests.")
62 if (fileExists(".kitchen.yml")) {
63 common.infoMsg("Ignoring the docker Kitchen test configuration file.")
64 }
65 openstackTest = true
66 } else {
67 common.infoMsg("Docker Kitchen test configuration found, running Docker kitchen tests.")
Martin Polreichc0995dc2018-11-05 14:40:02 +010068 }
Martin Polreich32b82222018-11-21 14:01:39 +010069 ruby.ensureRubyEnv()
70 if (fileExists(".travis.yml")) {
71 common.infoMsg(".travis.yml found, running custom kitchen init")
72 def kitchenConfigYML = readYaml(file: ".travis.yml")
73 def kitchenInit = kitchenConfigYML["install"]
74 def kitchenInstalled = false
75 if (kitchenInit && !kitchenInit.isEmpty()) {
76 for (int i = 0; i < kitchenInit.size(); i++) {
77 if (kitchenInit[i].trim().startsWith("if [ ! -e Gemfile ]")) { //found Gemfile config
78 common.infoMsg("Custom Gemfile configuration found, using them")
79 if (openstackTest) {
80 withCredentials([[$class: 'UsernamePasswordMultiBinding', credentialsId: openstack_credentials_id,
81 usernameVariable: 'OS_USERNAME', passwordVariable: 'OS_PASSWORD'], ]) {
82 env.OS_USERNAME = OS_USERNAME
83 env.OS_PASSWORD = OS_PASSWORD
84 env.OS_AUTH_URL = OS_AUTH_URL
85 env.OS_PROJECT_NAME = OS_PROJECT_NAME
86 env.OS_DOMAIN_NAME = OS_DOMAIN_NAME
87 env.OS_AZ = OS_AZ
88 }
Martin Polreichc39edae2018-11-07 08:42:11 +010089 }
Martin Polreich32b82222018-11-21 14:01:39 +010090 ruby.installKitchen(kitchenInit[i].trim())
91 kitchenInstalled = true
Martin Polreichc0995dc2018-11-05 14:40:02 +010092 }
Jakub Josefa63f9862018-01-11 17:58:38 +010093 }
94 }
Martin Polreich32b82222018-11-21 14:01:39 +010095 if (!kitchenInstalled) {
96 ruby.installKitchen()
97 }
98 } else {
99 common.infoMsg(".travis.yml not found, running default kitchen init")
Jakub Josefa63f9862018-01-11 17:58:38 +0100100 ruby.installKitchen()
101 }
Martin Polreich32b82222018-11-21 14:01:39 +0100102 common.infoMsg("Running part of kitchen test")
103 if (KITCHEN_ENV != null && !KITCHEN_ENV.isEmpty() && KITCHEN_ENV != "") {
104 cleanEnv = KITCHEN_ENV.replaceAll("\\s?SUITE=[^\\s]*", "")
105 if (openstackTest) { cleanEnv = "KITCHEN_YAML=.kitchen.openstack.yml " + cleanEnv }
106 sh("grep apt.mirantis.com -Ril | xargs -I{} bash -c \"echo {}; sed -i 's/apt.mirantis.com/apt.mcp.mirantis.net/g' {}\"")
107 sh("grep apt-mk.mirantis.com -Ril | xargs -I{} bash -c \"echo {}; sed -i 's/apt-mk.mirantis.com/apt.mcp.mirantis.net/g' {}\"")
108 def suite = ruby.getSuiteName(KITCHEN_ENV)
109 if (suite && suite != "") {
110 common.infoMsg("Running kitchen test with environment:" + KITCHEN_ENV.trim())
111 ruby.runKitchenTests(cleanEnv, suite)
112 } else {
113 common.warningMsg("No SUITE was found. Running with all suites.")
114 ruby.runKitchenTests(cleanEnv, "")
115 }
Jakub Josefa63f9862018-01-11 17:58:38 +0100116 } else {
Martin Polreich32b82222018-11-21 14:01:39 +0100117 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 +0100118 }
119 } else {
Martin Polreich32b82222018-11-21 14:01:39 +0100120 throw new Exception(".kitchen.yml file not found, no kitchen tests triggered.")
Jakub Josefa63f9862018-01-11 17:58:38 +0100121 }
Martin Polreich32b82222018-11-21 14:01:39 +0100122 }/** TODO: End of block for removal */
123 } else {
124 if (checkouted) {
125 travisLess = true
Martin Polreich5be29e32019-02-26 11:11:51 +0100126 if (fileExists(".kitchen.openstack.yml")) {
127 common.infoMsg("Openstack Kitchen test configuration found, running Openstack kitchen tests.")
128 kitchenFileName = ".kitchen.openstack.yml"
129 envOverrides.add("KITCHEN_YAML=${kitchenFileName}")
130 withCredentials([[$class: 'UsernamePasswordMultiBinding', credentialsId: openstack_credentials_id,
131 usernameVariable: 'OS_USERNAME', passwordVariable: 'OS_PASSWORD'], ]) {
132 env.OS_USERNAME = OS_USERNAME
133 env.OS_PASSWORD = OS_PASSWORD
134 env.OS_AUTH_URL = OS_AUTH_URL
135 env.OS_PROJECT_NAME = OS_PROJECT_NAME
136 env.OS_DOMAIN_NAME = OS_DOMAIN_NAME
137 env.OS_AZ = OS_AZ
Martin Polreich32b82222018-11-21 14:01:39 +0100138 }
Martin Polreich5be29e32019-02-26 11:11:51 +0100139 } else if (fileExists(".kitchen.yml")) {
140 common.infoMsg("Docker Kitchen test configuration found, running Docker kitchen tests.")
141 kitchenFileName = ".kitchen.yml"
142 }
143 if (kitchenFileName) {
Denis Egorenko5884e312020-12-16 15:46:20 +0400144 ruby.ensureRubyEnv()
Martin Polreich32b82222018-11-21 14:01:39 +0100145 if (!fileExists("Gemfile")) {
Taras Khlivnyak7a7bf1e2021-02-17 13:17:20 +0200146 sh("curl -s 'https://gerrit.mcp.mirantis.com/projects/salt-formulas%2Fsalt-formulas-scripts/branches/master/files/Gemfile/content' | base64 -d > ./Gemfile")
Martin Polreich32b82222018-11-21 14:01:39 +0100147 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}