Add unit tests for docker network actions

This commit is contained in:
Brian Cain 2019-03-05 09:49:15 -08:00
parent cccbedf4ce
commit 2be0bc2d81
No known key found for this signature in database
GPG Key ID: 9FC4639B2E4510A0
2 changed files with 231 additions and 0 deletions

View File

@ -0,0 +1,99 @@
require_relative "../../../../base"
require_relative "../../../../../../plugins/providers/docker/action/destroy_network"
describe VagrantPlugins::DockerProvider::Action::DestroyNetwork do
include_context "unit"
let(:sandbox) { isolated_environment }
let(:iso_env) do
# We have to create a Vagrantfile so there is a root path
sandbox.vagrantfile("")
sandbox.create_vagrant_env
end
let(:machine) do
iso_env.machine(iso_env.machine_names[0], :docker).tap do |m|
allow(m.provider).to receive(:driver).and_return(driver)
allow(m.config.vm).to receive(:networks).and_return(networks)
end
end
let(:env) {{ machine: machine, ui: machine.ui, root_path: Pathname.new(".") }}
let(:app) { lambda { |*args| }}
let(:driver) { double("driver", create: "abcd1234") }
let(:networks) { [[:private_network,
{: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"}],
[:private_network,
{:type=>"dhcp",
:ipv6=>"true",
:subnet=>"2a02:6b8:b010:9020:1::/80",
:protocol=>"tcp",
:id=>"b8f23054-38d5-45c3-99ea-d33fc5d1b9f2"}],
[:forwarded_port,
{:guest=>22, :host=>2200, :host_ip=>"127.0.0.1", :id=>"ssh", :auto_correct=>true, :protocol=>"tcp"}]]
}
subject { described_class.new(app, env) }
after do
sandbox.close
end
describe "#call" do
it "calls the next action in the chain" do
allow(driver).to receive(:host_vm?).and_return(false)
allow(driver).to receive(:existing_network?).and_return(true)
allow(driver).to receive(:network_used?).and_return(true)
called = false
app = ->(*args) { called = true }
action = described_class.new(app, env)
action.call(env)
expect(called).to eq(true)
end
it "calls the proper driver method to destroy the network" do
allow(driver).to receive(:host_vm?).and_return(false)
allow(driver).to receive(:existing_network?).with("vagrant_network_172.20.0.0/16").
and_return(true)
allow(driver).to receive(:network_used?).with("vagrant_network_172.20.0.0/16").
and_return(false)
allow(driver).to receive(:existing_network?).with("vagrant_network_2a02:6b8:b010:9020:1::/80").
and_return(true)
allow(driver).to receive(:network_used?).with("vagrant_network_2a02:6b8:b010:9020:1::/80").
and_return(false)
expect(driver).to receive(:rm_network).with("vagrant_network_172.20.0.0/16")
expect(driver).to receive(:rm_network).with("vagrant_network_2a02:6b8:b010:9020:1::/80")
subject.call(env)
end
it "doesn't destroy the network if another container is still using it" do
allow(driver).to receive(:host_vm?).and_return(false)
allow(driver).to receive(:existing_network?).with("vagrant_network_172.20.0.0/16").
and_return(true)
allow(driver).to receive(:network_used?).with("vagrant_network_172.20.0.0/16").
and_return(true)
allow(driver).to receive(:existing_network?).with("vagrant_network_2a02:6b8:b010:9020:1::/80").
and_return(true)
allow(driver).to receive(:network_used?).with("vagrant_network_2a02:6b8:b010:9020:1::/80").
and_return(true)
expect(driver).not_to receive(:rm_network).with("vagrant_network_172.20.0.0/16")
expect(driver).not_to receive(:rm_network).with("vagrant_network_2a02:6b8:b010:9020:1::/80")
subject.call(env)
end
end
end

View File

@ -0,0 +1,132 @@
require_relative "../../../../base"
require_relative "../../../../../../plugins/providers/docker/action/network"
describe VagrantPlugins::DockerProvider::Action::Network do
include_context "unit"
include_context "virtualbox"
let(:sandbox) { isolated_environment }
let(:iso_env) do
# We have to create a Vagrantfile so there is a root path
sandbox.vagrantfile("")
sandbox.create_vagrant_env
end
let(:machine) do
iso_env.machine(iso_env.machine_names[0], :docker).tap do |m|
allow(m.provider).to receive(:driver).and_return(driver)
allow(m.config.vm).to receive(:networks).and_return(networks)
end
end
let(:env) {{ machine: machine, ui: machine.ui, root_path: Pathname.new(".") }}
let(:app) { lambda { |*args| }}
let(:driver) { double("driver", create: "abcd1234") }
let(:networks) { [[:private_network,
{: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"}],
[:private_network,
{:type=>"dhcp",
:ipv6=>"true",
:subnet=>"2a02:6b8:b010:9020:1::/80",
:protocol=>"tcp",
:id=>"b8f23054-38d5-45c3-99ea-d33fc5d1b9f2"}],
[:forwarded_port,
{:guest=>22, :host=>2200, :host_ip=>"127.0.0.1", :id=>"ssh", :auto_correct=>true, :protocol=>"tcp"}]]
}
let(:invalid_network) {
[[:private_network,
{:ipv6=>"true",
:protocol=>"tcp",
:id=>"b8f23054-38d5-45c3-99ea-d33fc5d1b9f2"}]]
}
subject { described_class.new(app, env) }
after do
sandbox.close
end
describe "#call" do
it "calls the next action in the chain" do
allow(driver).to receive(:host_vm?).and_return(false)
allow(driver).to receive(:existing_network?).and_return(false)
allow(driver).to receive(:create_network).and_return(true)
allow(driver).to receive(:connect_network).and_return(true)
called = false
app = ->(*args) { called = true }
action = described_class.new(app, env)
action.call(env)
expect(called).to eq(true)
end
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_network?).and_return(false)
allow(driver).to receive(:create_network).and_return(true)
allow(driver).to receive(:connect_network).and_return(true)
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"])
expect(subject).to receive(:generate_connect_cli_arguments).
with(networks[0][1]).and_return(["--ipv6=true", "--subnet=2a02:6b8:b010:9020:1::/80"])
expect(subject).to receive(:generate_connect_cli_arguments).
with(networks[1][1]).and_return([])
subject.call(env)
end
it "raises an error if an inproper network configuration is given" do
allow(machine.config.vm).to receive(:networks).and_return(invalid_network)
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)
end
end
describe "#generate_connect_cli_arguments" do
let(:network_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"} }
it "returns an array of cli arguments" do
cli_args = subject.generate_connect_cli_arguments(network_options)
expect(cli_args).to eq(["--ip", "172.20.128.2", "--alias=mynetwork"])
end
end
describe "#generate_create_cli_arguments" do
let(:network_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"} }
it "returns an array of cli arguments" do
cli_args = subject.generate_create_cli_arguments(network_options)
expect(cli_args).to eq(["--subnet=172.20.0.0/16", "--driver=bridge", "--internal=true"])
end
end
end