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

Cherrypick #36578 - ensure puppet/qpidd are present before restore into 1.2.x #758

Merged
merged 2 commits into from
Sep 13, 2023
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
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
6 changes: 3 additions & 3 deletions test/definitions/checks/disk_performance_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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')
Expand All @@ -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')
Expand All @@ -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')
Expand Down
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
2 changes: 1 addition & 1 deletion test/definitions/test_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
2 changes: 1 addition & 1 deletion test/lib/concerns/upstream_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
26 changes: 25 additions & 1 deletion test/lib/utils/backup_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down 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
2 changes: 1 addition & 1 deletion test/support/minitest_spec_context.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
require 'minitest/spec'

module MiniTest
module Minitest
class Spec
class << self
alias context describe
Expand Down
Loading