Skip to content

Commit

Permalink
Cache hardware information
Browse files Browse the repository at this point in the history
  • Loading branch information
imobachgs committed Sep 2, 2019
1 parent 8270382 commit e793f66
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 10 deletions.
48 changes: 39 additions & 9 deletions src/lib/y2network/hwinfo.rb
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,28 @@ class HardwareWrapper
def initialize
Yast.include self, "network/routines.rb"
end

# Returns the network devices hardware information
#
# @return [Array<Hwinfo>]
def netcards
return @netcards if @netcards
@netcards = ReadHardware("netcard").map do |attrs|
add_missing_attrs(attrs)
Hwinfo.new(attrs)
end
end

private

def add_missing_attrs(hash)
name = hash["dev_name"]
return unless name
raw_dev_port = Yast::SCR.Read(
Yast::Path.new(".target.string"), "/sys/class_net/#{name}/dev_port"
).to_s.strip
hash["dev_port"] = raw_dev_port unless raw_dev_port.empty?
end
end

# Stores useful (from networking POV) items of hwinfo for an interface
Expand All @@ -50,6 +72,13 @@ def for(name)
hwinfo_from_hardware(name) || hwinfo_from_udev(name) || Hwinfo.new
end

# Resets the hardware information
#
# It will be re-read the next time is needed.
def reset
@hardware_wrapper = nil
end

private

# Returns hardware information for the given device
Expand All @@ -59,15 +88,16 @@ def for(name)
# @param name [String] Interface's name
# @return [Hwinfo,nil] Hardware info or nil if not found
def hwinfo_from_hardware(name)
netcards = HardwareWrapper.new.ReadHardware("netcard")
hw = netcards.find { |h| h["dev_name"] == name }
return nil if hw.nil?

raw_dev_port = Yast::SCR.Read(
Yast::Path.new(".target.string"), "/sys/class_net/#{name}/dev_port"
).to_s.strip
hw["dev_port"] = raw_dev_port unless raw_dev_port.empty?
new(hw)
hardware_wrapper.netcards.find { |h| h.dev_name == name }
end

# Hardware wrapper instance
#
# It memoizes the hardware wrapper in order to speed up the access
#
# @return [HardWrapper]
def hardware_wrapper
@hardware_wrapper = HardwareWrapper.new
end

# Returns hardware information for the given device
Expand Down
3 changes: 2 additions & 1 deletion test/y2network/hwinfo_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -28,12 +28,13 @@
end

let(:interface_name) { "enp1s0" }
let(:hw_wrapper) { double("Y2Network::HardwareWrapper", ReadHardware: hardware) }
let(:hw_wrapper) { Y2Network::HardwareWrapper.new }

before do
allow(Y2Network::Hwinfo).to receive(:hwinfo_from_hardware).and_call_original
allow(Y2Network::HardwareWrapper).to receive(:new).and_return(hw_wrapper)
allow(Y2Network::UdevRule).to receive(:find_for).with(interface_name).and_return(udev_rule)
allow(hw_wrapper).to receive(:ReadHardware).and_return(hardware)
end

let(:udev_rule) { nil }
Expand Down

0 comments on commit e793f66

Please sign in to comment.