Jakub Josef | 25c0ea5 | 2017-04-27 17:46:27 +0200 | [diff] [blame] | 1 | package com.mirantis.mk |
| 2 | |
| 3 | /** |
| 4 | * Ruby functions |
| 5 | */ |
| 6 | |
Jakub Josef | ad59b2f | 2017-04-28 16:08:40 +0200 | [diff] [blame] | 7 | /** |
| 8 | * Ensures Ruby environment with given version (install it if necessary) |
Jakub Josef | 7ad08f3 | 2017-05-03 13:23:42 +0200 | [diff] [blame] | 9 | * @param rubyVersion target ruby version (optional, default 2.2.3) |
Jakub Josef | ad59b2f | 2017-04-28 16:08:40 +0200 | [diff] [blame] | 10 | */ |
Jakub Josef | 4749451 | 2017-08-15 16:40:06 +0200 | [diff] [blame] | 11 | def ensureRubyEnv(rubyVersion="2.4.1"){ |
Martin Polreich | 3e55be9 | 2019-03-19 09:26:26 +0100 | [diff] [blame^] | 12 | def ruby_build_root = "${env.WORKSPACE}/.rbenv/plugins/ruby-build" |
Martin Polreich | e0e009e | 2019-03-04 11:35:09 +0100 | [diff] [blame] | 13 | if (!fileExists("/var/lib/jenkins/.rbenv/versions/${rubyVersion}/bin/ruby")){ |
Jakub Josef | 4749451 | 2017-08-15 16:40:06 +0200 | [diff] [blame] | 14 | //XXX: patch ruby-build because debian package is quite old |
Martin Polreich | 3e55be9 | 2019-03-19 09:26:26 +0100 | [diff] [blame^] | 15 | sh "rbenv install ${rubyVersion} -sv"; |
Jakub Josef | ad59b2f | 2017-04-28 16:08:40 +0200 | [diff] [blame] | 16 | } |
| 17 | sh "rbenv local ${rubyVersion};rbenv exec gem update --system" |
Martin Polreich | 3e55be9 | 2019-03-19 09:26:26 +0100 | [diff] [blame^] | 18 | sh "rm -rf ${ruby_build_root}" |
Jakub Josef | 25c0ea5 | 2017-04-27 17:46:27 +0200 | [diff] [blame] | 19 | } |
Jakub Josef | ad59b2f | 2017-04-28 16:08:40 +0200 | [diff] [blame] | 20 | |
| 21 | /** |
| 22 | * Install kitchen tools |
| 23 | */ |
Jakub Josef | e6f3ae4 | 2017-05-23 15:05:14 +0200 | [diff] [blame] | 24 | def installKitchen(kitchenInit=""){ |
Cedric Hnyda | e93bfb5 | 2017-11-06 15:03:41 +0000 | [diff] [blame] | 25 | sh """rbenv exec gem install bundler --conservative; |
Cedric Hnyda | c2c0c18 | 2017-11-06 14:27:25 +0000 | [diff] [blame] | 26 | rbenv exec gem install test-kitchen --conservative;""" |
Jakub Josef | e6f3ae4 | 2017-05-23 15:05:14 +0200 | [diff] [blame] | 27 | 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 Josef | ad31a6b | 2017-11-06 17:15:36 +0100 | [diff] [blame] | 39 | sh "rbenv exec bundle install --path vendor/bundle" |
Jakub Josef | 25c0ea5 | 2017-04-27 17:46:27 +0200 | [diff] [blame] | 40 | } |
| 41 | |
Jakub Josef | ad59b2f | 2017-04-28 16:08:40 +0200 | [diff] [blame] | 42 | /** |
| 43 | * Run kitchen tests in tests/integration |
Jakub Josef | b039fdd | 2017-05-22 16:28:06 +0200 | [diff] [blame] | 44 | * @param environment kitchen environment (optional can be empty) |
Martin Polreich | 232b198 | 2017-08-22 12:44:48 +0200 | [diff] [blame] | 45 | * @param suite name of test suite for kitchen |
Jakub Josef | ad59b2f | 2017-04-28 16:08:40 +0200 | [diff] [blame] | 46 | */ |
Martin Polreich | 232b198 | 2017-08-22 12:44:48 +0200 | [diff] [blame] | 47 | def runKitchenTests(environment="", suite= ""){ |
Jakub Josef | 2354733 | 2017-05-22 13:40:40 +0200 | [diff] [blame] | 48 | def common = new com.mirantis.mk.Common() |
Martin Polreich | 6610d50 | 2017-08-29 16:55:04 +0200 | [diff] [blame] | 49 | 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 Josef | 25c0ea5 | 2017-04-27 17:46:27 +0200 | [diff] [blame] | 53 | } |
| 54 | |
Jakub Josef | ad59b2f | 2017-04-28 16:08:40 +0200 | [diff] [blame] | 55 | /** |
| 56 | * Run kitchen command |
| 57 | * @param cmd kitchen command |
Jakub Josef | 2354733 | 2017-05-22 13:40:40 +0200 | [diff] [blame] | 58 | * @param environment kitchen environment properties (will be used before kitchen command), example: PLATFORM=ubuntu-16-04 |
| 59 | * @return return kitchen output |
Jakub Josef | ad59b2f | 2017-04-28 16:08:40 +0200 | [diff] [blame] | 60 | */ |
Jakub Josef | 2354733 | 2017-05-22 13:40:40 +0200 | [diff] [blame] | 61 | def runKitchenCommand(cmd, environment = null){ |
| 62 | if(environment && environment != ""){ |
Jakub Josef | ad31a6b | 2017-11-06 17:15:36 +0100 | [diff] [blame] | 63 | return sh(script: "${environment} rbenv exec bundle exec kitchen ${cmd}", returnStdout: true) |
Jakub Josef | 785d9f9 | 2017-05-18 17:58:59 +0200 | [diff] [blame] | 64 | }else{ |
Jakub Josef | ad31a6b | 2017-11-06 17:15:36 +0100 | [diff] [blame] | 65 | return sh(script: "rbenv exec bundle exec kitchen ${cmd}", returnStdout: true) |
Jakub Josef | 785d9f9 | 2017-05-18 17:58:59 +0200 | [diff] [blame] | 66 | } |
Jakub Josef | d324f46 | 2017-07-17 13:54:44 +0200 | [diff] [blame] | 67 | } |
Jakub Josef | ed663b8 | 2017-09-01 16:58:02 +0200 | [diff] [blame] | 68 | |
| 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 | */ |
| 74 | def 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 Polreich | 6781e9e | 2017-09-01 17:23:32 +0200 | [diff] [blame] | 79 | if(suite && suite != ""){ |
Jakub Josef | ed663b8 | 2017-09-01 16:58:02 +0200 | [diff] [blame] | 80 | return suite.replaceAll("_", "-") |
| 81 | } |
| 82 | } |
| 83 | return "" |
| 84 | } |