Skip to content

Commit

Permalink
Fixes #36578 - ensure puppet/qpidd are present before restore
Browse files Browse the repository at this point in the history
(cherry picked from commit 80cbb53)
  • Loading branch information
evgeni committed Sep 13, 2023
1 parent 1e11450 commit 0ab628a
Show file tree
Hide file tree
Showing 5 changed files with 119 additions and 0 deletions.
23 changes: 23 additions & 0 deletions definitions/procedures/restore/required_packages.rb
Original file line number Diff line number Diff line change
@@ -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
2 changes: 2 additions & 0 deletions definitions/scenarios/restore.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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?
Expand Down Expand Up @@ -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,
Expand Down
12 changes: 12 additions & 0 deletions lib/foreman_maintain/utils/backup.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
58 changes: 58 additions & 0 deletions test/definitions/procedures/restore/required_packages_test.rb
Original file line number Diff line number Diff line change
@@ -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
24 changes: 24 additions & 0 deletions test/lib/utils/backup_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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

0 comments on commit 0ab628a

Please sign in to comment.