Skip to content

Commit

Permalink
Move configurations register to Y2Network::Config
Browse files Browse the repository at this point in the history
* It was needed by Lan and LanItems, but the latter
  depended on the former.
  • Loading branch information
imobachgs committed Apr 9, 2019
1 parent 230ae03 commit f7312bb
Show file tree
Hide file tree
Showing 4 changed files with 63 additions and 4 deletions.
28 changes: 28 additions & 0 deletions src/lib/y2network/config.rb
Expand Up @@ -49,6 +49,34 @@ def from(source, opts = {})
reader = ConfigReader.for(source, opts)
reader.config
end

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

# Finds the configuration in the register
#
# @param id [Symbol] Configuration ID
# @return [Config,nil] Configuration with the given ID or nil if not found
def find(id)
configs[id]
end

# Resets the configuration register
def reset
configs.clear
end

private

# Configuration register
def configs
@configs ||= {}
end
end

# Constructor
Expand Down
6 changes: 3 additions & 3 deletions src/modules/Lan.rb
Expand Up @@ -974,20 +974,20 @@ 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)
configs[id]
Y2Network::Config.find(id)
end

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

# Clears the network configurations list
def clear_configs
configs.clear
Y2Network::Config.reset
end

# Returns the system configuration
Expand Down
5 changes: 4 additions & 1 deletion test/lan_test.rb
Expand Up @@ -12,6 +12,10 @@
subject { Yast::Lan }
let(:system_config) { instance_double(Y2Network::Config, "System") }

before do
Yast::Lan.clear_configs
end

describe "#Packages" do
before(:each) do
allow(Yast::NetworkService)
Expand Down Expand Up @@ -178,7 +182,6 @@ def expect_modification_succeedes(modname, method)
let(:routing) { Y2Network::Routing.new(tables: []) }

it "returns true when Routing module was modified" do
Yast::Lan.clear_configs
Yast::Lan.add_config(:system, config)
yast_config = config.copy
yast_config.routing.forward_ipv4 = !config.routing.forward_ipv4
Expand Down
28 changes: 28 additions & 0 deletions test/y2network/config_test.rb
Expand Up @@ -24,6 +24,10 @@
require "y2network/config_writer/sysconfig"

describe Y2Network::Config do
before do
Y2Network::Config.reset
end

subject(:config) do
described_class.new(interfaces: [eth0], routing: routing, source: :sysconfig)
end
Expand Down Expand Up @@ -53,6 +57,30 @@
end
end

describe ".add" do
it "adds the configuration to the register" do
expect { Y2Network::Config.add(:yast, config) }
.to change { Y2Network::Config.find(:yast) }
.from(nil).to(config)
end
end

describe ".find" do
before do
Y2Network::Config.add(:yast, config)
end

it "returns the registered config with the given ID" do
expect(Y2Network::Config.find(:yast)).to eq(config)
end

context "when a configuration with the given ID does not exist" do
it "returns nil" do
expect(Y2Network::Config.find(:test)).to be_nil
end
end
end

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

0 comments on commit f7312bb

Please sign in to comment.