(CONT-1001) Add litmus ~> 1.0
[puppet-modules/puppetlabs-apt.git] / spec / spec_helper_acceptance_local.rb
1 # frozen_string_literal: true
2
3 UNSUPPORTED_PLATFORMS = ['RedHat', 'Suse', 'windows', 'AIX', 'Solaris'].freeze
4 RETRY_WAIT            = 3
5 ERROR_MATCHER         = %r{(no valid OpenPGP data found|keyserver timed out|keyserver receive failed)}.freeze
6 MAX_RETRY_COUNT       = 10
7
8 RSpec.configure do |c|
9   c.before :suite do
10     # lsb-release is needed for facter 3 (puppet 6) to resolve os.distro facts. Not needed with facter
11     # 4 (puppet 7).
12     lsb_package = <<~MANIFEST
13       package { 'lsb-release':
14         ensure => installed,
15       }
16     MANIFEST
17     include PuppetLitmus
18     extend PuppetLitmus
19     apply_manifest(lsb_package)
20   end
21 end
22
23 # This method allows a block to be passed in and if an exception is raised
24 # that matches the 'error_matcher' matcher, the block will wait a set number
25 # of seconds before retrying.
26 # Params:
27 # - max_retry_count - Max number of retries
28 # - retry_wait_interval_secs - Number of seconds to wait before retry
29 # - error_matcher - Matcher which the exception raised must match to allow retry
30 # Example Usage:
31 # retry_on_error_matching(3, 5, /OpenGPG Error/) do
32 #   apply_manifest(pp, :catch_failures => true)
33 # end
34
35 def retry_on_error_matching(max_retry_count = MAX_RETRY_COUNT, retry_wait_interval_secs = RETRY_WAIT, error_matcher = ERROR_MATCHER)
36   try = 0
37   begin
38     puts "retry_on_error_matching: try #{try}" unless try.zero?
39     try += 1
40     yield
41   rescue StandardError => e
42     raise('Attempted this %{value0} times. Raising %{value1}' % { value0: max_retry_count, value1: e }) unless try < max_retry_count && (error_matcher.nil? || e.message =~ error_matcher)
43
44     sleep retry_wait_interval_secs
45     retry
46   end
47 end