blob: e56e7e23d7848d535da12e1246e1868f83af0f4f [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)
Jakub Josef59725a92017-05-23 17:12:06 +020044 * @param parallelTesting run kitchen test suites in parallel (optional, default true)
Jakub Josefad59b2f2017-04-28 16:08:40 +020045 */
Jakub Josef59725a92017-05-23 17:12:06 +020046def runKitchenTests(environment="", parallelTesting = true){
Jakub Josef23547332017-05-22 13:40:40 +020047 def common = new com.mirantis.mk.Common()
Jakub Josef7d7b8782017-06-22 17:33:02 +020048 def kitchenTests=runKitchenCommand("list -b 2>/dev/null \"\$SUITE_PATTERN\"", environment)
Jakub Josef23547332017-05-22 13:40:40 +020049 if(kitchenTests && kitchenTests != ""){
50 def kitchenTestsList = kitchenTests.trim().tokenize("\n")
51 def kitchenTestRuns = [:]
Jakub Josef59725a92017-05-23 17:12:06 +020052 common.infoMsg(String.format("Found %s kitchen test suites", kitchenTestsList.size()))
Jakub Josef23547332017-05-22 13:40:40 +020053 for(int i=0;i<kitchenTestsList.size();i++){
Jakub Josef8ed284e2017-05-22 15:22:15 +020054 def testSuite = kitchenTestsList[i]
Martin Polreich2000d522017-07-19 15:50:39 +020055 kitchenTestRuns["kitchen-${testSuite}-${i}".replaceAll("trevorj-salty-whales:", "")] = {
Jakub Josef59725a92017-05-23 17:12:06 +020056 common.infoMsg("Running kitchen test ${testSuite}")
Jakub Josefe98ce682017-06-01 15:44:25 +020057 println(runKitchenCommand("converge ${testSuite}", environment))
58 println runKitchenCommand("verify ${testSuite} -t tests/integration", environment)
59 println runKitchenCommand("destroy", environment)
Jakub Josef23547332017-05-22 13:40:40 +020060 }
61 }
Jakub Josef59725a92017-05-23 17:12:06 +020062 if(parallelTesting){
Jakub Josefb039fdd2017-05-22 16:28:06 +020063 parallel kitchenTestRuns
64 }else{
65 common.serial(kitchenTestRuns)
66 }
Jakub Josef23547332017-05-22 13:40:40 +020067 }else{
68 common.errorMsg("Cannot found kitchen test suites, kitchen list command returns bad output")
69 }
Jakub Josef25c0ea52017-04-27 17:46:27 +020070}
71
Jakub Josefad59b2f2017-04-28 16:08:40 +020072/**
73 * Run kitchen command
74 * @param cmd kitchen command
Jakub Josef23547332017-05-22 13:40:40 +020075 * @param environment kitchen environment properties (will be used before kitchen command), example: PLATFORM=ubuntu-16-04
76 * @return return kitchen output
Jakub Josefad59b2f2017-04-28 16:08:40 +020077 */
Jakub Josef23547332017-05-22 13:40:40 +020078def runKitchenCommand(cmd, environment = null){
79 if(environment && environment != ""){
80 return sh(script: "${environment} rbenv exec bundler exec kitchen ${cmd}", returnStdout: true)
Jakub Josef785d9f92017-05-18 17:58:59 +020081 }else{
Jakub Josef897c8fc2017-05-22 14:04:17 +020082 return sh(script: "rbenv exec bundler exec kitchen ${cmd}", returnStdout: true)
Jakub Josef785d9f92017-05-18 17:58:59 +020083 }
Jakub Josefd324f462017-07-17 13:54:44 +020084}
85
86/**
87 * Filters given kitchen env lists for forbidden env properties
88 * @param input list of kitchenEnvs
89 * @param cleanRegex regex will be used for env cleaning (default removing SUITE=* properties)
90 * @return filtered env list
91 */
92def filterKitchenEnvs(inputEnvs = [], cleanRegex = "\\s?SUITE\\=[^\\s]*") {
93 def output = []
94 for(int i=0; i<inputEnvs.size(); i++) {
95 def cleanEnv = inputEnvs[i].replaceAll(cleanRegex, "")
96 if(cleanEnv != "") {
97 output.add(cleanEnv.trim())
98 }
99 }
100 return output
Jakub Josef47494512017-08-15 16:40:06 +0200101}