blob: cd6837191ffa4737bfb5f4bce30a14d13bc15fcc [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 Josefe6f3ae42017-05-23 15:05:14 +020021def installKitchen(kitchenInit=""){
Jakub Josef8ed284e2017-05-22 15:22:15 +020022 sh """rbenv exec gem install bundler --conservative;
23 rbenv exec gem install test-kitchen --conservative;"""
Jakub Josefe6f3ae42017-05-23 15:05:14 +020024 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 Josef4cda59e2017-04-28 21:00:01 +020036 sh "rbenv exec bundler install --path vendor/bundle"
Jakub Josef25c0ea52017-04-27 17:46:27 +020037}
38
Jakub Josefad59b2f2017-04-28 16:08:40 +020039/**
40 * Run kitchen tests in tests/integration
Jakub Josefb039fdd2017-05-22 16:28:06 +020041 * @param environment kitchen environment (optional can be empty)
Jakub Josef59725a92017-05-23 17:12:06 +020042 * @param parallelTesting run kitchen test suites in parallel (optional, default true)
Jakub Josefad59b2f2017-04-28 16:08:40 +020043 */
Jakub Josef59725a92017-05-23 17:12:06 +020044def runKitchenTests(environment="", parallelTesting = true){
Jakub Josef23547332017-05-22 13:40:40 +020045 def common = new com.mirantis.mk.Common()
Jakub Josef7d7b8782017-06-22 17:33:02 +020046 def kitchenTests=runKitchenCommand("list -b 2>/dev/null \"\$SUITE_PATTERN\"", environment)
Jakub Josef23547332017-05-22 13:40:40 +020047 if(kitchenTests && kitchenTests != ""){
48 def kitchenTestsList = kitchenTests.trim().tokenize("\n")
49 def kitchenTestRuns = [:]
Jakub Josef59725a92017-05-23 17:12:06 +020050 common.infoMsg(String.format("Found %s kitchen test suites", kitchenTestsList.size()))
Jakub Josef23547332017-05-22 13:40:40 +020051 for(int i=0;i<kitchenTestsList.size();i++){
Jakub Josef8ed284e2017-05-22 15:22:15 +020052 def testSuite = kitchenTestsList[i]
53 kitchenTestRuns["kitchen-${testSuite}-${i}"] = {
Jakub Josef59725a92017-05-23 17:12:06 +020054 common.infoMsg("Running kitchen test ${testSuite}")
Jakub Josefe98ce682017-06-01 15:44:25 +020055 println(runKitchenCommand("converge ${testSuite}", environment))
56 println runKitchenCommand("verify ${testSuite} -t tests/integration", environment)
57 println runKitchenCommand("destroy", environment)
Jakub Josef23547332017-05-22 13:40:40 +020058 }
59 }
Jakub Josef59725a92017-05-23 17:12:06 +020060 if(parallelTesting){
Jakub Josefb039fdd2017-05-22 16:28:06 +020061 parallel kitchenTestRuns
62 }else{
63 common.serial(kitchenTestRuns)
64 }
Jakub Josef23547332017-05-22 13:40:40 +020065 }else{
66 common.errorMsg("Cannot found kitchen test suites, kitchen list command returns bad output")
67 }
Jakub Josef25c0ea52017-04-27 17:46:27 +020068}
69
Jakub Josefad59b2f2017-04-28 16:08:40 +020070/**
71 * Run kitchen command
72 * @param cmd kitchen command
Jakub Josef23547332017-05-22 13:40:40 +020073 * @param environment kitchen environment properties (will be used before kitchen command), example: PLATFORM=ubuntu-16-04
74 * @return return kitchen output
Jakub Josefad59b2f2017-04-28 16:08:40 +020075 */
Jakub Josef23547332017-05-22 13:40:40 +020076def runKitchenCommand(cmd, environment = null){
77 if(environment && environment != ""){
78 return sh(script: "${environment} rbenv exec bundler exec kitchen ${cmd}", returnStdout: true)
Jakub Josef785d9f92017-05-18 17:58:59 +020079 }else{
Jakub Josef897c8fc2017-05-22 14:04:17 +020080 return sh(script: "rbenv exec bundler exec kitchen ${cmd}", returnStdout: true)
Jakub Josef785d9f92017-05-18 17:58:59 +020081 }
Jakub Josefb039fdd2017-05-22 16:28:06 +020082}