Add a check for Puppet version to task helper
[puppet-modules/puppetlabs-apt.git] / spec / spec_helper_acceptance.rb
1 require 'beaker-rspec'
2 require 'beaker/puppet_install_helper'
3 require 'beaker/module_install_helper'
4
5 def install_bolt_on(hosts)
6   on(hosts, "/opt/puppetlabs/puppet/bin/gem install --source http://rubygems.delivery.puppetlabs.net bolt -v '> 0.0.1'", acceptable_exit_codes: [0, 1]).stdout
7 end
8
9 def pe_install?
10   ENV['PUPPET_INSTALL_TYPE'] =~ %r{pe}i
11 end
12
13 run_puppet_install_helper
14 install_bolt_on(hosts) unless pe_install?
15 install_module_on(hosts)
16 install_module_dependencies_on(hosts)
17
18 UNSUPPORTED_PLATFORMS = %w[RedHat Suse windows AIX Solaris].freeze
19
20 DEFAULT_PASSWORD = if default[:hypervisor] == 'vagrant'
21                      'vagrant'
22                    elsif default[:hypervisor] == 'vcloud'
23                      'Qu@lity!'
24                    end
25
26 def puppet_version
27   (on default, puppet('--version')).output.chomp
28 end
29
30 def run_puppet_access_login(user:, password: '~!@#$%^*-/ aZ', lifetime: '5y')
31   on(master, puppet('access', 'login', '--username', user, '--lifetime', lifetime), stdin: password)
32 end
33
34 def run_task(task_name:, params: nil, password: DEFAULT_PASSWORD)
35   if pe_install?
36     run_puppet_task(task_name: task_name, params: params)
37   else
38     run_bolt_task(task_name: task_name, params: params, password: password)
39   end
40 end
41
42 def run_bolt_task(task_name:, params: nil, password: DEFAULT_PASSWORD)
43   on(master, "/opt/puppetlabs/puppet/bin/bolt task run #{task_name} --modules /etc/puppetlabs/code/modules/service --nodes localhost --password #{password} #{params}", acceptable_exit_codes: [0, 1]).stdout # rubocop:disable Metrics/LineLength
44 end
45
46 def run_puppet_task(task_name:, params: nil)
47   on(master, puppet('task', 'run', task_name, '--nodes', fact_on(master, 'fqdn'), params.to_s), acceptable_exit_codes: [0, 1]).stdout
48 end
49
50 def expect_multiple_regexes(result:, regexes:)
51   regexes.each do |regex|
52     expect(result).to match(regex)
53   end
54 end
55
56 # This method allows a block to be passed in and if an exception is raised
57 # that matches the 'error_matcher' matcher, the block will wait a set number
58 # of seconds before retrying.
59 # Params:
60 # - max_retry_count - Max number of retries
61 # - retry_wait_interval_secs - Number of seconds to wait before retry
62 # - error_matcher - Matcher which the exception raised must match to allow retry
63 # Example Usage:
64 # retry_on_error_matching(3, 5, /OpenGPG Error/) do
65 #   apply_manifest(pp, :catch_failures => true)
66 # end
67 def retry_on_error_matching(max_retry_count = 3, retry_wait_interval_secs = 5, error_matcher = nil)
68   try = 0
69   begin
70     try += 1
71     yield
72   rescue Exception => e
73     if try < max_retry_count && (error_matcher.nil? || e.message =~ error_matcher)
74       sleep retry_wait_interval_secs
75       retry
76     else
77       raise
78     end
79   end
80 end
81
82 RSpec.configure do |c|
83   # Project root
84   proj_root = File.expand_path(File.join(File.dirname(__FILE__), '..'))
85
86   # Readable test descriptions
87   c.formatter = :documentation
88
89   # Configure all nodes in nodeset
90   c.before :suite do
91     run_puppet_access_login(user: 'admin') if pe_install? && puppet_version =~ %r{(5\.\d\.\d)}
92   end
93 end