PageRenderTime 48ms CodeModel.GetById 21ms RepoModel.GetById 0ms app.codeStats 0ms

/spec/unit/ipaddress6_spec.rb

https://github.com/lazyfrosch/facter
Ruby | 161 lines | 141 code | 19 blank | 1 comment | 11 complexity | 361ed1e219bd729f64d5e9148265e6c5 MD5 | raw file
Possible License(s): Apache-2.0
  1. #! /usr/bin/env ruby
  2. require 'spec_helper'
  3. require 'facter/util/ip'
  4. def ifconfig_fixture(filename)
  5. File.read(fixtures('ifconfig', filename))
  6. end
  7. describe "The IPv6 address fact" do
  8. include FacterSpec::ConfigHelper
  9. before do
  10. given_a_configuration_of(:is_windows => false)
  11. end
  12. it "should return ipaddress6 information for Darwin" do
  13. Facter::Core::Execution.stubs(:exec).with('uname -s').returns('Darwin')
  14. Facter::Util::IP.stubs(:get_ifconfig).returns("/sbin/ifconfig")
  15. Facter::Util::IP.stubs(:exec_ifconfig).with(["-a"]).
  16. returns(ifconfig_fixture('darwin_ifconfig_all_with_multiple_interfaces'))
  17. Facter.value(:ipaddress6).should == "2610:10:20:209:223:32ff:fed5:ee34"
  18. end
  19. it "should return ipaddress6 information for Linux" do
  20. Facter::Core::Execution.stubs(:exec).with('uname -s').returns('Linux')
  21. Facter::Util::IP.stubs(:get_ifconfig).returns("/sbin/ifconfig")
  22. Facter::Util::IP.stubs(:exec_ifconfig).with(["2>/dev/null"]).
  23. returns(ifconfig_fixture('linux_ifconfig_all_with_multiple_interfaces'))
  24. Facter.value(:ipaddress6).should == "2610:10:20:209:212:3fff:febe:2201"
  25. end
  26. it "should return ipaddress6 information for Linux with recent net-tools" do
  27. Facter::Core::Execution.stubs(:exec).with('uname -s').returns('Linux')
  28. Facter::Util::IP.stubs(:get_ifconfig).returns("/sbin/ifconfig")
  29. Facter::Util::IP.stubs(:exec_ifconfig).with(["2>/dev/null"]).
  30. returns(ifconfig_fixture('ifconfig_net_tools_1.60.txt'))
  31. Facter.value(:ipaddress6).should == "2610:10:20:209:212:3fff:febe:2201"
  32. end
  33. it "should return ipaddress6 information for Solaris" do
  34. Facter::Core::Execution.stubs(:exec).with('uname -s').returns('SunOS')
  35. Facter::Util::IP.stubs(:get_ifconfig).returns("/usr/sbin/ifconfig")
  36. Facter::Util::IP.stubs(:exec_ifconfig).with(["-a"]).
  37. returns(ifconfig_fixture('sunos_ifconfig_all_with_multiple_interfaces'))
  38. Facter.value(:ipaddress6).should == "2610:10:20:209:203:baff:fe27:a7c"
  39. end
  40. context "on Windows" do
  41. require 'facter/util/wmi'
  42. require 'facter/util/registry'
  43. require 'facter/util/ip/windows'
  44. require 'facter_spec/windows_network'
  45. include FacterSpec::WindowsNetwork
  46. before :each do
  47. Facter.fact(:kernel).stubs(:value).returns(:windows)
  48. Facter::Util::Registry.stubs(:hklm_read).returns(nic_bindings)
  49. given_a_configuration_of(:is_windows => true)
  50. end
  51. it "should do what when VPN is turned on?"
  52. context "when you have no active network adapter" do
  53. it "should return nil if there are no active (or any) network adapters" do
  54. Facter::Util::WMI.expects(:execquery).with(Facter::Util::IP::Windows::WMI_IP_INFO_QUERY).returns([])
  55. Facter.value(:ipaddress6).should == nil
  56. end
  57. end
  58. it "should return nil if the system doesn't have ipv6 installed", :if => Facter::Util::Config.is_windows? do
  59. Facter::Util::Resolution.any_instance.expects(:warn).never
  60. Facter::Util::Registry.stubs(:hklm_read).raises(Win32::Registry::Error, 2)
  61. Facter.value(:ipaddress6).should == nil
  62. end
  63. context "when you have one network adapter" do
  64. it "should return empty if ipv6 is not on" do
  65. nic = given_a_valid_windows_nic_with_ipv4_and_ipv6
  66. nic.expects(:IPAddress).returns([ipAddress1])
  67. Facter::Util::WMI.expects(:execquery).returns([nic])
  68. Facter.value(:ipaddress6).should == nil
  69. end
  70. it "should return the ipv6 address properly" do
  71. Facter::Util::WMI.expects(:execquery).returns([given_a_valid_windows_nic_with_ipv4_and_ipv6])
  72. Facter.value(:ipaddress6).should == ipv6Address0
  73. end
  74. it "should return the first ipv6 address if there is more than one (multi-homing)" do
  75. nic = given_a_valid_windows_nic_with_ipv4_and_ipv6
  76. nic.expects(:IPAddress).returns([ipAddress0, ipv6Address0,ipv6Address1])
  77. Facter::Util::WMI.expects(:execquery).returns([nic])
  78. Facter.value(:ipaddress6).should == ipv6Address0
  79. end
  80. it "should return return nil if the ipv6 address is link local" do
  81. nic = given_a_valid_windows_nic_with_ipv4_and_ipv6
  82. nic.expects(:IPAddress).returns([ipAddress0, ipv6LinkLocal])
  83. Facter::Util::WMI.expects(:execquery).returns([nic])
  84. Facter.value(:ipaddress6).should == nil
  85. end
  86. end
  87. context "when you have more than one network adapter" do
  88. it "should return empty if ipv6 is not on" do
  89. nics = given_two_valid_windows_nics_with_ipv4_and_ipv6
  90. nics[:nic0].expects(:IPAddress).returns([ipAddress0])
  91. nics[:nic1].expects(:IPAddress).returns([ipAddress1])
  92. Facter::Util::WMI.expects(:execquery).returns(nics.values)
  93. Facter.value(:ipaddress6).should == nil
  94. end
  95. it "should return the ipv6 of the adapter with the lowest IP connection metric (best connection)" do
  96. nics = given_two_valid_windows_nics_with_ipv4_and_ipv6
  97. nics[:nic1].expects(:IPConnectionMetric).returns(5)
  98. Facter::Util::WMI.expects(:execquery).returns(nics.values)
  99. Facter.value(:ipaddress6).should == ipv6Address1
  100. end
  101. it "should return the ipv6 of the adapter with the lowest IP connection metric (best connection) that has ipv6 enabled" do
  102. nics = given_two_valid_windows_nics_with_ipv4_and_ipv6
  103. nics[:nic1].expects(:IPConnectionMetric).returns(5)
  104. nics[:nic1].expects(:IPAddress).returns([ipAddress1])
  105. Facter::Util::WMI.expects(:execquery).returns(nics.values)
  106. Facter.value(:ipaddress6).should == ipv6Address0
  107. end
  108. context "when the IP connection metric is the same" do
  109. it "should return the ipv6 of the adapter with the lowest binding order" do
  110. nics = given_two_valid_windows_nics_with_ipv4_and_ipv6
  111. Facter::Util::WMI.expects(:execquery).returns(nics.values)
  112. Facter.value(:ipaddress6).should == ipv6Address0
  113. end
  114. it "should return the ipv6 of the adapter with the lowest binding order even if the adapter is not first" do
  115. nics = given_two_valid_windows_nics_with_ipv4_and_ipv6
  116. Facter::Util::Registry.stubs(:hklm_read).returns(["\\Device\\#{settingId1}", "\\Device\\#{settingId0}" ])
  117. Facter::Util::WMI.expects(:execquery).returns(nics.values)
  118. Facter.value(:ipaddress6).should == ipv6Address1
  119. end
  120. end
  121. end
  122. end
  123. end