Skip to content

Commit

Permalink
Fix IP address handling in InterfaceFile
Browse files Browse the repository at this point in the history
* Unit tests are using a real file now.
  • Loading branch information
imobachgs committed Jul 12, 2019
1 parent 8d44d52 commit dc06c7d
Show file tree
Hide file tree
Showing 5 changed files with 34 additions and 16 deletions.
6 changes: 3 additions & 3 deletions src/lib/y2network/sysconfig/interface_file.rb
Expand Up @@ -275,12 +275,12 @@ def value_as_symbol(value)
value.nil? || value.empty? ? nil : value.to_sym
end

# Converts the value into a IPAddr (or nil if empty)
# Converts the value into a IPAddress (or nil if empty)
#
# @param [String] value
# @return [IPAddr,nil]
# @return [Y2Network::IPAddress,nil]
def value_as_ipaddr(value)
value.nil? || value.empty? ? nil : IPAddr.new(value)
value.nil? || value.empty? ? nil : Y2Network::IPAddress.from_string(value)
end

# Writes an array as a value for a given key
Expand Down
11 changes: 11 additions & 0 deletions test/data/scr_read/etc/sysconfig/network/ifcfg-eth1
@@ -0,0 +1,11 @@
BOOTPROTO='static'
BROADCAST=''
ETHTOOL_OPTIONS=''
IPADDR_0='192.168.123.1/24'
IPADDR_1='10.0.0.1'
NETMASK_1='255.0.0.0'
MTU=''
NAME='Ethernet Card 0'
NETWORK=''
REMOTE_IPADDR=''
STARTMODE='auto'
5 changes: 5 additions & 0 deletions test/data/scr_read/etc/sysconfig/network/ifcfg-eth2
@@ -0,0 +1,5 @@
BOOTPROTO='dhcp'
BROADCAST=''
MTU=''
NAME='Ethernet Card 0'
STARTMODE='auto'
6 changes: 6 additions & 0 deletions test/data/scr_read/etc/sysconfig/network/ifcfg-eth3
@@ -0,0 +1,6 @@
BOOTPROTO=''
BROADCAST=''
MTU=''
IPADDR=''
NAME='Ethernet Card 0'
STARTMODE='auto'
22 changes: 9 additions & 13 deletions test/y2network/sysconfig/interface_file_test.rb
Expand Up @@ -22,7 +22,9 @@
require "tmpdir"

describe Y2Network::Sysconfig::InterfaceFile do
subject(:file) { described_class.new("eth0") }
subject(:file) { described_class.new(interface_name) }

let(:interface_name) { "eth0" }

def file_content(scr_root, file)
path = File.join(scr_root, file.path.to_s)
Expand Down Expand Up @@ -61,26 +63,20 @@ def file_content(scr_root, file)
end

describe "#ip_address" do
let(:ipaddr) { "192.168.122.122/24" }

before do
allow(file).to receive(:fetch).with("IPADDR").and_return(ipaddr)
end

it "returns the IP address" do
expect(file.ip_address).to eq(IPAddr.new(ipaddr))
expect(file.ip_address).to eq(Y2Network::IPAddress.from_string("192.168.123.1/24"))
end

context "when the IP address is empty" do
let(:ipaddr) { "" }
let(:interface_name) { "eth1" }

it "returns nil" do
expect(file.ip_address).to be_nil
end
end

context "when the IP address is undefined" do
let(:ipaddr) { nil }
context "when the IP address is missing" do
let(:interface_name) { "eth2" }

it "returns nil" do
expect(file.ip_address).to be_nil
Expand All @@ -90,8 +86,8 @@ def file_content(scr_root, file)

describe "#ipaddr=" do
it "sets the bootproto" do
expect { file.ipaddr = IPAddr.new("10.0.0.1") }
.to change { file.ipaddr }.to(IPAddr.new("10.0.0.1"))
expect { file.ipaddr = Y2Network::IPAddress.from_string("10.0.0.1") }
.to change { file.ipaddr }.to(Y2Network::IPAddress.from_string("10.0.0.1"))
end
end

Expand Down

0 comments on commit dc06c7d

Please sign in to comment.