Skip to content

Commit

Permalink
Move routing tables into RoutingConfig class
Browse files Browse the repository at this point in the history
  • Loading branch information
imobachgs committed Mar 22, 2019
1 parent 2a27a57 commit 8be64b0
Show file tree
Hide file tree
Showing 8 changed files with 93 additions and 40 deletions.
18 changes: 7 additions & 11 deletions src/lib/y2network/config.rb
Expand Up @@ -37,10 +37,8 @@ class Config
attr_reader :id
# @return [Array<Interface>]
attr_reader :interfaces
# @return [Array<RoutingTable>]
attr_reader :routing_tables
# @return [Array<RoutingConfig>]
attr_reader :routing_config
attr_reader :routing
# @return [Symbol] Information source (see {Y2Network::Reader} and {Y2Network::Writer})
attr_reader :source

Expand All @@ -55,15 +53,13 @@ def from(source)

# Constructor
#
# @param id [Symbol] Configuration ID
# @param interfaces [Array<Interface>] List of interfaces
# @param routing_tables [Array<RoutingTable>] List of routing tables
# @param routing_config [Array<RoutingConfig>] Routing configuration
def initialize(id: :system, interfaces:, routing_tables:, routing_config:, source:)
# @param id [Symbol] Configuration ID
# @param interfaces [Array<Interface>] List of interfaces
# @param routing [Array<RoutingConfig>] Routing configuration
def initialize(id: :system, interfaces:, routing:, source:)
@id = id
@interfaces = interfaces
@routing_tables = routing_tables
@routing_config = routing_config
@routing = routing
@source = source
end

