Skip to content

Commit

Permalink
Adapt ConfigReader to use ConnectionConfigsReader
Browse files Browse the repository at this point in the history
  • Loading branch information
imobachgs committed Feb 4, 2021
1 parent c8e9d7c commit 075ea0a
Show file tree
Hide file tree
Showing 2 changed files with 92 additions and 9 deletions.
42 changes: 36 additions & 6 deletions src/lib/y2network/wicked/config_reader.rb
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@
require "y2network/wicked/dns_reader"
require "y2network/wicked/hostname_reader"
require "y2network/wicked/interfaces_reader"
require "y2network/interfaces_collection"
require "y2network/wicked/connection_configs_reader"

Yast.import "NetworkInterfaces"
Yast.import "Host"
Expand All @@ -45,17 +45,22 @@ def config
# NOTE: /etc/hosts cache - nothing to do with /etc/hostname
Yast::Host.Read

routing_tables = find_routing_tables(interfaces_reader.interfaces)
interfaces = interfaces_reader.interfaces
s390_devices = interfaces_reader.s390_devices
connections = connections_configs_reader.connections(interfaces)
add_missing_interfaces(interfaces, connections)

routing_tables = find_routing_tables(interfaces)
routing = Routing.new(
tables: routing_tables,
forward_ipv4: sysctl_config_file.forward_ipv4,
forward_ipv6: sysctl_config_file.forward_ipv6
)

result = Config.new(
interfaces: interfaces_reader.interfaces,
connections: interfaces_reader.connections,
s390_devices: interfaces_reader.s390_devices,
interfaces: interfaces,
connections: connections,
s390_devices: s390_devices,
drivers: interfaces_reader.drivers,
routing: routing,
dns: dns,
Expand All @@ -71,11 +76,36 @@ def config

# Returns an interfaces reader instance
#
# @return [SysconfigInterfaces] Interfaces reader
# @return [InterfacesReader] Interfaces reader
def interfaces_reader
@interfaces_reader ||= Y2Network::Wicked::InterfacesReader.new
end

# @param interfaces [Y2Network::InterfacesCollection] Known interfaces
# @return [Y2Network::ConnectionConfigsCollection] Connection configurations collection
def connections_configs_reader
@connection_configs_reader ||= Y2Network::Wicked::ConnectionConfigsReader.new
end

# Adds missing interfaces from connections
#
# @param interfaces [Y2Network::InterfacesCollection] Known interfaces
# @param interfaces [Y2Network::ConnectionConfigsCollection] Known interfaces
def add_missing_interfaces(interfaces, connections)
connections.each do |conn|
interface = interfaces.by_name(conn.interface)
next if interface

missing_interface =
if conn.virtual?
VirtualInterface.from_connection(conn)
else
PhysicalInterface.new(conn.name, hardware: Hwinfo.for(conn.name))
end
interfaces << missing_interface
end
end

# Reads routes
#
# Merges routes from /etc/sysconfig/network/routes and /etc/sysconfig/network/ifroute-*
Expand Down
59 changes: 56 additions & 3 deletions test/y2network/wicked/config_reader_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,13 @@
let(:eth0) { Y2Network::Interface.new("eth0") }
let(:wlan0) { Y2Network::Interface.new("wlan0") }
let(:interfaces) { [eth0, wlan0] }
let(:eth0_config) { instance_double(Y2Network::ConnectionConfig::Ethernet) }
let(:connections) { [eth0_config] }
let(:eth0_conn) do
Y2Network::ConnectionConfig::Ethernet.new.tap do |conn|
conn.interface = "eth0"
conn.name = "eth0"
end
end
let(:connections) { [eth0_conn] }
let(:s390_devices) { [] }
let(:drivers) { Y2Network::Driver.new("virtio_net", "") }
let(:routes_file) { instance_double(CFA::RoutesFile, load: nil, routes: []) }
Expand All @@ -37,11 +42,16 @@
instance_double(
Y2Network::Wicked::InterfacesReader,
interfaces: Y2Network::InterfacesCollection.new(interfaces),
connections: connections,
s390_devices: s390_devices,
drivers: drivers
)
end
let(:connection_configs_reader) do
instance_double(
Y2Network::Wicked::ConnectionConfigsReader,
connections: connections
)
end

let(:dns) { double("Y2Network::Wicked::DNSReader") }
let(:hostname) { double("Y2Network::Wicked::HostnameReader") }
Expand All @@ -50,6 +60,8 @@
allow(Y2Network::Wicked::DNSReader).to receive(:new).and_return(dns_reader)
allow(Y2Network::Wicked::HostnameReader).to receive(:new).and_return(hostname_reader)
allow(Y2Network::Wicked::InterfacesReader).to receive(:new).and_return(interfaces_reader)
allow(Y2Network::Wicked::ConnectionConfigsReader).to receive(:new)
.and_return(connection_configs_reader)
end

around { |e| change_scr_root(File.join(DATA_PATH, "scr_read"), &e) }
Expand All @@ -60,6 +72,47 @@
expect(config.interfaces.map(&:name)).to eq(["eth0", "wlan0"])
end

it "returns a configuration including connection configurations" do
config = reader.config
expect(config.connections).to eq([eth0_conn])
end

context "when a connection for a not existing device is found" do
let(:connections) { [eth0_conn, extra_config] }

context "and it is a virtual connection" do
let(:extra_config) do
Y2Network::ConnectionConfig::Bridge.new.tap do |conn|
conn.interface = "br0"
conn.name = "br0"
conn.ports = ["eth0"]
end
end

it "creates a virtual interface" do
config = reader.config
bridge = config.interfaces.by_name("br0")
expect(bridge).to be_a Y2Network::VirtualInterface
end
end

context "and it is not a virtual connection" do
let(:extra_config) do
Y2Network::ConnectionConfig::Ethernet.new.tap do |conn|
conn.interface = "eth1"
conn.name = "eth1"
end
end

it "creates a not present physical interface" do
config = reader.config
eth1 = config.interfaces.by_name("eth1")
expect(eth1).to be_a Y2Network::PhysicalInterface
expect(eth1).to_not be_present
end
end
end

it "links routes with detected network devices" do
config = reader.config
route = config.routing.routes.find(&:interface)
Expand Down

0 comments on commit 075ea0a

Please sign in to comment.