PageRenderTime 43ms CodeModel.GetById 18ms RepoModel.GetById 1ms app.codeStats 0ms

/test/unit/plugins/providers/virtualbox/action/network_fix_ipv6_test.rb

https://gitlab.com/thomasphillips3/vagrant
Ruby | 165 lines | 148 code | 15 blank | 2 comment | 1 complexity | 1d54c217e87bbf7d84524aa0c80113b3 MD5 | raw file
  1. require_relative "../base"
  2. require 'socket'
  3. describe VagrantPlugins::ProviderVirtualBox::Action::NetworkFixIPv6 do
  4. include_context "unit"
  5. include_context "virtualbox"
  6. let(:iso_env) do
  7. env = isolated_environment
  8. env.vagrantfile("")
  9. env.create_vagrant_env
  10. end
  11. let(:machine) do
  12. iso_env.machine(iso_env.machine_names[0], :dummy).tap do |m|
  13. m.provider.stub(driver: driver)
  14. end
  15. end
  16. let(:env) {{ machine: machine }}
  17. let(:app) { lambda { |*args| }}
  18. let(:driver) { double("driver") }
  19. subject { described_class.new(app, env) }
  20. it "ignores nil IP addresses" do
  21. allow(machine.config.vm).to receive(:networks)
  22. .and_return(private_network: { ip: nil })
  23. expect { subject.call(env) }.to_not raise_error
  24. end
  25. it "blank nil IP addresses" do
  26. allow(machine.config.vm).to receive(:networks)
  27. .and_return(private_network: { ip: "" })
  28. expect { subject.call(env) }.to_not raise_error
  29. end
  30. context "with IPv6 interfaces" do
  31. let(:socket) { double("socket") }
  32. before do
  33. # This address is only used to trigger the fixup code. It doesn't matter
  34. # what it is.
  35. allow(machine.config.vm).to receive(:networks)
  36. .and_return(private_network: { ip: 'fe:80::' })
  37. allow(UDPSocket).to receive(:new).with(Socket::AF_INET6)
  38. .and_return(socket)
  39. socket.stub(:connect)
  40. end
  41. it "only checks the interfaces associated with the VM" do
  42. all_networks = [{name: "vboxnet0",
  43. ipv6: "dead:beef::",
  44. ipv6_prefix: 64,
  45. status: 'Up'
  46. },
  47. {name: "vboxnet1",
  48. ipv6: "badd:badd::",
  49. ipv6_prefix: 64,
  50. status: 'Up'
  51. }
  52. ]
  53. ifaces = { 1 => {type: :hostonly, hostonly: "vboxnet0"}
  54. }
  55. allow(machine.provider.driver).to receive(:read_network_interfaces)
  56. .and_return(ifaces)
  57. allow(machine.provider.driver).to receive(:read_host_only_interfaces)
  58. .and_return(all_networks)
  59. subject.call(env)
  60. expect(socket).to have_received(:connect)
  61. .with(all_networks[0][:ipv6] + (['ffff']*4).join(':'), 80)
  62. end
  63. it "correctly uses the netmask to figure out the probe address" do
  64. all_networks = [{name: "vboxnet0",
  65. ipv6: "dead:beef::",
  66. ipv6_prefix: 113,
  67. status: 'Up'
  68. }
  69. ]
  70. ifaces = { 1 => {type: :hostonly, hostonly: "vboxnet0"}
  71. }
  72. allow(machine.provider.driver).to receive(:read_network_interfaces)
  73. .and_return(ifaces)
  74. allow(machine.provider.driver).to receive(:read_host_only_interfaces)
  75. .and_return(all_networks)
  76. subject.call(env)
  77. expect(socket).to have_received(:connect)
  78. .with(all_networks[0][:ipv6] + '7fff', 80)
  79. end
  80. it "should ignore interfaces that are down" do
  81. all_networks = [{name: "vboxnet0",
  82. ipv6: "dead:beef::",
  83. ipv6_prefix: 64,
  84. status: 'Down'
  85. }
  86. ]
  87. ifaces = { 1 => {type: :hostonly, hostonly: "vboxnet0"}
  88. }
  89. allow(machine.provider.driver).to receive(:read_network_interfaces)
  90. .and_return(ifaces)
  91. allow(machine.provider.driver).to receive(:read_host_only_interfaces)
  92. .and_return(all_networks)
  93. subject.call(env)
  94. expect(socket).to_not have_received(:connect)
  95. end
  96. it "should ignore interfaces without an IPv6 address" do
  97. all_networks = [{name: "vboxnet0",
  98. ipv6: "",
  99. ipv6_prefix: 0,
  100. status: 'Up'
  101. }
  102. ]
  103. ifaces = { 1 => {type: :hostonly, hostonly: "vboxnet0"}
  104. }
  105. allow(machine.provider.driver).to receive(:read_network_interfaces)
  106. .and_return(ifaces)
  107. allow(machine.provider.driver).to receive(:read_host_only_interfaces)
  108. .and_return(all_networks)
  109. subject.call(env)
  110. expect(socket).to_not have_received(:connect)
  111. end
  112. it "should ignore nat interfaces" do
  113. all_networks = [{name: "vboxnet0",
  114. ipv6: "",
  115. ipv6_prefix: 0,
  116. status: 'Up'
  117. }
  118. ]
  119. ifaces = { 1 => {type: :nat}
  120. }
  121. allow(machine.provider.driver).to receive(:read_network_interfaces)
  122. .and_return(ifaces)
  123. allow(machine.provider.driver).to receive(:read_host_only_interfaces)
  124. .and_return(all_networks)
  125. subject.call(env)
  126. expect(socket).to_not have_received(:connect)
  127. end
  128. it "should reconfigure an interface if unreachable" do
  129. all_networks = [{name: "vboxnet0",
  130. ipv6: "dead:beef::",
  131. ipv6_prefix: 64,
  132. status: 'Up'
  133. }
  134. ]
  135. ifaces = { 1 => {type: :hostonly, hostonly: "vboxnet0"}
  136. }
  137. allow(machine.provider.driver).to receive(:read_network_interfaces)
  138. .and_return(ifaces)
  139. allow(machine.provider.driver).to receive(:read_host_only_interfaces)
  140. .and_return(all_networks)
  141. allow(socket).to receive(:connect)
  142. .with(all_networks[0][:ipv6] + (['ffff']*4).join(':'), 80)
  143. .and_raise Errno::EHOSTUNREACH
  144. allow(machine.provider.driver).to receive(:reconfig_host_only)
  145. subject.call(env)
  146. expect(machine.provider.driver).to have_received(:reconfig_host_only)
  147. .with(all_networks[0])
  148. end
  149. end
  150. end