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