Skip to content

Commit

Permalink
Fixes #33272 - implement dry-run for restore
Browse files Browse the repository at this point in the history
  • Loading branch information
evgeni authored and upadhyeammit committed Nov 23, 2021
1 parent 2b14b54 commit 49163fd
Show file tree
Hide file tree
Showing 4 changed files with 71 additions and 3 deletions.
13 changes: 11 additions & 2 deletions definitions/scenarios/restore.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,19 +6,27 @@ class Restore < ForemanMaintain::Scenario
description 'Restore backup'
param :backup_dir, 'Path to backup directory'
param :incremental_backup, 'Is the backup incremental?'
param :dry_run, 'Check if backup could be restored, without performing the restore'
manual_detection
end

# rubocop:disable Metrics/MethodLength,Metrics/AbcSize
# rubocop:disable Metrics/CyclomaticComplexity,Metrics/PerceivedComplexity
def compose
backup = ForemanMaintain::Utils::Backup.new(context.get(:backup_dir))

add_steps(find_checks(:root_user))
supported_version_check
add_steps_with_context(Checks::Restore::ValidateBackup,
Checks::Restore::ValidateHostname,
Checks::Restore::ValidateInterfaces,
Procedures::Restore::Confirmation,
Checks::Restore::ValidateInterfaces)

if context.get(:dry_run)
self.class.metadata[:run_strategy] = :fail_slow
return
end

add_steps_with_context(Procedures::Restore::Confirmation,
Procedures::Selinux::SetFileSecurity,
Procedures::Restore::Configs)
add_step_with_context(Procedures::Crond::Stop) if feature(:cron)
Expand Down Expand Up @@ -48,6 +56,7 @@ def compose
add_step_with_context(Procedures::Crond::Start) if feature(:cron)
end
# rubocop:enable Metrics/MethodLength,Metrics/AbcSize
# rubocop:enable Metrics/CyclomaticComplexity,Metrics/PerceivedComplexity

def drop_dbs(backup)
if backup.file_map[:candlepin_dump][:present] ||
Expand Down
6 changes: 5 additions & 1 deletion lib/foreman_maintain/cli/restore_command.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,15 @@ class RestoreCommand < Base

option ['-i', '--incremental'], :flag, 'Restore an incremental backup',
:attribute_name => :incremental, :completion => { :type => :directory }
option ['-n', '--dry-run'], :flag,
'Check if backup could be restored, without performing the restore',
:attribute_name => :dry_run

def execute
scenario = Scenarios::Restore.new(
:backup_dir => @backup_dir,
:incremental_backup => @incremental || incremental_backup?
:incremental_backup => @incremental || incremental_backup?,
:dry_run => @dry_run
)
rescue_scenario = Scenarios::RestoreRescue.new
run_scenario(scenario, rescue_scenario)
Expand Down
45 changes: 45 additions & 0 deletions test/definitions/scenarios/restore_test.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
require 'test_helper'

module Scenarios
describe ForemanMaintain::Scenarios::Restore do
include DefinitionsTestHelper

let(:checks) do
[Checks::Restore::ValidateBackup,
Checks::Restore::ValidateHostname,
Checks::Restore::ValidateInterfaces]
end

describe 'with default params' do
let(:scenario) do
ForemanMaintain::Scenarios::Restore.new(:backup_dir => '.',
:incremental_backup => false,
:dry_run => false)
end

it 'composes all steps' do
checks.each do |check|
assert_scenario_has_step(scenario, check)
end
assert_scenario_has_step(scenario, Procedures::Restore::Confirmation)
assert_scenario_has_step(scenario, Procedures::Restore::Configs)
end
end

describe 'with dry_run=true' do
let(:scenario) do
ForemanMaintain::Scenarios::Restore.new(:backup_dir => '.',
:incremental_backup => false,
:dry_run => true)
end

it 'composes only check steps' do
checks.each do |check|
assert_scenario_has_step(scenario, check)
end
refute_scenario_has_step(scenario, Procedures::Restore::Confirmation)
refute_scenario_has_step(scenario, Procedures::Restore::Configs)
end
end
end
end
10 changes: 10 additions & 0 deletions test/definitions/test_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,16 @@ def refute_scenario(filter, version)
refute scenario, "Expected the scenario #{filter} to be absent"
end

def assert_scenario_has_step(scenario, scenario_step)
assert(scenario.steps.find { |step| step.is_a? scenario_step },
"Expected scenario to have #{scenario_step}")
end

def refute_scenario_has_step(scenario, scenario_step)
refute(scenario.steps.find { |step| step.is_a? scenario_step },
"Expected scenario not to have #{scenario_step}")
end

def hammer_config_dirs(dirs)
Features::Hammer.any_instance.stubs(:config_directories).returns(dirs)
end
Expand Down

0 comments on commit 49163fd

Please sign in to comment.