Skip to content

Commit

Permalink
WIP
Browse files Browse the repository at this point in the history
  • Loading branch information
imobachgs committed Sep 2, 2019
1 parent 9b9b756 commit 0368041
Show file tree
Hide file tree
Showing 5 changed files with 29 additions and 28 deletions.
19 changes: 18 additions & 1 deletion src/lib/y2network/hwinfo.rb
Original file line number Diff line number Diff line change
Expand Up @@ -21,17 +21,24 @@
require "y2network/driver"
require "y2network/udev_rule"

Yast.import "LanItems"

module Y2Network
class HardwareWrapper
def initialize
Yast.include self, "network/routines.rb"
end

def reset
@netcards = nil
end

# Returns the network devices hardware information
#
# @return [Array<Hwinfo>]
def netcards
return @netcards if @netcards
read_hardware
@netcards = ReadHardware("netcard").map do |attrs|
add_missing_attrs(attrs)
Hwinfo.new(attrs)
Expand All @@ -48,6 +55,11 @@ def add_missing_attrs(hash)
).to_s.strip
hash["dev_port"] = raw_dev_port unless raw_dev_port.empty?
end

# Makes sure that the hardware information was read
def read_hardware
Yast::LanItems.ReadHw if Yast::LanItems.Hardware.empty?
end
end

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

def netcards
hardware_wrapper.netcards
end

# Resets the hardware information
#
# It will be re-read the next time is needed.
Expand Down Expand Up @@ -173,7 +189,8 @@ def initialize(hwinfo = {})
{ name: "wl_channels", default: nil },
{ name: "wl_bitrates", default: nil },
{ name: "dev_port", default: nil },
{ name: "type", default: nil }
{ name: "type", default: nil },
{ name: "name", default: "" }
].each do |hwinfo_item|
define_method hwinfo_item[:name].downcase do
@hwinfo ? @hwinfo.fetch(hwinfo_item[:name], hwinfo_item[:default]) : hwinfo_item[:default]
Expand Down
3 changes: 2 additions & 1 deletion src/lib/y2network/physical_interface.rb
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,8 @@ class PhysicalInterface < Interface
def initialize(name, type: InterfaceType::ETHERNET, hardware: nil)
super(name, type: type)
# @hardware and @name should not change during life of the object
@hardware = hardware || Hwinfo.for(name)
@hardware = hardware || Hwinfo.for(name) || Hwinfo.new
@description = @hardware.name
end

# Determines whether the interface is present (attached)
Expand Down
26 changes: 4 additions & 22 deletions src/lib/y2network/sysconfig/interfaces_reader.rb
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,6 @@
require "y2network/sysconfig/type_detector"
require "y2network/udev_rule"

Yast.import "LanItems"

module Y2Network
module Sysconfig
# This class reads interfaces configuration from sysconfig files
Expand Down Expand Up @@ -70,29 +68,14 @@ def interfaces
private

# Finds the physical interfaces
#
# Physical interfaces are read from the old LanItems module
def find_physical_interfaces
return if @interfaces
physical_interfaces = hardware.map do |h|
physical_interfaces = Hwinfo.netcards.map do |h|
build_physical_interface(h)
end
@interfaces = Y2Network::InterfacesCollection.new(physical_interfaces)
end

# Returns hardware information
#
# This method makes sure that the hardware information was read.
#
# @todo It still relies on Yast::LanItems.Hardware
#
# @return [Array<Hash>] Hardware information
def hardware
Yast::LanItems.Hardware unless Yast::LanItems.Hardware.empty?
Yast::LanItems.ReadHw # try again if no hardware was found
Yast::LanItems.Hardware
end

# Finds the connections configurations
def find_connections
@connections ||=
Expand All @@ -114,11 +97,10 @@ def find_connections
# @option data [String] "dev_name" Device name ("eth0")
# @option data [String] "name" Device description
# @option data [String] "type" Device type ("eth", "wlan", etc.)
def build_physical_interface(data)
Y2Network::PhysicalInterface.new(data["dev_name"]).tap do |iface|
iface.description = data["name"]
def build_physical_interface(hwinfo)
Y2Network::PhysicalInterface.new(hwinfo.dev_name, hardware: hwinfo).tap do |iface|
iface.renaming_mechanism = renaming_mechanism_for(iface.name)
iface.type = InterfaceType.from_short_name(data["type"]) || TypeDetector.type_of(iface.name)
iface.type = InterfaceType.from_short_name(hwinfo.type) || TypeDetector.type_of(iface.name)
end
end

Expand Down
2 changes: 0 additions & 2 deletions test/y2network/autoinst/config_reader_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -62,8 +62,6 @@

describe "#config" do
it "builds a new Y2Network::Config from a Y2Networking::Section" do
# TODO: mock hardware properly
allow_any_instance_of(Y2Network::Sysconfig::InterfacesReader).to receive(:hardware).and_return([])
expect(subject.config).to be_a Y2Network::Config
expect(subject.config.routing).to be_a Y2Network::Routing
expect(subject.config.dns).to be_a Y2Network::DNS
Expand Down
7 changes: 5 additions & 2 deletions test/y2network/sysconfig/interfaces_reader_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -38,16 +38,19 @@
Y2Network::UdevRule.new(
[
Y2Network::UdevRulePart.new("ATTR{address}", "==", "00:12:34:56:78"),
Y2Network::UdevRulePart.new("ACTION", "=", "eth0")
Y2Network::UdevRulePart.new("NAME", "=", "eth0")
]
)
end

let(:configured_interfaces) { ["lo", "eth0"] }
let(:hardware_wrapper) { Y2Network::HardwareWrapper.new }

TYPES = { "eth0" => "eth" }.freeze

before do
allow(Yast::LanItems).to receive(:Hardware).and_return(netcards)
allow(hardware_wrapper).to receive(:ReadHardware).and_return(netcards)
allow(Y2Network::HardwareWrapper).to receive(:new).and_return(hardware_wrapper)
allow(Yast::SCR).to receive(:Dir).with(Yast::Path.new(".network.section"))
.and_return(configured_interfaces)
allow(Yast::SCR).to receive(:Dir).and_call_original
Expand Down

0 comments on commit 0368041

Please sign in to comment.