Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixes #36731 - Use facts, not operatingsystem name, to identify RHEL hosts #10738

Merged
merged 3 commits into from
Sep 28, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 25 additions & 2 deletions app/models/katello/concerns/host_managed_extensions.rb
Original file line number Diff line number Diff line change
Expand Up @@ -108,8 +108,6 @@ def remote_execution_proxies(provider, *_rest)
delegate :content_source_id, :single_content_view, :single_lifecycle_environment, :default_environment?, :single_content_view_environment?, :multi_content_view_environment?, :kickstart_repository_id, :bound_repositories,
:installable_errata, :installable_rpms, to: :content_facet, allow_nil: true

delegate :rhel_eos_schedule_index, to: :operatingsystem, allow_nil: true

has_many :content_view_environment_content_facets, through: :content_facet, class_name: 'Katello::ContentViewEnvironmentContentFacet'
has_many :content_view_environments, through: :content_view_environment_content_facets
has_many :content_views, through: :content_view_environments
Expand Down Expand Up @@ -524,6 +522,31 @@ def traces_helpers(search:)
::Katello::HostTracer.helpers_for(traces)
end

def probably_rhel?
# Get the os name from sub-man facts rather than operatingsystem. This is
# less likely to have been changed by the user.
os_name, = facts('distribution::name').values # only query for that one fact, then get its value
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm using facts('distribution::name') here instead of facts['distribution::name'] because this guarantees that the SQL query will only retrieve that one fact, not all facts for the host.

With the comma, this line is equivalent to

os_name = facts('distribution::name').values.first

but avoids the need for safe navigation (&.).

# if this fact isn't there, we can ignore it because the host is not "managed"
os_name.present? && os_name.start_with?('Red Hat Enterprise Linux')
end

def rhel_eos_schedule_index
return nil unless probably_rhel?
major = operatingsystem.major
return "RHEL#{major}" unless major == "7"
arch_name = architecture&.name
case arch_name
when "ppc64le"
"RHEL7 (POWER9)"
when "aarch64"
"RHEL7 (ARM)"
when "s390x"
"RHEL7 (System z (Structure A))"
else
"RHEL#{major}"
end
end

def package_names_for_job_template(action:, search:, versions: nil)
actions = %w(install remove update).freeze
case action
Expand Down
17 changes: 0 additions & 17 deletions app/models/katello/concerns/operatingsystem_extensions.rb
Original file line number Diff line number Diff line change
Expand Up @@ -44,23 +44,6 @@ def set_atomic_attributes
def atomic?
name.match(/.*atomic.*/i)
end

def rhel_eos_schedule_index(arch_name: nil)
return nil unless name == "RedHat" # using name and not title so we get specifically RHEL, not rebuilds
return "RHEL#{major}" unless major == "7"
case arch_name
when "x86_64", nil
"RHEL7"
when "ppc64le"
"RHEL7 (POWER9)"
when "aarch64"
"RHEL7 (ARM)"
when "s390x"
"RHEL7 (System z (Structure A))"
else
"RHEL#{major}"
end
end
end
end
end
Expand Down
6 changes: 3 additions & 3 deletions app/models/katello/rhel_lifecycle_status.rb
Original file line number Diff line number Diff line change
Expand Up @@ -179,7 +179,7 @@ def maintenance_support_end_date
end

def rhel_eos_schedule_index
host&.operatingsystem&.rhel_eos_schedule_index(arch_name: host&.arch&.name)
host&.rhel_eos_schedule_index
end

def to_global(_options = {})
Expand All @@ -195,12 +195,12 @@ def to_global(_options = {})
end

def to_status
self.class.to_status(rhel_eos_schedule_index: self.host&.operatingsystem&.rhel_eos_schedule_index)
self.class.to_status(rhel_eos_schedule_index: self.host&.rhel_eos_schedule_index)
end

# this status is only relevant for RHEL
def relevant?(_options = {})
host&.operatingsystem&.rhel_eos_schedule_index
host&.rhel_eos_schedule_index.present?
end
end
end
37 changes: 37 additions & 0 deletions test/models/concerns/host_managed_extensions_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -423,6 +423,43 @@ def test_search_known_traces
class HostRhelEosSchedulesTest < ActiveSupport::TestCase
let(:host) { FactoryBot.create(:host, :with_subscription) }

def test_probably_rhel?
host.expects(:facts).with('distribution::name').returns({'distribution::name' => 'Red Hat Enterprise Linux'})
assert host.probably_rhel?
end

def test_probably_not_rhel
host.expects(:facts).with('distribution::name').returns({'distribution::name' => 'CentOS'})
refute host.probably_rhel?
end