Expand All @@ -73,7 +69,7 @@ def initialize(id: :system, interfaces:, routing_tables:, routing_config:, sourc
#
# @return [Array<Route>] List of routes which are defined in the configuration
def routes
routing_tables.flat_map(&:to_a)
routing.routes
end

# Writes the configuration into the YaST modules
Expand Down
25 changes: 13 additions & 12 deletions src/lib/y2network/config_reader/sysconfig.rb
Expand Up @@ -33,10 +33,9 @@ class Sysconfig
def config
interfaces = find_interfaces
Config.new(
interfaces: interfaces,
routing_tables: find_routing_tables(interfaces),
routing_config: find_routing_config,
source: :sysconfig
interfaces: interfaces,
routing: find_routing_config(interfaces),
source: :sysconfig
)
end

Expand All @@ -57,6 +56,16 @@ def find_interfaces
end
end

def find_routing_config(interfaces)
Yast::Routing.Read
tables = find_routing_tables(interfaces)
Y2Network::RoutingConfig.new(
tables: tables,
forward_v4: Yast::Routing.Forward_v4,
forward_v6: Yast::Routing.Forward_v6
)
end

# Find routing tables
#
# @note For the time being, only one routing table is considered.
Expand All @@ -66,19 +75,11 @@ def find_interfaces
#
# @see Yast::Routing.Routes
def find_routing_tables(interfaces)
Yast::Routing.Read
routes = Yast::Routing.Routes.map { |h| build_route(interfaces, h) }
table = Y2Network::RoutingTable.new(routes)
[table]
end

def find_routing_config
Y2Network::RoutingConfig.new(
forward_v4: Yast::Routing.Forward_v4,
forward_v6: Yast::Routing.Forward_v6
)
end

# Build a route given a hash from the SCR agent
#
# @param interfaces [Array<Interface>] List of detected interfaces
Expand Down
2 changes: 1 addition & 1 deletion src/lib/y2network/config_writer/sysconfig.rb
Expand Up @@ -31,7 +31,7 @@ class Sysconfig
#
# @param config [Y2Network::Config] Configuration to write
def write(config)
routes = config.routes.map { |r| route_to_hash(r) }
routes = config.routing.routes.map { |r| route_to_hash(r) }
Yast::Routing.Import("routes" => routes)
end

Expand Down
14 changes: 13 additions & 1 deletion src/lib/y2network/routing_config.rb
Expand Up @@ -20,14 +20,26 @@ module Y2Network
# This class stores general routing configuration options, like IP forwarding
# configuration.
class RoutingConfig
# @return [Array<RoutingTable>] List of routing tables
attr_accessor :tables
# @return [Boolean] Whether forwarding for IPv4 is enabled
attr_accessor :forward_v4
# @return [Boolean] Whether forwarding for IPv6 is enabled
attr_accessor :forward_v6

def initialize(forward_v4: false, forward_v6: false)
def initialize(tables: [], forward_v4: false, forward_v6: false)
@tables = tables
@forward_v4 = forward_v4
@forward_v6 = forward_v6
end

# Routes in the configuration
#
# Convenience method to iterate through the routes in all routing tables.
#
# @return [Array<Route>] List of routes which are defined in the configuration
def routes
tables.flat_map(&:routes)
end
end
end
4 changes: 2 additions & 2 deletions test/y2network/config_reader/sysconfig_test.rb
Expand Up @@ -79,7 +79,7 @@
describe "routing settings" do
context "when IP forwarding is enabled for IPv4" do
it "sets the IPv4 forwarding to true in the routing configuration" do
routing = reader.config.routing_config
routing = reader.config.routing
expect(routing.forward_v4).to eq(true)
end
end
Expand All @@ -88,7 +88,7 @@
let(:forward_v4) { false }

it "sets the IPv4 forwarding to false in the routing configuration" do
routing = reader.config.routing_config
routing = reader.config.routing
expect(routing.forward_v4).to eq(false)
end
end
Expand Down
14 changes: 6 additions & 8 deletions test/y2network/config_test.rb
Expand Up @@ -21,15 +21,15 @@
require "y2network/routing_config"
require "y2network/routing_table"
require "y2network/interface"
require "y2network/config_reader/sysconfig"
require "y2network/config_writer/sysconfig"

describe Y2Network::Config do
subject(:config) do
described_class.new(
interfaces: [eth0],
routing_tables: routing_tables,
routing_config: routing_config,
source: :sysconfig
interfaces: [eth0],
routing: routing,
source: :sysconfig
)
end

Expand All @@ -38,12 +38,10 @@

let(:table1) { Y2Network::RoutingTable.new([route1]) }
let(:table2) { Y2Network::RoutingTable.new([route2]) }
let(:routing_config) { double("Y2Network::RoutingConfig")}
let(:routing) { instance_double(Y2Network::RoutingConfig, routes: [route1, route2]) }

let(:eth0) { Y2Network::Interface.new("eth0") }

let(:routing_tables) { [table1, table2] }

describe ".from" do
let(:reader) do
instance_double(Y2Network::ConfigReader::Sysconfig, config: config)
Expand All @@ -60,7 +58,7 @@
end

describe "#routes" do
it "returns routes from all tables" do
it "returns routes" do
expect(config.routes).to eq([route1, route2])
end
end
Expand Down
10 changes: 5 additions & 5 deletions test/y2network/config_writer/sysconfig_test.rb
Expand Up @@ -21,6 +21,7 @@
require "y2network/config"
require "y2network/interface"
require "y2network/route"
require "y2network/routing_config"
require "y2network/routing_table"

describe Y2Network::ConfigWriter::Sysconfig do
Expand All @@ -29,10 +30,9 @@
describe "#write" do
let(:config) do
Y2Network::Config.new(
interfaces: [eth0],
routing_tables: [routing_table],
routing_config: routing_config,
source: :sysconfig
interfaces: [eth0],
routing: routing,
source: :sysconfig
)
end

Expand All @@ -44,7 +44,7 @@
gateway: IPAddr.new("192.168.122.1")
)
end
let(:routing_config) { Y2Network::RoutingConfig }
let(:routing) { Y2Network::RoutingConfig.new(tables: [routing_table]) }
let(:routing_table) { Y2Network::RoutingTable.new([route]) }

it "imports defined routes using placeholders" do
Expand Down
46 changes: 46 additions & 0 deletions test/y2network/routing_config_test.rb
@@ -0,0 +1,46 @@
# Copyright (c) [2019] SUSE LLC
#
# All Rights Reserved.
#
# This program is free software; you can redistribute it and/or modify it
# under the terms of version 2 of the GNU General Public License as published
# by the Free Software Foundation.
#
# This program is distributed in the hope that it will be useful, but WITHOUT
# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
# FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
# more details.
#
# You should have received a copy of the GNU General Public License along
# with this program; if not, contact SUSE LLC.
#
# To contact SUSE LLC about this file by physical or electronic mail, you may
# find current contact information at www.suse.com.
require_relative "../test_helper"
require "y2network/routing_config"

describe Y2Network::RoutingConfig do
subject(:config) do
described_class.new(
tables: tables
)
end

let(:route1) { double("Y2Network::Route") }
let(:route2) { double("Y2Network::Route") }

let(:table1) { Y2Network::RoutingTable.new([route1]) }
let(:table2) { Y2Network::RoutingTable.new([route2]) }

let(:tables) { [table1, table2] }

describe "#forward_v4"

describe "#forward_v6"

describe "#routes" do
it "returns routes from all tables" do
expect(config.routes).to eq([route1, route2])
end
end
end

0 comments on commit 8be64b0

Please sign in to comment.