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"){ |
Jakub Josef | ad59b2f | 2017-04-28 16:08:40 +0200 | [diff] [blame] | 12 | if(!fileExists("/var/lib/jenkins/.rbenv/versions/${rubyVersion}/bin/ruby")){ |
Jakub Josef | 4749451 | 2017-08-15 16:40:06 +0200 | [diff] [blame] | 13 | //XXX: patch ruby-build because debian package is quite old |
| 14 | sh "git clone https://github.com/rbenv/ruby-build.git ~/.rbenv/plugins/ruby-build" |
Jakub Josef | ad59b2f | 2017-04-28 16:08:40 +0200 | [diff] [blame] | 15 | sh "rbenv install ${rubyVersion}"; |
| 16 | } |
| 17 | sh "rbenv local ${rubyVersion};rbenv exec gem update --system" |
Jakub Josef | 25c0ea5 | 2017-04-27 17:46:27 +0200 | [diff] [blame] | 18 | } |
Jakub Josef | ad59b2f | 2017-04-28 16:08:40 +0200 | [diff] [blame] | 19 | |
| 20 | /** |
| 21 | * Install kitchen tools |
| 22 | */ |
Jakub Josef | e6f3ae4 | 2017-05-23 15:05:14 +0200 | [diff] [blame] | 23 | def installKitchen(kitchenInit=""){ |
Jakub Josef | 8ed284e | 2017-05-22 15:22:15 +0200 | [diff] [blame] | 24 | sh """rbenv exec gem install bundler --conservative; |
| 25 | rbenv exec gem install test-kitchen --conservative;""" |
Jakub Josef | e6f3ae4 | 2017-05-23 15:05:14 +0200 | [diff] [blame] | 26 | 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 Josef | 4cda59e | 2017-04-28 21:00:01 +0200 | [diff] [blame] | 38 | sh "rbenv exec bundler install --path vendor/bundle" |
Jakub Josef | 25c0ea5 | 2017-04-27 17:46:27 +0200 | [diff] [blame] | 39 | } |
| 40 | |
Jakub Josef | ad59b2f | 2017-04-28 16:08:40 +0200 | [diff] [blame] | 41 | /** |
| 42 | * Run kitchen tests in tests/integration |
Jakub Josef | b039fdd | 2017-05-22 16:28:06 +0200 | [diff] [blame] | 43 | * @param environment kitchen environment (optional can be empty) |
Martin Polreich | 232b198 | 2017-08-22 12:44:48 +0200 | [diff] [blame^] | 44 | * @param suite name of test suite for kitchen |
Jakub Josef | ad59b2f | 2017-04-28 16:08:40 +0200 | [diff] [blame] | 45 | */ |
Martin Polreich | 232b198 | 2017-08-22 12:44:48 +0200 | [diff] [blame^] | 46 | def runKitchenTests(environment="", suite= ""){ |
Jakub Josef | 2354733 | 2017-05-22 13:40:40 +0200 | [diff] [blame] | 47 | def common = new com.mirantis.mk.Common() |
Martin Polreich | 232b198 | 2017-08-22 12:44:48 +0200 | [diff] [blame^] | 48 | kitchenTestRuns["kitchen-${suite}".replaceAll("trevorj-salty-whales:", "")] = { |
| 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 | 2354733 | 2017-05-22 13:40:40 +0200 | [diff] [blame] | 53 | } |
Jakub Josef | 25c0ea5 | 2017-04-27 17:46:27 +0200 | [diff] [blame] | 54 | } |
| 55 | |
Jakub Josef | ad59b2f | 2017-04-28 16:08:40 +0200 | [diff] [blame] | 56 | /** |
| 57 | * Run kitchen command |
| 58 | * @param cmd kitchen command |
Jakub Josef | 2354733 | 2017-05-22 13:40:40 +0200 | [diff] [blame] | 59 | * @param environment kitchen environment properties (will be used before kitchen command), example: PLATFORM=ubuntu-16-04 |
| 60 | * @return return kitchen output |
Jakub Josef | ad59b2f | 2017-04-28 16:08:40 +0200 | [diff] [blame] | 61 | */ |
Jakub Josef | 2354733 | 2017-05-22 13:40:40 +0200 | [diff] [blame] | 62 | def runKitchenCommand(cmd, environment = null){ |
| 63 | if(environment && environment != ""){ |
| 64 | return sh(script: "${environment} rbenv exec bundler exec kitchen ${cmd}", returnStdout: true) |
Jakub Josef | 785d9f9 | 2017-05-18 17:58:59 +0200 | [diff] [blame] | 65 | }else{ |
Jakub Josef | 897c8fc | 2017-05-22 14:04:17 +0200 | [diff] [blame] | 66 | return sh(script: "rbenv exec bundler exec kitchen ${cmd}", returnStdout: true) |
Jakub Josef | 785d9f9 | 2017-05-18 17:58:59 +0200 | [diff] [blame] | 67 | } |
Jakub Josef | d324f46 | 2017-07-17 13:54:44 +0200 | [diff] [blame] | 68 | } |