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 | 7ad08f3 | 2017-05-03 13:23:42 +0200 | [diff] [blame] | 11 | def ensureRubyEnv(rubyVersion="2.2.3"){ |
Jakub Josef | ad59b2f | 2017-04-28 16:08:40 +0200 | [diff] [blame] | 12 | if(!fileExists("/var/lib/jenkins/.rbenv/versions/${rubyVersion}/bin/ruby")){ |
| 13 | sh "rbenv install ${rubyVersion}"; |
| 14 | } |
| 15 | sh "rbenv local ${rubyVersion};rbenv exec gem update --system" |
Jakub Josef | 25c0ea5 | 2017-04-27 17:46:27 +0200 | [diff] [blame] | 16 | } |
Jakub Josef | ad59b2f | 2017-04-28 16:08:40 +0200 | [diff] [blame] | 17 | |
| 18 | /** |
| 19 | * Install kitchen tools |
| 20 | */ |
Jakub Josef | 25c0ea5 | 2017-04-27 17:46:27 +0200 | [diff] [blame] | 21 | def installKitchen(){ |
Jakub Josef | 8ed284e | 2017-05-22 15:22:15 +0200 | [diff] [blame] | 22 | sh """rbenv exec gem install bundler --conservative; |
| 23 | rbenv exec gem install test-kitchen --conservative;""" |
Jakub Josef | 4cda59e | 2017-04-28 21:00:01 +0200 | [diff] [blame] | 24 | sh """ test -e Gemfile || cat <<EOF > Gemfile |
| 25 | source 'https://rubygems.org' |
| 26 | gem 'rake' |
| 27 | gem 'test-kitchen' |
| 28 | gem 'kitchen-docker' |
| 29 | gem 'kitchen-inspec' |
| 30 | gem 'inspec' |
Jakub Josef | 698c612 | 2017-05-19 20:57:33 +0200 | [diff] [blame] | 31 | gem 'kitchen-salt', :git => 'https://github.com/salt-formulas/kitchen-salt.git'""" |
Jakub Josef | 4cda59e | 2017-04-28 21:00:01 +0200 | [diff] [blame] | 32 | sh "rbenv exec bundler install --path vendor/bundle" |
Jakub Josef | 25c0ea5 | 2017-04-27 17:46:27 +0200 | [diff] [blame] | 33 | } |
| 34 | |
Jakub Josef | ad59b2f | 2017-04-28 16:08:40 +0200 | [diff] [blame] | 35 | /** |
| 36 | * Run kitchen tests in tests/integration |
Jakub Josef | b039fdd | 2017-05-22 16:28:06 +0200 | [diff] [blame^] | 37 | * @param environment kitchen environment (optional can be empty) |
| 38 | * @param parallel run kitchen test suites in parallel (optional, default true) |
Jakub Josef | ad59b2f | 2017-04-28 16:08:40 +0200 | [diff] [blame] | 39 | */ |
Jakub Josef | b039fdd | 2017-05-22 16:28:06 +0200 | [diff] [blame^] | 40 | def runKitchenTests(environment="", parallel = true){ |
Jakub Josef | 2354733 | 2017-05-22 13:40:40 +0200 | [diff] [blame] | 41 | def common = new com.mirantis.mk.Common() |
Jakub Josef | fa1bfcb | 2017-05-22 14:25:23 +0200 | [diff] [blame] | 42 | def kitchenTests=runKitchenCommand("list -b", environment) |
Jakub Josef | 2354733 | 2017-05-22 13:40:40 +0200 | [diff] [blame] | 43 | if(kitchenTests && kitchenTests != ""){ |
| 44 | def kitchenTestsList = kitchenTests.trim().tokenize("\n") |
| 45 | def kitchenTestRuns = [:] |
| 46 | for(int i=0;i<kitchenTestsList.size();i++){ |
Jakub Josef | 8ed284e | 2017-05-22 15:22:15 +0200 | [diff] [blame] | 47 | def testSuite = kitchenTestsList[i] |
| 48 | kitchenTestRuns["kitchen-${testSuite}-${i}"] = { |
| 49 | runKitchenCommand("converge " + testSuite, environment) |
Jakub Josef | 2354733 | 2017-05-22 13:40:40 +0200 | [diff] [blame] | 50 | } |
| 51 | } |
Jakub Josef | b039fdd | 2017-05-22 16:28:06 +0200 | [diff] [blame^] | 52 | if(parallel){ |
| 53 | parallel kitchenTestRuns |
| 54 | }else{ |
| 55 | common.serial(kitchenTestRuns) |
| 56 | } |
Jakub Josef | 2354733 | 2017-05-22 13:40:40 +0200 | [diff] [blame] | 57 | runKitchenCommand("destroy", environment) |
| 58 | runKitchenCommand("verify -t tests/integration", environment) |
| 59 | }else{ |
| 60 | common.errorMsg("Cannot found kitchen test suites, kitchen list command returns bad output") |
| 61 | } |
Jakub Josef | 25c0ea5 | 2017-04-27 17:46:27 +0200 | [diff] [blame] | 62 | } |
| 63 | |
Jakub Josef | ad59b2f | 2017-04-28 16:08:40 +0200 | [diff] [blame] | 64 | /** |
| 65 | * Run kitchen command |
| 66 | * @param cmd kitchen command |
Jakub Josef | 2354733 | 2017-05-22 13:40:40 +0200 | [diff] [blame] | 67 | * @param environment kitchen environment properties (will be used before kitchen command), example: PLATFORM=ubuntu-16-04 |
| 68 | * @return return kitchen output |
Jakub Josef | ad59b2f | 2017-04-28 16:08:40 +0200 | [diff] [blame] | 69 | */ |
Jakub Josef | 2354733 | 2017-05-22 13:40:40 +0200 | [diff] [blame] | 70 | def runKitchenCommand(cmd, environment = null){ |
| 71 | if(environment && environment != ""){ |
| 72 | return sh(script: "${environment} rbenv exec bundler exec kitchen ${cmd}", returnStdout: true) |
Jakub Josef | 785d9f9 | 2017-05-18 17:58:59 +0200 | [diff] [blame] | 73 | }else{ |
Jakub Josef | 897c8fc | 2017-05-22 14:04:17 +0200 | [diff] [blame] | 74 | return sh(script: "rbenv exec bundler exec kitchen ${cmd}", returnStdout: true) |
Jakub Josef | 785d9f9 | 2017-05-18 17:58:59 +0200 | [diff] [blame] | 75 | } |
Jakub Josef | b039fdd | 2017-05-22 16:28:06 +0200 | [diff] [blame^] | 76 | } |