diff --git a/definitions/features/foreman_install.rb b/definitions/features/foreman_install.rb index fc50612f7..f053a5ab5 100644 --- a/definitions/features/foreman_install.rb +++ b/definitions/features/foreman_install.rb @@ -9,4 +9,16 @@ class Features::ForemanInstall < ForemanMaintain::Feature !feature(:instance).downstream && feature(:foreman_server) end end + + def target_version + '3.11' + end + + def current_version + @current_version ||= package_version(package_name) + end + + def package_name + 'foreman' + end end diff --git a/definitions/features/instance.rb b/definitions/features/instance.rb index e036510e3..858440927 100644 --- a/definitions/features/instance.rb +++ b/definitions/features/instance.rb @@ -91,6 +91,24 @@ def upstream_install feature(:foreman_install) || feature(:katello_install) end + def current_version + version = if feature(:instance).downstream + feature(:instance).downstream.current_version + else + feature(:foreman_install).current_version + end + + version.to_s[/^\d+\.\d+\.\d+/] + end + + def target_version + if feature(:instance).downstream + Features::Satellite.new.target_version + else + Features::ForemanInstall.new.target_version + end + end + private # rubocop:disable Metrics/AbcSize diff --git a/definitions/features/satellite.rb b/definitions/features/satellite.rb index 7154a51da..58777af74 100644 --- a/definitions/features/satellite.rb +++ b/definitions/features/satellite.rb @@ -10,6 +10,10 @@ class Features::Satellite < ForemanMaintain::Feature end end + def target_version + '6.16' + end + def current_version @current_version ||= package_version(package_name) || version_from_source end diff --git a/definitions/scenarios/self_upgrade.rb b/definitions/scenarios/self_upgrade.rb index 841d5876d..2e0ad91ee 100644 --- a/definitions/scenarios/self_upgrade.rb +++ b/definitions/scenarios/self_upgrade.rb @@ -4,11 +4,11 @@ class SelfUpgradeBase < ForemanMaintain::Scenario include ForemanMaintain::Concerns::Versions def target_version - @target_version ||= Gem::Version.new(current_version).bump.to_s + feature(:instance).target_version end def current_version - feature(:instance).downstream.current_version.to_s[/^\d+\.\d+\.\d+/] + feature(:instance).current_version end def maintenance_repo_label @@ -47,12 +47,11 @@ def req_repos_to_update_pkgs end end - def upstream_target_version - if feature(:katello_install) - return foreman_version_by_katello(target_version) - else - target_version - end + def self_upgrade_allowed? + target = Gem::Version.new(target_version).version + + Gem::Version.new(current_version).segments[0..1].join('.') == target || + Gem::Version.new(current_version).bump.segments[0..1].join('.') == target end end @@ -65,6 +64,16 @@ class SelfUpgrade < SelfUpgradeBase end def downstream_self_upgrade(pkgs_to_update) + unless self_upgrade_allowed? + raise( + ForemanMaintain::Error::Warn, + "foreman-maintain is too many versions ahead. The target " \ + "version is #{target_version} while the currently installed " \ + "version is #{current_version}. Please rollback " \ + "foreman-maintain to the proper version." + ) + end + ForemanMaintain.enable_maintenance_module add_step(Procedures::Packages::Update.new(packages: pkgs_to_update, assumeyes: true, @@ -77,7 +86,7 @@ def upstream_self_upgrade(pkgs_to_update) # 2. Update the foreman-maintain packages from next major version repository # 3. Rollback the repository to current major version - add_step(Procedures::Repositories::Setup.new(:version => upstream_target_version)) + add_step(Procedures::Repositories::Setup.new(:version => target_version)) add_step(Procedures::Packages::Update.new(packages: pkgs_to_update, assumeyes: true)) ensure rollback_repositories diff --git a/test/definitions/scenarios/self_upgrade_test.rb b/test/definitions/scenarios/self_upgrade_test.rb index 3276530f5..a5077be55 100644 --- a/test/definitions/scenarios/self_upgrade_test.rb +++ b/test/definitions/scenarios/self_upgrade_test.rb @@ -1,29 +1,120 @@ require 'test_helper' +require_relative '../test_helper' +require_relative '../../../definitions/scenarios/self_upgrade' -module Scenarios - describe ForemanMaintain::Scenarios::SelfUpgradeBase do - include DefinitionsTestHelper +describe ForemanMaintain::Scenarios::SelfUpgradeBase do + include ::DefinitionsTestHelper - describe 'with default params' do - let(:scenario) do - ForemanMaintain::Scenarios::SelfUpgradeBase.new - end + before do + File.stubs(:exist?).with('/etc/redhat-release').returns(true) + end + + let(:scenario) do + ForemanMaintain::Scenarios::SelfUpgradeBase.new + end + + it 'computes the target version correctly for downstream' do + assume_satellite_present + + assert_equal '6.16', scenario.target_version + end + + it 'returns target version correctly for upstream' do + assume_feature_present(:foreman_install) + + assert_equal '3.11', scenario.target_version + end + + it 'allow upgrade when versions are equal' do + assume_satellite_present + scenario.expects(:current_version).returns('6.16.0') + + assert scenario.self_upgrade_allowed? + end + + it 'allow upgrade when versions are equal for z-stream' do + assume_satellite_present + scenario.expects(:current_version).returns('6.16.4') + + assert scenario.self_upgrade_allowed? + end + + it 'allow upgrade when versions are off by 1' do + assume_satellite_present + scenario.expects(:current_version).twice.returns('6.15.8') - it 'computes the target version correctly coming from normal release 6.10.0' do - assume_satellite_present do |feature_class| - feature_class.any_instance.stubs(:current_version => version('6.10.0')) - end + assert scenario.self_upgrade_allowed? + end + + it 'does not allow upgrade when versions are off by 2 or more' do + assume_satellite_present + scenario.expects(:current_version).twice.returns('6.14.4') + + refute scenario.self_upgrade_allowed? + end +end + +describe ForemanMaintain::Scenarios::SelfUpgrade do + include ::DefinitionsTestHelper + + before do + File.stubs(:exist?).with('/etc/redhat-release').returns(true) + end - assert_equal '6.11', scenario.target_version - end + it 'runs successfully for downstream Satellite' do + ForemanMaintain. + package_manager. + expects(:find_installed_package). + with('satellite', "%{VERSION}"). + returns('6.16.0') + assume_feature_present(:satellite) + scenario = ForemanMaintain::Scenarios::SelfUpgrade.new + scenario.compose + + assert run_step(scenario) + end + + it 'runs successfully for downstream Capsule' do + ForemanMaintain. + package_manager. + expects(:find_installed_package). + with('satellite-capsule', "%{VERSION}"). + returns('6.16.3') + assume_feature_present(:capsule) + scenario = ForemanMaintain::Scenarios::SelfUpgrade.new + scenario.compose + + assert run_step(scenario) + end + + it 'run successfully if current and target version are off by 1' do + ForemanMaintain. + package_manager. + expects(:find_installed_package). + with('satellite', "%{VERSION}"). + returns('6.15.3') + assume_feature_present(:satellite) + scenario = ForemanMaintain::Scenarios::SelfUpgrade.new + scenario.compose + + assert run_step(scenario) + end - it 'computes the target version correctly coming from an async release 6.11.1.1' do - assume_satellite_present do |feature_class| - feature_class.any_instance.stubs(:current_version => version('6.11.1.1')) - end + it 'fails if versions are off 2 or more' do + ForemanMaintain. + package_manager. + expects(:find_installed_package). + with('satellite', "%{VERSION}"). + returns('6.14.1') + assume_feature_present(:satellite) - assert_equal '6.12', scenario.target_version - end + msg = "foreman-maintain is too many versions ahead. The target " \ + "version is 6.16 while the currently installed " \ + "version is 6.14.1. Please rollback " \ + "foreman-maintain to the proper version." + assert_raises(ForemanMaintain::Error::Warn, msg) do + scenario = ForemanMaintain::Scenarios::SelfUpgrade.new + scenario.compose end end end diff --git a/test/definitions/test_helper.rb b/test/definitions/test_helper.rb index 000d09ff5..c4fd85cbd 100644 --- a/test/definitions/test_helper.rb +++ b/test/definitions/test_helper.rb @@ -1,5 +1,6 @@ require File.expand_path('../test_helper', File.dirname(__FILE__)) require File.expand_path('assume_feature_dependencies_helper', File.dirname(__FILE__)) + module DefinitionsTestHelper include ForemanMaintain::Concerns::Finders include ForemanMaintain::Concerns::SystemService