From 0ab628a80044f2364763dee963bccd71726c3e8c Mon Sep 17 00:00:00 2001 From: Evgeni Golov Date: Tue, 11 Jul 2023 10:00:31 +0200 Subject: [PATCH 1/2] Fixes #36578 - ensure puppet/qpidd are present before restore (cherry picked from commit 80cbb5316b07af3f55d27155015350561d623560) --- .../procedures/restore/required_packages.rb | 23 ++++++++ definitions/scenarios/restore.rb | 2 + lib/foreman_maintain/utils/backup.rb | 12 ++++ .../restore/required_packages_test.rb | 58 +++++++++++++++++++ test/lib/utils/backup_test.rb | 24 ++++++++ 5 files changed, 119 insertions(+) create mode 100644 definitions/procedures/restore/required_packages.rb create mode 100644 test/definitions/procedures/restore/required_packages_test.rb diff --git a/definitions/procedures/restore/required_packages.rb b/definitions/procedures/restore/required_packages.rb new file mode 100644 index 000000000..c60eecce8 --- /dev/null +++ b/definitions/procedures/restore/required_packages.rb @@ -0,0 +1,23 @@ +module Procedures::Restore + class RequiredPackages < ForemanMaintain::Procedure + metadata do + description 'Ensure required packages are installed before restore' + + param :backup_dir, + 'Path to backup directory', + :required => true + end + + def run + backup = ForemanMaintain::Utils::Backup.new(@backup_dir) + required_packages = [] + required_packages << 'puppetserver' if backup.with_puppetserver? + required_packages << 'qpid-cpp-server' if backup.with_qpidd? + if required_packages.any? + with_spinner('Installing required packages') do + ForemanMaintain.package_manager.install(required_packages, assumeyes: true) + end + end + end + end +end diff --git a/definitions/scenarios/restore.rb b/definitions/scenarios/restore.rb index 6751b2f59..37211e945 100644 --- a/definitions/scenarios/restore.rb +++ b/definitions/scenarios/restore.rb @@ -28,6 +28,7 @@ def compose add_steps_with_context(Procedures::Restore::Confirmation, Procedures::Selinux::SetFileSecurity, + Procedures::Restore::RequiredPackages, Procedures::Restore::Configs) add_step_with_context(Procedures::Crond::Stop) if feature(:cron) unless backup.incremental? @@ -103,6 +104,7 @@ def set_context_mapping Checks::Restore::ValidateBackup => :backup_dir, Checks::Restore::ValidateHostname => :backup_dir, Checks::Restore::ValidateInterfaces => :backup_dir, + Procedures::Restore::RequiredPackages => :backup_dir, Procedures::Restore::Configs => :backup_dir, Procedures::Restore::DropDatabases => :backup_dir, Procedures::Restore::CandlepinDump => :backup_dir, diff --git a/lib/foreman_maintain/utils/backup.rb b/lib/foreman_maintain/utils/backup.rb index 815b4357f..1d6fb6ce1 100644 --- a/lib/foreman_maintain/utils/backup.rb +++ b/lib/foreman_maintain/utils/backup.rb @@ -337,6 +337,18 @@ def incremental? def online_backup? !!metadata.fetch('online', false) end + + def installed_rpms + metadata.fetch('rpms', metadata.fetch(:rpms, [])) + end + + def with_puppetserver? + installed_rpms.any? { |rpm| rpm.start_with?('puppetserver-') } + end + + def with_qpidd? + installed_rpms.any? { |rpm| rpm.start_with?('qpid-cpp-server-') } + end end end end diff --git a/test/definitions/procedures/restore/required_packages_test.rb b/test/definitions/procedures/restore/required_packages_test.rb new file mode 100644 index 000000000..bddb29930 --- /dev/null +++ b/test/definitions/procedures/restore/required_packages_test.rb @@ -0,0 +1,58 @@ +require 'test_helper' + +describe Procedures::Restore::RequiredPackages do + include DefinitionsTestHelper + + subject do + Procedures::Restore::RequiredPackages.new(:backup_dir => '.') + end + + it 'installs puppetserver if it was in the backup' do + ForemanMaintain::Utils::Backup.any_instance.stubs(:with_puppetserver?).returns(true) + ForemanMaintain.package_manager.expects(:install). + with(['puppetserver'], assumeyes: true).once + result = run_procedure(subject) + assert result.success?, 'the procedure was expected to succeed' + end + + it 'doesnt install puppetserver if it wasnt in the backup' do + ForemanMaintain::Utils::Backup.any_instance.stubs(:with_puppetserver?).returns(false) + ForemanMaintain.package_manager.expects(:install). + with(['puppetserver'], assumeyes: true).never + result = run_procedure(subject) + assert result.success?, 'the procedure was expected to succeed' + end + + it 'installs qpidd if it was in the backup' do + ForemanMaintain::Utils::Backup.any_instance.stubs(:with_qpidd?).returns(true) + ForemanMaintain.package_manager.expects(:install). + with(['qpid-cpp-server'], assumeyes: true).once + result = run_procedure(subject) + assert result.success?, 'the procedure was expected to succeed' + end + + it 'doesnt install qpidd if it wasnt in the backup' do + ForemanMaintain::Utils::Backup.any_instance.stubs(:with_qpidd?).returns(false) + ForemanMaintain.package_manager.expects(:install). + with(['qpid-cpp-server'], assumeyes: true).never + result = run_procedure(subject) + assert result.success?, 'the procedure was expected to succeed' + end + + it 'installs puppetserver and qpidd if it was in the backup' do + ForemanMaintain::Utils::Backup.any_instance.stubs(:with_puppetserver?).returns(true) + ForemanMaintain::Utils::Backup.any_instance.stubs(:with_qpidd?).returns(true) + ForemanMaintain.package_manager.expects(:install). + with(['puppetserver', 'qpid-cpp-server'], assumeyes: true).once + result = run_procedure(subject) + assert result.success?, 'the procedure was expected to succeed' + end + + it 'doesnt install anything if it was not in the backup' do + ForemanMaintain::Utils::Backup.any_instance.stubs(:with_puppetserver?).returns(false) + ForemanMaintain::Utils::Backup.any_instance.stubs(:with_qpidd?).returns(false) + ForemanMaintain.package_manager.expects(:install).never + result = run_procedure(subject) + assert result.success?, 'the procedure was expected to succeed' + end +end diff --git a/test/lib/utils/backup_test.rb b/test/lib/utils/backup_test.rb index 824729231..0b9647d82 100644 --- a/test/lib/utils/backup_test.rb +++ b/test/lib/utils/backup_test.rb @@ -378,5 +378,29 @@ def feature_with_local_method(label, return_value) assert backup.validate_interfaces['dns']['configured'] = 'eth0' assert backup.validate_interfaces['dhcp']['configured'] = 'eth0' end + + it 'detects backup with puppetserver installed' do + backup = subject.new(katello_standard_pulpcore_database) + backup.stubs(:metadata).returns('rpms' => ['puppetserver-7.4.2-1.el8.noarch']) + assert backup.with_puppetserver? + end + + it 'detects backup without puppetserver installed' do + backup = subject.new(katello_standard_pulpcore_database) + backup.stubs(:metadata).returns('rpms' => ['ansible-core-2.14.2-4.el8_8.x86_64']) + refute backup.with_puppetserver? + end + + it 'detects backup with qpidd installed' do + backup = subject.new(katello_standard_pulpcore_database) + backup.stubs(:metadata).returns('rpms' => ['qpid-cpp-server-1.36.0-32.el7_9amq.x86_64']) + assert backup.with_qpidd? + end + + it 'detects backup without qpidd installed' do + backup = subject.new(katello_standard_pulpcore_database) + backup.stubs(:metadata).returns('rpms' => ['qpid-cpp-client-1.36.0-32.el7_9amq.x86_64']) + refute backup.with_qpidd? + end end end From 145adff8b106e7f705b2a3e235572e4fd0b0f788 Mon Sep 17 00:00:00 2001 From: "Eric D. Helms" Date: Thu, 3 Aug 2023 11:21:41 -0400 Subject: [PATCH 2/2] Fix change from MiniTest to Minitest --- test/definitions/checks/disk_performance_test.rb | 6 +++--- test/definitions/test_helper.rb | 2 +- test/lib/concerns/upstream_test.rb | 2 +- test/lib/utils/backup_test.rb | 2 +- test/support/minitest_spec_context.rb | 2 +- 5 files changed, 7 insertions(+), 7 deletions(-) diff --git a/test/definitions/checks/disk_performance_test.rb b/test/definitions/checks/disk_performance_test.rb index b5f6e188d..4bd7d9bdb 100644 --- a/test/definitions/checks/disk_performance_test.rb +++ b/test/definitions/checks/disk_performance_test.rb @@ -24,7 +24,7 @@ end check_disk_performance.stubs(:check_only_single_device?).returns(true) - io_obj = MiniTest::Mock.new + io_obj = Minitest::Mock.new io_obj.expect(:read_speed, 90) io_obj.expect(:slow_disk_error_msg, 'Slow disk') io_obj.expect(:name, '/dev/sda') @@ -47,7 +47,7 @@ check_disk_performance.stubs(:check_only_single_device?).returns(true) - io_obj = MiniTest::Mock.new + io_obj = Minitest::Mock.new 2.times { io_obj.expect(:read_speed, slow_speed) } io_obj.expect(:slow_disk_error_msg, err_msg) io_obj.expect(:name, '/dev/sda') @@ -74,7 +74,7 @@ check_disk_performance.stubs(:check_only_single_device?).returns(true) - io_obj = MiniTest::Mock.new + io_obj = Minitest::Mock.new 2.times { io_obj.expect(:read_speed, slow_speed) } io_obj.expect(:slow_disk_error_msg, err_msg) io_obj.expect(:name, '/dev/sda') diff --git a/test/definitions/test_helper.rb b/test/definitions/test_helper.rb index c16c335f2..00df82df7 100644 --- a/test/definitions/test_helper.rb +++ b/test/definitions/test_helper.rb @@ -50,7 +50,7 @@ def assert_stdout(expected_output) alias run_procedure run_step def mock_with_spinner(definition) - mock_spinner = MiniTest::Mock.new + mock_spinner = Minitest::Mock.new mock_spinner.expect(:update, nil) definition.stubs(:with_spinner).returns(mock_spinner) diff --git a/test/lib/concerns/upstream_test.rb b/test/lib/concerns/upstream_test.rb index 7e3001931..c5de19793 100644 --- a/test/lib/concerns/upstream_test.rb +++ b/test/lib/concerns/upstream_test.rb @@ -150,7 +150,7 @@ def stub_unknown(fake_system) it 'returns repositories matching with regex' do stub_el(katello_system) katello_repo_url = 'https://yum.theforeman.org/katello/4.5/katello/el8/x86_64/' - repository_manager_obj = MiniTest::Mock.new + repository_manager_obj = Minitest::Mock.new system_repositories = { 'non_foreman' => 'abc.example.com', 'katello' => katello_repo_url } repository_manager_obj.expect(:enabled_repos, system_repositories) diff --git a/test/lib/utils/backup_test.rb b/test/lib/utils/backup_test.rb index 0b9647d82..6f6d17783 100644 --- a/test/lib/utils/backup_test.rb +++ b/test/lib/utils/backup_test.rb @@ -111,7 +111,7 @@ def assume_feature_absent(label) end def feature_with_local_method(label, return_value) - fake_feature = MiniTest::Mock.new + fake_feature = Minitest::Mock.new ret_hash = { 'host' => 'abc.example.com' } fake_feature.expect(:configuration, ret_hash) fake_feature.expect(:local?, return_value) diff --git a/test/support/minitest_spec_context.rb b/test/support/minitest_spec_context.rb index 52771ad77..a38cbfadc 100644 --- a/test/support/minitest_spec_context.rb +++ b/test/support/minitest_spec_context.rb @@ -1,6 +1,6 @@ require 'minitest/spec' -module MiniTest +module Minitest class Spec class << self alias context describe