blob: 7802a11d29588479b2ec86ea0f2367816191ab9f [file] [log] [blame]
Jakub Josef25c0ea52017-04-27 17:46:27 +02001package com.mirantis.mk
2
3/**
4 * Ruby functions
5 */
6
Jakub Josefad59b2f2017-04-28 16:08:40 +02007/**
8 * Ensures Ruby environment with given version (install it if necessary)
Jakub Josef7ad08f32017-05-03 13:23:42 +02009 * @param rubyVersion target ruby version (optional, default 2.2.3)
Jakub Josefad59b2f2017-04-28 16:08:40 +020010 */
Jakub Josef47494512017-08-15 16:40:06 +020011def ensureRubyEnv(rubyVersion="2.4.1"){
Martin Polreich3e55be92019-03-19 09:26:26 +010012 def ruby_build_root = "${env.WORKSPACE}/.rbenv/plugins/ruby-build"
Martin Polreiche0e009e2019-03-04 11:35:09 +010013 if (!fileExists("/var/lib/jenkins/.rbenv/versions/${rubyVersion}/bin/ruby")){
Jakub Josef47494512017-08-15 16:40:06 +020014 //XXX: patch ruby-build because debian package is quite old
Martin Polreich3e55be92019-03-19 09:26:26 +010015 sh "rbenv install ${rubyVersion} -sv";
Jakub Josefad59b2f2017-04-28 16:08:40 +020016 }
17 sh "rbenv local ${rubyVersion};rbenv exec gem update --system"
Martin Polreich3e55be92019-03-19 09:26:26 +010018 sh "rm -rf ${ruby_build_root}"
Jakub Josef25c0ea52017-04-27 17:46:27 +020019}
Jakub Josefad59b2f2017-04-28 16:08:40 +020020
21/**
22 * Install kitchen tools
23 */
Jakub Josefe6f3ae42017-05-23 15:05:14 +020024def installKitchen(kitchenInit=""){
Cedric Hnydae93bfb52017-11-06 15:03:41 +000025 sh """rbenv exec gem install bundler --conservative;
Cedric Hnydac2c0c182017-11-06 14:27:25 +000026 rbenv exec gem install test-kitchen --conservative;"""
Jakub Josefe6f3ae42017-05-23 15:05:14 +020027 if(kitchenInit!=""){
28 sh kitchenInit
29 }else{
30 sh """ test -e Gemfile || cat <<EOF > Gemfile
31 source 'https://rubygems.org'
32 gem 'rake'
33 gem 'test-kitchen'
34 gem 'kitchen-docker'
35 gem 'kitchen-inspec'
36 gem 'inspec'
37 gem 'kitchen-salt', :git => 'https://github.com/salt-formulas/kitchen-salt.git'"""
38 }
Jakub Josefad31a6b2017-11-06 17:15:36 +010039 sh "rbenv exec bundle install --path vendor/bundle"
Jakub Josef25c0ea52017-04-27 17:46:27 +020040}
41
Jakub Josefad59b2f2017-04-28 16:08:40 +020042/**
43 * Run kitchen tests in tests/integration
Jakub Josefb039fdd2017-05-22 16:28:06 +020044 * @param environment kitchen environment (optional can be empty)
Martin Polreich232b1982017-08-22 12:44:48 +020045 * @param suite name of test suite for kitchen
Jakub Josefad59b2f2017-04-28 16:08:40 +020046 */
Martin Polreich232b1982017-08-22 12:44:48 +020047def runKitchenTests(environment="", suite= ""){
Jakub Josef23547332017-05-22 13:40:40 +020048 def common = new com.mirantis.mk.Common()
Martin Polreich6610d502017-08-29 16:55:04 +020049 common.infoMsg("Running kitchen test ${suite}")
50 println(runKitchenCommand("converge ${suite}", environment))
51 println runKitchenCommand("verify ${suite} -t tests/integration", environment)
52 println runKitchenCommand("destroy", environment)
Jakub Josef25c0ea52017-04-27 17:46:27 +020053}
54
Jakub Josefad59b2f2017-04-28 16:08:40 +020055/**
56 * Run kitchen command
57 * @param cmd kitchen command
Jakub Josef23547332017-05-22 13:40:40 +020058 * @param environment kitchen environment properties (will be used before kitchen command), example: PLATFORM=ubuntu-16-04
59 * @return return kitchen output
Jakub Josefad59b2f2017-04-28 16:08:40 +020060 */
Jakub Josef23547332017-05-22 13:40:40 +020061def runKitchenCommand(cmd, environment = null){
62 if(environment && environment != ""){
Jakub Josefad31a6b2017-11-06 17:15:36 +010063 return sh(script: "${environment} rbenv exec bundle exec kitchen ${cmd}", returnStdout: true)
Jakub Josef785d9f92017-05-18 17:58:59 +020064 }else{
Jakub Josefad31a6b2017-11-06 17:15:36 +010065 return sh(script: "rbenv exec bundle exec kitchen ${cmd}", returnStdout: true)
Jakub Josef785d9f92017-05-18 17:58:59 +020066 }
Jakub Josefd324f462017-07-17 13:54:44 +020067}
Jakub Josefed663b82017-09-01 16:58:02 +020068
69/**
70 * Returns suite name from given env
71 * @param kitchenEnv kitchen env string
72 * @return suite name of empty string if no suite found
73 */
74def getSuiteName(kitchenEnv){
75 def suitePattern = java.util.regex.Pattern.compile("\\s?SUITE=([^\\s]*)")
76 def suiteMatcher = suitePattern.matcher(kitchenEnv)
77 if (suiteMatcher.find()) {
78 def suite = suiteMatcher.group(1)
Martin Polreich6781e9e2017-09-01 17:23:32 +020079 if(suite && suite != ""){
Jakub Josefed663b82017-09-01 16:58:02 +020080 return suite.replaceAll("_", "-")
81 }
82 }
83 return ""
84}