Skip to content

Commit

Permalink
Merge 3bd003e into 174d81c
Browse files Browse the repository at this point in the history
  • Loading branch information
imobachgs committed Sep 3, 2019
2 parents 174d81c + 3bd003e commit b36f024
Show file tree
Hide file tree
Showing 10 changed files with 138 additions and 127 deletions.
38 changes: 0 additions & 38 deletions src/lib/y2network/fake_interface.rb

This file was deleted.

84 changes: 74 additions & 10 deletions src/lib/y2network/hwinfo.rb
Expand Up @@ -19,12 +19,48 @@

require "yast"
require "y2network/driver"
require "y2network/udev_rule"

Yast.import "LanItems"

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

# Returns the network devices hardware information
#
# @return [Array<Hwinfo>] Hardware information for netword devices
def netcards
return @netcards if @netcards
read_hardware
@netcards = ReadHardware("netcard").map do |attrs|
name = attrs["dev_name"]
extra_attrs = name ? extra_attrs_for(name) : {}
Hwinfo.new(attrs.merge(extra_attrs))
end
end

private

# Add aditional attributes
#
# @param name [String] Device name
# @return [Hash] Hash containing extra attributes
def extra_attrs_for(name)
extra = {}
raw_dev_port = Yast::SCR.Read(
Yast::Path.new(".target.string"), "/sys/class_net/#{name}/dev_port"
).to_s.strip
extra["dev_port"] = raw_dev_port unless raw_dev_port.empty?
extra
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 @@ -50,6 +86,20 @@ def for(name)
hwinfo_from_hardware(name) || hwinfo_from_udev(name) || Hwinfo.new
end

# Returns the network devices hardware information
#
# @return [Array<Hwinfo>] Hardware information for netword devices
def netcards
hardware_wrapper.netcards
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 +109,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 Expand Up @@ -141,7 +192,9 @@ def initialize(hwinfo = {})
{ name: "wl_enc_modes", default: nil },
{ name: "wl_channels", default: nil },
{ name: "wl_bitrates", default: nil },
{ name: "dev_port", default: nil }
{ name: "dev_port", 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 Expand Up @@ -184,6 +237,17 @@ def drivers
modules.map { |m| Driver.new(*m) }
end

# Determines whether the hardware is available (plugged)
#
# If the hardware layer was able to get its type, it consider the hardware to be connected. Bear
# in mind that it is not possible to just rely in #exists? because it could include some info
# from udev rules.
#
# @return [Boolean]
def present?
!!type
end

# Determines whether two objects are equivalent
#
# Ignores any element having a nil value.
Expand Down
3 changes: 1 addition & 2 deletions src/lib/y2network/interface.rb
Expand Up @@ -78,9 +78,8 @@ def initialize(name, type: InterfaceType::ETHERNET)
@name = name
@description = ""
@type = type
# TODO: move renaming logic to physical interfaces only
@renaming_mechanism = :none
# @hardware and @name should not change during life of the object
@hardware = Hwinfo.for(name)

init(name)
end
Expand Down
23 changes: 23 additions & 0 deletions src/lib/y2network/physical_interface.rb
Expand Up @@ -18,11 +18,34 @@
# find current contact information at www.suse.com.

require "y2network/interface"
require "y2network/hwinfo"

module Y2Network
# Physical interface class (ethernet, wireless, infiniband...)
class PhysicalInterface < Interface
# @return [String]
attr_accessor :ethtool_options

# Constructor
#
# @param name [String] Interface name (e.g., "eth0")
# @param type [InterfaceType] Interface type
# @param hardware [Hwinfo] Hardware information
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) || Hwinfo.new
@description = @hardware.name
end

# Determines whether the interface is present (attached)
#
# It relies in the hardware information
#
# @return [Boolean]
# @see Interface#present?
def present?
@hardware.present?
end
end
end
41 changes: 12 additions & 29 deletions src/lib/y2network/sysconfig/interfaces_reader.rb
Expand Up @@ -22,15 +22,12 @@
require "y2network/interface_type"
require "y2network/virtual_interface"
require "y2network/physical_interface"
require "y2network/fake_interface"
require "y2network/sysconfig/connection_config_reader"
require "y2network/interfaces_collection"
require "y2network/connection_configs_collection"
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 @@ -71,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 @@ -111,15 +93,11 @@ def find_connections

# Instantiates an interface given a hash containing hardware details
#
# @param data [Hash] hardware information
# @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"]
# @param hwinfo [Hash] hardware information
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 All @@ -144,8 +122,13 @@ def configured_devices
# @param conn [ConnectionConfig] Connection configuration related to the
# network interface
def add_interface(name, conn)
interface_class = conn.virtual? ? VirtualInterface : FakeInterface
@interfaces << interface_class.from_connection(name, conn)
interface =
if conn.virtual?
VirtualInterface.from_connection(name, conn)
else
PhysicalInterface.new(conn.name, hardware: Hwinfo.for(conn.name))
end
@interfaces << interface
end

# Detects the renaming mechanism used by the interface
Expand Down
2 changes: 0 additions & 2 deletions test/y2network/autoinst/config_reader_test.rb
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
40 changes: 0 additions & 40 deletions test/y2network/fake_interface_test.rb

This file was deleted.

21 changes: 20 additions & 1 deletion test/y2network/hwinfo_test.rb
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 Expand Up @@ -148,4 +149,22 @@
.to eq(described_class.new("dev_name" => "eth0"))
end
end

describe "#present?" do
context "when the hardware was detected" do
subject(:hwinfo) { described_class.new("type" => "eth") }

it "returns true" do
expect(hwinfo).to be_present
end
end

context "when the hardware was not detected" do
subject(:hwinfo) { described_class.new({}) }

it "returns false" do
expect(hwinfo).to_not be_present
end
end
end
end
Expand Up @@ -20,7 +20,6 @@
require_relative "../../../test_helper"

require "y2network/sysconfig/connection_config_writers/vlan"
require "y2network/fake_interface"
require "y2network/startmode"
require "y2network/boot_protocol"
require "y2network/connection_config/vlan"
Expand Down

0 comments on commit b36f024

Please sign in to comment.