Updated tests to operate on a dummy interface, rather than lo.

This commit is contained in:
Patrick J Cherry
2013-07-05 13:51:48 +01:00
parent f8ffbb09c5
commit 760b973303

View File

@@ -5,40 +5,125 @@ require 'linux/netlink/route'
# (Should we use different algorithm for generating the PID? PID + seq?) # (Should we use different algorithm for generating the PID? PID + seq?)
$ip ||= Linux::Netlink::Route::Socket.new $ip ||= Linux::Netlink::Route::Socket.new
#
# Ruby 1.8.7 appears to lack the KeyError constant.
#
if RUBY_VERSION == "1.8.7"
KeyError = IndexError
end
class TestAddr < Test::Unit::TestCase class TestAddr < Test::Unit::TestCase
context "With netlink route socket" do context "With netlink route socket" do
setup do setup do
@ip = $ip @ip = $ip
end end
teardown do
begin
delete_test_interface
rescue KeyError, IndexError
# Do nothing
end
end
test "Read link type" do test "Read link type" do
assert_equal Linux::ARPHRD_LOOPBACK, @ip.link["lo"].type assert_equal Linux::ARPHRD_LOOPBACK, @ip.link["lo"].type
end end
def create_test_interface(ifname = "test_#{$$}")
begin
@ip.link.add(
:ifname => ifname,
:linkinfo => Linux::Netlink::LinkInfo.new(
:kind => "dummy"
)
)
rescue Errno::EPERM => err
if self.respond_to?(:skip)
skip err.to_s
else
puts "Skipping #{self.method_name} -- #{err.to_s}"
return nil
end
end
return ifname
end
def set_interface_up(ifname)
link = @ip.link.list.find{|l| l.ifname == ifname}
#
# Bring the link up
#
@ip.link.change(
:index => link.index,
:flags => link.flags | Linux::IFF_UP | Linux::IFF_RUNNING
)
link = @ip.link.list.find{|l| l.ifname == ifname}
assert_equal(Linux::IFF_UP, link.flags & Linux::IFF_UP, "Link does not have the IFF_UP flag set")
assert_equal(Linux::IFF_RUNNING, link.flags & Linux::IFF_RUNNING, "Link does not have the IFF_RUNNING flag set")
end
def set_interface_down(ifname)
link = @ip.link.list.find{|l| l.ifname == ifname}
return if link.nil?
return unless link.flags == (link.flags | Linux::IFF_UP | Linux::IFF_RUNNING)
#
# Bring the link up
#
@ip.link.change(
:index => link.index,
:flags => link.flags & ~Linux::IFF_UP & ~Linux::IFF_RUNNING
)
link = @ip.link.list.find{|l| l.ifname == ifname}
assert_equal(0, link.flags & Linux::IFF_UP, "Link still has the IFF_UP flag set")
assert_equal(0, link.flags & Linux::IFF_RUNNING, "Link still has the IFF_RUNNING flag set")
end
def delete_test_interface(ifname = "test_#{$$}")
begin
set_interface_down(ifname)
ensure
@ip.link.delete(:index => ifname)
end
end
test "Add and remove dummy interface" do
ifname = create_test_interface
return if ifname.nil?
delete_test_interface(ifname)
end
def addrlist(opt = {:index=>"lo"}) def addrlist(opt = {:index=>"lo"})
res = @ip.addr.list(opt).map { |x| x.address.to_s }.sort res = @ip.addr.list(opt).map { |x| x.address.to_s }.sort
end end
def add_and_remove_addr(testaddr, pfx) def add_and_remove_addr(testaddr, pfx)
begin ifname = create_test_interface
@ip.addr.delete(:index=>"lo", :local=>testaddr, :prefixlen=>pfx) return if ifname.nil?
rescue Errno::EADDRNOTAVAIL
end
addrs1 = addrlist addrs1 = addrlist({:index => ifname})
assert !addrs1.include?(testaddr) assert !addrs1.include?(testaddr)
@ip.addr.add(:index=>"lo", :local=>testaddr, :prefixlen=>pfx) @ip.addr.add(:index=>ifname, :local=>testaddr, :prefixlen=>pfx)
assert_raises(Errno::EEXIST) { assert_raises(Errno::EEXIST) {
@ip.addr.add(:index=>"lo", :local=>testaddr, :prefixlen=>pfx) @ip.addr.add(:index=>ifname, :local=>testaddr, :prefixlen=>pfx)
} }
addrs2 = addrlist addrs2 = addrlist({:index => ifname})
assert addrs2.include?(testaddr), "#{addrs2.inspect} doesn't include #{testaddr}" assert addrs2.include?(testaddr), "#{addrs2.inspect} doesn't include #{testaddr}"
@ip.addr.delete(:index=>"lo", :local=>testaddr, :prefixlen=>pfx) @ip.addr.delete(:index=>ifname, :local=>testaddr, :prefixlen=>pfx)
addrs3 = addrlist addrs3 = addrlist({:index => ifname})
assert_equal addrs1, addrs3 assert_equal addrs1, addrs3
end end
@@ -61,67 +146,74 @@ class TestAddr < Test::Unit::TestCase
end end
test "Add and remove V4 address" do test "Add and remove V4 address" do
add_and_remove_addr("1.2.3.4", 32) add_and_remove_addr("127.1.0.0", 32)
end end
test "Add and remove V6 address" do test "Add and remove V6 address" do
add_and_remove_addr("2001:dead:beef::1", 64) add_and_remove_addr("2001:dead:beef::1", 64)
end end
def vlanlist def vlanlist(ifname)
@ip.vlan.list(:link=>"lo").map { |x| x.linkinfo.data.id } @ip.vlan.list(:link=>ifname).map { |x| x.linkinfo.data.id }
end end
test "Add and remove vlan" do test "Add and remove vlan" do
begin ifname = create_test_interface
@ip.vlan.delete(:link=>"lo", :vlan_id=>1234) return if ifname.nil?
rescue Errno::ENODEV
end
vlans1 = vlanlist vlans1 = vlanlist(ifname)
assert !vlans1.include?(1234) assert !vlans1.include?(1234)
@ip.vlan.add(:link=>"lo", :vlan_id=>1234) @ip.vlan.add(:link=>ifname, :vlan_id=>1234)
vlans2 = vlanlist vlans2 = vlanlist(ifname)
assert vlans2.include?(1234) assert vlans2.include?(1234)
@ip.vlan.delete(:link=>"lo", :vlan_id=>1234) @ip.vlan.delete(:link=>ifname, :vlan_id=>1234)
vlans3 = vlanlist vlans3 = vlanlist(ifname)
assert_equal vlans1, vlans3 assert_equal vlans1, vlans3
@ip.link.delete(:index => ifname)
end end
def routes def routes
@ip.route.list(:table=>Linux::RT_TABLE_MAIN).map { |x| [x.dst.to_s, x.dst_len] } @ip.route.list(:table=>Linux::RT_TABLE_MAIN).map { |x| [x.dst.to_s, x.dst_len, x.oif] }
end
def v4routes
@ip.route.list(:table=>Linux::RT_TABLE_MAIN).select{|x| Socket::AF_INET == x.family}.
map { |x| [x.dst.to_s, x.dst_len, x.oif] }
end end
test "We have a V4 default route" do test "We have a V4 default route" do
# note that kernel doesn't send us rtattr dst 0.0.0.0, so it shows as nil # note that kernel doesn't send us rtattr dst 0.0.0.0, so it shows as nil
assert_equal [["",0]], routes.select { |x| x == ["",0] } assert v4routes.find{|x| x[0] == "" and x[1] == 0}
end end
def add_and_remove_route(info) def add_and_remove_route(info)
begin ifname = create_test_interface
@ip.route.delete(info) return if ifname.nil?
rescue Errno::ESRCH
end
assert_equal 0, routes.select { |x| x == [info[:dst], info[:dst_len]] }.size set_interface_up(ifname)
info[:oif] = ifname
ifidx = @ip.link.list.find{|x| x.ifname == info[:oif]}.index
assert_equal 0, routes.select { |x| x == [info[:dst], info[:dst_len], ifidx] }.size
@ip.route.add(info) @ip.route.add(info)
assert_equal 1, routes.select { |x| x == [info[:dst], info[:dst_len]] }.size assert_equal 1, routes.select { |x| x == [info[:dst], info[:dst_len], ifidx] }.size
@ip.route.delete(info) @ip.route.delete(info)
assert_equal 0, routes.select { |x| x == [info[:dst], info[:dst_len]] }.size assert_equal 0, routes.select { |x| x == [info[:dst], info[:dst_len], ifidx] }.size
end end
test "Add and remove V4 route" do test "Add and remove V4 route" do
add_and_remove_route(:oif=>"lo", :dst=>"1.2.3.4", :dst_len=>32, :gateway=>"127.0.0.1") add_and_remove_route(:dst=>"127.1.0.1", :dst_len=>32)
end end
test "Add and remove V6 route" do test "Add and remove V6 route" do
add_and_remove_route(:oif=>"lo", :dst=>"2001:f000:baaa::", :dst_len=>64) add_and_remove_route(:dst=>"fc10::", :dst_len=>64)
end end
end end
end end