blob: 939bf2c8de6a0417c5e9bf8d7bce28b4199456b1 [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 Josef7ad08f32017-05-03 13:23:42 +020011def ensureRubyEnv(rubyVersion="2.2.3"){
Jakub Josefad59b2f2017-04-28 16:08:40 +020012 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 Josef25c0ea52017-04-27 17:46:27 +020016}
Jakub Josefad59b2f2017-04-28 16:08:40 +020017
18/**
19 * Install kitchen tools
20 */
Jakub Josef25c0ea52017-04-27 17:46:27 +020021def installKitchen(){
Jakub Josef8ed284e2017-05-22 15:22:15 +020022 sh """rbenv exec gem install bundler --conservative;
23 rbenv exec gem install test-kitchen --conservative;"""
Jakub Josef4cda59e2017-04-28 21:00:01 +020024 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 Josef698c6122017-05-19 20:57:33 +020031 gem 'kitchen-salt', :git => 'https://github.com/salt-formulas/kitchen-salt.git'"""
Jakub Josef4cda59e2017-04-28 21:00:01 +020032 sh "rbenv exec bundler install --path vendor/bundle"
Jakub Josef25c0ea52017-04-27 17:46:27 +020033}
34
Jakub Josefad59b2f2017-04-28 16:08:40 +020035/**
36 * Run kitchen tests in tests/integration
Jakub Josefb039fdd2017-05-22 16:28:06 +020037 * @param environment kitchen environment (optional can be empty)
38 * @param parallel run kitchen test suites in parallel (optional, default true)
Jakub Josefad59b2f2017-04-28 16:08:40 +020039 */
Jakub Josefb039fdd2017-05-22 16:28:06 +020040def runKitchenTests(environment="", parallel = true){
Jakub Josef23547332017-05-22 13:40:40 +020041 def common = new com.mirantis.mk.Common()
Jakub Joseffa1bfcb2017-05-22 14:25:23 +020042 def kitchenTests=runKitchenCommand("list -b", environment)
Jakub Josef23547332017-05-22 13:40:40 +020043 if(kitchenTests && kitchenTests != ""){
44 def kitchenTestsList = kitchenTests.trim().tokenize("\n")
45 def kitchenTestRuns = [:]
46 for(int i=0;i<kitchenTestsList.size();i++){
Jakub Josef8ed284e2017-05-22 15:22:15 +020047 def testSuite = kitchenTestsList[i]
48 kitchenTestRuns["kitchen-${testSuite}-${i}"] = {
49 runKitchenCommand("converge " + testSuite, environment)
Jakub Josef23547332017-05-22 13:40:40 +020050 }
51 }
Jakub Josefb039fdd2017-05-22 16:28:06 +020052 if(parallel){
53 parallel kitchenTestRuns
54 }else{
55 common.serial(kitchenTestRuns)
56 }
Jakub Josef23547332017-05-22 13:40:40 +020057 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 Josef25c0ea52017-04-27 17:46:27 +020062}
63
Jakub Josefad59b2f2017-04-28 16:08:40 +020064/**
65 * Run kitchen command
66 * @param cmd kitchen command
Jakub Josef23547332017-05-22 13:40:40 +020067 * @param environment kitchen environment properties (will be used before kitchen command), example: PLATFORM=ubuntu-16-04
68 * @return return kitchen output
Jakub Josefad59b2f2017-04-28 16:08:40 +020069 */
Jakub Josef23547332017-05-22 13:40:40 +020070def runKitchenCommand(cmd, environment = null){
71 if(environment && environment != ""){
72 return sh(script: "${environment} rbenv exec bundler exec kitchen ${cmd}", returnStdout: true)
Jakub Josef785d9f92017-05-18 17:58:59 +020073 }else{
Jakub Josef897c8fc2017-05-22 14:04:17 +020074 return sh(script: "rbenv exec bundler exec kitchen ${cmd}", returnStdout: true)
Jakub Josef785d9f92017-05-18 17:58:59 +020075 }
Jakub Josefb039fdd2017-05-22 16:28:06 +020076}