| package com.mirantis.mk |
| |
| /** |
| * Ruby functions |
| */ |
| |
| /** |
| * Ensures Ruby environment with given version (install it if necessary) |
| * @param rubyVersion target ruby version (optional, default 2.2.3) |
| */ |
| def ensureRubyEnv(rubyVersion="2.2.3"){ |
| if(!fileExists("/var/lib/jenkins/.rbenv/versions/${rubyVersion}/bin/ruby")){ |
| sh "rbenv install ${rubyVersion}"; |
| } |
| sh "rbenv local ${rubyVersion};rbenv exec gem update --system" |
| } |
| |
| /** |
| * Install kitchen tools |
| */ |
| def installKitchen(kitchenInit=""){ |
| sh """rbenv exec gem install bundler --conservative; |
| rbenv exec gem install test-kitchen --conservative;""" |
| if(kitchenInit!=""){ |
| sh kitchenInit |
| }else{ |
| sh """ test -e Gemfile || cat <<EOF > Gemfile |
| source 'https://rubygems.org' |
| gem 'rake' |
| gem 'test-kitchen' |
| gem 'kitchen-docker' |
| gem 'kitchen-inspec' |
| gem 'inspec' |
| gem 'kitchen-salt', :git => 'https://github.com/salt-formulas/kitchen-salt.git'""" |
| } |
| sh "rbenv exec bundler install --path vendor/bundle" |
| } |
| |
| /** |
| * Run kitchen tests in tests/integration |
| * @param environment kitchen environment (optional can be empty) |
| * @param parallelTesting run kitchen test suites in parallel (optional, default true) |
| */ |
| def runKitchenTests(environment="", parallelTesting = true){ |
| def common = new com.mirantis.mk.Common() |
| def kitchenTests=runKitchenCommand("list -b 2>/dev/null \"\$SUITE\"", environment) |
| if(kitchenTests && kitchenTests != ""){ |
| def kitchenTestsList = kitchenTests.trim().tokenize("\n") |
| def kitchenTestRuns = [:] |
| common.infoMsg(String.format("Found %s kitchen test suites", kitchenTestsList.size())) |
| for(int i=0;i<kitchenTestsList.size();i++){ |
| def testSuite = kitchenTestsList[i] |
| kitchenTestRuns["kitchen-${testSuite}-${i}"] = { |
| common.infoMsg("Running kitchen test ${testSuite}") |
| println(runKitchenCommand("converge ${testSuite}", environment)) |
| println runKitchenCommand("verify ${testSuite} -t tests/integration", environment) |
| println runKitchenCommand("destroy", environment) |
| } |
| } |
| if(parallelTesting){ |
| parallel kitchenTestRuns |
| }else{ |
| common.serial(kitchenTestRuns) |
| } |
| }else{ |
| common.errorMsg("Cannot found kitchen test suites, kitchen list command returns bad output") |
| } |
| } |
| |
| /** |
| * Run kitchen command |
| * @param cmd kitchen command |
| * @param environment kitchen environment properties (will be used before kitchen command), example: PLATFORM=ubuntu-16-04 |
| * @return return kitchen output |
| */ |
| def runKitchenCommand(cmd, environment = null){ |
| if(environment && environment != ""){ |
| return sh(script: "${environment} rbenv exec bundler exec kitchen ${cmd}", returnStdout: true) |
| }else{ |
| return sh(script: "rbenv exec bundler exec kitchen ${cmd}", returnStdout: true) |
| } |
| } |