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 | a3e3012 | 2017-08-22 12:43:55 +0200 | [diff] [blame] | 25 | def checkouted = false |
Martin Polreich | c0995dc | 2018-11-05 14:40:02 +0100 | [diff] [blame] | 26 | def openstackTest = false |
Martin Polreich | 32b8222 | 2018-11-21 14:01:39 +0100 | [diff] [blame^] | 27 | def travisLess = false /** TODO: Remove once formulas are witched to new config */ |
| 28 | def cleanEnv = '' /** TODO: Remove once formulas are witched to new config */ |
| 29 | def testSuite = '' |
Martin Polreich | a3e3012 | 2017-08-22 12:43:55 +0200 | [diff] [blame] | 30 | |
chnyda | 03f3ad4 | 2017-09-19 14:41:07 +0200 | [diff] [blame] | 31 | throttle(['test-formula']) { |
Jakub Josef | db3ddd8 | 2018-01-23 13:30:47 +0100 | [diff] [blame] | 32 | timeout(time: 1, unit: 'HOURS') { |
Jakub Josef | 22815b0 | 2018-01-30 16:05:26 +0100 | [diff] [blame] | 33 | node("python&&docker") { |
Jakub Josef | a63f986 | 2018-01-11 17:58:38 +0100 | [diff] [blame] | 34 | try { |
| 35 | stage("checkout") { |
| 36 | if (defaultGitRef && defaultGitUrl) { |
| 37 | checkouted = gerrit.gerritPatchsetCheckout(defaultGitUrl, defaultGitRef, "HEAD", CREDENTIALS_ID) |
chnyda | 03f3ad4 | 2017-09-19 14:41:07 +0200 | [diff] [blame] | 38 | } else { |
Jakub Josef | a63f986 | 2018-01-11 17:58:38 +0100 | [diff] [blame] | 39 | throw new Exception("Cannot checkout gerrit patchset, DEFAULT_GIT_REF is null") |
chnyda | 03f3ad4 | 2017-09-19 14:41:07 +0200 | [diff] [blame] | 40 | } |
| 41 | } |
Jakub Josef | a63f986 | 2018-01-11 17:58:38 +0100 | [diff] [blame] | 42 | stage("cleanup") { |
| 43 | if (checkouted) { |
| 44 | sh("make clean") |
| 45 | } |
| 46 | } |
| 47 | stage("kitchen") { |
Martin Polreich | 32b8222 | 2018-11-21 14:01:39 +0100 | [diff] [blame^] | 48 | 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 Polreich | c0995dc | 2018-11-05 14:40:02 +0100 | [diff] [blame] | 59 | } |
Martin Polreich | 32b8222 | 2018-11-21 14:01:39 +0100 | [diff] [blame^] | 60 | 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 Polreich | c39edae | 2018-11-07 08:42:11 +0100 | [diff] [blame] | 80 | } |
Martin Polreich | 32b8222 | 2018-11-21 14:01:39 +0100 | [diff] [blame^] | 81 | ruby.installKitchen(kitchenInit[i].trim()) |
| 82 | kitchenInstalled = true |
Martin Polreich | c0995dc | 2018-11-05 14:40:02 +0100 | [diff] [blame] | 83 | } |
Jakub Josef | a63f986 | 2018-01-11 17:58:38 +0100 | [diff] [blame] | 84 | } |
| 85 | } |
Martin Polreich | 32b8222 | 2018-11-21 14:01:39 +0100 | [diff] [blame^] | 86 | if (!kitchenInstalled) { |
| 87 | ruby.installKitchen() |
| 88 | } |
| 89 | } else { |
| 90 | common.infoMsg(".travis.yml not found, running default kitchen init") |
Jakub Josef | a63f986 | 2018-01-11 17:58:38 +0100 | [diff] [blame] | 91 | ruby.installKitchen() |
| 92 | } |
Martin Polreich | 32b8222 | 2018-11-21 14:01:39 +0100 | [diff] [blame^] | 93 | 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 Josef | a63f986 | 2018-01-11 17:58:38 +0100 | [diff] [blame] | 107 | } else { |
Martin Polreich | 32b8222 | 2018-11-21 14:01:39 +0100 | [diff] [blame^] | 108 | 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] | 109 | } |
| 110 | } else { |
Martin Polreich | 32b8222 | 2018-11-21 14:01:39 +0100 | [diff] [blame^] | 111 | throw new Exception(".kitchen.yml file not found, no kitchen tests triggered.") |
Jakub Josef | a63f986 | 2018-01-11 17:58:38 +0100 | [diff] [blame] | 112 | } |
Martin Polreich | 32b8222 | 2018-11-21 14:01:39 +0100 | [diff] [blame^] | 113 | }/** 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 Josef | a63f986 | 2018-01-11 17:58:38 +0100 | [diff] [blame] | 158 | } |
| 159 | } |
| 160 | } |
| 161 | } catch (Throwable e) { |
| 162 | // If there was an error or exception thrown, the build failed |
| 163 | currentBuild.result = "FAILURE" |
Dzmitry Stremkouski | ca76bfa | 2018-11-07 14:19:58 +0100 | [diff] [blame] | 164 | 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^] | 165 | 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 Josef | a63f986 | 2018-01-11 17:58:38 +0100 | [diff] [blame] | 170 | 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 | } |
chnyda | 03f3ad4 | 2017-09-19 14:41:07 +0200 | [diff] [blame] | 176 | } |
Martin Polreich | a3e3012 | 2017-08-22 12:43:55 +0200 | [diff] [blame] | 177 | } |
Martin Polreich | a3e3012 | 2017-08-22 12:43:55 +0200 | [diff] [blame] | 178 | } |
Martin Polreich | b33d23e | 2017-09-01 15:22:16 +0000 | [diff] [blame] | 179 | } |