Skip to content

Commit

Permalink
Decouple Yast::Lan from Yast::Routing
Browse files Browse the repository at this point in the history
  • Loading branch information
imobachgs committed Mar 29, 2019
1 parent e81bbc8 commit 422df58
Show file tree
Hide file tree
Showing 8 changed files with 80 additions and 36 deletions.
5 changes: 3 additions & 2 deletions src/lib/y2network/config.rb
Original file line number Diff line number Diff line change
Expand Up @@ -43,8 +43,9 @@ class Config

class << self
# @param source [Symbol] Source to read the configuration from
def from(source)
reader = ConfigReader.for(source)
# @param opts [Hash] Reader options
def from(source, opts = {})
reader = ConfigReader.for(source, opts)
reader.config
end
end
Expand Down
3 changes: 2 additions & 1 deletion src/lib/y2network/config_reader.rb
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,9 @@ module ConfigReader
# Config reader for a given source
#
# @param source [Symbol] Source name (e.g., :sysconfig)
# @param opts [Hash] Reader options
# @return [#config] Configuration reader from {Y2Network::ConfigReader}
def self.for(source)
def self.for(source, opts = {})
require "y2network/config_reader/#{source}"
name = source.to_s.split("_").map(&:capitalize).join
klass = const_get(name)
Expand Down
3 changes: 3 additions & 0 deletions src/lib/y2network/config_reader/sysconfig.rb
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,9 @@ module Y2Network
module ConfigReader
# This class reads the current configuration from `/etc/sysconfig` files
class Sysconfig
def initialize(_opts = {})
end

# @return [Y2Network::Config] Network configuration
def config
interfaces = find_interfaces
Expand Down
58 changes: 37 additions & 21 deletions src/modules/Lan.rb
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
require "network/confirm_virt_proposal"
require "ui/text_helpers"
require "y2firewall/firewalld"
require "y2network/autoinst_profile/networking_section"

require "shellwords"

Expand Down Expand Up @@ -97,7 +98,7 @@ def main
@backend = nil

# Y2Network::Config objects
@configs = []
@configs = {}
end

#------------------
Expand All @@ -109,8 +110,7 @@ def main
def Modified
return true if LanItems.GetModified
return true if DNS.modified
# TODO: compare system and running configuration
# return true if running_config != yast_config
return true unless running_config == yast_config
return true if NetworkConfig.Modified
return true if NetworkService.Modified
return true if Host.GetModified
Expand Down Expand Up @@ -266,13 +266,8 @@ def Read(cache)
end

running_config = Y2Network::Config.from(:sysconfig)
# TODO: we could consider to add an :id argument to the .from method
running_config.id = :system
add_config(running_config)

yast_config = running_config.copy
yast_config.id = :yast
add_config(yast_config)
add_config(:system, running_config)
add_config(:yast, running_config.copy)

# Read dialog caption
caption = _("Initializing Network Configuration")
Expand Down Expand Up @@ -532,8 +527,8 @@ def Write(gui: true)
# Progress step 5
ProgressNextStage(_("Writing routing configuration..."))
orig = Progress.set(false)
config = find_config(id: :system)
config.write

yast_config.write
Progress.set(orig)
Builtins.sleep(sl)

Expand Down Expand Up @@ -753,9 +748,9 @@ def FromAY(input)
def Import(settings)
settings = {} if settings.nil?

config = Y2Network::Config.from(:autoyast, settings)
config.id = :yast
add_config(config)
profile = Y2Network::AutoinstProfile::NetworkingSection.new_from_hashes(settings)
config = Y2Network::Config.from(:autoyast, profile)
add_config(:yast, config)

