Skip to content

Commit

Permalink
Ensure hosts are destroyed if post-suite fails
Browse files Browse the repository at this point in the history
When running `rake ci:test:aio[true]`, if the post-suite failed and we're not
preserving hosts, then the `beaker destroy` command would be skipped. Use a
nested ensure so the failure of post-suite doesn't prevent `beaker destroy` from
being called.

The nested ensure also guarantees that the `post-suite` will be called even if
the `pre-suite` fails, which can happen if the agent fails to install. This
matches the behavior for the "non-retry" version of the `ci:test:aio` rake task.

```
$ git diff
diff --git a/acceptance/teardown/common/099_Archive_Logs.rb b/acceptance/teardown/common/099_Archive_Logs.rb
index d940e2bea..2302633e1 100644
--- a/acceptance/teardown/common/099_Archive_Logs.rb
+++ b/acceptance/teardown/common/099_Archive_Logs.rb
@@ -1,5 +1,7 @@
 require 'date'

+raise "whoops"
+
 def file_glob(host, path)
   result = on(host, "ls #{path}", :acceptable_exit_codes => [0, 2])
   return [] if result.exit_code != 0

$ export BEAKER_PUPPET_VERSION=/home/josh/work/beaker-puppet
$ bundle update
...
$ bundle exec rake 'ci:test:aio[true]' SHA=8.4.0 TESTS=tests/load_libfacter.rb HOSTS=hosts.yaml
...
Begin teardown/common/099_Archive_Logs.rb
RuntimeError: whoops
...
beaker destroy
```
  • Loading branch information
joshcooper committed Jan 23, 2024
1 parent a8fb322 commit 1d6b1d9
Showing 1 changed file with 17 additions and 14 deletions.
31 changes: 17 additions & 14 deletions tasks/ci.rake
Original file line number Diff line number Diff line change
Expand Up @@ -334,23 +334,26 @@ def beaker_suite_retry(type)
beaker(:provision)

begin
beaker(:exec, 'pre-suite', '--preserve-state', '--pre-suite', pre_suites(type))
beaker(:exec, 'pre-suite', '--preserve-state')

begin
json_results_file = Tempfile.new
beaker(:exec, ENV.fetch('TESTS', nil), '--test-results-file', json_results_file.path)
rescue RuntimeError => e
puts "ERROR: #{e.message}"
tests_to_rerun = JSON.load(File.read(json_results_file.path))
raise e if tests_to_rerun.nil? || tests_to_rerun.empty?

puts '*** Retrying the following:'
puts tests_to_rerun.map { |spec| " #{spec}" }
beaker(:exec, tests_to_rerun.map { |str| "#{str}" }.join(','))
beaker(:exec, 'pre-suite', '--preserve-state', '--pre-suite', pre_suites(type))
beaker(:exec, 'pre-suite', '--preserve-state')

begin
json_results_file = Tempfile.new
beaker(:exec, ENV.fetch('TESTS', nil), '--test-results-file', json_results_file.path)
rescue RuntimeError => e
puts "ERROR: #{e.message}"
tests_to_rerun = JSON.load(File.read(json_results_file.path))
raise e if tests_to_rerun.nil? || tests_to_rerun.empty?

puts '*** Retrying the following:'
puts tests_to_rerun.map { |spec| " #{spec}" }
beaker(:exec, tests_to_rerun.map { |str| "#{str}" }.join(','))
end
ensure
beaker(:exec, 'post-suite')
end
ensure
beaker(:exec, 'post-suite')
preserve_hosts = ENV['OPTIONS'].include?('--preserve-hosts=always') if ENV['OPTIONS']
beaker(:destroy) unless preserve_hosts
end
Expand Down

0 comments on commit 1d6b1d9

Please sign in to comment.