diff --git a/src/lib/y2network/config.rb b/src/lib/y2network/config.rb index 9745db461..0be85dce8 100644 --- a/src/lib/y2network/config.rb +++ b/src/lib/y2network/config.rb @@ -37,10 +37,8 @@ class Config attr_reader :id # @return [Array] attr_reader :interfaces - # @return [Array] - attr_reader :routing_tables # @return [Array] - attr_reader :routing_config + attr_reader :routing # @return [Symbol] Information source (see {Y2Network::Reader} and {Y2Network::Writer}) attr_reader :source @@ -55,15 +53,13 @@ def from(source) # Constructor # - # @param id [Symbol] Configuration ID - # @param interfaces [Array] List of interfaces - # @param routing_tables [Array] List of routing tables - # @param routing_config [Array] Routing configuration - def initialize(id: :system, interfaces:, routing_tables:, routing_config:, source:) + # @param id [Symbol] Configuration ID + # @param interfaces [Array] List of interfaces + # @param routing [Array] 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 @@ -73,7 +69,7 @@ def initialize(id: :system, interfaces:, routing_tables:, routing_config:, sourc # # @return [Array] 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 diff --git a/src/lib/y2network/config_reader/sysconfig.rb b/src/lib/y2network/config_reader/sysconfig.rb index a7856cc90..b778c3fa8 100644 --- a/src/lib/y2network/config_reader/sysconfig.rb +++ b/src/lib/y2network/config_reader/sysconfig.rb @@ -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 @@ -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. @@ -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] List of detected interfaces diff --git a/src/lib/y2network/config_writer/sysconfig.rb b/src/lib/y2network/config_writer/sysconfig.rb index 0d048bbca..0a2b2ec74 100644 --- a/src/lib/y2network/config_writer/sysconfig.rb +++ b/src/lib/y2network/config_writer/sysconfig.rb @@ -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 diff --git a/src/lib/y2network/routing_config.rb b/src/lib/y2network/routing_config.rb index 9ea894002..c6ac72dd1 100644 --- a/src/lib/y2network/routing_config.rb +++ b/src/lib/y2network/routing_config.rb @@ -20,14 +20,26 @@ module Y2Network # This class stores general routing configuration options, like IP forwarding # configuration. class RoutingConfig + # @return [Array] 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] List of routes which are defined in the configuration + def routes + tables.flat_map(&:routes) + end end end diff --git a/test/y2network/config_reader/sysconfig_test.rb b/test/y2network/config_reader/sysconfig_test.rb index eecd396dc..fca0d7cc7 100644 --- a/test/y2network/config_reader/sysconfig_test.rb +++ b/test/y2network/config_reader/sysconfig_test.rb @@ -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 @@ -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 diff --git a/test/y2network/config_test.rb b/test/y2network/config_test.rb index 35784bb94..2016fc21e 100644 --- a/test/y2network/config_test.rb +++ b/test/y2network/config_test.rb @@ -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 @@ -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) @@ -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 diff --git a/test/y2network/config_writer/sysconfig_test.rb b/test/y2network/config_writer/sysconfig_test.rb index 818b0c3ec..b91c75490 100644 --- a/test/y2network/config_writer/sysconfig_test.rb +++ b/test/y2network/config_writer/sysconfig_test.rb @@ -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 @@ -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 @@ -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 diff --git a/test/y2network/routing_config_test.rb b/test/y2network/routing_config_test.rb new file mode 100644 index 000000000..632c423b8 --- /dev/null +++ b/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