LanItems.Import(settings)
NetworkConfig.Import(settings["config"] || {})
Expand All @@ -777,6 +772,7 @@ def Import(settings)
# we export a 2-level map of typed "devices"
# @return dumped settings
def Export
profile = Y2Network::AutoinstProfile::NetworkingSection.new_from_network(yast_config)
devices = NetworkInterfaces.Export("")
udev_rules = LanItems.export(devices)
ay = {
Expand All @@ -790,7 +786,7 @@ def Export
"config" => NetworkConfig.Export,
"devices" => devices,
"ipv6" => @ipv6,
"routing" => find_config(id: :system).routing.to_h,
"routing" => profile.routing,
"managed" => NetworkService.is_network_manager,
"start_immediately" => Ops.get_boolean(
LanItems.autoinstall_settings,
Expand Down Expand Up @@ -976,15 +972,16 @@ def dhcp_ntp_servers
#
# @param id [Symbol] Network configuration ID
# @return [Y2Network::Config,nil] Network configuration with the given ID or nil if not found
def find_config(id: :system)
configs.find { |c| c.id == id }
def find_config(id)
configs[id]
end

# Adds the configuration
#
# @param id [Symbol] Configuration ID
# @param config [Y2Network::Config] Network configuration
def add_config(config)
configs << config
def add_config(id, config)
configs[id] = config
end

# Clears the network configurations list
Expand Down Expand Up @@ -1117,12 +1114,31 @@ def refresh_lan_items
# Returns the routing summary
#
# @param mode [String,Symbol] Summary mode
# @return [String]
def routing_summary(mode)
config = find_config(id: :yast)
config = find_config(:yast)
presenter = Y2Network::Presenters::RoutingSummary.new(config.routing)
presenter.text(mode: mode.to_sym)
end

# Returns the system configuration
#
# Just a convenience method.
#
# @return [Y2Network::Config]
def running_config
find_config(:system)
end

# Returns YaST configuration
#
# Just a convenience method.
#
# @return [Y2Network::Config]
def yast_config
find_config(:yast)
end

def firewalld
Y2Firewall::Firewalld.instance
end
Expand Down
36 changes: 25 additions & 11 deletions test/lan_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,13 @@

require "yast"
require "y2network/config"
require "y2network/routing"

Yast.import "Lan"

describe "LanClass" do
subject { Yast::Lan }
let(:system_config) { instance_double(Y2Network::Config, "System", id: :system) }
let(:system_config) { instance_double(Y2Network::Config, "System") }

describe "#Packages" do
before(:each) do
Expand Down Expand Up @@ -147,7 +148,6 @@
def reset_modification_statuses
allow(Yast::LanItems).to receive(:GetModified).and_return false
allow(Yast::DNS).to receive(:modified).and_return false
allow(Yast::Routing).to receive(:Modified).and_return false
allow(Yast::NetworkConfig).to receive(:Modified).and_return false
allow(Yast::NetworkService).to receive(:Modified).and_return false
allow(Yast::Host).to receive(:GetModified).and_return false
Expand All @@ -172,8 +172,21 @@ def expect_modification_succeedes(modname, method)
expect_modification_succeedes(Yast::DNS, :modified)
end

let(:config) do
Y2Network::Config.new(interfaces: [], routing: routing, source: :sysconfig)
end
let(:routing) { Y2Network::Routing.new(tables: []) }

it "returns true when Routing module was modified" do
expect_modification_succeedes(Yast::Routing, :Modified)
Yast::Lan.clear_configs
Yast::Lan.add_config(:system, config)
yast_config = config.copy
yast_config.routing.forward_ipv4 = !config.routing.forward_ipv4
Yast::Lan.add_config(:yast, yast_config)

reset_modification_statuses
expect(Yast::Lan.Modified).to eq(true)
Yast::Lan.clear_configs
end

it "returns true when NetworkConfig module was modified" do
Expand Down Expand Up @@ -496,34 +509,34 @@ def expect_modification_succeedes(modname, method)
end

describe "#find_config" do
let(:autoyast_config) { instance_double(Y2Network::Config, "AutoYaST", id: :autoyast) }
let(:yast_config) { instance_double(Y2Network::Config, "YaST") }

before do
subject.main
subject.add_config(system_config)
subject.add_config(autoyast_config)
subject.add_config(:system, system_config)
subject.add_config(:yast, yast_config)
end

it "retuns the network configuration with the given ID" do
expect(subject.find_config(id: :autoyast)).to eq(autoyast_config)
expect(subject.find_config(:yast)).to eq(yast_config)
end

context "when a network configuration with the given ID is not found" do
it "returns nil" do
expect(subject.find_config(id: :missing)).to be_nil
expect(subject.find_config(:missing)).to be_nil
end
end
end

describe "#clear_configs" do
before do
subject.main
subject.add_config(system_config)
subject.add_config(:system, system_config)
end

it "cleans the configurations list" do
expect { subject.clear_configs }
.to change { subject.find_config(id: :system) }
.to change { subject.find_config(:system) }
.from(system_config).to(nil)
end
end
Expand All @@ -532,7 +545,8 @@ def expect_modification_succeedes(modname, method)
before { subject.main }

it "adds the configuration to the list" do
expect { subject.add_config(system_config) }.to change { subject.find_config(id: :system) }
expect { subject.add_config(:system, system_config) }
.to change { subject.find_config(:system) }
.from(nil).to(system_config)
end
end
Expand Down
2 changes: 1 addition & 1 deletion test/y2network/config_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@
end

before do
allow(Y2Network::ConfigReader).to receive(:for).with(:sysconfig)
allow(Y2Network::ConfigReader).to receive(:for).with(:sysconfig, {})
.and_return(reader)
end

Expand Down
1 change: 1 addition & 0 deletions test/y2network/config_writer/sysconfig_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
require "y2network/config_writer/sysconfig"
require "y2network/config"
require "y2network/interface"
require "y2network/routing"
require "y2network/route"
require "y2network/routing_table"

Expand Down
8 changes: 8 additions & 0 deletions test/y2network/interface_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -40,5 +40,13 @@
expect(interface).to_not eq(other)
end
end

context "comparing with a symbol" do
let(:other) { :any }

it "returns false" do
expect(interface).to_not eq(other)
end
end
end
end

0 comments on commit 422df58

Please sign in to comment.