From 82700d95b3d5e78e12f3d428ebf12d28ffc96628 Mon Sep 17 00:00:00 2001 From: Brian Cain Date: Thu, 21 Mar 2019 15:29:04 -0700 Subject: [PATCH] Ensure subnet is used if specified from user config options --- .../docker/action/prepare_networks.rb | 11 ++++++++-- .../docker/action/prepare_networks_test.rb | 22 +++++++++++++++++++ 2 files changed, 31 insertions(+), 2 deletions(-) diff --git a/plugins/providers/docker/action/prepare_networks.rb b/plugins/providers/docker/action/prepare_networks.rb index fba101204..21d3607af 100644 --- a/plugins/providers/docker/action/prepare_networks.rb +++ b/plugins/providers/docker/action/prepare_networks.rb @@ -124,11 +124,18 @@ module VagrantPlugins # With no network name, process options to find or determine # name for new network if !network_name - subnet = IPAddr.new("#{addr}/#{root_options[:netmask]}") - network = "#{subnet}/#{root_options[:netmask]}" + if !root_options[:subnet] + # Only generate a subnet if not given one + subnet = IPAddr.new("#{addr}/#{root_options[:netmask]}") + network = "#{subnet}/#{root_options[:netmask]}" + else + network = root_options[:subnet] + end + network_options[:subnet] = network existing_network = env[:machine].provider.driver. network_defined?(network) + if !existing_network network_name = "vagrant_network_#{network}" else diff --git a/test/unit/plugins/providers/docker/action/prepare_networks_test.rb b/test/unit/plugins/providers/docker/action/prepare_networks_test.rb index ba903ed84..4f42e84f3 100644 --- a/test/unit/plugins/providers/docker/action/prepare_networks_test.rb +++ b/test/unit/plugins/providers/docker/action/prepare_networks_test.rb @@ -256,6 +256,28 @@ describe VagrantPlugins::DockerProvider::Action::PrepareNetworks do end describe "#process_private_network" do + let(:options) { {:ip=>"172.20.128.2", :subnet=>"172.20.0.0/16", :driver=>"bridge", :internal=>"true", :alias=>"mynetwork", :protocol=>"tcp", :id=>"80e017d5-388f-4a2f-a3de-f8dce8156a58", :netmask=>24} } + let(:dhcp_options) { {type: "dhcp"} } + let(:bad_options) { {driver: "bridge"} } + + it "generates a network name and config for a dhcp private network" do + network_name, network_options = subject.process_private_network(dhcp_options, {}, env) + + expect(network_name).to eq("vagrant_network") + expect(network_options).to eq({}) + end + + it "generates a network name and options for a static ip" do + allow(driver).to receive(:network_defined?).and_return(nil) + network_name, network_options = subject.process_private_network(options, {}, env) + expect(network_name).to eq("vagrant_network_172.20.0.0/16") + expect(network_options).to eq({:ipv6=>false, :subnet=>"172.20.0.0/16"}) + end + + it "raises an error if no ip address or type `dhcp` was given" do + expect{subject.process_private_network(bad_options, {}, env)}. + to raise_error(VagrantPlugins::DockerProvider::Errors::NetworkIPAddressRequired) + end end describe "#process_public_network" do