blob: 2e1e4945873c16bf8886634a020613c8645f7547 [file] [log] [blame]
package com.mirantis.mk
/**
* Ruby functions
*/
/**
* Ensures Ruby environment with given version (install it if necessary)
* @param rubyVersion target ruby version (optional, default 2.6.6)
*/
def ensureRubyEnv(rubyVersion="2.6.6"){
if (!fileExists("/var/lib/jenkins/.rbenv/versions/${rubyVersion}/bin/ruby")){
//XXX: patch ruby-build because debian package is quite old
sh "git clone https://github.com/rbenv/ruby-build.git ~/.rbenv/plugins/ruby-build || git -C ~/.rbenv/plugins/ruby-build pull origin master"
sh "rbenv install ${rubyVersion} -sv";
}
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 bundle install --path vendor/bundle"
}
/**
* Run kitchen tests in tests/integration
* @param environment kitchen environment (optional can be empty)
* @param suite name of test suite for kitchen
*/
def runKitchenTests(environment="", suite= ""){
def common = new com.mirantis.mk.Common()
common.infoMsg("Running kitchen test ${suite}")
println(runKitchenCommand("converge ${suite}", environment))
println runKitchenCommand("verify ${suite} -t tests/integration", environment)
println runKitchenCommand("destroy", environment)
}
/**
* 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 bundle exec kitchen ${cmd}", returnStdout: true)
}else{
return sh(script: "rbenv exec bundle exec kitchen ${cmd}", returnStdout: true)
}
}
/**
* Returns suite name from given env
* @param kitchenEnv kitchen env string
* @return suite name of empty string if no suite found
*/
def getSuiteName(kitchenEnv){
def suitePattern = java.util.regex.Pattern.compile("\\s?SUITE=([^\\s]*)")
def suiteMatcher = suitePattern.matcher(kitchenEnv)
if (suiteMatcher.find()) {
def suite = suiteMatcher.group(1)
if(suite && suite != ""){
return suite.replaceAll("_", "-")
}
}
return ""
}