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() |
| 9 | |
| 10 | def defaultGitRef, defaultGitUrl |
| 11 | try { |
| 12 | defaultGitRef = DEFAULT_GIT_REF |
| 13 | defaultGitUrl = DEFAULT_GIT_URL |
| 14 | } catch (MissingPropertyException e) { |
| 15 | defaultGitRef = null |
| 16 | defaultGitUrl = null |
| 17 | } |
| 18 | |
| 19 | def checkouted = false |
| 20 | |
| 21 | node("python") { |
| 22 | try { |
| 23 | stage("checkout") { |
| 24 | if (defaultGitRef && defaultGitUrl) { |
| 25 | checkouted = gerrit.gerritPatchsetCheckout(defaultGitUrl, defaultGitRef, "HEAD", CREDENTIALS_ID) |
| 26 | } else { |
| 27 | throw new Exception("Cannot checkout gerrit patchset, DEFAULT_GIT_REF is null") |
| 28 | } |
| 29 | } |
| 30 | stage("test") { |
| 31 | if (checkouted) { |
| 32 | sh("make clean") |
| 33 | sh("[ $SALT_VERSION != 'latest' ] || export SALT_VERSION=''; make test") |
| 34 | } |
| 35 | } |
| 36 | stage("kitchen") { |
| 37 | if (checkouted) { |
| 38 | if (fileExists(".kitchen.yml")) { |
| 39 | common.infoMsg(".kitchen.yml found, running kitchen tests") |
| 40 | ruby.ensureRubyEnv() |
| 41 | if (fileExists(".travis.yml")) { |
| 42 | common.infoMsg(".travis.yml found, running custom kitchen init") |
| 43 | def kitchenConfigYML = readYaml(file: ".travis.yml") |
| 44 | def kitchenInit = kitchenConfigYML["install"] |
| 45 | def kitchenInstalled = false |
| 46 | if (kitchenInit && !kitchenInit.isEmpty()) { |
| 47 | for (int i = 0; i < kitchenInit.size(); i++) { |
| 48 | if (kitchenInit[i].trim().startsWith("test -e Gemfile")) { //found Gemfile config |
| 49 | common.infoMsg("Custom Gemfile configuration found, using them") |
| 50 | ruby.installKitchen(kitchenInit[i].trim()) |
| 51 | kitchenInstalled = true |
| 52 | } |
| 53 | } |
| 54 | } |
| 55 | if (!kitchenInstalled) { |
| 56 | ruby.installKitchen() |
| 57 | } |
| 58 | } else { |
| 59 | common.infoMsg(".travis.yml not found, running default kitchen init") |
| 60 | ruby.installKitchen() |
| 61 | } |
| 62 | common.infoMsg("Running part of kitchen test") |
| 63 | if (common.validInputParam(KITCHEN_ENV)) { |
| 64 | def cleanEnv = KITCHEN_ENV.replaceAll("\\s?SUITE=[^\\s]*", "") |
| 65 | def suitePattern = java.util.regex.Pattern.compile("\\s?SUITE=([^\\s]*)") |
| 66 | def suiteMatcher = suitePattern.matcher(KITCHEN_ENV) |
| 67 | if (suiteMatcher.find()) { |
| 68 | def suite = suiteMatcher.group(1) |
| 69 | def cleanSuite = suite.replaceAll("_", "-") |
| 70 | common.infoMsg("Running kitchen test with environment:" + filteredEnvs.trim()) |
| 71 | ruby.runKitchenTests(cleanEnv, cleanSuite) |
| 72 | } else { |
| 73 | common.warningMsg("No SUITE was found. Running with all suites.") |
| 74 | ruby.runKitchenTests(cleanEnv, "") |
| 75 | } |
| 76 | } else { |
| 77 | throw new Exception("KITCHEN_ENV parameter is empty or invalid. This may indicate wrong env settings of initial test job or .travis.yml file.") |
| 78 | } |
| 79 | } else { |
| 80 | throw new Exception(".kitchen.yml file not found, no kitchen tests triggered.") |
| 81 | } |
| 82 | } |
| 83 | } |
| 84 | } catch (Throwable e) { |
| 85 | // If there was an error or exception thrown, the build failed |
| 86 | currentBuild.result = "FAILURE" |
| 87 | ruby.runKitchenCommand("destroy") |
| 88 | throw e |
| 89 | } finally { |
| 90 | if (currentBuild.result == "FAILURE" && fileExists(".kitchen/logs/kitchen.log")) { |
| 91 | common.errorMsg("----------------KITCHEN LOG:---------------") |
| 92 | println readFile(".kitchen/logs/kitchen.log") |
| 93 | } |
| 94 | common.sendNotification(currentBuild.result, "", ["slack"]) |
| 95 | } |
| 96 | } |