Skip to content

Commit

Permalink
Updated testsuite
Browse files Browse the repository at this point in the history
  • Loading branch information
mchf committed Mar 27, 2019
1 parent 6801f7c commit 1e5dd11
Show file tree
Hide file tree
Showing 4 changed files with 43 additions and 4 deletions.
5 changes: 4 additions & 1 deletion src/lib/y2network/config_reader/sysconfig_routes_reader.rb
Expand Up @@ -47,6 +47,8 @@ def config

MISSING_VALUE = "-".freeze
private_constant :MISSING_VALUE
DEFAULT_DEST = "default".freeze
private_constant :DEFAULT_DEST

# Loads routes from system
#
Expand Down Expand Up @@ -91,6 +93,7 @@ def normalize_routes(routes)
# @return [IPAddr,nil] The IP address or `nil` if the IP is missing
def build_ip(ip_str, netmask_str = MISSING_VALUE)
return nil if ip_str == MISSING_VALUE

ip = IPAddr.new(ip_str)
netmask_str == MISSING_VALUE ? ip : ip.mask(netmask_str)
end
Expand All @@ -108,7 +111,7 @@ def build_route(hash)
mask = hash["netmask"] =~ /\/[0-9]+/ ? hash["netmask"][1..-1] : hash["netmask"]

Y2Network::Route.new(
to: build_ip(hash["destination"], mask) || :default,
to: hash["destination"] != DEFAULT_DEST ? build_ip(hash["destination"], mask) : :default,
interface: iface,
gateway: build_ip(hash["gateway"], MISSING_VALUE)
)
Expand Down
File renamed without changes.
40 changes: 38 additions & 2 deletions test/y2network/config_reader/sysconfig_routes_reader_test.rb
Expand Up @@ -24,8 +24,44 @@
subject(:reader) { described_class.new }

describe "#config" do
it "returns a RoutingTable object" do
expect(reader.config).to be_instance_of Y2Network::RoutingTable
old_SCR = Yast::WFM.SCRGetDefault

def agent_stub_scr_read(agent)
new_SCR = Yast::WFM.SCROpen("chroot=#{DATA_PATH}/scr_read:scr", false)

Yast::WFM.SCRSetDefault(new_SCR)
info = Yast::SCR.Read(agent)

mock_path(agent, info)
end

before(:each) do
agent_stub_scr_read(".routes")
end

after(:each) do
Yast::WFM.SCRSetDefault(old_SCR)
end

it "returns a RoutingTable with routes" do
routing_table = reader.config

expect(routing_table).to be_instance_of Y2Network::RoutingTable
expect(routing_table.routes).not_to be_empty
end

it "contains default gw definition" do
expect(reader.config.routes.any? { |r| r.default? }).to be_truthy
end

it "accepts prefix from gateway field" do
route = reader.config.routes.find { |r| r.to == "10.192.0.0" }

expect(route.to.prefix).to eql 10
end

it "stores device when set" do
expect(reader.config.routes.any? { |r| r.interface.name == "eth0" }).to be_truthy
end
end
end
2 changes: 1 addition & 1 deletion test/y2network/config_reader/sysconfig_test.rb
Expand Up @@ -64,7 +64,7 @@
expect(config.source).to eq(:sysconfig)
end

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

it "sets the gateway to nil" do
Expand Down

0 comments on commit 1e5dd11

Please sign in to comment.