spec_helper_acceptance_local.rb: improve comment about lsb-release
[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 MAX_RETRY_COUNT       = 5
5 RETRY_WAIT            = 3
6 ERROR_MATCHER         = %r{(no valid OpenPGP data found|keyserver timed out|keyserver receive failed)}.freeze
7
8 # lsb-release is needed for facter 3 (puppet 6) to resolve os.distro facts. Not needed with facter
9 # 4 (puppet 7).
10 lsb_package = <<-MANIFEST
11 package { 'lsb-release':
12   ensure => installed,
13 }
14 MANIFEST
15
16 include PuppetLitmus
17 apply_manifest(lsb_package)
18
19 # This method allows a block to be passed in and if an exception is raised
20 # that matches the 'error_matcher' matcher, the block will wait a set number
21 # of seconds before retrying.
22 # Params:
23 # - max_retry_count - Max number of retries
24 # - retry_wait_interval_secs - Number of seconds to wait before retry
25 # - error_matcher - Matcher which the exception raised must match to allow retry
26 # Example Usage:
27 # retry_on_error_matching(3, 5, /OpenGPG Error/) do
28 #   apply_manifest(pp, :catch_failures => true)
29 # end
30
31 def retry_on_error_matching(max_retry_count = MAX_RETRY_COUNT, retry_wait_interval_secs = RETRY_WAIT, error_matcher = ERROR_MATCHER)
32   try = 0
33   begin
34     puts "retry_on_error_matching: try #{try}" unless try.zero?
35     try += 1
36     yield
37   rescue StandardError => e
38     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)
39     sleep retry_wait_interval_secs
40     retry
41   end
42 end