Skip to content

Commit

Permalink
Testsuite for LanItems#find_dhcp_ifaces
Browse files Browse the repository at this point in the history
  • Loading branch information
mchf committed Nov 22, 2016
1 parent 5f8ffab commit c69f6db
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 2 deletions.
4 changes: 2 additions & 2 deletions src/modules/LanItems.rb
Expand Up @@ -220,7 +220,7 @@ def main
#
# @param itemId [Integer] a key for {#Items}
def GetLanItem(itemId)
Ops.get_map(@Items, itemId, {})
Items()[itemId] || {}
end

# Returns configuration for currently modified item.
Expand Down Expand Up @@ -909,7 +909,7 @@ def GetBridgeableInterfaces(bridgeMaster)
# It means list of item ids of all netcards which are detected and/or
# configured in the system
def GetNetcardInterfaces
@Items.keys
Items().keys
end

# Creates list of names of all known netcards configured even unconfigured
Expand Down
45 changes: 45 additions & 0 deletions test/lan_items_helpers_test.rb
Expand Up @@ -266,4 +266,49 @@ def check_GetItemUdev(key, expected_value)
end
end

describe "LanItems#find_dhcp_ifaces" do
DHCP_MAPS = {
"eth0" => { "BOOTPROTO" => "dhcp" },
"eth1" => { "BOOTPROTO" => "dhcp4" },
"eth2" => { "BOOTPROTO" => "dhcp6" },
"eth3" => { "BOOTPROTO" => "dhcp+autoip" }
}.freeze

NON_DHCP_MAPS = {
"eth4" => { "BOOTPROTO" => "static" },
"eth5" => { "BOOTPROTO" => "none" }
}.freeze

def mock_items(dev_maps)
# mock LanItems#Items
item_maps = dev_maps.keys.map { |dev| { "ifcfg" => dev } }
lan_items = [*0..dev_maps.keys.size - 1].zip(item_maps).to_h
allow(Yast::LanItems)
.to receive(:Items)
.and_return(lan_items)

# mock each device sysconfig map
allow(Yast::LanItems)
.to receive(:GetDeviceMap)
.and_return({})

lan_items.each_pair do |index, item_map|
allow(Yast::LanItems)
.to receive(:GetDeviceMap)
.with(index)
.and_return(dev_maps[item_map["ifcfg"]])
end
end

it "finds all dhcp aware interfaces" do
mock_items(DHCP_MAPS.merge(NON_DHCP_MAPS))

expect(Yast::LanItems.find_dhcp_ifaces).to eql ["eth0", "eth1", "eth2", "eth3"]
end

it "returns empty array when no dhcp configuration is present" do
mock_items(NON_DHCP_MAPS)

expect(Yast::LanItems.find_dhcp_ifaces).to eql []
end
end

0 comments on commit c69f6db

Please sign in to comment.