Addressing Rubocop Errors
[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:
31                             '~!@#$%^*-/ aZ', lifetime: '5y')
32   on(master, puppet('access', 'login', '--username', user, '--lifetime', lifetime), stdin: password)
33 end
34
35 def run_task(task_name:, params: nil, password: DEFAULT_PASSWORD)
36   if pe_install?
37     run_puppet_task(task_name: task_name, params: params)
38   else
39     run_bolt_task(task_name: task_name, params: params, password: password)
40   end
41 end
42
43 def run_bolt_task(task_name:, params: nil, password: DEFAULT_PASSWORD)
44   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
45 end
46
47 def run_puppet_task(task_name:, params: nil)
48   on(master, puppet('task', 'run', task_name, '--nodes', fact_on(master, 'fqdn'), params.to_s), acceptable_exit_codes: [0, 1]).stdout
49 end
50
51 def expect_multiple_regexes(result:, regexes:)
52   regexes.each do |regex|
53     expect(result).to match(regex)
54   end
55 end
56
57 # This method allows a block to be passed in and if an exception is raised
58 # that matches the 'error_matcher' matcher, the block will wait a set number
59 # of seconds before retrying.
60 # Params:
61 # - max_retry_count - Max number of retries
62 # - retry_wait_interval_secs - Number of seconds to wait before retry
63 # - error_matcher - Matcher which the exception raised must match to allow retry
64 # Example Usage:
65 # retry_on_error_matching(3, 5, /OpenGPG Error/) do
66 #   apply_manifest(pp, :catch_failures => true)
67 # end
68 def retry_on_error_matching(max_retry_count = 3, retry_wait_interval_secs = 5, error_matcher = nil)
69   try = 0
70   begin
71     try += 1
72     yield
73   rescue StandardError => e
74     raise unless try < max_retry_count && (error_matcher.nil? || e.message =~ error_matcher)
75     sleep retry_wait_interval_secs
76     retry
77   end
78 end
79
80 RSpec.configure do |c|
81   File.expand_path(File.join(File.dirname(__FILE__), '..'))
82
83   # Readable test descriptions
84   c.formatter = :documentation
85
86   # Configure all nodes in nodeset
87   c.before :suite do
88     run_puppet_access_login(user: 'admin') if pe_install? && puppet_version =~ %r{(5\.\d\.\d)}
89   end
90 end