Skip to content

Commit

Permalink
Merge pull request #1313 from puppetlabs/CAT-1713/main/retries
Browse files Browse the repository at this point in the history
(CAT-1713) Add retry to flaky tests
  • Loading branch information
jordanbreen28 authored Feb 2, 2024
2 parents 040963a + cde38d6 commit 4eb9812
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 2 deletions.
8 changes: 6 additions & 2 deletions spec/acceptance/remove_config_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,9 @@
it 'saves the setting' do
# Force the command to run if not already
subject.exit_status
expect(File).to exist(ENV.fetch('PDK_ANSWER_FILE', nil))
retry_on_error_matching(60, 5) do
expect(File).to exist(ENV.fetch('PDK_ANSWER_FILE', nil))
end

actual_content = File.open(ENV.fetch('PDK_ANSWER_FILE', nil), 'rb:utf-8', &:read)
expect(actual_content).to eq(new_content)
Expand All @@ -19,7 +21,9 @@
it 'saves the setting' do
# Force the command to run if not already
subject.exit_status
expect(File).to exist(ENV.fetch('PDK_ANSWER_FILE', nil))
retry_on_error_matching(60, 5) do
expect(File).to exist(ENV.fetch('PDK_ANSWER_FILE', nil))
end

actual_content_raw = File.open(ENV.fetch('PDK_ANSWER_FILE', nil), 'rb:utf-8', &:read)
actual_json_content = JSON.parse(actual_content_raw)
Expand Down
24 changes: 24 additions & 0 deletions spec/spec_helper_acceptance.rb
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,30 @@ def default_installed_bin_dir
end
end

# This method allows a block to be passed in and if an exception is raised
# that matches the 'error_matcher' matcher, the block will wait a set number
# of seconds before retrying.
# Params:
# - max_retry_count - Max number of retries
# - retry_wait_interval_secs - Number of seconds to wait before retry
# - error_matcher - Matcher which the exception raised must match to allow retry
# Example Usage:
# retry_on_error_matching(3, 5, /OpenGPG Error/) do
# apply_manifest(pp, :catch_failures => true)
# end
def retry_on_error_matching(max_retry_count = 3, retry_wait_interval_secs = 5, error_matcher = nil)
try = 0
begin
try += 1
yield
rescue StandardError => e
raise unless try < max_retry_count && (error_matcher.nil? || e.message =~ error_matcher)

sleep retry_wait_interval_secs
retry
end
end

module Specinfra
module Backend
class Cmd
Expand Down

0 comments on commit 4eb9812

Please sign in to comment.