From 6320b6ce1a53b3902738351c99aed886b6cbf10c Mon Sep 17 00:00:00 2001 From: Evgeni Golov Date: Mon, 17 Jun 2024 11:14:18 +0200 Subject: [PATCH] check for running tasks before a backup is done optionally allow waiting for them to complete --- definitions/scenarios/backup.rb | 9 ++++ lib/foreman_maintain/cli/backup_command.rb | 2 + test/definitions/scenarios/backup_test.rb | 58 +++++++++++++++++++++- 3 files changed, 67 insertions(+), 2 deletions(-) diff --git a/definitions/scenarios/backup.rb b/definitions/scenarios/backup.rb index 9ec32173b..bd1377146 100644 --- a/definitions/scenarios/backup.rb +++ b/definitions/scenarios/backup.rb @@ -14,13 +14,17 @@ class Backup < ForemanMaintain::Scenario param :proxy_features, 'List of proxy features to backup (default: all)', :array => true param :skip_pulp_content, 'Skip Pulp content during backup' param :tar_volume_size, 'Size of tar volume (indicates splitting)' + param :wait_for_tasks, 'Wait for running tasks to complete instead of aborting' end + # rubocop:disable Metrics/AbcSize def compose check_valid_strategy add_step_with_context(Checks::Backup::IncrementalParentType, :online_backup => strategy == :online, :sql_tar => feature(:instance).postgresql_local?) + add_step(Checks::ForemanTasks::NotRunning.new(:wait_for_tasks => wait_for_tasks?)) + add_step(Checks::Pulpcore::NoRunningTasks.new(:wait_for_tasks => wait_for_tasks?)) safety_confirmation add_step_with_context(Procedures::Backup::AccessibilityConfirmation) if strategy == :offline add_step_with_context(Procedures::Backup::PrepareDirectory, @@ -36,6 +40,7 @@ def compose add_step_with_context(Procedures::Backup::CompressData) end + # rubocop:enable Metrics/AbcSize # rubocop:disable Metrics/MethodLength def set_context_mapping @@ -126,6 +131,10 @@ def strategy def include_db_dumps? !!context.get(:include_db_dumps) end + + def wait_for_tasks? + !!context.get(:wait_for_tasks) + end end class BackupRescueCleanup < ForemanMaintain::Scenario diff --git a/lib/foreman_maintain/cli/backup_command.rb b/lib/foreman_maintain/cli/backup_command.rb index 0b4c93180..fa4a7859e 100644 --- a/lib/foreman_maintain/cli/backup_command.rb +++ b/lib/foreman_maintain/cli/backup_command.rb @@ -57,6 +57,7 @@ def common_backup_options option '--features', 'FEATURES', "#{proxy_name} features to include in the backup. " \ 'Valid features are tftp, dns, dhcp, openscap, and all.', :multivalued => true + option '--wait-for-tasks', :flag, 'Wait for running tasks to complete instead of aborting' end # rubocop:enable Metrics/MethodLength @@ -89,6 +90,7 @@ def backup_scenario(options, strategy) :tar_volume_size => split_pulp_tar, :skip_pulp_content => skip_pulp_content?, :incremental_dir => incremental, + :wait_for_tasks => wait_for_tasks?, }.merge(options)) end diff --git a/test/definitions/scenarios/backup_test.rb b/test/definitions/scenarios/backup_test.rb index b916e4434..67052c93d 100644 --- a/test/definitions/scenarios/backup_test.rb +++ b/test/definitions/scenarios/backup_test.rb @@ -12,13 +12,30 @@ module Scenarios end end + + let(:task_checks) do + [Checks::ForemanTasks::NotRunning, + Checks::Pulpcore::NoRunningTasks] + end + + let(:checks) do + [Checks::Backup::IncrementalParentType] + task_checks + end + describe 'offline' do let(:scenario) do ForemanMaintain::Scenarios::Backup.new(:backup_dir => '.', :strategy => :offline) end it 'composes all steps' do - assert_scenario_has_step(scenario, Checks::Backup::IncrementalParentType) + task_checks.each do |check| + assert_scenario_has_step(scenario, check) do |step| + refute step.options['wait_for_tasks'] + end + end + checks.each do |check| + assert_scenario_has_step(scenario, check) + end assert_scenario_has_step(scenario, Procedures::Backup::AccessibilityConfirmation) assert_scenario_has_step(scenario, Procedures::Backup::PrepareDirectory) assert_scenario_has_step(scenario, Procedures::Backup::Metadata) @@ -37,13 +54,35 @@ module Scenarios end end + describe 'offline with wait_for_tasks' do + let(:scenario) do + ForemanMaintain::Scenarios::Backup.new(:backup_dir => '.', :strategy => :offline, + :wait_for_tasks => true) + end + + it 'composes all steps' do + task_checks.each do |check| + assert_scenario_has_step(scenario, check) do |step| + assert step.options['wait_for_tasks'] + end + end + end + end + describe 'online' do let(:scenario) do ForemanMaintain::Scenarios::Backup.new(:backup_dir => '.', :strategy => :online) end it 'composes all steps' do - assert_scenario_has_step(scenario, Checks::Backup::IncrementalParentType) + task_checks.each do |check| + assert_scenario_has_step(scenario, check) do |step| + refute step.options['wait_for_tasks'] + end + end + checks.each do |check| + assert_scenario_has_step(scenario, check) + end assert_scenario_has_step(scenario, Procedures::Backup::Online::SafetyConfirmation) refute_scenario_has_step(scenario, Procedures::Backup::AccessibilityConfirmation) assert_scenario_has_step(scenario, Procedures::Backup::PrepareDirectory) @@ -58,6 +97,21 @@ module Scenarios assert_scenario_has_step(scenario, Procedures::Backup::CompressData) end end + + describe 'online with wait_for_tasks' do + let(:scenario) do + ForemanMaintain::Scenarios::Backup.new(:backup_dir => '.', :strategy => :online, + :wait_for_tasks => true) + end + + it 'composes all steps' do + task_checks.each do |check| + assert_scenario_has_step(scenario, check) do |step| + assert step.options['wait_for_tasks'] + end + end + end + end end describe ForemanMaintain::Scenarios::BackupRescueCleanup do