From ab8824793b5596bea4e5236ed5f3823061c4de39 Mon Sep 17 00:00:00 2001 From: "Eric D. Helms" Date: Wed, 14 Aug 2024 09:47:36 -0400 Subject: [PATCH] Drop --target-version parameter This parameter is no longer used by the upgrade runner as it is defined internally by the scenarios. --- definitions/features/instance.rb | 4 + definitions/scenarios/update.rb | 2 +- lib/foreman_maintain/cli/upgrade_command.rb | 79 +++++++--------- lib/foreman_maintain/runner.rb | 7 +- lib/foreman_maintain/upgrade_runner.rb | 25 +++--- test/lib/cli/upgrade_command_test.rb | 89 ++++++++----------- .../definitions/features/fake_instance.rb | 12 +++ test/lib/upgrade_runner_test.rb | 8 +- 8 files changed, 106 insertions(+), 120 deletions(-) diff --git a/definitions/features/instance.rb b/definitions/features/instance.rb index 858440927..f8b11e22c 100644 --- a/definitions/features/instance.rb +++ b/definitions/features/instance.rb @@ -101,6 +101,10 @@ def current_version version.to_s[/^\d+\.\d+\.\d+/] end + def current_major_version + current_version.to_s[/^\d+\.\d+/] + end + def target_version if feature(:instance).downstream Features::Satellite.new.target_version diff --git a/definitions/scenarios/update.rb b/definitions/scenarios/update.rb index 53b0dae60..d0a3688af 100644 --- a/definitions/scenarios/update.rb +++ b/definitions/scenarios/update.rb @@ -5,7 +5,7 @@ def self.update_metadata(&block) tags :update_scenario confine do - feature(:instance).target_version == feature(:instance).current_version.to_s[/^\d+\.\d+/] + feature(:instance).target_version == feature(:instance).current_major_version end instance_eval(&block) diff --git a/lib/foreman_maintain/cli/upgrade_command.rb b/lib/foreman_maintain/cli/upgrade_command.rb index 940ce61bb..84df01aba 100644 --- a/lib/foreman_maintain/cli/upgrade_command.rb +++ b/lib/foreman_maintain/cli/upgrade_command.rb @@ -1,85 +1,72 @@ module ForemanMaintain module Cli class UpgradeCommand < Base - def self.target_version_option - option '--target-version', 'TARGET_VERSION', 'Target version of the upgrade', - :required => false - end - def self.disable_self_upgrade_option option '--disable-self-upgrade', :flag, 'Disable automatic self upgrade', :default => false end - def current_target_version - current_target_version = ForemanMaintain::UpgradeRunner.current_target_version - if current_target_version && target_version && target_version != current_target_version - raise Error::UsageError, - "Can't set target version #{target_version}, "\ - "#{current_target_version} already in progress" - end - @target_version = current_target_version if current_target_version - return true if current_target_version - end - - def validate_target_version! - return if current_target_version - unless UpgradeRunner.available_targets.include?(target_version) - message_start = if target_version - "Can't upgrade to #{target_version}" - else - '--target-version not specified' - end - message = <<~MESSAGE - #{message_start} - Possible target versions are: - MESSAGE - versions = UpgradeRunner.available_targets.join("\n") - raise Error::UsageError, message + versions - end - end - def upgrade_runner return @upgrade_runner if defined? @upgrade_runner - validate_target_version! - @upgrade_runner = ForemanMaintain::UpgradeRunner.new(target_version, reporter, + @upgrade_runner = ForemanMaintain::UpgradeRunner.new(reporter, :assumeyes => assumeyes?, :whitelist => whitelist || [], :force => force?).tap(&:load) end - def print_versions(target_versions) - target_versions.sort.each { |version| puts version } - end - def allow_self_upgrade? !disable_self_upgrade? end + def try_upgrade + if upgrade_runner.available? + yield + else + instance = ForemanMaintain.detector.feature(:instance) + msg = <<~BANNER + + There are no upgrades available. + The current version of #{instance.product_name} is #{instance.current_major_version}. + Consider using the update command. + BANNER + + puts msg + ForemanMaintain::UpgradeRunner::WARNING_EXIT_CODE + end + end + subcommand 'check', 'Run pre-upgrade checks before upgrading to specified version' do - target_version_option interactive_option disable_self_upgrade_option def execute ForemanMaintain.validate_downstream_packages ForemanMaintain.perform_self_upgrade if allow_self_upgrade? - upgrade_runner.run_phase(:pre_upgrade_checks) - exit upgrade_runner.exit_code + + exit_code = try_upgrade do + upgrade_runner.run_phase(:pre_upgrade_checks) + upgrade_runner.exit_code + end + + exit exit_code end end subcommand 'run', 'Run full upgrade to a specified version' do - target_version_option interactive_option disable_self_upgrade_option def execute ForemanMaintain.validate_downstream_packages ForemanMaintain.perform_self_upgrade if allow_self_upgrade? - upgrade_runner.run - upgrade_runner.save - exit upgrade_runner.exit_code + + exit_code = try_upgrade do + upgrade_runner.run + upgrade_runner.save + upgrade_runner.exit_code + end + + exit exit_code end end end diff --git a/lib/foreman_maintain/runner.rb b/lib/foreman_maintain/runner.rb index ab73d236a..442a52f84 100644 --- a/lib/foreman_maintain/runner.rb +++ b/lib/foreman_maintain/runner.rb @@ -4,6 +4,9 @@ class Runner include Concerns::Logger attr_reader :reporter, :exit_code + WARNING_EXIT_CODE = 78 + FAILURE_EXIT_CODE = 1 + require 'foreman_maintain/runner/execution' require 'foreman_maintain/runner/stored_execution' def initialize(reporter, scenarios, options = {}) @@ -61,8 +64,8 @@ def run_scenario(scenario) @last_scenario = scenario @last_scenario_continuation_confirmed = false end - @exit_code = 78 if scenario.warning? - @exit_code = 1 if scenario.failed? + @exit_code = WARNING_EXIT_CODE if scenario.warning? + @exit_code = FAILURE_EXIT_CODE if scenario.failed? end def whitelisted_step?(step) diff --git a/lib/foreman_maintain/upgrade_runner.rb b/lib/foreman_maintain/upgrade_runner.rb index f67b7e63e..ff047e561 100644 --- a/lib/foreman_maintain/upgrade_runner.rb +++ b/lib/foreman_maintain/upgrade_runner.rb @@ -34,32 +34,31 @@ def clear_current_target_version attr_reader :version, :tag, :phase - def initialize(version, reporter, options = {}) + def initialize(reporter, options = {}) super(reporter, [], options) - @version = version - scenarios_present = find_scenarios(:tags => :upgrade_scenario).any?(&matching_version_test) - raise "Unknown version #{version}" unless scenarios_present - @scenario_cache = {} - self.phase = :pre_upgrade_checks + @phase = :pre_upgrade_checks + condition = { :tags => [:upgrade_scenario, phase] } + matching_scenarios = find_scenarios(condition) + @version = matching_scenarios.first&.target_version + end + + def available? + condition = { :tags => [:upgrade_scenario, :pre_upgrade_check] } + matching_scenarios = find_scenarios(condition) + !matching_scenarios.empty? end def scenario(phase) return @scenario_cache[phase] if @scenario_cache.key?(phase) condition = { :tags => [:upgrade_scenario, phase] } - matching_scenarios = find_scenarios(condition).select(&matching_version_test) + matching_scenarios = find_scenarios(condition) raise "Too many scenarios match #{condition.inspect}" if matching_scenarios.size > 1 @scenario_cache[phase] = matching_scenarios.first end - def matching_version_test - proc do |scenario| - scenario.respond_to?(:target_version) && scenario.target_version == @version - end - end - def run self.class.current_target_version = @version PHASES.each do |phase| diff --git a/test/lib/cli/upgrade_command_test.rb b/test/lib/cli/upgrade_command_test.rb index 60839eee1..c6fc0c8ed 100644 --- a/test/lib/cli/upgrade_command_test.rb +++ b/test/lib/cli/upgrade_command_test.rb @@ -58,7 +58,6 @@ def foreman_maintain_update_unavailable it 'run self upgrade if upgrade available for foreman-maintain' do foreman_maintain_update_available - command << '--target-version=1.15' assert_cmd <<~OUTPUT Checking for new version of rubygem-foreman_maintain... @@ -71,8 +70,8 @@ def foreman_maintain_update_unavailable it 'runs the upgrade checks when update is not available for foreman-maintain' do foreman_maintain_update_unavailable - command << '--target-version=1.15' UpgradeRunner.any_instance.expects(:run_phase).with(:pre_upgrade_checks) + UpgradeRunner.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. @@ -83,11 +82,22 @@ def foreman_maintain_update_unavailable foreman_maintain_update_available command << '--disable-self-upgrade' UpgradeRunner.any_instance.expects(:run_phase).with(:pre_upgrade_checks) - run_cmd(['--target-version=1.15']) + UpgradeRunner.any_instance.expects(:available?).returns(true) + run_cmd([]) end - it 'should raise UsageError and exit with code 1' do - Cli::MainCommand.any_instance.stubs(:exit!) + it 'throws an error message if no upgrade is available' do + foreman_maintain_update_unavailable + UpgradeRunner.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. + + There are no upgrades available. + The current version of FakeyFakeFake is 3.14. + Consider using the update command. + OUTPUT run_cmd([]) end @@ -100,7 +110,6 @@ def foreman_maintain_update_unavailable it 'run self upgrade if upgrade available for foreman-maintain' do foreman_maintain_update_available - command << '--target-version=1.15' assert_cmd <<~OUTPUT Checking for new version of rubygem-foreman_maintain... @@ -113,8 +122,8 @@ def foreman_maintain_update_unavailable it 'runs the full upgrade when update is not available for foreman-maintain' do foreman_maintain_update_unavailable - command << '--target-version=1.15' UpgradeRunner.any_instance.expects(:run) + UpgradeRunner.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. @@ -124,25 +133,29 @@ def foreman_maintain_update_unavailable it 'skip self upgrade and runs the full upgrade for version' do command << '--disable-self-upgrade' UpgradeRunner.any_instance.expects(:run) - run_cmd(['--target-version=1.15']) + UpgradeRunner.any_instance.expects(:available?).returns(true) + run_cmd([]) end - it 'runs the self upgrade when update available for rubygem-foreman_maintain' do - foreman_maintain_update_available + it 'throws an error message if no upgrade is available' do + foreman_maintain_update_unavailable + UpgradeRunner.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. - Updating rubygem-foreman_maintain package. - - The rubygem-foreman_maintain package successfully updated. - Re-run foreman-maintain with required options! + There are no upgrades available. + The current version of FakeyFakeFake is 3.14. + Consider using the update command. OUTPUT - UpgradeRunner.current_target_version = '1.15' - - run_cmd + run_cmd([]) + end - assert_cmd(<<~OUTPUT, ['--target-version', '1.16']) + it 'runs the self upgrade when update available for rubygem-foreman_maintain' do + foreman_maintain_update_available + assert_cmd <<~OUTPUT Checking for new version of rubygem-foreman_maintain... Updating rubygem-foreman_maintain package. @@ -150,50 +163,18 @@ def foreman_maintain_update_unavailable The rubygem-foreman_maintain package successfully updated. Re-run foreman-maintain with required options! OUTPUT - end - - it 'remembers the current target version and informs no update available' do - foreman_maintain_update_unavailable - Cli::MainCommand.any_instance.expects(:exit!).twice - assert_cmd <<~OUTPUT - Checking for new version of rubygem-foreman_maintain... - Nothing to update, can't find new version of rubygem-foreman_maintain. - --target-version not specified - Possible target versions are: - 1.15 - OUTPUT UpgradeRunner.current_target_version = '1.15' - UpgradeRunner.any_instance.expects(:run) run_cmd - assert_cmd(<<~OUTPUT, ['--target-version', '1.16']) + assert_cmd(<<~OUTPUT, []) Checking for new version of rubygem-foreman_maintain... - Nothing to update, can't find new version of rubygem-foreman_maintain. - Can't set target version 1.16, 1.15 already in progress - OUTPUT - end - it 'remembers the current target version when self upgrade disabled' do - command << '--disable-self-upgrade' - Cli::MainCommand.any_instance.expects(:exit!) - assert_cmd <<~OUTPUT - --target-version not specified - Possible target versions are: - 1.15 - OUTPUT - end - - it 'does not allow the another upgrade when one is going on' do - foreman_maintain_update_unavailable - UpgradeRunner.current_target_version = '1.15' - Cli::MainCommand.any_instance.expects(:exit!) + Updating rubygem-foreman_maintain package. - assert_cmd(<<~OUTPUT, ['--target-version', '1.16']) - Checking for new version of rubygem-foreman_maintain... - Nothing to update, can't find new version of rubygem-foreman_maintain. - Can't set target version 1.16, 1.15 already in progress + The rubygem-foreman_maintain package successfully updated. + Re-run foreman-maintain with required options! OUTPUT end end diff --git a/test/lib/support/definitions/features/fake_instance.rb b/test/lib/support/definitions/features/fake_instance.rb index 2c05574b5..802000aa2 100644 --- a/test/lib/support/definitions/features/fake_instance.rb +++ b/test/lib/support/definitions/features/fake_instance.rb @@ -10,4 +10,16 @@ class Features::FakeInstance < ForemanMaintain::Feature def downstream false end + + def product_name + "FakeyFakeFake" + end + + def current_version + '3.14.2' + end + + def current_major_version + current_version.to_s[/^\d+\.\d+/] + end end diff --git a/test/lib/upgrade_runner_test.rb b/test/lib/upgrade_runner_test.rb index 803584022..91a21f80e 100644 --- a/test/lib/upgrade_runner_test.rb +++ b/test/lib/upgrade_runner_test.rb @@ -12,11 +12,11 @@ module ForemanMaintain end let(:upgrade_runner) do - UpgradeRunner.new('1.15', reporter) + UpgradeRunner.new(reporter) end let(:upgrade_runner_with_whitelist) do - UpgradeRunner.new('1.15', reporter, + UpgradeRunner.new(reporter, :whitelist => %w[present-service-is-running service-is-stopped]) end @@ -56,7 +56,7 @@ module ForemanMaintain original_scenario = upgrade_runner_with_whitelist.scenario(:pre_upgrade_checks) ForemanMaintain.detector.refresh - new_upgrade_runner = UpgradeRunner.new('1.15', reporter) + new_upgrade_runner = UpgradeRunner.new(reporter) new_upgrade_runner.load _(new_upgrade_runner.phase).must_equal :migrations restored_scenario = new_upgrade_runner.scenario(:pre_upgrade_checks) @@ -87,7 +87,7 @@ module ForemanMaintain upgrade_runner_with_whitelist.run upgrade_runner_with_whitelist.save - new_upgrade_runner = UpgradeRunner.new('1.15', reporter) + new_upgrade_runner = UpgradeRunner.new(reporter) new_upgrade_runner.load _(new_upgrade_runner.phase).must_equal :pre_upgrade_checks _(UpgradeRunner.current_target_version).must_be_nil