Skip to content

Commit

Permalink
Add a class to store routing configuration
Browse files Browse the repository at this point in the history
* For the time being, it only holds IP forwarding configuration.
  • Loading branch information
imobachgs committed Mar 22, 2019
1 parent a3f3a1a commit 2a27a57
Show file tree
Hide file tree
Showing 6 changed files with 86 additions and 6 deletions.
6 changes: 5 additions & 1 deletion src/lib/y2network/config.rb
Expand Up @@ -39,6 +39,8 @@ class Config
attr_reader :interfaces
# @return [Array<RoutingTable>]
attr_reader :routing_tables
# @return [Array<RoutingConfig>]
attr_reader :routing_config
# @return [Symbol] Information source (see {Y2Network::Reader} and {Y2Network::Writer})
attr_reader :source

Expand All @@ -56,10 +58,12 @@ def from(source)
# @param id [Symbol] Configuration ID
# @param interfaces [Array<Interface>] List of interfaces
# @param routing_tables [Array<RoutingTable>] List of routing tables
def initialize(id: :system, interfaces:, routing_tables:, source:)
# @param routing_config [Array<RoutingConfig>] Routing configuration
def initialize(id: :system, interfaces:, routing_tables:, routing_config:, source:)
@id = id
@interfaces = interfaces
@routing_tables = routing_tables
@routing_config = routing_config
@source = source
end

Expand Down
16 changes: 14 additions & 2 deletions src/lib/y2network/config_reader/sysconfig.rb
Expand Up @@ -18,6 +18,7 @@
# find current contact information at www.suse.com.
require "y2network/config"
require "y2network/interface"
require "y2network/routing_config"
require "y2network/routing_table"
require "y2network/route"

Expand All @@ -31,8 +32,12 @@ class Sysconfig
# @return [Y2Network::Config] Network configuration
def config
interfaces = find_interfaces
routing_tables = find_routing_tables(interfaces)
Config.new(interfaces: interfaces, routing_tables: routing_tables, source: :sysconfig)
Config.new(
interfaces: interfaces,
routing_tables: find_routing_tables(interfaces),
routing_config: find_routing_config,
source: :sysconfig
)
end

private
Expand Down Expand Up @@ -67,6 +72,13 @@ def find_routing_tables(interfaces)
[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
33 changes: 33 additions & 0 deletions src/lib/y2network/routing_config.rb
@@ -0,0 +1,33 @@
# 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.
module Y2Network
# This class stores general routing configuration options, like IP forwarding
# configuration.
class RoutingConfig
# @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)
@forward_v4 = forward_v4
@forward_v6 = forward_v6
end
end
end
26 changes: 24 additions & 2 deletions test/y2network/config_reader/sysconfig_test.rb
Expand Up @@ -33,8 +33,10 @@
let(:routing) do
instance_double(
Yast::RoutingClass,
Read: nil,
Routes: [scr_route]
Read: nil,
Routes: [scr_route],
Forward_v4: forward_v4,
Forward_v6: forward_v6
)
end

Expand All @@ -47,6 +49,8 @@
let(:device) { "eth0" }
let(:gateway) { "192.168.122.1" }
let(:netmask) { "255.255.255.0" }
let(:forward_v4) { true }
let(:forward_v6) { true }

describe "#config" do
before do
Expand All @@ -72,6 +76,24 @@
expect(config.source).to eq(:sysconfig)
end

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
expect(routing.forward_v4).to eq(true)
end
end

context "when IP forwarding is disabled for IPv4" do
let(:forward_v4) { false }

it "sets the IPv4 forwarding to false in the routing configuration" do
routing = reader.config.routing_config
expect(routing.forward_v4).to eq(false)
end
end
end

context "when there is not gateway" do
let(:gateway) { "-" }

Expand Down
9 changes: 8 additions & 1 deletion test/y2network/config_test.rb
Expand Up @@ -18,20 +18,27 @@
# find current contact information at www.suse.com.
require_relative "../test_helper"
require "y2network/config"
require "y2network/routing_config"
require "y2network/routing_table"
require "y2network/interface"
require "y2network/config_writer/sysconfig"

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

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

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

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

Expand Down
2 changes: 2 additions & 0 deletions test/y2network/config_writer/sysconfig_test.rb
Expand Up @@ -31,6 +31,7 @@
Y2Network::Config.new(
interfaces: [eth0],
routing_tables: [routing_table],
routing_config: routing_config,
source: :sysconfig
)
end
Expand All @@ -43,6 +44,7 @@
gateway: IPAddr.new("192.168.122.1")
)
end
let(:routing_config) { Y2Network::RoutingConfig }
let(:routing_table) { Y2Network::RoutingTable.new([route]) }

it "imports defined routes using placeholders" do
Expand Down

0 comments on commit 2a27a57

Please sign in to comment.