From cde38d6095ddcaa504e20e442458747959b3a854 Mon Sep 17 00:00:00 2001 From: david22swan Date: Thu, 1 Feb 2024 14:15:21 +0000 Subject: [PATCH] (CAT-1713) Add retry to flaky tests --- spec/acceptance/remove_config_spec.rb | 8 ++++++-- spec/spec_helper_acceptance.rb | 24 ++++++++++++++++++++++++ 2 files changed, 30 insertions(+), 2 deletions(-) diff --git a/spec/acceptance/remove_config_spec.rb b/spec/acceptance/remove_config_spec.rb index 46da7276f..b98a7a5af 100644 --- a/spec/acceptance/remove_config_spec.rb +++ b/spec/acceptance/remove_config_spec.rb @@ -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) @@ -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) diff --git a/spec/spec_helper_acceptance.rb b/spec/spec_helper_acceptance.rb index 00dfd70e5..941bfe555 100644 --- a/spec/spec_helper_acceptance.rb +++ b/spec/spec_helper_acceptance.rb @@ -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