Skip to content

Commit

Permalink
[WIP] Read routes
Browse files Browse the repository at this point in the history
  • Loading branch information
imobachgs committed Mar 19, 2019
1 parent 0a8e481 commit 4a4c13e
Show file tree
Hide file tree
Showing 5 changed files with 60 additions and 10 deletions.
9 changes: 8 additions & 1 deletion src/lib/y2network/config.rb
Expand Up @@ -28,6 +28,8 @@ module Y2Network
class Config
# @return [Array<Interface>]
attr_reader :interfaces
# @return [Array<RoutingTable>]
attr_reader :routing_tables

# @param source [Symbol] Source to read the configuration from
class << self
Expand All @@ -38,8 +40,13 @@ def from(source)
end

# @param devices [Array<Device>]
def initialize(interfaces:)
def initialize(interfaces:, routing_tables:)
@interfaces = interfaces
@routing_tables = routing_tables
end

def routes
routing_tables.flat_map(&:routes)
end
end
end
25 changes: 22 additions & 3 deletions src/lib/y2network/config_reader/sysconfig.rb
Expand Up @@ -17,25 +17,44 @@
# To contact SUSE LLC about this file by physical or electronic mail, you may
# find current contact information at www.suse.com.
require "y2network/config"
require "y2network/interface"
require "y2network/routing_table"

Yast.import "NetworkInterfaces"
Yast.import "Routing"

module Y2Network
module ConfigReader
# This class reads the current configuration from the system
class Sysconfig
# @return [Y2Network::Config] Network configuration
def config
Yast::NetworkInterfaces.Read
Config.new(interfaces: interfaces)
interfaces = find_interfaces
routing_tables = find_routing_tables(interfaces)
Config.new(interfaces: interfaces, routing_tables: routing_tables)
end

def interfaces
private

def find_interfaces
Yast::NetworkInterfaces.Read
# TODO: for the time being, we are just relying in the underlying stuff.
Yast::NetworkInterfaces.List("").map do |name|
Y2Network::Interface.new(name)
end
end

def find_routing_tables(interfaces)
Yast::Routing.Read

routes = Yast::Routing.Routes.map do |route|
dest = IPAddr.new(route["destination"]).mask(route["netmask"])
iface = interfaces.find { |i| i.name == route["device"] }
Y2Network::Route.new(dest, iface, gateway: IPAddr.new(route["gateway"]))
end
table = Y2Network::RoutingTable.new(routes)
[table]
end
end
end
end
8 changes: 4 additions & 4 deletions src/lib/y2network/route.rb
Expand Up @@ -21,16 +21,16 @@ module Y2Network
class Route
# @return [IPAddr]
attr_reader :to
# @return [Device]
attr_reader :device
# @return [Interface]
attr_reader :interface
# @return [IPAddr,nil]
attr_reader :source
# @return [IPAddr,nil]
attr_reader :gateway

def initialize(to, device, gateway: nil, source: nil, preference: nil)
def initialize(to, interface, gateway: nil, source: nil, preference: nil)
@to = to
@device = device
@interface = interface
@gateway = gateway
@source = source
@preference = preference
Expand Down
4 changes: 2 additions & 2 deletions src/lib/y2network/routing_table.rb
Expand Up @@ -22,9 +22,9 @@ class RoutingTable

# MAIN_TABLE_ID = 254

def initialize
def initialize(routes = [])
# @id = MAIN_TABLE_ID
@routes = []
@routes = routes
end

# @param route [Y2Network::Route] Route to add
Expand Down
24 changes: 24 additions & 0 deletions test/y2network/config_reader/sysconfig_test.rb
Expand Up @@ -30,14 +30,38 @@
)
end

let(:routing) do
instance_double(
Yast::RoutingClass,
Read: nil,
Routes: [
{
"destination" => "192.168.122.0", "device" => "eth0",
"gateway" => "192.168.122.1", "netmask" => "255.255.255.0"
}
]
)
end

# FIXME: "gateway" => "-", "netmask" => "-"

describe "" do
before do
stub_const("Yast::NetworkInterfaces", network_interfaces)
stub_const("Yast::Routing", routing)
end

it "returns a configuration including network devices" do
config = reader.config
expect(config.interfaces.map(&:name)).to eq(["lo", "eth0", "wlan0"])
end

it "returns a configuration including network devices" do
config = reader.config
expect(config.routes.size).to eq(1)
route = config.routes.first
expect(route.to).to eq(IPAddr.new("192.168.122.0/24"))
expect(route.interface.name).to eq("eth0")
end
end
end

0 comments on commit 4a4c13e

Please sign in to comment.