Skip to content

Commit cf3f969

Browse files
authored
Merge pull request #1983 from bwitt/retry-on-array
Allow `retry_on` to work for host array
2 parents c7cda1c + 4591211 commit cf3f969

3 files changed

Lines changed: 46 additions & 22 deletions

File tree

acceptance/tests/base/dsl/helpers/host_helpers/retry_on_test.rb

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -24,10 +24,7 @@
2424
assert_equal "", result.stdout
2525
end
2626

27-
step "#retry_on CURRENTLY fails when provided a host array" do
28-
# NOTE: would expect this to work across hosts, or be better documented and
29-
# to raise Beaker::Host::CommandFailure
30-
27+
step "#retry_on works when provided a host array" do
3128
remote_tmpdir = default.tmpdir
3229
remote_script_file = File.join(remote_tmpdir, "test.sh")
3330

@@ -36,8 +33,13 @@
3633
create_remote_file_from_fixture("retry_script", host, remote_tmpdir, "test.sh")
3734
end
3835

39-
assert_raises NoMethodError do
40-
retry_on hosts, "bash #{remote_script_file} #{remote_tmpdir} 2", { :max_retries => 4, :retry_interval => 0.1 }
36+
results = retry_on hosts, "bash #{remote_script_file} #{remote_tmpdir} 2", { :max_retries => 4, :retry_interval => 0.1 }
37+
38+
assert_kind_of Array, results
39+
assert_equal hosts.length, results.length
40+
results.each do |result|
41+
assert_equal 0, result.exit_code
42+
assert_equal "", result.stdout
4143
end
4244
end
4345
end

lib/beaker/dsl/helpers/host_helpers.rb

Lines changed: 21 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -557,25 +557,30 @@ def retry_on(host, command, opts = {}, &)
557557
retry_interval = option_retry_interval == 0 ? 1 : option_retry_interval
558558
verbose = true.to_s == opts[:verbose]
559559

560-
log_prefix = host.log_prefix
561-
logger.debug "\n#{log_prefix} #{Time.new.strftime('%H:%M:%S')}$ #{command}"
562-
logger.debug " Trying command #{max_retries} times."
563-
logger.debug ".", add_newline: false
564-
565-
result = on(host, command, { :accept_all_exit_codes => true, :silent => !verbose }, &)
566-
num_retries = 0
567-
until desired_exit_codes.include?(result.exit_code)
568-
sleep retry_interval
569-
result = on(host, command, { :accept_all_exit_codes => true, :silent => !verbose }, &)
570-
num_retries += 1
560+
block_opts = {}
561+
block_opts[:run_in_parallel] = opts[:run_in_parallel] if opts.key?(:run_in_parallel)
562+
563+
block_on host, block_opts do |single_host|
564+
log_prefix = single_host.log_prefix
565+
logger.debug "\n#{log_prefix} #{Time.new.strftime('%H:%M:%S')}$ #{command}"
566+
logger.debug " Trying command #{max_retries} times."
571567
logger.debug ".", add_newline: false
572-
if (num_retries > max_retries)
573-
logger.debug " Command \`#{command}\` failed."
574-
fail("Command \`#{command}\` failed.")
568+
569+
result = on(single_host, command, { :accept_all_exit_codes => true, :silent => !verbose }, &)
570+
num_retries = 0
571+
until desired_exit_codes.include?(result.exit_code)
572+
sleep retry_interval
573+
result = on(single_host, command, { :accept_all_exit_codes => true, :silent => !verbose }, &)
574+
num_retries += 1
575+
logger.debug ".", add_newline: false
576+
if (num_retries > max_retries)
577+
logger.debug " Command \`#{command}\` failed."
578+
fail("Command \`#{command}\` failed.")
579+
end
575580
end
581+
logger.debug "\n#{log_prefix} #{Time.new.strftime('%H:%M:%S')}$ #{command} ostensibly successful."
582+
result
576583
end
577-
logger.debug "\n#{log_prefix} #{Time.new.strftime('%H:%M:%S')}$ #{command} ostensibly successful."
578-
result
579584
end
580585

581586
# FIX: this should be moved into host/platform

spec/beaker/dsl/helpers/host_helpers_spec.rb

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -241,6 +241,23 @@ def logger
241241
result_given = subject.retry_on(host, command, opts)
242242
expect(result_given.exit_code).to be === 0
243243
end
244+
245+
it 'operates correctly when host is an array' do
246+
result.stdout = 'stdout'
247+
result.stderr = 'stderr'
248+
result.exit_code = 0
249+
250+
opts = {
251+
:max_retries => 5,
252+
:retry_interval => 0.0001,
253+
}
254+
255+
allow(subject).to receive(:on).and_return(result)
256+
expect(subject).to receive(:on).exactly(hosts.length).times
257+
258+
results = subject.retry_on(hosts, command, opts)
259+
expect(results).to eq(Array.new(hosts.length, result))
260+
end
244261
end
245262

246263
describe "shell" do

0 commit comments

Comments
 (0)