Bug 2196540

Summary: "Host-Registered Content Hosts" Report gives error while generating - undefined method `nvra' for nil:NilClass
Product: Red Hat Satellite Reporter: vivek singh <vivsingh>
Component: ReportingAssignee: Samir Jha <sajha>
Status: VERIFIED --- QA Contact: Lucy Fu <lufu>
Severity: high Docs Contact:
Priority: high    
Version: 6.13.0CC: ahumbe, iballou, lufu, pcreech, sajha, saydas
Target Milestone: 6.14.0Keywords: EasyFix, Regression, Triaged
Target Release: Unused   
Hardware: Unspecified   
OS: Unspecified   
Whiteboard:
Fixed In Version: Doc Type: If docs needed, set a value
Doc Text:
Story Points: ---
Clone Of:
: 2218660 (view as bug list) Environment:
Last Closed: Type: Bug
Regression: --- Mount Type: ---
Documentation: --- CRM:
Verified Versions: Category: ---
oVirt Team: --- RHEL 7.3 requirements from Atomic Host:
Cloudforms Team: --- Target Upstream Version:
Embargoed:

Description vivek singh 2023-05-09 12:43:05 UTC
Description of problem: 

 While generating Host-Registered Content Hosts report template getting error "undefined method `nvra' for nil:NilClass" 


Version-Release number of selected component (if applicable): Satellite 6.13.0 


How reproducible: always 


Steps to Reproduce:
1. Register hosts to satellite 

2. Go to webUI > monitor > Report-templates > click generate in front of "Host-Registered Content Hosts" > select date/time and generate . 

 

Actual results: Report not generated and giving error .


Expected results: Report should generate and show all registered hosts details 


Additional info:

Comment 1 Sayan Das 2023-05-09 12:45:38 UTC
Analysis:

Old code: ( 6.12 and before )

      def host_latest_applicable_rpm_version(host, package)
        host.applicable_rpms.where(name: package).order(:version_sortable).limit(1).pluck(:nvra).first
      end


New Code: ( > = 6.13 )

      def host_latest_applicable_rpm_version(host, package)
        ::Katello::Rpm.latest(host.applicable_rpms.where(name: package)).first.nvra
      end

Test of new code:

irb(main):004:0> host = Host.second
=> #<Host::Managed id: 2, name: "dhcp130-139.example.com", last_compile: "2023-05-09 09:35:24.000000000 +0000", last_rep...

irb(main):007:0> package = 'kernel'
=> "kernel"


irb(main):008:0> ::Katello::Rpm.latest(host.applicable_rpms.where(name: package)).first.nvra
Traceback (most recent call last):
        2: from lib/tasks/console.rake:5:in `block in <top (required)>'
        1: from (irb):8
NoMethodError (undefined method `nvra' for nil:NilClass)

irb(main):009:0> ::Katello::Rpm.latest(host.applicable_rpms.where(name: package)).first
=> nil

irb(main):011:0> ::Katello::Rpm.latest(host.applicable_rpms.where(name: package)).first.try(:nvra)
=> nil

Basically, If the host.applicable_rpms have no results or host.applicable_rpms.where(name: package)).first have no results, we run into this and if we use the try method , that helps to get past the issue


So following, fixes the issue:

diff --git a/app/lib/katello/concerns/base_template_scope_extensions.rb b/app/lib/katello/concerns/base_template_scope_extensions.rb
index 873c7bf..6cfe8f8 100644
--- a/app/lib/katello/concerns/base_template_scope_extensions.rb
@@ -161,7 +161,7 @@ module Katello
         returns String, desc: 'Package version'
       end
       def host_latest_applicable_rpm_version(host, package)
-        ::Katello::Rpm.latest(host.applicable_rpms.where(name: package)).first.nvra
+        ::Katello::Rpm.latest(host.applicable_rpms.where(name: package)).first.try(:nvra)
       end
 
       apipie :method, 'Loads Pool objects' do

Comment 2 Sayan Das 2023-05-09 12:53:37 UTC
I am guessing this also fixes the issue instead of what I tried in Coment 1 

https://github.com/Katello/katello/pull/10497/files#diff-97789298ab25018d9ce64de6b68fa6cfcc0b066e351c4156ebd5d70176022281R182-R186

      def host_latest_applicable_rpm_version(host, package)
        return [] if host.content_facet.nil?
        applicable = ::Katello::Rpm.latest(host.applicable_rpms.where(name: package))
        applicable.present? ? applicable.first.nvra : []
      end


This change is scheduled to be part of Satellite 6.14 along with the inclusion Installable ERRATA template 

But perhaps we should consider this or part of the fix for 6.13.z or else many customers could end up hitting this issue when a host would not have any applicable rpms 


CC'ing Ian from the Katello team

Comment 3 Ian Ballou 2023-05-09 13:44:51 UTC
We can use the same code from the "Installable ERRATA template" RFE but we'll need something that only applies to the 6.13 / Katello 4.7 branches.

Comment 4 Ian Ballou 2023-05-09 13:57:26 UTC
To complete this issue I'd recommend making a Katello 4.7 issue upstream and then create the PR against the Katello 4.7 branch only. That should ensure that, next time there's a Katello 4.8 release, it'll be marked as a cherry-pick.

Comment 5 Samir Jha 2023-06-07 16:17:38 UTC
Created redmine issue https://projects.theforeman.org/issues/36486 from this bug

Comment 6 Bryan Kearney 2023-06-12 16:03:11 UTC
Moving this bug to POST for triage into Satellite since the upstream issue https://projects.theforeman.org/issues/36486 has been resolved.