blob: 257c0abdca4e009179b09282abcd18ac422f213b [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 Polreicha3e30122017-08-22 12:43:55 +020030
chnyda03f3ad42017-09-19 14:41:07 +020031throttle(['test-formula']) {
Jakub Josefdb3ddd82018-01-23 13:30:47 +010032 timeout(time: 1, unit: 'HOURS') {
Jakub Josef22815b02018-01-30 16:05:26 +010033 node("python&&docker") {
Jakub Josefa63f9862018-01-11 17:58:38 +010034 try {
35 stage("checkout") {
36 if (defaultGitRef && defaultGitUrl) {
37 checkouted = gerrit.gerritPatchsetCheckout(defaultGitUrl, defaultGitRef, "HEAD", CREDENTIALS_ID)
chnyda03f3ad42017-09-19 14:41:07 +020038 } else {
Jakub Josefa63f9862018-01-11 17:58:38 +010039 throw new Exception("Cannot checkout gerrit patchset, DEFAULT_GIT_REF is null")
chnyda03f3ad42017-09-19 14:41:07 +020040 }
41 }
Jakub Josefa63f9862018-01-11 17:58:38 +010042 stage("cleanup") {
43 if (checkouted) {
44 sh("make clean")
45 }
46 }
47 stage("kitchen") {
Martin Polreich32b82222018-11-21 14:01:39 +010048 if (fileExists(".travis.yml")) {/** TODO: Remove this legacy block once formulas are witched to new config */
49 if (checkouted) {
50 if (fileExists(".kitchen.yml") || fileExists(".kitchen.openstack.yml")) {
51 if (fileExists(".kitchen.openstack.yml")) {
52 common.infoMsg("Openstack Kitchen test configuration found, running Openstack kitchen tests.")
53 if (fileExists(".kitchen.yml")) {
54 common.infoMsg("Ignoring the docker Kitchen test configuration file.")
55 }
56 openstackTest = true
57 } else {
58 common.infoMsg("Docker Kitchen test configuration found, running Docker kitchen tests.")
Martin Polreichc0995dc2018-11-05 14:40:02 +010059 }
Martin Polreich32b82222018-11-21 14:01:39 +010060 ruby.ensureRubyEnv()
61 if (fileExists(".travis.yml")) {
62 common.infoMsg(".travis.yml found, running custom kitchen init")
63 def kitchenConfigYML = readYaml(file: ".travis.yml")
64 def kitchenInit = kitchenConfigYML["install"]
65 def kitchenInstalled = false
66 if (kitchenInit && !kitchenInit.isEmpty()) {
67 for (int i = 0; i < kitchenInit.size(); i++) {
68 if (kitchenInit[i].trim().startsWith("if [ ! -e Gemfile ]")) { //found Gemfile config
69 common.infoMsg("Custom Gemfile configuration found, using them")
70 if (openstackTest) {
71 withCredentials([[$class: 'UsernamePasswordMultiBinding', credentialsId: openstack_credentials_id,
72 usernameVariable: 'OS_USERNAME', passwordVariable: 'OS_PASSWORD'], ]) {
73 env.OS_USERNAME = OS_USERNAME
74 env.OS_PASSWORD = OS_PASSWORD
75 env.OS_AUTH_URL = OS_AUTH_URL
76 env.OS_PROJECT_NAME = OS_PROJECT_NAME
77 env.OS_DOMAIN_NAME = OS_DOMAIN_NAME
78 env.OS_AZ = OS_AZ
79 }
Martin Polreichc39edae2018-11-07 08:42:11 +010080 }
Martin Polreich32b82222018-11-21 14:01:39 +010081 ruby.installKitchen(kitchenInit[i].trim())
82 kitchenInstalled = true
Martin Polreichc0995dc2018-11-05 14:40:02 +010083 }
Jakub Josefa63f9862018-01-11 17:58:38 +010084 }
85 }
Martin Polreich32b82222018-11-21 14:01:39 +010086 if (!kitchenInstalled) {
87 ruby.installKitchen()
88 }
89 } else {
90 common.infoMsg(".travis.yml not found, running default kitchen init")
Jakub Josefa63f9862018-01-11 17:58:38 +010091 ruby.installKitchen()
92 }
Martin Polreich32b82222018-11-21 14:01:39 +010093 common.infoMsg("Running part of kitchen test")
94 if (KITCHEN_ENV != null && !KITCHEN_ENV.isEmpty() && KITCHEN_ENV != "") {
95 cleanEnv = KITCHEN_ENV.replaceAll("\\s?SUITE=[^\\s]*", "")
96 if (openstackTest) { cleanEnv = "KITCHEN_YAML=.kitchen.openstack.yml " + cleanEnv }
97 sh("grep apt.mirantis.com -Ril | xargs -I{} bash -c \"echo {}; sed -i 's/apt.mirantis.com/apt.mcp.mirantis.net/g' {}\"")
98 sh("grep apt-mk.mirantis.com -Ril | xargs -I{} bash -c \"echo {}; sed -i 's/apt-mk.mirantis.com/apt.mcp.mirantis.net/g' {}\"")
99 def suite = ruby.getSuiteName(KITCHEN_ENV)
100 if (suite && suite != "") {
101 common.infoMsg("Running kitchen test with environment:" + KITCHEN_ENV.trim())
102 ruby.runKitchenTests(cleanEnv, suite)
103 } else {
104 common.warningMsg("No SUITE was found. Running with all suites.")
105 ruby.runKitchenTests(cleanEnv, "")
106 }
Jakub Josefa63f9862018-01-11 17:58:38 +0100107 } else {
Martin Polreich32b82222018-11-21 14:01:39 +0100108 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 +0100109 }
110 } else {
Martin Polreich32b82222018-11-21 14:01:39 +0100111 throw new Exception(".kitchen.yml file not found, no kitchen tests triggered.")
Jakub Josefa63f9862018-01-11 17:58:38 +0100112 }
Martin Polreich32b82222018-11-21 14:01:39 +0100113 }/** TODO: End of block for removal */
114 } else {
115 if (checkouted) {
116 travisLess = true
117 if (fileExists(".kitchen.yml") || fileExists(".kitchen.openstack.yml")) {
118 if (fileExists(".kitchen.openstack.yml")) {
119 common.infoMsg("Openstack Kitchen test configuration found, running Openstack kitchen tests.")
120 if (fileExists(".kitchen.yml")) {
121 common.infoMsg("Ignoring the docker Kitchen test configuration file.")
122 }
123 openstackTest = true
124 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
132 }
133 } else {
134 common.infoMsg("Docker Kitchen test configuration found, running Docker kitchen tests.")
135 }
136 ruby.ensureRubyEnv()
137 if (!fileExists("Gemfile")) {
138 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'")
139 ruby.installKitchen()
140 } else {
141 common.infoMsg("Override Gemfile found in the kitchen directory, using it.")
142 ruby.installKitchen()
143 }
144 common.infoMsg("Running part of kitchen test")
145 if (KITCHEN_ENV != null && !KITCHEN_ENV.isEmpty() && KITCHEN_ENV != "") {
146 testSuite = KITCHEN_ENV.replaceAll("_", "-").trim()
147 if (openstackTest) { testSuite = "KITCHEN_YAML=.kitchen.openstack.yml " + testSuite }
148 sh("grep apt.mirantis.com -Ril | xargs -I{} bash -c \"echo {}; sed -i 's/apt.mirantis.com/apt.mcp.mirantis.net/g' {}\"")
149 sh("grep apt-mk.mirantis.com -Ril | xargs -I{} bash -c \"echo {}; sed -i 's/apt-mk.mirantis.com/apt.mcp.mirantis.net/g' {}\"")
150 common.infoMsg("Running kitchen test with environment:" + testSuite)
151 ruby.runKitchenTests("", testSuite)
152 } else {
153 throw new Exception("KITCHEN_ENV parameter is empty or invalid. This may indicate wrong env settings of initial test job or .travis.yml file.")
154 }
155 } else {
156 throw new Exception(".kitchen.yml file not found, no kitchen tests triggered.")
157 }
Jakub Josefa63f9862018-01-11 17:58:38 +0100158 }
159 }
160 }
161 } catch (Throwable e) {
162 // If there was an error or exception thrown, the build failed
163 currentBuild.result = "FAILURE"
Dzmitry Stremkouskica76bfa2018-11-07 14:19:58 +0100164 sh(script: 'find .kitchen/logs/ -type f -iname "*.log" | xargs -I{} bash -c "echo {}; cat {}"')
Martin Polreich32b82222018-11-21 14:01:39 +0100165 if (travisLess) {
166 ruby.runKitchenCommand("destroy", testSuite)
167 } else {
168 ruby.runKitchenCommand("destroy", cleanEnv) /** TODO: Remove once formulas are witched to new config */
169 }
Jakub Josefa63f9862018-01-11 17:58:38 +0100170 throw e
171 } finally {
172 if (currentBuild.result == "FAILURE" && fileExists(".kitchen/logs/kitchen.log")) {
173 common.errorMsg("----------------KITCHEN LOG:---------------")
174 println readFile(".kitchen/logs/kitchen.log")
175 }
chnyda03f3ad42017-09-19 14:41:07 +0200176 }
Martin Polreicha3e30122017-08-22 12:43:55 +0200177 }
Martin Polreicha3e30122017-08-22 12:43:55 +0200178 }
Martin Polreichb33d23e2017-09-01 15:22:16 +0000179}