blob: b1cd59d32b16de110d1708c9662c359793c1b466 [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"){
Jakub Josefad59b2f2017-04-28 16:08:40 +020012 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
14 sh "git clone https://github.com/rbenv/ruby-build.git ~/.rbenv/plugins/ruby-build"
Jakub Josefad59b2f2017-04-28 16:08:40 +020015 sh "rbenv install ${rubyVersion}";
16 }
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=""){
Jakub Josef8ed284e2017-05-22 15:22:15 +020024 sh """rbenv exec gem install bundler --conservative;
25 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 Josef4cda59e2017-04-28 21:00:01 +020038 sh "rbenv exec bundler 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 Polreich232b1982017-08-22 12:44:48 +020048 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 Josef23547332017-05-22 13:40:40 +020053 }
Jakub Josef25c0ea52017-04-27 17:46:27 +020054}
55
Jakub Josefad59b2f2017-04-28 16:08:40 +020056/**
57 * Run kitchen command
58 * @param cmd kitchen command
Jakub Josef23547332017-05-22 13:40:40 +020059 * @param environment kitchen environment properties (will be used before kitchen command), example: PLATFORM=ubuntu-16-04
60 * @return return kitchen output
Jakub Josefad59b2f2017-04-28 16:08:40 +020061 */
Jakub Josef23547332017-05-22 13:40:40 +020062def runKitchenCommand(cmd, environment = null){
63 if(environment && environment != ""){
64 return sh(script: "${environment} rbenv exec bundler exec kitchen ${cmd}", returnStdout: true)
Jakub Josef785d9f92017-05-18 17:58:59 +020065 }else{
Jakub Josef897c8fc2017-05-22 14:04:17 +020066 return sh(script: "rbenv exec bundler exec kitchen ${cmd}", returnStdout: true)
Jakub Josef785d9f92017-05-18 17:58:59 +020067 }
Jakub Josefd324f462017-07-17 13:54:44 +020068}