From 5215354d16e6bbdca16288b13d1e7d8c242b4a7f Mon Sep 17 00:00:00 2001 From: Brian Cain Date: Thu, 21 Mar 2019 10:50:45 -0700 Subject: [PATCH] Fix missing docker error classes, and finish out initial #call tests --- plugins/providers/docker/errors.rb | 12 +++ templates/locales/providers_docker.yml | 2 + .../docker/action/prepare_networks_test.rb | 74 ++++++++++++++----- 3 files changed, 71 insertions(+), 17 deletions(-) diff --git a/plugins/providers/docker/errors.rb b/plugins/providers/docker/errors.rb index dc4e2e549..84b5e8f4e 100644 --- a/plugins/providers/docker/errors.rb +++ b/plugins/providers/docker/errors.rb @@ -45,6 +45,18 @@ module VagrantPlugins error_key(:docker_provider_nfs_without_privileged) end + class NetworkAddressInvalid < DockerError + error_key(:network_address_invalid) + end + + class NetworkIPAddressRequired < DockerError + error_key(:network_address_required) + end + + class NetworkSubnetInvalid < DockerError + error_key(:network_subnet_invalid) + end + class NetworkInvalidOption < DockerError error_key(:network_invalid_option) end diff --git a/templates/locales/providers_docker.yml b/templates/locales/providers_docker.yml index 6d5c13635..00c929e8f 100644 --- a/templates/locales/providers_docker.yml +++ b/templates/locales/providers_docker.yml @@ -236,6 +236,8 @@ en: Configured address: %{address} Network name: %{network_name} + network_address_required: |- + An IP address is required if not using `type: "dhcp"` or not specifying a `subnet`. network_invalid_option: |- Invalid option given for docker network for guest "%{container}". Must specify either a `subnet` or use `type: "dhcp"`. 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 6815356d1..d12fe391f 100644 --- a/test/unit/plugins/providers/docker/action/prepare_networks_test.rb +++ b/test/unit/plugins/providers/docker/action/prepare_networks_test.rb @@ -32,6 +32,11 @@ describe VagrantPlugins::DockerProvider::Action::PrepareNetworks do :alias=>"mynetwork", :protocol=>"tcp", :id=>"80e017d5-388f-4a2f-a3de-f8dce8156a58"}], + [:public_network, + {:ip=>"172.30.130.2", + :subnet=>"172.30.0.0/16", + :driver=>"bridge", + :id=>"30e017d5-488f-5a2f-a3ke-k8dce8246b60"}], [:private_network, {:type=>"dhcp", :ipv6=>"true", @@ -60,8 +65,6 @@ describe VagrantPlugins::DockerProvider::Action::PrepareNetworks do allow(driver).to receive(:host_vm?).and_return(false) allow(driver).to receive(:existing_named_network?).and_return(false) allow(driver).to receive(:create_network).and_return(true) - allow(driver).to receive(:connect_network).and_return(true) - allow(driver).to receive(:subnet_defined?).and_return(nil) called = false app = ->(*args) { called = true } @@ -79,39 +82,76 @@ describe VagrantPlugins::DockerProvider::Action::PrepareNetworks do it "calls the proper driver methods to setup a network" do allow(driver).to receive(:host_vm?).and_return(false) allow(driver).to receive(:existing_named_network?).and_return(false) - allow(driver).to receive(:create_network).and_return(true) - allow(driver).to receive(:connect_network).and_return(true) - allow(driver).to receive(:subnet_defined?).and_return(nil) allow(driver).to receive(:network_containing_address). with("172.20.128.2").and_return(nil) + allow(driver).to receive(:network_containing_address). + with("192.168.1.1").and_return(nil) allow(driver).to receive(:network_defined?).with("172.20.128.0/24"). and_return(false) + allow(driver).to receive(:network_defined?).with("172.30.128.0/24"). + and_return(false) allow(driver).to receive(:network_defined?).with("2a02:6b8:b010:9020:1::/80"). and_return(false) + allow(subject).to receive(:request_public_gateway).and_return("1234") + allow(subject).to receive(:request_public_iprange).and_return("1234") - # TODO: Still need to finish up the rest of this method - # with the proper generate create tests - expect(subject).to receive(:generate_create_cli_arguments). - with(networks[0][1]).and_return(["--subnet=172.20.0.0/16", "--driver=bridge", "--internal=true"]) - expect(subject).to receive(:generate_create_cli_arguments). - with(networks[1][1]).and_return(["--ipv6=true", "--subnet=2a02:6b8:b010:9020:1::/80"]) + allow(machine.ui).to receive(:ask).and_return("1") + + expect(driver).to receive(:create_network). + with("vagrant_network_172.20.128.0/24", ["--subnet", "172.20.128.0/24"]) + expect(driver).to receive(:create_network). + with("vagrant_network_public_wlp4s0", ["--opt", "parent=wlp4s0", "--subnet", "192.168.1.0/24", "--driver", "macvlan", "--gateway", "1234", "--ip-range", "1234"]) + expect(driver).to receive(:create_network). + with("vagrant_network_2a02:6b8:b010:9020:1::/80", ["--ipv6", "--subnet", "2a02:6b8:b010:9020:1::/80"]) - expect(driver).to receive(:create_network).twice subject.call(env) + + expect(env[:docker_connects]).to eq({0=>"vagrant_network_172.20.128.0/24", 1=>"vagrant_network_public_wlp4s0", 2=>"vagrant_network_2a02:6b8:b010:9020:1::/80"}) end it "uses an existing network if a matching subnet is found" do allow(driver).to receive(:host_vm?).and_return(false) - allow(driver).to receive(:existing_network?).and_return(true) - allow(driver).to receive(:create_network).and_return(true) - allow(driver).to receive(:connect_network).and_return(true) - allow(driver).to receive(:subnet_defined?).and_return("my_cool_subnet_network") + allow(driver).to receive(:network_containing_address). + with("172.20.128.2").and_return(nil) + allow(driver).to receive(:network_containing_address). + with("192.168.1.1").and_return(nil) + allow(driver).to receive(:network_defined?).with("172.20.128.0/24"). + and_return("vagrant_network_172.20.128.0/24") + allow(driver).to receive(:network_defined?).with("172.30.128.0/24"). + and_return("vagrant_network_public_wlp4s0") + allow(driver).to receive(:network_defined?).with("2a02:6b8:b010:9020:1::/80"). + and_return("vagrant_network_2a02:6b8:b010:9020:1::/80") + + expect(driver).to receive(:existing_named_network?). + with("vagrant_network_172.20.128.0/24").and_return(true) + expect(driver).to receive(:existing_named_network?). + with("vagrant_network_public_wlp4s0").and_return(true) + expect(driver).to receive(:existing_named_network?). + with("vagrant_network_public_wlp4s0").and_return(true) + expect(driver).to receive(:existing_named_network?). + with("vagrant_network_2a02:6b8:b010:9020:1::/80").and_return(true) + + allow(machine.ui).to receive(:ask).and_return("1") expect(driver).not_to receive(:create_network) + expect(subject).to receive(:validate_network_configuration!). + with("vagrant_network_172.20.128.0/24", networks[0][1], + {:ipv6=>false, :subnet=>"172.20.128.0/24"}, driver) + + expect(subject).to receive(:validate_network_configuration!). + with("vagrant_network_public_wlp4s0", networks[1][1], + {"opt"=>"parent=wlp4s0", "subnet"=>"192.168.1.0/24", "driver"=>"macvlan", "gateway"=>"192.168.1.1", "ipv6"=>false}, driver) + + + expect(subject).to receive(:validate_network_configuration!). + with("vagrant_network_2a02:6b8:b010:9020:1::/80", networks[2][1], + {:ipv6=>true, :subnet=>"2a02:6b8:b010:9020:1::/80"}, driver) + subject.call(env) + end it "raises an error if an inproper network configuration is given" do @@ -119,7 +159,7 @@ describe VagrantPlugins::DockerProvider::Action::PrepareNetworks do allow(driver).to receive(:host_vm?).and_return(false) allow(driver).to receive(:existing_network?).and_return(false) - expect{ subject.call(env) }.to raise_error(VagrantPlugins::DockerProvider::Errors::NetworkInvalidOption) + expect{ subject.call(env) }.to raise_error(VagrantPlugins::DockerProvider::Errors::NetworkIPAddressRequired) end end