blob: 32b595e7389e42a79c9ba92e5e6d016aa28e6b9d [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 Polreiche0e009e2019-03-04 11:35:09 +010012 def ruby_build_root = "~/.rbenv/plugins/ruby-build"
13 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 Polreiche0e009e2019-03-04 11:35:09 +010015 // Check if git repo exists
16 if (!fileExists("${ruby_build_root}/.git")) {
17 sh "git clone https://github.com/rbenv/ruby-build.git ${ruby_build_root}"
18 } else {
19 // Ensure the repo is up-to-date
20 sh "cd ${ruby_build_root}"
21 sh "git pull"
22 }
Jakub Josefad59b2f2017-04-28 16:08:40 +020023 sh "rbenv install ${rubyVersion}";
24 }
25 sh "rbenv local ${rubyVersion};rbenv exec gem update --system"
Jakub Josef25c0ea52017-04-27 17:46:27 +020026}
Jakub Josefad59b2f2017-04-28 16:08:40 +020027
28/**
29 * Install kitchen tools
30 */
Jakub Josefe6f3ae42017-05-23 15:05:14 +020031def installKitchen(kitchenInit=""){
Cedric Hnydae93bfb52017-11-06 15:03:41 +000032 sh """rbenv exec gem install bundler --conservative;
Cedric Hnydac2c0c182017-11-06 14:27:25 +000033 rbenv exec gem install test-kitchen --conservative;"""
Jakub Josefe6f3ae42017-05-23 15:05:14 +020034 if(kitchenInit!=""){
35 sh kitchenInit
36 }else{
37 sh """ test -e Gemfile || cat <<EOF > Gemfile
38 source 'https://rubygems.org'
39 gem 'rake'
40 gem 'test-kitchen'
41 gem 'kitchen-docker'
42 gem 'kitchen-inspec'
43 gem 'inspec'
44 gem 'kitchen-salt', :git => 'https://github.com/salt-formulas/kitchen-salt.git'"""
45 }
Jakub Josefad31a6b2017-11-06 17:15:36 +010046 sh "rbenv exec bundle install --path vendor/bundle"
Jakub Josef25c0ea52017-04-27 17:46:27 +020047}
48
Jakub Josefad59b2f2017-04-28 16:08:40 +020049/**
50 * Run kitchen tests in tests/integration
Jakub Josefb039fdd2017-05-22 16:28:06 +020051 * @param environment kitchen environment (optional can be empty)
Martin Polreich232b1982017-08-22 12:44:48 +020052 * @param suite name of test suite for kitchen
Jakub Josefad59b2f2017-04-28 16:08:40 +020053 */
Martin Polreich232b1982017-08-22 12:44:48 +020054def runKitchenTests(environment="", suite= ""){
Jakub Josef23547332017-05-22 13:40:40 +020055 def common = new com.mirantis.mk.Common()
Martin Polreich6610d502017-08-29 16:55:04 +020056 common.infoMsg("Running kitchen test ${suite}")
57 println(runKitchenCommand("converge ${suite}", environment))
58 println runKitchenCommand("verify ${suite} -t tests/integration", environment)
59 println runKitchenCommand("destroy", environment)
Jakub Josef25c0ea52017-04-27 17:46:27 +020060}
61
Jakub Josefad59b2f2017-04-28 16:08:40 +020062/**
63 * Run kitchen command
64 * @param cmd kitchen command
Jakub Josef23547332017-05-22 13:40:40 +020065 * @param environment kitchen environment properties (will be used before kitchen command), example: PLATFORM=ubuntu-16-04
66 * @return return kitchen output
Jakub Josefad59b2f2017-04-28 16:08:40 +020067 */
Jakub Josef23547332017-05-22 13:40:40 +020068def runKitchenCommand(cmd, environment = null){
69 if(environment && environment != ""){
Jakub Josefad31a6b2017-11-06 17:15:36 +010070 return sh(script: "${environment} rbenv exec bundle exec kitchen ${cmd}", returnStdout: true)
Jakub Josef785d9f92017-05-18 17:58:59 +020071 }else{
Jakub Josefad31a6b2017-11-06 17:15:36 +010072 return sh(script: "rbenv exec bundle exec kitchen ${cmd}", returnStdout: true)
Jakub Josef785d9f92017-05-18 17:58:59 +020073 }
Jakub Josefd324f462017-07-17 13:54:44 +020074}
Jakub Josefed663b82017-09-01 16:58:02 +020075
76/**
77 * Returns suite name from given env
78 * @param kitchenEnv kitchen env string
79 * @return suite name of empty string if no suite found
80 */
81def getSuiteName(kitchenEnv){
82 def suitePattern = java.util.regex.Pattern.compile("\\s?SUITE=([^\\s]*)")
83 def suiteMatcher = suitePattern.matcher(kitchenEnv)
84 if (suiteMatcher.find()) {
85 def suite = suiteMatcher.group(1)
Martin Polreich6781e9e2017-09-01 17:23:32 +020086 if(suite && suite != ""){
Jakub Josefed663b82017-09-01 16:58:02 +020087 return suite.replaceAll("_", "-")
88 }
89 }
90 return ""
91}