Martin Polreich | a3e3012 | 2017-08-22 12:43:55 +0200 | [diff] [blame] | 1 | /** |
| 2 | * Test salt formulas pipeline |
| 3 | * DEFAULT_GIT_REF |
| 4 | * DEFAULT_GIT_URL |
| 5 | * CREDENTIALS_ID |
| 6 | */ |
| 7 | def common = new com.mirantis.mk.Common() |
| 8 | def ruby = new com.mirantis.mk.Ruby() |
Martin Polreich | 9a1d16b | 2017-08-29 17:16:12 +0200 | [diff] [blame] | 9 | def gerrit = new com.mirantis.mk.Gerrit() |
Martin Polreich | a3e3012 | 2017-08-22 12:43:55 +0200 | [diff] [blame] | 10 | |
| 11 | def defaultGitRef, defaultGitUrl |
| 12 | try { |
| 13 | defaultGitRef = DEFAULT_GIT_REF |
| 14 | defaultGitUrl = DEFAULT_GIT_URL |
| 15 | } catch (MissingPropertyException e) { |
| 16 | defaultGitRef = null |
| 17 | defaultGitUrl = null |
| 18 | } |
| 19 | |
Martin Polreich | c39edae | 2018-11-07 08:42:11 +0100 | [diff] [blame] | 20 | def openstack_credentials_id = '' |
| 21 | if (env.OPENSTACK_API_CREDENTIALS) { |
| 22 | openstack_credentials_id = OPENSTACK_API_CREDENTIALS |
| 23 | } |
| 24 | |
Martin Polreich | 4dedbcf | 2019-04-09 09:28:46 +0200 | [diff] [blame] | 25 | env.GERRIT_BRANCH = 'master' |
Martin Polreich | 37da131 | 2019-04-09 15:41:46 +0200 | [diff] [blame] | 26 | if (common.validInputParam('GERRIT_PARENT_BRANCH')) { |
Martin Polreich | 4dedbcf | 2019-04-09 09:28:46 +0200 | [diff] [blame] | 27 | env.GERRIT_BRANCH = GERRIT_PARENT_BRANCH |
| 28 | } |
| 29 | |
Martin Polreich | a3e3012 | 2017-08-22 12:43:55 +0200 | [diff] [blame] | 30 | def checkouted = false |
Martin Polreich | c0995dc | 2018-11-05 14:40:02 +0100 | [diff] [blame] | 31 | def openstackTest = false |
Martin Polreich | 32b8222 | 2018-11-21 14:01:39 +0100 | [diff] [blame] | 32 | def travisLess = false /** TODO: Remove once formulas are witched to new config */ |
| 33 | def cleanEnv = '' /** TODO: Remove once formulas are witched to new config */ |
| 34 | def testSuite = '' |
Martin Polreich | 5be29e3 | 2019-02-26 11:11:51 +0100 | [diff] [blame] | 35 | envOverrides = [] |
| 36 | kitchenFileName = '' |
Martin Polreich | a3e3012 | 2017-08-22 12:43:55 +0200 | [diff] [blame] | 37 | |
chnyda | 03f3ad4 | 2017-09-19 14:41:07 +0200 | [diff] [blame] | 38 | throttle(['test-formula']) { |
Jakub Josef | db3ddd8 | 2018-01-23 13:30:47 +0100 | [diff] [blame] | 39 | timeout(time: 1, unit: 'HOURS') { |
Vladimir Khlyunev | 2140cbb | 2020-06-17 12:34:23 +0000 | [diff] [blame] | 40 | node("python&&docker") { |
Jakub Josef | a63f986 | 2018-01-11 17:58:38 +0100 | [diff] [blame] | 41 | try { |
| 42 | stage("checkout") { |
| 43 | if (defaultGitRef && defaultGitUrl) { |
| 44 | checkouted = gerrit.gerritPatchsetCheckout(defaultGitUrl, defaultGitRef, "HEAD", CREDENTIALS_ID) |
chnyda | 03f3ad4 | 2017-09-19 14:41:07 +0200 | [diff] [blame] | 45 | } else { |
Jakub Josef | a63f986 | 2018-01-11 17:58:38 +0100 | [diff] [blame] | 46 | throw new Exception("Cannot checkout gerrit patchset, DEFAULT_GIT_REF is null") |
chnyda | 03f3ad4 | 2017-09-19 14:41:07 +0200 | [diff] [blame] | 47 | } |
| 48 | } |
Jakub Josef | a63f986 | 2018-01-11 17:58:38 +0100 | [diff] [blame] | 49 | stage("cleanup") { |
| 50 | if (checkouted) { |
| 51 | sh("make clean") |
| 52 | } |
| 53 | } |
| 54 | stage("kitchen") { |
Martin Polreich | 32b8222 | 2018-11-21 14:01:39 +0100 | [diff] [blame] | 55 | if (fileExists(".travis.yml")) {/** TODO: Remove this legacy block once formulas are witched to new config */ |
| 56 | if (checkouted) { |
| 57 | if (fileExists(".kitchen.yml") || fileExists(".kitchen.openstack.yml")) { |
| 58 | if (fileExists(".kitchen.openstack.yml")) { |
| 59 | common.infoMsg("Openstack Kitchen test configuration found, running Openstack kitchen tests.") |
| 60 | if (fileExists(".kitchen.yml")) { |
| 61 | common.infoMsg("Ignoring the docker Kitchen test configuration file.") |
| 62 | } |
| 63 | openstackTest = true |
| 64 | } else { |
| 65 | common.infoMsg("Docker Kitchen test configuration found, running Docker kitchen tests.") |
Martin Polreich | c0995dc | 2018-11-05 14:40:02 +0100 | [diff] [blame] | 66 | } |
Martin Polreich | 32b8222 | 2018-11-21 14:01:39 +0100 | [diff] [blame] | 67 | ruby.ensureRubyEnv() |
| 68 | if (fileExists(".travis.yml")) { |
| 69 | common.infoMsg(".travis.yml found, running custom kitchen init") |
| 70 | def kitchenConfigYML = readYaml(file: ".travis.yml") |
| 71 | def kitchenInit = kitchenConfigYML["install"] |
| 72 | def kitchenInstalled = false |
| 73 | if (kitchenInit && !kitchenInit.isEmpty()) { |
| 74 | for (int i = 0; i < kitchenInit.size(); i++) { |
| 75 | if (kitchenInit[i].trim().startsWith("if [ ! -e Gemfile ]")) { //found Gemfile config |
| 76 | common.infoMsg("Custom Gemfile configuration found, using them") |
| 77 | if (openstackTest) { |
| 78 | withCredentials([[$class: 'UsernamePasswordMultiBinding', credentialsId: openstack_credentials_id, |
| 79 | usernameVariable: 'OS_USERNAME', passwordVariable: 'OS_PASSWORD'], ]) { |
| 80 | env.OS_USERNAME = OS_USERNAME |
| 81 | env.OS_PASSWORD = OS_PASSWORD |
| 82 | env.OS_AUTH_URL = OS_AUTH_URL |
| 83 | env.OS_PROJECT_NAME = OS_PROJECT_NAME |
| 84 | env.OS_DOMAIN_NAME = OS_DOMAIN_NAME |
| 85 | env.OS_AZ = OS_AZ |
| 86 | } |
Martin Polreich | c39edae | 2018-11-07 08:42:11 +0100 | [diff] [blame] | 87 | } |
Martin Polreich | 32b8222 | 2018-11-21 14:01:39 +0100 | [diff] [blame] | 88 | ruby.installKitchen(kitchenInit[i].trim()) |
| 89 | kitchenInstalled = true |
Martin Polreich | c0995dc | 2018-11-05 14:40:02 +0100 | [diff] [blame] | 90 | } |
Jakub Josef | a63f986 | 2018-01-11 17:58:38 +0100 | [diff] [blame] | 91 | } |
| 92 | } |
Martin Polreich | 32b8222 | 2018-11-21 14:01:39 +0100 | [diff] [blame] | 93 | if (!kitchenInstalled) { |
| 94 | ruby.installKitchen() |
| 95 | } |
| 96 | } else { |
| 97 | common.infoMsg(".travis.yml not found, running default kitchen init") |
Jakub Josef | a63f986 | 2018-01-11 17:58:38 +0100 | [diff] [blame] | 98 | ruby.installKitchen() |
| 99 | } |
Martin Polreich | 32b8222 | 2018-11-21 14:01:39 +0100 | [diff] [blame] | 100 | common.infoMsg("Running part of kitchen test") |
| 101 | if (KITCHEN_ENV != null && !KITCHEN_ENV.isEmpty() && KITCHEN_ENV != "") { |
| 102 | cleanEnv = KITCHEN_ENV.replaceAll("\\s?SUITE=[^\\s]*", "") |
| 103 | if (openstackTest) { cleanEnv = "KITCHEN_YAML=.kitchen.openstack.yml " + cleanEnv } |
| 104 | sh("grep apt.mirantis.com -Ril | xargs -I{} bash -c \"echo {}; sed -i 's/apt.mirantis.com/apt.mcp.mirantis.net/g' {}\"") |
| 105 | sh("grep apt-mk.mirantis.com -Ril | xargs -I{} bash -c \"echo {}; sed -i 's/apt-mk.mirantis.com/apt.mcp.mirantis.net/g' {}\"") |
| 106 | def suite = ruby.getSuiteName(KITCHEN_ENV) |
| 107 | if (suite && suite != "") { |
| 108 | common.infoMsg("Running kitchen test with environment:" + KITCHEN_ENV.trim()) |
| 109 | ruby.runKitchenTests(cleanEnv, suite) |
| 110 | } else { |
| 111 | common.warningMsg("No SUITE was found. Running with all suites.") |
| 112 | ruby.runKitchenTests(cleanEnv, "") |
| 113 | } |
Jakub Josef | a63f986 | 2018-01-11 17:58:38 +0100 | [diff] [blame] | 114 | } else { |
Martin Polreich | 32b8222 | 2018-11-21 14:01:39 +0100 | [diff] [blame] | 115 | 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 Josef | a63f986 | 2018-01-11 17:58:38 +0100 | [diff] [blame] | 116 | } |
| 117 | } else { |
Martin Polreich | 32b8222 | 2018-11-21 14:01:39 +0100 | [diff] [blame] | 118 | throw new Exception(".kitchen.yml file not found, no kitchen tests triggered.") |
Jakub Josef | a63f986 | 2018-01-11 17:58:38 +0100 | [diff] [blame] | 119 | } |
Martin Polreich | 32b8222 | 2018-11-21 14:01:39 +0100 | [diff] [blame] | 120 | }/** TODO: End of block for removal */ |
| 121 | } else { |
| 122 | if (checkouted) { |
| 123 | travisLess = true |
Martin Polreich | 5be29e3 | 2019-02-26 11:11:51 +0100 | [diff] [blame] | 124 | if (fileExists(".kitchen.openstack.yml")) { |
| 125 | common.infoMsg("Openstack Kitchen test configuration found, running Openstack kitchen tests.") |
| 126 | kitchenFileName = ".kitchen.openstack.yml" |
| 127 | envOverrides.add("KITCHEN_YAML=${kitchenFileName}") |
Martin Polreich | 73c8e51 | 2019-02-26 15:16:33 +0100 | [diff] [blame] | 128 | rubyVersion = '2.5.0' |
Martin Polreich | 5be29e3 | 2019-02-26 11:11:51 +0100 | [diff] [blame] | 129 | withCredentials([[$class: 'UsernamePasswordMultiBinding', credentialsId: openstack_credentials_id, |
| 130 | usernameVariable: 'OS_USERNAME', passwordVariable: 'OS_PASSWORD'], ]) { |
| 131 | env.OS_USERNAME = OS_USERNAME |
| 132 | env.OS_PASSWORD = OS_PASSWORD |
| 133 | env.OS_AUTH_URL = OS_AUTH_URL |
| 134 | env.OS_PROJECT_NAME = OS_PROJECT_NAME |
| 135 | env.OS_DOMAIN_NAME = OS_DOMAIN_NAME |
| 136 | env.OS_AZ = OS_AZ |
Martin Polreich | 32b8222 | 2018-11-21 14:01:39 +0100 | [diff] [blame] | 137 | } |
Martin Polreich | 5be29e3 | 2019-02-26 11:11:51 +0100 | [diff] [blame] | 138 | } else if (fileExists(".kitchen.yml")) { |
| 139 | common.infoMsg("Docker Kitchen test configuration found, running Docker kitchen tests.") |
| 140 | kitchenFileName = ".kitchen.yml" |
Martin Polreich | 73c8e51 | 2019-02-26 15:16:33 +0100 | [diff] [blame] | 141 | rubyVersion = '2.4.1' |
Martin Polreich | 5be29e3 | 2019-02-26 11:11:51 +0100 | [diff] [blame] | 142 | } |
| 143 | if (kitchenFileName) { |
Martin Polreich | 5be29e3 | 2019-02-26 11:11:51 +0100 | [diff] [blame] | 144 | ruby.ensureRubyEnv(rubyVersion) |
Martin Polreich | 32b8222 | 2018-11-21 14:01:39 +0100 | [diff] [blame] | 145 | if (!fileExists("Gemfile")) { |
| 146 | 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'") |
| 147 | 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 Polreich | 32b8222 | 2018-11-21 14:01:39 +0100 | [diff] [blame] | 155 | 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 Polreich | 5be29e3 | 2019-02-26 11:11:51 +0100 | [diff] [blame] | 158 | ruby.runKitchenTests(envOverrides.join(' '), testSuite) |
Martin Polreich | 32b8222 | 2018-11-21 14:01:39 +0100 | [diff] [blame] | 159 | } 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 Polreich | 5be29e3 | 2019-02-26 11:11:51 +0100 | [diff] [blame] | 163 | throw new Exception(".kitchen.yml nor .kitchen.openstack.yml file not found, no kitchen tests triggered.") |
Martin Polreich | 32b8222 | 2018-11-21 14:01:39 +0100 | [diff] [blame] | 164 | } |
Jakub Josef | a63f986 | 2018-01-11 17:58:38 +0100 | [diff] [blame] | 165 | } |
| 166 | } |
| 167 | } |
| 168 | } catch (Throwable e) { |
| 169 | // If there was an error or exception thrown, the build failed |
| 170 | currentBuild.result = "FAILURE" |
Dzmitry Stremkouski | ca76bfa | 2018-11-07 14:19:58 +0100 | [diff] [blame] | 171 | sh(script: 'find .kitchen/logs/ -type f -iname "*.log" | xargs -I{} bash -c "echo {}; cat {}"') |
Martin Polreich | 32b8222 | 2018-11-21 14:01:39 +0100 | [diff] [blame] | 172 | 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 Josef | a63f986 | 2018-01-11 17:58:38 +0100 | [diff] [blame] | 177 | 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 | } |
chnyda | 03f3ad4 | 2017-09-19 14:41:07 +0200 | [diff] [blame] | 183 | } |
Martin Polreich | a3e3012 | 2017-08-22 12:43:55 +0200 | [diff] [blame] | 184 | } |
Martin Polreich | a3e3012 | 2017-08-22 12:43:55 +0200 | [diff] [blame] | 185 | } |
Martin Polreich | b33d23e | 2017-09-01 15:22:16 +0000 | [diff] [blame] | 186 | } |