blob: 0af5deb3fa43c6162dfbd5cb4c703bb7b968c317 [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 */
Vladimir Khlyunevcec6b922022-02-01 13:11:32 +040011def ensureRubyEnv(rubyVersion="2.6.6", host_to_lock=""){
12 def lock_name = "install_ruby_system_${host_to_lock}"
13 lock(lock_name) {
Vladimir Khlyunevafc358d2022-01-31 22:57:13 +040014 if (!fileExists("/var/lib/jenkins/.rbenv/versions/${rubyVersion}/bin/ruby")){
15 //XXX: patch ruby-build because debian package is quite old
16 sh "rm -rf ~/.rbenv/plugins/ruby-build"
17 sh "git clone https://github.com/rbenv/ruby-build.git ~/.rbenv/plugins/ruby-build"
18 sh "rbenv install ${rubyVersion} -sv";
19 }
Jakub Josefad59b2f2017-04-28 16:08:40 +020020 }
21 sh "rbenv local ${rubyVersion};rbenv exec gem update --system"
Jakub Josef25c0ea52017-04-27 17:46:27 +020022}
Jakub Josefad59b2f2017-04-28 16:08:40 +020023
24/**
25 * Install kitchen tools
26 */
Jakub Josefe6f3ae42017-05-23 15:05:14 +020027def installKitchen(kitchenInit=""){
Cedric Hnydae93bfb52017-11-06 15:03:41 +000028 sh """rbenv exec gem install bundler --conservative;
Cedric Hnydac2c0c182017-11-06 14:27:25 +000029 rbenv exec gem install test-kitchen --conservative;"""
Jakub Josefe6f3ae42017-05-23 15:05:14 +020030 if(kitchenInit!=""){
31 sh kitchenInit
32 }else{
33 sh """ test -e Gemfile || cat <<EOF > Gemfile
34 source 'https://rubygems.org'
35 gem 'rake'
36 gem 'test-kitchen'
37 gem 'kitchen-docker'
38 gem 'kitchen-inspec'
39 gem 'inspec'
40 gem 'kitchen-salt', :git => 'https://github.com/salt-formulas/kitchen-salt.git'"""
41 }
Jakub Josefad31a6b2017-11-06 17:15:36 +010042 sh "rbenv exec bundle install --path vendor/bundle"
Jakub Josef25c0ea52017-04-27 17:46:27 +020043}
44
Jakub Josefad59b2f2017-04-28 16:08:40 +020045/**
46 * Run kitchen tests in tests/integration
Jakub Josefb039fdd2017-05-22 16:28:06 +020047 * @param environment kitchen environment (optional can be empty)
Martin Polreich232b1982017-08-22 12:44:48 +020048 * @param suite name of test suite for kitchen
Jakub Josefad59b2f2017-04-28 16:08:40 +020049 */
Martin Polreich232b1982017-08-22 12:44:48 +020050def runKitchenTests(environment="", suite= ""){
Jakub Josef23547332017-05-22 13:40:40 +020051 def common = new com.mirantis.mk.Common()
Martin Polreich6610d502017-08-29 16:55:04 +020052 common.infoMsg("Running kitchen test ${suite}")
53 println(runKitchenCommand("converge ${suite}", environment))
54 println runKitchenCommand("verify ${suite} -t tests/integration", environment)
55 println runKitchenCommand("destroy", environment)
Jakub Josef25c0ea52017-04-27 17:46:27 +020056}
57
Jakub Josefad59b2f2017-04-28 16:08:40 +020058/**
59 * Run kitchen command
60 * @param cmd kitchen command
Jakub Josef23547332017-05-22 13:40:40 +020061 * @param environment kitchen environment properties (will be used before kitchen command), example: PLATFORM=ubuntu-16-04
62 * @return return kitchen output
Jakub Josefad59b2f2017-04-28 16:08:40 +020063 */
Jakub Josef23547332017-05-22 13:40:40 +020064def runKitchenCommand(cmd, environment = null){
65 if(environment && environment != ""){
Jakub Josefad31a6b2017-11-06 17:15:36 +010066 return sh(script: "${environment} rbenv exec bundle exec kitchen ${cmd}", returnStdout: true)
Jakub Josef785d9f92017-05-18 17:58:59 +020067 }else{
Jakub Josefad31a6b2017-11-06 17:15:36 +010068 return sh(script: "rbenv exec bundle exec kitchen ${cmd}", returnStdout: true)
Jakub Josef785d9f92017-05-18 17:58:59 +020069 }
Jakub Josefd324f462017-07-17 13:54:44 +020070}
Jakub Josefed663b82017-09-01 16:58:02 +020071
72/**
73 * Returns suite name from given env
74 * @param kitchenEnv kitchen env string
75 * @return suite name of empty string if no suite found
76 */
77def getSuiteName(kitchenEnv){
78 def suitePattern = java.util.regex.Pattern.compile("\\s?SUITE=([^\\s]*)")
79 def suiteMatcher = suitePattern.matcher(kitchenEnv)
80 if (suiteMatcher.find()) {
81 def suite = suiteMatcher.group(1)
Martin Polreich6781e9e2017-09-01 17:23:32 +020082 if(suite && suite != ""){
Jakub Josefed663b82017-09-01 16:58:02 +020083 return suite.replaceAll("_", "-")
84 }
85 }
86 return ""
87}