blob: 2e1e4945873c16bf8886634a020613c8645f7547 [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)
Denis Egorenkoa451c6b2020-12-16 15:43:12 +04009 * @param rubyVersion target ruby version (optional, default 2.6.6)
Jakub Josefad59b2f2017-04-28 16:08:40 +020010 */
Denis Egorenkoa451c6b2020-12-16 15:43:12 +040011def ensureRubyEnv(rubyVersion="2.6.6"){
Martin Polreiche0e009e2019-03-04 11:35:09 +010012 if (!fileExists("/var/lib/jenkins/.rbenv/versions/${rubyVersion}/bin/ruby")){
Jakub Josef47494512017-08-15 16:40:06 +020013 //XXX: patch ruby-build because debian package is quite old
Vladimir Khlyunev9959de52020-06-15 14:44:56 +040014 sh "git clone https://github.com/rbenv/ruby-build.git ~/.rbenv/plugins/ruby-build || git -C ~/.rbenv/plugins/ruby-build pull origin master"
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"
Jakub Josef25c0ea52017-04-27 17:46:27 +020018}
Jakub Josefad59b2f2017-04-28 16:08:40 +020019
20/**
21 * Install kitchen tools
22 */
Jakub Josefe6f3ae42017-05-23 15:05:14 +020023def installKitchen(kitchenInit=""){
Cedric Hnydae93bfb52017-11-06 15:03:41 +000024 sh """rbenv exec gem install bundler --conservative;
Cedric Hnydac2c0c182017-11-06 14:27:25 +000025 rbenv exec gem install test-kitchen --conservative;"""
Jakub Josefe6f3ae42017-05-23 15:05:14 +020026 if(kitchenInit!=""){
27 sh kitchenInit
28 }else{
29 sh """ test -e Gemfile || cat <<EOF > Gemfile
30 source 'https://rubygems.org'
31 gem 'rake'
32 gem 'test-kitchen'
33 gem 'kitchen-docker'
34 gem 'kitchen-inspec'
35 gem 'inspec'
36 gem 'kitchen-salt', :git => 'https://github.com/salt-formulas/kitchen-salt.git'"""
37 }
Jakub Josefad31a6b2017-11-06 17:15:36 +010038 sh "rbenv exec bundle install --path vendor/bundle"
Jakub Josef25c0ea52017-04-27 17:46:27 +020039}
40
Jakub Josefad59b2f2017-04-28 16:08:40 +020041/**
42 * Run kitchen tests in tests/integration
Jakub Josefb039fdd2017-05-22 16:28:06 +020043 * @param environment kitchen environment (optional can be empty)
Martin Polreich232b1982017-08-22 12:44:48 +020044 * @param suite name of test suite for kitchen
Jakub Josefad59b2f2017-04-28 16:08:40 +020045 */
Martin Polreich232b1982017-08-22 12:44:48 +020046def runKitchenTests(environment="", suite= ""){
Jakub Josef23547332017-05-22 13:40:40 +020047 def common = new com.mirantis.mk.Common()
Martin Polreich6610d502017-08-29 16:55:04 +020048 common.infoMsg("Running kitchen test ${suite}")
49 println(runKitchenCommand("converge ${suite}", environment))
50 println runKitchenCommand("verify ${suite} -t tests/integration", environment)
51 println runKitchenCommand("destroy", environment)
Jakub Josef25c0ea52017-04-27 17:46:27 +020052}
53
Jakub Josefad59b2f2017-04-28 16:08:40 +020054/**
55 * Run kitchen command
56 * @param cmd kitchen command
Jakub Josef23547332017-05-22 13:40:40 +020057 * @param environment kitchen environment properties (will be used before kitchen command), example: PLATFORM=ubuntu-16-04
58 * @return return kitchen output
Jakub Josefad59b2f2017-04-28 16:08:40 +020059 */
Jakub Josef23547332017-05-22 13:40:40 +020060def runKitchenCommand(cmd, environment = null){
61 if(environment && environment != ""){
Jakub Josefad31a6b2017-11-06 17:15:36 +010062 return sh(script: "${environment} rbenv exec bundle exec kitchen ${cmd}", returnStdout: true)
Jakub Josef785d9f92017-05-18 17:58:59 +020063 }else{
Jakub Josefad31a6b2017-11-06 17:15:36 +010064 return sh(script: "rbenv exec bundle exec kitchen ${cmd}", returnStdout: true)
Jakub Josef785d9f92017-05-18 17:58:59 +020065 }
Jakub Josefd324f462017-07-17 13:54:44 +020066}
Jakub Josefed663b82017-09-01 16:58:02 +020067
68/**
69 * Returns suite name from given env
70 * @param kitchenEnv kitchen env string
71 * @return suite name of empty string if no suite found
72 */
73def getSuiteName(kitchenEnv){
74 def suitePattern = java.util.regex.Pattern.compile("\\s?SUITE=([^\\s]*)")
75 def suiteMatcher = suitePattern.matcher(kitchenEnv)
76 if (suiteMatcher.find()) {
77 def suite = suiteMatcher.group(1)
Martin Polreich6781e9e2017-09-01 17:23:32 +020078 if(suite && suite != ""){
Jakub Josefed663b82017-09-01 16:58:02 +020079 return suite.replaceAll("_", "-")
80 }
81 }
82 return ""
83}