def test_rhel_eos_schedule_index
os = Operatingsystem.create!(:name => "RedHat", :major => "7", :minor => "3")
host.expects(:facts).at_least_once.returns({'distribution::name' => 'Red Hat Enterprise Linux Server'})
host.operatingsystem = os
host.architecture = architectures(:x86_64)
host.architecture.expects(:name).at_least_once.returns("x86_64")
assert_equal "RHEL7", host.rhel_eos_schedule_index
host.architecture.expects(:name).returns("ppc64le")
assert_equal "RHEL7 (POWER9)", host.rhel_eos_schedule_index
host.architecture.expects(:name).returns("aarch64")
assert_equal "RHEL7 (ARM)", host.rhel_eos_schedule_index
host.architecture.expects(:name).returns("s390x")
assert_equal "RHEL7 (System z (Structure A))", host.rhel_eos_schedule_index

os = Operatingsystem.create!(:name => "RedHat", :major => "6", :minor => "3")
host.expects(:facts).returns({'distribution::name' => 'Red Hat Enterprise Linux'})
host.operatingsystem = os
assert_equal "RHEL6", host.rhel_eos_schedule_index
end

def test_rhel_eos_schedule_index_non_rhel
os = Operatingsystem.create!(:name => "CentOS_Stream", :major => "8", :minor => "")
host.operatingsystem = os
host.expects(:facts).returns({'distribution::name' => 'CentOS Stream'})
assert_nil host.rhel_eos_schedule_index
end

def test_full_support_end_dates
host.expects(:rhel_eos_schedule_index).returns('RHEL9')
expected_date = ::Katello::RhelLifecycleStatus.full_support_end_dates['RHEL9']
Expand Down
16 changes: 0 additions & 16 deletions test/models/concerns/operatingsystem_extensions_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -37,21 +37,5 @@ def test_assign_template_for_atomic
assert_equal "x86_64", os.architectures.first.name
assert_equal "#{::Operatingsystem::REDHAT_ATOMIC_HOST_DISTRO_NAME} 7.3", os.description
end

def test_rhel_eos_schedule_index
os = Operatingsystem.create!(:name => "RedHat", :major => "7", :minor => "3")
assert_equal "RHEL7", os.rhel_eos_schedule_index
assert_equal "RHEL7 (POWER9)", os.rhel_eos_schedule_index(arch_name: "ppc64le")
assert_equal "RHEL7 (ARM)", os.rhel_eos_schedule_index(arch_name: "aarch64")
assert_equal "RHEL7 (System z (Structure A))", os.rhel_eos_schedule_index(arch_name: "s390x")

os = Operatingsystem.create!(:name => "RedHat", :major => "6", :minor => "3")
assert_equal "RHEL6", os.rhel_eos_schedule_index
end

def test_rhel_eos_schedule_index_non_rhel
os = Operatingsystem.create!(:name => "CentOS_Stream", :major => "8", :minor => "3")
assert_nil os.rhel_eos_schedule_index
end
end
end
15 changes: 7 additions & 8 deletions test/models/rhel_lifecycle_status_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ def fake_extended_support_end_date(date)
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)
host.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)
Expand All @@ -81,7 +81,7 @@ def test_to_status_full_support
def test_to_status_maintenance_support
os.hosts << host
host.operatingsystem.update(:name => "RedHat", :major => "9", :minor => "0")
host.operatingsystem.expects(:rhel_eos_schedule_index).returns(release)
host.expects(:rhel_eos_schedule_index).returns(release)
fake_full_support_end_date(Date.today - 1.year)
fake_maintenance_support_end_date(Date.today + 2.years)
fake_extended_support_end_date(Date.today + 10.years)
Expand All @@ -91,15 +91,15 @@ def test_to_status_maintenance_support
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)
host.expects(:rhel_eos_schedule_index).returns(release)
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

def test_to_status_extended_support
os.hosts << host
host.operatingsystem.update(:name => "RedHat", :major => "9", :minor => "0")
host.operatingsystem.expects(:rhel_eos_schedule_index).returns(release)
host.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 + 2.years)
Expand All @@ -109,7 +109,7 @@ def test_to_status_extended_support
def test_to_status_support_ended
os.hosts << host
host.operatingsystem.update(:name => "RedHat", :major => "9", :minor => "0")
host.operatingsystem.expects(:rhel_eos_schedule_index).returns(release)
host.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 - 1.year)
Expand Down Expand Up @@ -149,13 +149,12 @@ def test_eos_date_no_extended_support
end

def test_relevant
os.hosts << host
host.expects(:rhel_eos_schedule_index).returns('RHEL9')
assert status.relevant?
end

def test_relevant_non_rhel
os.update(:name => "CentOS_Stream")
os.hosts << host
host.expects(:rhel_eos_schedule_index).returns(nil)
refute status.relevant?
end
end
Expand Down
Loading