Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

make backup scenario more readable #857

Merged
merged 2 commits into from
May 29, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 9 additions & 20 deletions definitions/scenarios/backup.rb
Original file line number Diff line number Diff line change
Expand Up @@ -19,16 +19,17 @@ class Backup < ForemanMaintain::Scenario
def compose
check_valid_strategy
safety_confirmation
accessibility_confirmation
prepare_directory
add_step_with_context(Procedures::Backup::Metadata, :online_backup => online_backup?)
add_step_with_context(Procedures::Backup::AccessibilityConfirmation) if strategy == :offline
add_step_with_context(Procedures::Backup::PrepareDirectory)
add_step_with_context(Procedures::Backup::Metadata, :online_backup => strategy == :online)

case strategy
when :online
add_online_backup_steps
when :offline
add_offline_backup_steps
end

add_step_with_context(Procedures::Backup::CompressData)
end

Expand Down Expand Up @@ -64,18 +65,8 @@ def set_context_mapping

private

def prepare_directory
add_step_with_context(Procedures::Backup::PrepareDirectory)
end

def accessibility_confirmation
if strategy == :offline
add_step_with_context(Procedures::Backup::AccessibilityConfirmation)
end
end

def safety_confirmation
if online_backup? || include_db_dumps?
if strategy == :online || include_db_dumps?
add_step_with_context(Procedures::Backup::Online::SafetyConfirmation)
end
end
Expand Down Expand Up @@ -132,10 +123,6 @@ def strategy
def include_db_dumps?
!!context.get(:include_db_dumps)
end

def online_backup?
strategy == :online
end
end

class BackupRescueCleanup < ForemanMaintain::Scenario
Expand All @@ -149,8 +136,10 @@ class BackupRescueCleanup < ForemanMaintain::Scenario
end

def compose
add_step_with_context(Procedures::Service::Start) if strategy != :online
add_steps_with_context(find_procedures(:maintenance_mode_off)) if strategy != :online
if strategy == :offline
add_step_with_context(Procedures::Service::Start)
add_steps_with_context(find_procedures(:maintenance_mode_off))
end
add_step_with_context(Procedures::Backup::Clean)
end

Expand Down
88 changes: 88 additions & 0 deletions test/definitions/scenarios/backup_test.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
require 'test_helper'

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

before do
%w[candlepin_database foreman_database pulpcore_database].each do |feature|
assume_feature_present(feature.to_sym) do |db|
db.any_instance.stubs(:local?).returns(true)
end
end
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, Procedures::Backup::AccessibilityConfirmation)
assert_scenario_has_step(scenario, Procedures::Backup::PrepareDirectory)
assert_scenario_has_step(scenario, Procedures::Backup::Metadata)
assert_scenario_has_step(scenario, Procedures::Service::Stop) do |step|
assert_empty(step.common_options[:only])
end
assert_scenario_has_step(scenario, Procedures::Backup::ConfigFiles)
assert_scenario_has_step(scenario, Procedures::Backup::Pulp)
assert_scenario_has_step(scenario, Procedures::Backup::Offline::CandlepinDB)
assert_scenario_has_step(scenario, Procedures::Backup::Offline::ForemanDB)
assert_scenario_has_step(scenario, Procedures::Backup::Offline::PulpcoreDB)
assert_scenario_has_step(scenario, Procedures::Service::Start) do |step|
assert_empty(step.common_options[:only])
end
assert_scenario_has_step(scenario, Procedures::Backup::CompressData)
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, Procedures::Backup::Online::SafetyConfirmation)
refute_scenario_has_step(scenario, Procedures::Backup::AccessibilityConfirmation)
assert_scenario_has_step(scenario, Procedures::Backup::PrepareDirectory)
assert_scenario_has_step(scenario, Procedures::Backup::Metadata)
refute_scenario_has_step(scenario, Procedures::Service::Stop)
assert_scenario_has_step(scenario, Procedures::Backup::ConfigFiles)
assert_scenario_has_step(scenario, Procedures::Backup::Pulp)
assert_scenario_has_step(scenario, Procedures::Backup::Online::CandlepinDB)
assert_scenario_has_step(scenario, Procedures::Backup::Online::ForemanDB)
assert_scenario_has_step(scenario, Procedures::Backup::Online::PulpcoreDB)
refute_scenario_has_step(scenario, Procedures::Service::Start)
assert_scenario_has_step(scenario, Procedures::Backup::CompressData)
end
end
end

describe ForemanMaintain::Scenarios::BackupRescueCleanup do
include DefinitionsTestHelper

describe 'offline' do
let(:scenario) do
ForemanMaintain::Scenarios::BackupRescueCleanup.new(:backup_dir => '.',
:strategy => :offline)
end

it 'composes all steps' do
assert_scenario_has_step(scenario, Procedures::Service::Start)
assert_scenario_has_step(scenario, Procedures::Backup::Clean)
end
end

describe 'online' do
let(:scenario) do
ForemanMaintain::Scenarios::BackupRescueCleanup.new(:backup_dir => '.',
:strategy => :online)
end

it 'composes all steps' do
refute_scenario_has_step(scenario, Procedures::Service::Start)
assert_scenario_has_step(scenario, Procedures::Backup::Clean)
end
end
end
end
5 changes: 3 additions & 2 deletions test/definitions/test_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -104,8 +104,9 @@ def refute_scenario(filter, version)
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}")
step = scenario.steps.find { |s| s.is_a? scenario_step }
assert(step, "Expected scenario to have #{scenario_step}")
yield step if block_given?
end

def refute_scenario_has_step(scenario, scenario_step)
Expand Down
Loading