From 55f149b1ffc932f8b7e6ebeca456624f4475b13c Mon Sep 17 00:00:00 2001 From: Jeremy Lenz Date: Thu, 31 Aug 2023 16:59:07 -0400 Subject: [PATCH] Refs #36693 - address comments; remove some unused methods --- .../concerns/host_managed_extensions.rb | 14 +-- app/models/katello/rhel_lifecycle_status.rb | 93 +++++++++---------- .../concerns/host_managed_extensions_test.rb | 26 ++---- test/models/rhel_lifecycle_status_test.rb | 31 ++++--- 4 files changed, 73 insertions(+), 91 deletions(-) diff --git a/app/models/katello/concerns/host_managed_extensions.rb b/app/models/katello/concerns/host_managed_extensions.rb index d6957db4830..aacdf9fd3f1 100644 --- a/app/models/katello/concerns/host_managed_extensions.rb +++ b/app/models/katello/concerns/host_managed_extensions.rb @@ -492,23 +492,15 @@ def rhel_lifecycle_status_label end def full_support_end_date - ::Katello::RhelLifecycleStatus.full_support_end_date(eos_schedule_index: rhel_eos_schedule_index) - end - - def approaching_end_of_maintenance_date - ::Katello::RhelLifecycleStatus.maintenance_warn_date(eos_schedule_index: rhel_eos_schedule_index) + ::Katello::RhelLifecycleStatus.full_support_end_dates[rhel_eos_schedule_index] end def maintenance_support_end_date - ::Katello::RhelLifecycleStatus.maintenance_support_end_date(eos_schedule_index: rhel_eos_schedule_index) + ::Katello::RhelLifecycleStatus.maintenance_support_end_dates[rhel_eos_schedule_index] end def extended_support_end_date - ::Katello::RhelLifecycleStatus.extended_support_end_date(eos_schedule_index: rhel_eos_schedule_index) - end - - def approaching_end_of_support_date - ::Katello::RhelLifecycleStatus.warn_date(eos_schedule_index: rhel_eos_schedule_index) + ::Katello::RhelLifecycleStatus.extended_support_end_dates[rhel_eos_schedule_index] end def end_of_support_date diff --git a/app/models/katello/rhel_lifecycle_status.rb b/app/models/katello/rhel_lifecycle_status.rb index e0c26d2b892..ffc003d8a30 100644 --- a/app/models/katello/rhel_lifecycle_status.rb +++ b/app/models/katello/rhel_lifecycle_status.rb @@ -74,33 +74,37 @@ def self.status_map map end + def self.approaching_end_of_category(eos_schedule_index:) + RHEL_EOS_SCHEDULE[eos_schedule_index].select { |k, v| (Time.now.utc .. Time.now.utc + EOS_WARNING_THRESHOLD).cover?(v) } + end + def self.to_status(rhel_eos_schedule_index: nil) release = rhel_eos_schedule_index return UNKNOWN unless release.present? && RHEL_EOS_SCHEDULE.key?(release) - - # Full support - full_support_end_date = full_support_end_date(eos_schedule_index: release) - return FULL_SUPPORT if Date.today <= full_support_end_date - - # Approach warnings - maintenance_approach_date = maintenance_warn_date(eos_schedule_index: release) - maintenance_end_of_support_date = maintenance_support_end_date(eos_schedule_index: release) - if between_dates?(maintenance_approach_date, maintenance_end_of_support_date) - return APPROACHING_END_OF_MAINTENANCE + approach = approaching_end_of_category(eos_schedule_index: release) + if approach.present? + case approach.keys.first + when last_support_category(eos_schedule_index: release) + return APPROACHING_END_OF_SUPPORT + when 'maintenance_support' + return APPROACHING_END_OF_MAINTENANCE + end end - approach_date = warn_date(eos_schedule_index: release) - end_of_support_date = eos_date(eos_schedule_index: release) - if between_dates?(approach_date, end_of_support_date) - return APPROACHING_END_OF_SUPPORT + + full_support_end_date = RHEL_EOS_SCHEDULE[release]['full_support'] + maintenance_support_end_date = RHEL_EOS_SCHEDULE[release]['maintenance_support'] + extended_support_end_date = RHEL_EOS_SCHEDULE[release]['extended_support'] + + case + when Date.today <= full_support_end_date + return FULL_SUPPORT + when Date.today <= maintenance_support_end_date + return MAINTENANCE_SUPPORT + when extended_support_end_date.present? && Date.today <= extended_support_end_date + return EXTENDED_SUPPORT + else + return SUPPORT_ENDED end - - # Maintenance support - return MAINTENANCE_SUPPORT if Date.today <= maintenance_end_of_support_date - - # Extended support - return EXTENDED_SUPPORT if Date.today <= end_of_support_date - - return SUPPORT_ENDED end def self.status_name @@ -111,39 +115,34 @@ def self.humanized_name 'rhel_lifecycle' end - def self.full_support_end_date(eos_schedule_index: nil) - return nil unless eos_schedule_index - RHEL_EOS_SCHEDULE[eos_schedule_index]&.[]('full_support') + # {"RHEL9"=>2035-05-31 23:59:59.999999999 UTC, + # "RHEL8"=>2032-05-31 23:59:59.999999999 UTC, ... } + def self.schedule_slice(support_category) + {}.merge(*RHEL_EOS_SCHEDULE.keys.map do |release| + { release => RHEL_EOS_SCHEDULE[release]&.[](support_category) } + end) end - def self.maintenance_support_end_date(eos_schedule_index: nil) - return nil unless eos_schedule_index - RHEL_EOS_SCHEDULE[eos_schedule_index]&.[]('maintenance_support') + def self.full_support_end_dates + schedule_slice('full_support') end - def self.extended_support_end_date(eos_schedule_index: nil) - return nil unless eos_schedule_index - RHEL_EOS_SCHEDULE[eos_schedule_index]&.[]('extended_support') + def self.maintenance_support_end_dates + schedule_slice('maintenance_support') end - def self.eos_date(eos_schedule_index: nil) - return nil unless eos_schedule_index - RHEL_EOS_SCHEDULE[eos_schedule_index]&.[]('extended_support') || - RHEL_EOS_SCHEDULE[eos_schedule_index]&.[]('maintenance_support') + def self.extended_support_end_dates + schedule_slice('extended_support') end - def self.warn_date(eos_schedule_index: nil) - return nil unless eos_schedule_index - end_of_support_date = eos_date(eos_schedule_index: eos_schedule_index) - return nil unless end_of_support_date - end_of_support_date - EOS_WARNING_THRESHOLD + def self.last_support_category(eos_schedule_index:) + RHEL_EOS_SCHEDULE[eos_schedule_index].keys.last end - def self.maintenance_warn_date(eos_schedule_index: nil) + def self.eos_date(eos_schedule_index: nil) return nil unless eos_schedule_index - end_of_maintenance_support_date = maintenance_support_end_date(eos_schedule_index: eos_schedule_index) - return nil unless end_of_maintenance_support_date - end_of_maintenance_support_date - EOS_WARNING_THRESHOLD + RHEL_EOS_SCHEDULE[eos_schedule_index]&.[]('extended_support') || + RHEL_EOS_SCHEDULE[eos_schedule_index]&.[]('maintenance_support') end def self.to_label(status, eos_date: nil, maintenance_support_end_date: nil) @@ -182,11 +181,7 @@ def eos_date end def maintenance_support_end_date - self.class.maintenance_support_end_date(eos_schedule_index: rhel_eos_schedule_index) - end - - def warn_date - self.class.warn_date(eos_schedule_index: rhel_eos_schedule_index) + self.class.maintenance_support_end_dates[rhel_eos_schedule_index] end def rhel_eos_schedule_index diff --git a/test/models/concerns/host_managed_extensions_test.rb b/test/models/concerns/host_managed_extensions_test.rb index 95bee626b23..12c41750145 100644 --- a/test/models/concerns/host_managed_extensions_test.rb +++ b/test/models/concerns/host_managed_extensions_test.rb @@ -423,37 +423,25 @@ def test_search_known_traces class HostRhelEosSchedulesTest < ActiveSupport::TestCase let(:host) { FactoryBot.create(:host, :with_subscription) } - def test_full_support_end_date + def test_full_support_end_dates host.expects(:rhel_eos_schedule_index).returns('RHEL9') - expected_date = ::Katello::RhelLifecycleStatus.full_support_end_date(eos_schedule_index: 'RHEL9') + expected_date = ::Katello::RhelLifecycleStatus.full_support_end_dates['RHEL9'] assert_equal expected_date, host.full_support_end_date end - def test_maintenance_support_end_date + def test_maintenance_support_end_dates host.expects(:rhel_eos_schedule_index).returns('RHEL9') - expected_date = ::Katello::RhelLifecycleStatus.maintenance_support_end_date(eos_schedule_index: 'RHEL9') + expected_date = ::Katello::RhelLifecycleStatus.maintenance_support_end_dates['RHEL9'] assert_equal expected_date, host.maintenance_support_end_date end - def test_approaching_end_of_maintenance_date + def test_extended_support_end_dates host.expects(:rhel_eos_schedule_index).returns('RHEL9') - expected_date = ::Katello::RhelLifecycleStatus.maintenance_warn_date(eos_schedule_index: 'RHEL9') - assert_equal expected_date, host.approaching_end_of_maintenance_date - end - - def test_extended_support_end_date - host.expects(:rhel_eos_schedule_index).returns('RHEL9') - expected_date = ::Katello::RhelLifecycleStatus.extended_support_end_date(eos_schedule_index: 'RHEL9') + expected_date = ::Katello::RhelLifecycleStatus.extended_support_end_dates['RHEL9'] assert_equal expected_date, host.extended_support_end_date end - def test_approaching_end_of_support_date - host.expects(:rhel_eos_schedule_index).returns('RHEL9') - expected_date = ::Katello::RhelLifecycleStatus.warn_date(eos_schedule_index: 'RHEL9') - assert_equal expected_date, host.approaching_end_of_support_date - end - - def test_end_of_support_date + def test_end_of_support_dates host.expects(:rhel_eos_schedule_index).returns('RHEL9') expected_date = ::Katello::RhelLifecycleStatus.eos_date(eos_schedule_index: 'RHEL9') assert_equal expected_date, host.end_of_support_date diff --git a/test/models/rhel_lifecycle_status_test.rb b/test/models/rhel_lifecycle_status_test.rb index a81e69ed417..e3ccdd096fd 100644 --- a/test/models/rhel_lifecycle_status_test.rb +++ b/test/models/rhel_lifecycle_status_test.rb @@ -71,7 +71,10 @@ def test_to_status_full_support os.hosts << host host.operatingsystem.update(:name => "RedHat", :major => "9", :minor => "0") host.operatingsystem.expects(:rhel_eos_schedule_index).returns(release) + Katello::RhelLifecycleStatus.expects(:approaching_end_of_category).returns({}) fake_full_support_end_date(Date.today + 2.years) + fake_maintenance_support_end_date(Date.today + 10.years) + fake_extended_support_end_date(Date.today + 20.years) assert_equal Katello::RhelLifecycleStatus::FULL_SUPPORT, status.to_status end @@ -89,9 +92,7 @@ def test_to_status_approaching_end_of_support os.hosts << host host.operatingsystem.update(:name => "RedHat", :major => "9", :minor => "0") host.operatingsystem.expects(:rhel_eos_schedule_index).returns(release) - fake_full_support_end_date(Date.today - 5.years) - fake_maintenance_support_end_date(Date.today - 3.years) - fake_extended_support_end_date(Date.today + 10.days) + Katello::RhelLifecycleStatus.expects(:approaching_end_of_category).returns({ 'extended_support' => Date.today + 2.days }) assert_equal Katello::RhelLifecycleStatus::APPROACHING_END_OF_SUPPORT, status.to_status end @@ -115,19 +116,25 @@ def test_to_status_support_ended assert_equal Katello::RhelLifecycleStatus::SUPPORT_ENDED, status.to_status end - def test_full_support_end_date - fake_full_support_end_date(Date.today + 2.years) - assert_equal Date.today + 2.years, Katello::RhelLifecycleStatus.full_support_end_date(eos_schedule_index: release) + def test_full_support_end_dates + assert_equal_arrays Katello::RhelLifecycleStatus::RHEL_EOS_SCHEDULE.keys, Katello::RhelLifecycleStatus.full_support_end_dates.keys + Katello::RhelLifecycleStatus::RHEL_EOS_SCHEDULE.each do |release, schedule| + assert_equal schedule['full_support'], Katello::RhelLifecycleStatus.full_support_end_dates[release] + end end - def test_maintenance_support_end_date - fake_maintenance_support_end_date(Date.today + 2.years) - assert_equal Date.today + 2.years, Katello::RhelLifecycleStatus.maintenance_support_end_date(eos_schedule_index: release) + def test_maintenance_support_end_dates + assert_equal_arrays Katello::RhelLifecycleStatus::RHEL_EOS_SCHEDULE.keys, Katello::RhelLifecycleStatus.maintenance_support_end_dates.keys + Katello::RhelLifecycleStatus::RHEL_EOS_SCHEDULE.each do |release, schedule| + assert_equal schedule['maintenance_support'], Katello::RhelLifecycleStatus.maintenance_support_end_dates[release] + end end - def test_extended_support_end_date - fake_extended_support_end_date(Date.today + 2.years) - assert_equal Date.today + 2.years, Katello::RhelLifecycleStatus.extended_support_end_date(eos_schedule_index: release) + def test_extended_support_end_dates + assert_equal_arrays Katello::RhelLifecycleStatus::RHEL_EOS_SCHEDULE.keys, Katello::RhelLifecycleStatus.extended_support_end_dates.keys + Katello::RhelLifecycleStatus::RHEL_EOS_SCHEDULE.each do |release, schedule| + assert_equal schedule['extended_support'], Katello::RhelLifecycleStatus.extended_support_end_dates[release] + end end def test_eos_date