Browse Source

Fixes #5402 - Type Error in report page.

Co-authored-by: Dominik Klein <dk@zammad.com>
Rolf Schmidt 3 months ago
parent
commit
5045c3861b

+ 7 - 7
app/models/system_report/plugin/hardware.rb

@@ -12,7 +12,7 @@ class SystemReport::Plugin::Hardware < SystemReport::Plugin
   end
 
   def total_memory
-    open3_data&.dig('children')&.find { |entry| entry['description'] == 'Motherboard' }&.dig('children')&.find { |entry| entry['description'] == 'System memory' }&.dig('size')
+    open3_data&.dig('children')&.find { |entry| entry['description'].downcase == 'motherboard' }&.dig('children')&.find { |entry| entry['description'].downcase == 'system memory' }&.dig('size')
   end
 
   def df_zammad_root
@@ -26,6 +26,7 @@ class SystemReport::Plugin::Hardware < SystemReport::Plugin
 
     data = execute
     return {} if data.blank?
+    return data.first if data.is_a?(Array) # https://github.com/zammad/zammad/issues/5402
 
     data
   end
@@ -39,12 +40,11 @@ class SystemReport::Plugin::Hardware < SystemReport::Plugin
       return {}
     end
 
-    begin
-      JSON.parse(stdout)
-    rescue
-      Rails.logger.error("lshw failed: #{stdout}")
-      {}
-    end
+    JSON.parse(stdout)
+  rescue => e
+    Rails.logger.error "lshw failed: #{e.message}"
+    Rails.logger.error e
+    {}
   end
 
   def binary_path

+ 16 - 0
spec/models/system_report/plugin/hardware_spec.rb

@@ -0,0 +1,16 @@
+# Copyright (C) 2012-2024 Zammad Foundation, https://zammad-foundation.org/
+
+require 'rails_helper'
+
+RSpec.describe SystemReport::Plugin::Hardware, current_user_id: 1, type: :model do
+  it 'does calculate total memory' do
+    expect(described_class.new.fetch['total_memory'].class).to eq(Integer)
+  end
+
+  it 'does also work when the open result is an array #5402', :aggregate_failures do
+    instance = described_class.new
+    result = described_class.new.send(:execute)
+    allow(instance).to receive(:execute).and_return(Array.wrap(result))
+    expect { instance.fetch }.not_to raise_error
+  end
+end