Skip to content

Commit

Permalink
Report issues when the netmask or the prefix are wrong
Browse files Browse the repository at this point in the history
  • Loading branch information
teclator committed Dec 29, 2022
1 parent ef4a692 commit 991985f
Show file tree
Hide file tree
Showing 3 changed files with 66 additions and 7 deletions.
36 changes: 30 additions & 6 deletions src/lib/y2network/wicked/connection_config_readers/base.rb
Original file line number Diff line number Diff line change
Expand Up @@ -142,7 +142,8 @@ def all_ips
@all_ips ||= file.ipaddrs.each_with_object([]) do |(id, ip), all|
next unless ip.is_a?(Y2Network::IPAddress)

ip_address = build_ip(ip, file.prefixlens[id], file.netmasks[id])
ip_address = build_ip(ip, id)

all << Y2Network::ConnectionConfig::IPConfig.new(
ip_address,
id: id,
Expand All @@ -158,14 +159,37 @@ def all_ips
# It takes an IP address and, optionally, a prefix or a netmask.
#
# @param ip [Y2Network::IPAddress] IP address
# @param prefix [Integer,nil] Address prefix
# @param netmask [String,nil] Netmask
def build_ip(ip, prefix, netmask)
# @param id [String] Hash key for the IP Address
def build_ip(ip, id)
ipaddr = ip.clone
return ipaddr if ip.prefix?

ipaddr.netmask = netmask if netmask
ipaddr.prefix = prefix if prefix
begin
netmask = file.netmasks[id]
ipaddr.netmask = netmask if netmask
rescue StandardError
issue_location = "file:#{file.path}:NETMASK#{id}"
issue = Y2Issues::InvalidValue.new(
netmask, fallback: nil, location: issue_location
)
issues_list << issue
end

prefix = file.prefixlens[id]
return ipaddr unless prefix

begin
address = ipaddr.address.clone
address.prefix = prefix
ipaddr.prefix = prefix
rescue StandardError
issue_location = "file:#{file.path}:PREFIXLEN#{id}"
issue = Y2Issues::InvalidValue.new(
prefix, fallback: nil, location: issue_location
)
issues_list << issue
end

ipaddr
end

Expand Down
2 changes: 1 addition & 1 deletion test/data/scr_read/etc/sysconfig/network/ifcfg-eth0
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
BOOTPROTO='static'
BROADCAST=''
ETHTOOL_OPTIONS=''
IPADDR='192.168.123.1/24'
IPADDR='192.168.123.1/24'
MTU='1500'
NAME='Ethernet Card 0'
NETWORK=''
Expand Down
35 changes: 35 additions & 0 deletions test/y2network/wicked/connection_config_readers/ethernet_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -145,5 +145,40 @@
expect(issue.message).to include("Invalid value 'automatic'")
end
end

context "when the NETWASK is not valid" do
let(:interface_name) { "eth1" }

before do
allow(file).to receive(:netmasks).and_return("_1" => "255.255.252.298")
end

it "registers an issue" do
handler.connection_config
issue = issues_list.first
expect(issue.location.to_s).to eq(
"file:/etc/sysconfig/network/ifcfg-eth1:NETMASK_1"
)
expect(issue.message).to include("Invalid value '255.255.252.298'")
end
end

context "when the PREFIXLEN is not valid" do
let(:interface_name) { "eth2" }

before do
allow(file).to receive(:prefixlens).and_return("" => 244)
end

it "registers an issue" do
handler.connection_config
issue = issues_list.first
expect(issue.location.to_s).to eq(
"file:/etc/sysconfig/network/ifcfg-eth2:PREFIXLEN"
)
expect(issue.message).to include("Invalid value '244'")
end
end

end
end

0 comments on commit 991985f

Please sign in to comment.