From a75cf55780e50c69c794422be98f5891f129a974 Mon Sep 17 00:00:00 2001 From: Evgeni Golov Date: Wed, 29 May 2024 10:45:18 +0200 Subject: [PATCH 1/2] make backup scenario more readable --- definitions/scenarios/backup.rb | 29 +++++++++-------------------- 1 file changed, 9 insertions(+), 20 deletions(-) diff --git a/definitions/scenarios/backup.rb b/definitions/scenarios/backup.rb index a1c611957..effa24430 100644 --- a/definitions/scenarios/backup.rb +++ b/definitions/scenarios/backup.rb @@ -19,9 +19,9 @@ 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 @@ -29,6 +29,7 @@ def compose when :offline add_offline_backup_steps end + add_step_with_context(Procedures::Backup::CompressData) end @@ -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 @@ -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 @@ -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 From 58a9623e654a930ed154845a9cbc4972eefa1d9b Mon Sep 17 00:00:00 2001 From: Evgeni Golov Date: Wed, 29 May 2024 11:08:54 +0200 Subject: [PATCH 2/2] add simple backup scenario tests --- test/definitions/scenarios/backup_test.rb | 88 +++++++++++++++++++++++ test/definitions/test_helper.rb | 5 +- 2 files changed, 91 insertions(+), 2 deletions(-) create mode 100644 test/definitions/scenarios/backup_test.rb diff --git a/test/definitions/scenarios/backup_test.rb b/test/definitions/scenarios/backup_test.rb new file mode 100644 index 000000000..5952d53eb --- /dev/null +++ b/test/definitions/scenarios/backup_test.rb @@ -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 diff --git a/test/definitions/test_helper.rb b/test/definitions/test_helper.rb index 2fc987ddf..000d09ff5 100644 --- a/test/definitions/test_helper.rb +++ b/test/definitions/test_helper.rb @@ -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)