Merge pull request #13024 from chrisroberts/vbox-net-type

Set address type when not provided
This commit is contained in:
Chris Roberts 2022-12-09 14:14:48 -08:00 committed by GitHub
commit d6109b2edf
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 42 additions and 0 deletions

View File

@ -71,6 +71,21 @@ module VagrantPlugins
type = :internal_network
end
if !options.key?(:type) && options.key?(:ip)
begin
addr = IPAddr.new(options[:ip])
options[:type] = if addr.ipv4?
:static
else
:static6
end
rescue IPAddr::Error => err
raise Vagrant::Errors::NetworkAddressInvalid,
address: options[:ip], mask: options[:netmask],
error: err.message
end
end
# Configure it
data = nil
if type == :private_network

View File

@ -402,6 +402,33 @@ describe VagrantPlugins::ProviderVirtualBox::Action::Network do
end
end
context "without type set" do
before { allow(subject).to receive(:hostonly_adapter).and_return({}) }
[
{ ip: "192.168.63.5" },
{ ip: "192.168.63.5", netmask: "255.255.255.0" },
{ ip: "dead:beef::100" },
{ ip: "dead:beef::100", netmask: 96 },
].each do |args|
it "sets the type automatically" do
machine.config.vm.network "private_network", **args
expect(subject).to receive(:hostonly_config) do |config|
expect(config).to have_key(:type)
addr = IPAddr.new(args[:ip])
if addr.ipv4?
expect(config[:type]).to eq(:static)
else
expect(config[:type]).to eq(:static6)
end
config
end
subject.call(env)
end
end
end
describe "#hostonly_find_matching_network" do
let(:ip){ "192.168.55.2" }
let(:config){ {ip: ip, netmask: "255.255.255.0"} }