| /** |
| * Test salt formulas pipeline |
| * DEFAULT_GIT_REF |
| * DEFAULT_GIT_URL |
| * CREDENTIALS_ID |
| */ |
| def common = new com.mirantis.mk.Common() |
| def ruby = new com.mirantis.mk.Ruby() |
| def gerrit = new com.mirantis.mk.Gerrit() |
| |
| def defaultGitRef, defaultGitUrl |
| try { |
| defaultGitRef = DEFAULT_GIT_REF |
| defaultGitUrl = DEFAULT_GIT_URL |
| } catch (MissingPropertyException e) { |
| defaultGitRef = null |
| defaultGitUrl = null |
| } |
| |
| def openstack_credentials_id = '' |
| if (env.OPENSTACK_API_CREDENTIALS) { |
| openstack_credentials_id = OPENSTACK_API_CREDENTIALS |
| } |
| |
| def nodeLabel = 'old16.04' |
| |
| def checkouted = false |
| def openstackTest = false |
| def travisLess = false /** TODO: Remove once formulas are witched to new config */ |
| def cleanEnv = '' /** TODO: Remove once formulas are witched to new config */ |
| def testSuite = '' |
| |
| throttle(['test-formula']) { |
| timeout(time: 1, unit: 'HOURS') { |
| node(nodeLabel) { |
| try { |
| stage("checkout") { |
| if (defaultGitRef && defaultGitUrl) { |
| checkouted = gerrit.gerritPatchsetCheckout(defaultGitUrl, defaultGitRef, "HEAD", CREDENTIALS_ID) |
| } else { |
| throw new Exception("Cannot checkout gerrit patchset, DEFAULT_GIT_REF is null") |
| } |
| } |
| stage("cleanup") { |
| if (checkouted) { |
| sh("make clean") |
| } |
| } |
| stage("kitchen") { |
| if (fileExists(".travis.yml")) {/** TODO: Remove this legacy block once formulas are witched to new config */ |
| if (checkouted) { |
| if (fileExists(".kitchen.yml") || fileExists(".kitchen.openstack.yml")) { |
| if (fileExists(".kitchen.openstack.yml")) { |
| common.infoMsg("Openstack Kitchen test configuration found, running Openstack kitchen tests.") |
| if (fileExists(".kitchen.yml")) { |
| common.infoMsg("Ignoring the docker Kitchen test configuration file.") |
| } |
| openstackTest = true |
| } else { |
| common.infoMsg("Docker Kitchen test configuration found, running Docker kitchen tests.") |
| } |
| ruby.ensureRubyEnv() |
| if (fileExists(".travis.yml")) { |
| common.infoMsg(".travis.yml found, running custom kitchen init") |
| def kitchenConfigYML = readYaml(file: ".travis.yml") |
| def kitchenInit = kitchenConfigYML["install"] |
| def kitchenInstalled = false |
| if (kitchenInit && !kitchenInit.isEmpty()) { |
| for (int i = 0; i < kitchenInit.size(); i++) { |
| if (kitchenInit[i].trim().startsWith("if [ ! -e Gemfile ]")) { //found Gemfile config |
| common.infoMsg("Custom Gemfile configuration found, using them") |
| if (openstackTest) { |
| withCredentials([[$class: 'UsernamePasswordMultiBinding', credentialsId: openstack_credentials_id, |
| usernameVariable: 'OS_USERNAME', passwordVariable: 'OS_PASSWORD'], ]) { |
| env.OS_USERNAME = OS_USERNAME |
| env.OS_PASSWORD = OS_PASSWORD |
| env.OS_AUTH_URL = OS_AUTH_URL |
| env.OS_PROJECT_NAME = OS_PROJECT_NAME |
| env.OS_DOMAIN_NAME = OS_DOMAIN_NAME |
| env.OS_AZ = OS_AZ |
| } |
| } |
| ruby.installKitchen(kitchenInit[i].trim()) |
| kitchenInstalled = true |
| } |
| } |
| } |
| if (!kitchenInstalled) { |
| ruby.installKitchen() |
| } |
| } else { |
| common.infoMsg(".travis.yml not found, running default kitchen init") |
| ruby.installKitchen() |
| } |
| common.infoMsg("Running part of kitchen test") |
| if (KITCHEN_ENV != null && !KITCHEN_ENV.isEmpty() && KITCHEN_ENV != "") { |
| cleanEnv = KITCHEN_ENV.replaceAll("\\s?SUITE=[^\\s]*", "") |
| if (openstackTest) { cleanEnv = "KITCHEN_YAML=.kitchen.openstack.yml " + cleanEnv } |
| sh("grep apt.mirantis.com -Ril | xargs -I{} bash -c \"echo {}; sed -i 's/apt.mirantis.com/apt.mcp.mirantis.net/g' {}\"") |
| sh("grep apt-mk.mirantis.com -Ril | xargs -I{} bash -c \"echo {}; sed -i 's/apt-mk.mirantis.com/apt.mcp.mirantis.net/g' {}\"") |
| def suite = ruby.getSuiteName(KITCHEN_ENV) |
| if (suite && suite != "") { |
| common.infoMsg("Running kitchen test with environment:" + KITCHEN_ENV.trim()) |
| ruby.runKitchenTests(cleanEnv, suite) |
| } else { |
| common.warningMsg("No SUITE was found. Running with all suites.") |
| ruby.runKitchenTests(cleanEnv, "") |
| } |
| } else { |
| throw new Exception("KITCHEN_ENV parameter is empty or invalid. This may indicate wrong env settings of initial test job or .travis.yml file.") |
| } |
| } else { |
| throw new Exception(".kitchen.yml file not found, no kitchen tests triggered.") |
| } |
| }/** TODO: End of block for removal */ |
| } else { |
| if (checkouted) { |
| travisLess = true |
| if (fileExists(".kitchen.yml") || fileExists(".kitchen.openstack.yml")) { |
| if (fileExists(".kitchen.openstack.yml")) { |
| common.infoMsg("Openstack Kitchen test configuration found, running Openstack kitchen tests.") |
| if (fileExists(".kitchen.yml")) { |
| common.infoMsg("Ignoring the docker Kitchen test configuration file.") |
| } |
| openstackTest = true |
| withCredentials([[$class: 'UsernamePasswordMultiBinding', credentialsId: openstack_credentials_id, |
| usernameVariable: 'OS_USERNAME', passwordVariable: 'OS_PASSWORD'], ]) { |
| env.OS_USERNAME = OS_USERNAME |
| env.OS_PASSWORD = OS_PASSWORD |
| env.OS_AUTH_URL = OS_AUTH_URL |
| env.OS_PROJECT_NAME = OS_PROJECT_NAME |
| env.OS_DOMAIN_NAME = OS_DOMAIN_NAME |
| env.OS_AZ = OS_AZ |
| } |
| } else { |
| common.infoMsg("Docker Kitchen test configuration found, running Docker kitchen tests.") |
| } |
| ruby.ensureRubyEnv() |
| if (!fileExists("Gemfile")) { |
| sh("curl -s 'https://gerrit.mcp.mirantis.com/projects/salt-formulas%2Fsalt-formulas-scripts/branches/master/files/Gemfile/content' | base64 -d > ./Gemfile") |
| ruby.installKitchen() |
| } else { |
| common.infoMsg("Override Gemfile found in the kitchen directory, using it.") |
| ruby.installKitchen() |
| } |
| common.infoMsg("Running part of kitchen test") |
| if (KITCHEN_ENV != null && !KITCHEN_ENV.isEmpty() && KITCHEN_ENV != "") { |
| testSuite = KITCHEN_ENV.replaceAll("_", "-").trim() |
| if (openstackTest) { testSuite = "KITCHEN_YAML=.kitchen.openstack.yml " + testSuite } |
| sh("grep apt.mirantis.com -Ril | xargs -I{} bash -c \"echo {}; sed -i 's/apt.mirantis.com/apt.mcp.mirantis.net/g' {}\"") |
| sh("grep apt-mk.mirantis.com -Ril | xargs -I{} bash -c \"echo {}; sed -i 's/apt-mk.mirantis.com/apt.mcp.mirantis.net/g' {}\"") |
| common.infoMsg("Running kitchen test with environment:" + testSuite) |
| ruby.runKitchenTests("", testSuite) |
| } else { |
| throw new Exception("KITCHEN_ENV parameter is empty or invalid. This may indicate wrong env settings of initial test job or .travis.yml file.") |
| } |
| } else { |
| throw new Exception(".kitchen.yml file not found, no kitchen tests triggered.") |
| } |
| } |
| } |
| } |
| } catch (Throwable e) { |
| // If there was an error or exception thrown, the build failed |
| currentBuild.result = "FAILURE" |
| sh(script: 'find .kitchen/logs/ -type f -iname "*.log" | xargs -I{} bash -c "echo {}; cat {}"') |
| if (travisLess) { |
| ruby.runKitchenCommand("destroy", testSuite) |
| } else { |
| ruby.runKitchenCommand("destroy", cleanEnv) /** TODO: Remove once formulas are witched to new config */ |
| } |
| throw e |
| } finally { |
| if (currentBuild.result == "FAILURE" && fileExists(".kitchen/logs/kitchen.log")) { |
| common.errorMsg("----------------KITCHEN LOG:---------------") |
| println readFile(".kitchen/logs/kitchen.log") |
| } |
| } |
| } |
| } |
| } |