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 authored and evgeni committed Aug 29, 2024
1 parent d274464 commit 28e75a8
Show file tree
Hide file tree
Showing 4 changed files with 92 additions and 7 deletions.
45 changes: 38 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,42 @@ def update_runner
update_runner
end

def try_update
if update_runner.available?
yield
else
instance = ForemanMaintain.detector.feature(:instance)
msg = <<~BANNER
This version of #{ForemanMaintain.command_name} only supports #{instance.target_version},
but the installed version of #{instance.product_name} is #{instance.current_major_version}.
Therefore the update command is not available right now.
Please install a version of #{ForemanMaintain.command_name} that supports #{instance.current_major_version}
or perform an upgrade to #{instance.target_version} using the upgrade command.
BANNER

puts msg
ForemanMaintain::UpdateRunner::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 +65,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_update_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
44 changes: 44 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,29 @@ 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.
This version of foreman-maintain only supports 3.15.0,
but the installed version of FakeyFakeFake is 3.14.
Therefore the update command is not available right now.
Please install a version of foreman-maintain that supports 3.14
or perform an upgrade to 3.15.0 using the upgrade command.
OUTPUT

run_cmd([])
end
end

describe 'run' do
Expand All @@ -109,6 +131,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 +142,30 @@ 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.
This version of foreman-maintain only supports 3.15.0,
but the installed version of FakeyFakeFake is 3.14.
Therefore the update command is not available right now.
Please install a version of foreman-maintain that supports 3.14
or perform an upgrade to 3.15.0 using the upgrade command.
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
4 changes: 4 additions & 0 deletions test/lib/support/definitions/features/fake_instance.rb
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,10 @@ def current_version
'3.14.2'
end

def target_version
'3.15.0'
end

def current_major_version
current_version.to_s[/^\d+\.\d+/]
end
Expand Down

0 comments on commit 28e75a8

Please sign in to comment.