From 760b973303f44f731177f59077b90368741b136b Mon Sep 17 00:00:00 2001 From: Patrick J Cherry Date: Fri, 5 Jul 2013 13:51:48 +0100 Subject: [PATCH] Updated tests to operate on a dummy interface, rather than lo. --- test/t_route.rb | 162 +++++++++++++++++++++++++++++++++++++----------- 1 file changed, 127 insertions(+), 35 deletions(-) diff --git a/test/t_route.rb b/test/t_route.rb index 4196762..1e82934 100644 --- a/test/t_route.rb +++ b/test/t_route.rb @@ -5,40 +5,125 @@ require 'linux/netlink/route' # (Should we use different algorithm for generating the PID? PID + seq?) $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 context "With netlink route socket" do setup do @ip = $ip end + teardown do + begin + delete_test_interface + rescue KeyError, IndexError + # Do nothing + end + end + test "Read link type" do assert_equal Linux::ARPHRD_LOOPBACK, @ip.link["lo"].type 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"}) res = @ip.addr.list(opt).map { |x| x.address.to_s }.sort end def add_and_remove_addr(testaddr, pfx) - begin - @ip.addr.delete(:index=>"lo", :local=>testaddr, :prefixlen=>pfx) - rescue Errno::EADDRNOTAVAIL - end - - addrs1 = addrlist + ifname = create_test_interface + return if ifname.nil? + + addrs1 = addrlist({:index => ifname}) 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) { - @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}" - @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 end @@ -61,67 +146,74 @@ class TestAddr < Test::Unit::TestCase end 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 test "Add and remove V6 address" do add_and_remove_addr("2001:dead:beef::1", 64) end - def vlanlist - @ip.vlan.list(:link=>"lo").map { |x| x.linkinfo.data.id } + def vlanlist(ifname) + @ip.vlan.list(:link=>ifname).map { |x| x.linkinfo.data.id } end test "Add and remove vlan" do - begin - @ip.vlan.delete(:link=>"lo", :vlan_id=>1234) - rescue Errno::ENODEV - end + ifname = create_test_interface + return if ifname.nil? - vlans1 = vlanlist + vlans1 = vlanlist(ifname) 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) - @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 + @ip.link.delete(:index => ifname) end 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 + 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 - assert_equal [["",0]], routes.select { |x| x == ["",0] } + assert v4routes.find{|x| x[0] == "" and x[1] == 0} end def add_and_remove_route(info) - begin - @ip.route.delete(info) - rescue Errno::ESRCH - end - - assert_equal 0, routes.select { |x| x == [info[:dst], info[:dst_len]] }.size + ifname = create_test_interface + return if ifname.nil? + + 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) - 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) - 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 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 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