From 3e3130c03f76075b2db29b1ce0ea267fd700514f Mon Sep 17 00:00:00 2001 From: "Eric D. Helms" Date: Thu, 22 Aug 2024 21:36:31 -0400 Subject: [PATCH] Print message if update is unavailable --- lib/foreman_maintain/cli/update_command.rb | 45 ++++++++++++++++--- lib/foreman_maintain/update_runner.rb | 6 +++ test/lib/cli/update_command_test.rb | 44 ++++++++++++++++++ .../definitions/features/fake_instance.rb | 4 ++ 4 files changed, 92 insertions(+), 7 deletions(-) diff --git a/lib/foreman_maintain/cli/update_command.rb b/lib/foreman_maintain/cli/update_command.rb index 05f6cbfa5..76a33c30c 100644 --- a/lib/foreman_maintain/cli/update_command.rb +++ b/lib/foreman_maintain/cli/update_command.rb @@ -19,6 +19,27 @@ 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 @@ -26,9 +47,14 @@ def update_runner 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 @@ -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 diff --git a/lib/foreman_maintain/update_runner.rb b/lib/foreman_maintain/update_runner.rb index 1acfbcaad..5ac7cef11 100644 --- a/lib/foreman_maintain/update_runner.rb +++ b/lib/foreman_maintain/update_runner.rb @@ -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) diff --git a/test/lib/cli/update_command_test.rb b/test/lib/cli/update_command_test.rb index 0258485ba..4291e5920 100644 --- a/test/lib/cli/update_command_test.rb +++ b/test/lib/cli/update_command_test.rb @@ -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. @@ -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 @@ -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... @@ -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 diff --git a/test/lib/support/definitions/features/fake_instance.rb b/test/lib/support/definitions/features/fake_instance.rb index 802000aa2..b504e65d1 100644 --- a/test/lib/support/definitions/features/fake_instance.rb +++ b/test/lib/support/definitions/features/fake_instance.rb @@ -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