Skip to content

Commit

Permalink
Print message if update is unavailable
Browse files Browse the repository at this point in the history
  • Loading branch information
ehelms committed Aug 23, 2024
1 parent 138cd96 commit 1fde30f
Show file tree
Hide file tree
Showing 3 changed files with 76 additions and 7 deletions.
41 changes: 34 additions & 7 deletions lib/foreman_maintain/cli/update_command.rb
Original file line number Diff line number Diff line change
Expand Up @@ -19,16 +19,38 @@ def update_runner
update_runner
end

def try_update
if update_runner.available?
yield
else
instance = ForemanMaintain.detector.feature(:instance)
msg = <<~BANNER
Update is not available.
The system appears to be in the middle of an upgrade.
The current version of #{instance.product_name} is #{instance.current_major_version}.
BANNER

puts msg
ForemanMaintain::UpgradeRunner::WARNING_EXIT_CODE
end
end

subcommand 'check', 'Run pre-update checks before updating' do
interactive_option
disable_self_update_option

def execute
ForemanMaintain.validate_downstream_packages
ForemanMaintain.perform_self_upgrade unless disable_self_update?
runner = update_runner
runner.run_phase(:pre_update_checks)
exit runner.exit_code

exit_code = try_update do
runner = update_runner
runner.run_phase(:pre_update_checks)
runner.exit_code
end

exit exit_code
end
end

Expand All @@ -39,10 +61,15 @@ def execute
def execute
ForemanMaintain.validate_downstream_packages
ForemanMaintain.perform_self_upgrade unless disable_self_update?
runner = update_runner
runner.run
runner.save
exit runner.exit_code

exit_code = try_update do
runner = update_runner
runner.run
runner.save
runner.exit_code
end

exit exit_code
end
end
end
Expand Down
6 changes: 6 additions & 0 deletions lib/foreman_maintain/update_runner.rb
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,12 @@ def initialize(reporter, options = {})
self.phase = :pre_update_checks
end

def available?
condition = { :tags => [:update_scenario, :pre_upgrade_checks] }
matching_scenarios = find_scenarios(condition)
!matching_scenarios.empty?
end

def find_scenario(phase)
return @scenario_cache[phase] if @scenario_cache.key?(phase)

Expand Down
36 changes: 36 additions & 0 deletions test/lib/cli/update_command_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,7 @@ def foreman_maintain_update_unavailable
it 'runs the update checks when update is not available for foreman-maintain' do
foreman_maintain_update_unavailable
UpdateRunner.any_instance.expects(:run_phase).with(:pre_update_checks)
UpdateRunner.any_instance.expects(:available?).returns(true)
assert_cmd <<~OUTPUT
Checking for new version of rubygem-foreman_maintain...
Nothing to update, can't find new version of rubygem-foreman_maintain.
Expand All @@ -86,8 +87,25 @@ def foreman_maintain_update_unavailable
foreman_maintain_update_available
command << '--disable-self-update'
UpdateRunner.any_instance.expects(:run_phase).with(:pre_update_checks)
UpdateRunner.any_instance.expects(:available?).returns(true)
run_cmd
end

it 'throws an error message if no update is available' do
foreman_maintain_update_unavailable
UpdateRunner.any_instance.expects(:available?).twice.returns(false)

assert_cmd <<~OUTPUT
Checking for new version of rubygem-foreman_maintain...
Nothing to update, can't find new version of rubygem-foreman_maintain.
Update is not available.
The system appears to be in the middle of an upgrade.
The current version of FakeyFakeFake is 3.14.
OUTPUT

run_cmd([])
end
end

describe 'run' do
Expand All @@ -109,6 +127,7 @@ def foreman_maintain_update_unavailable

it 'runs the update when update is not available for foreman-maintain' do
foreman_maintain_update_unavailable
UpdateRunner.any_instance.expects(:available?).returns(true)
UpdateRunner.any_instance.expects(:run)
assert_cmd <<~OUTPUT
Checking for new version of rubygem-foreman_maintain...
Expand All @@ -119,9 +138,26 @@ def foreman_maintain_update_unavailable
it 'skip self update and runs the full update for version' do
command << '--disable-self-update'
UpdateRunner.any_instance.expects(:run)
UpdateRunner.any_instance.expects(:available?).returns(true)
run_cmd
end

it 'throws an error message if no update is available' do
foreman_maintain_update_unavailable
UpdateRunner.any_instance.expects(:available?).twice.returns(false)

assert_cmd <<~OUTPUT
Checking for new version of rubygem-foreman_maintain...
Nothing to update, can't find new version of rubygem-foreman_maintain.
Update is not available.
The system appears to be in the middle of an upgrade.
The current version of FakeyFakeFake is 3.14.
OUTPUT

run_cmd([])
end

it 'runs the self update when update available for rubygem-foreman_maintain' do
foreman_maintain_update_available
assert_cmd <<~OUTPUT
Expand Down

0 comments on commit 1fde30f

Please sign in to comment.