From 516ac4b0ab7350485067855503397278ef1f012e Mon Sep 17 00:00:00 2001 From: Chris Roberts Date: Fri, 9 Dec 2022 13:22:37 -0800 Subject: [PATCH] Set address type when not provided When an address is provided and the type has not been explicitly provided, parse the address and automatically set the type. --- .../providers/virtualbox/action/network.rb | 15 +++++++++++ .../virtualbox/action/network_test.rb | 27 +++++++++++++++++++ 2 files changed, 42 insertions(+) diff --git a/plugins/providers/virtualbox/action/network.rb b/plugins/providers/virtualbox/action/network.rb index 6793808d3..a42d62b66 100644 --- a/plugins/providers/virtualbox/action/network.rb +++ b/plugins/providers/virtualbox/action/network.rb @@ -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 diff --git a/test/unit/plugins/providers/virtualbox/action/network_test.rb b/test/unit/plugins/providers/virtualbox/action/network_test.rb index b74c46b79..4903c5c80 100644 --- a/test/unit/plugins/providers/virtualbox/action/network_test.rb +++ b/test/unit/plugins/providers/virtualbox/action/network_test.rb @@ -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"} }