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