Merge pull request #10890 from briancain/add-usability-check-for-docker-plus-tests

Add usability check for docker plus tests
This commit is contained in:
Brian Cain 2019-06-05 07:58:23 -07:00 committed by GitHub
commit fb80e8da9f
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 233 additions and 0 deletions

View File

@ -45,6 +45,14 @@ module VagrantPlugins
@driver @driver
end end
def usable?(raise_error=false)
driver.execute("docker", "version")
true
rescue Vagrant::Errors::CommandUnavailable, Errors::ExecuteError
raise if raise_error
return false
end
# This returns the {Vagrant::Machine} that is our host machine. # This returns the {Vagrant::Machine} that is our host machine.
# It does not perform any action on the machine or verify it is # It does not perform any action on the machine or verify it is
# running. # running.

View File

@ -0,0 +1,128 @@
require_relative "../../../base"
require Vagrant.source_root.join("plugins/providers/docker/provider")
describe VagrantPlugins::DockerProvider::Provider do
let(:driver_obj){ double("driver") }
let(:provider){ double("provider", driver: driver_obj) }
let(:provider_config){ double("provider_config", force_host_vm: false) }
let(:ssh) { double("ssh", guest_port: 22) }
let(:config) { double("config", ssh: ssh) }
let(:machine){ double("machine", provider: provider, provider_config: provider_config, config: config) }
let(:platform) { double("platform") }
subject { described_class.new(machine) }
before do
stub_const("Vagrant::Util::Platform", platform)
allow(machine).to receive(:id).and_return("foo")
end
describe ".usable?" do
subject { described_class.new(machine) }
it "returns true if usable" do
allow(provider_config).to receive(:compose).and_return(false)
allow(subject.driver).to receive(:execute).with("docker", "version").and_return(true)
expect(subject).to be_usable
end
it "raises an exception if docker is not available" do
allow(VagrantPlugins::DockerProvider::Driver).to receive(:new).and_return(driver_obj)
allow(provider_config).to receive(:compose).and_return(false)
allow(platform).to receive(:windows?).and_return(false)
allow(platform).to receive(:darwin?).and_return(false)
allow(driver_obj).to receive(:execute).with("docker", "version").
and_raise(Vagrant::Errors::CommandUnavailable, file: "docker")
expect { subject.usable?(true) }.
to raise_error(Vagrant::Errors::CommandUnavailable)
end
end
describe "#driver" do
it "is initialized" do
allow(provider_config).to receive(:compose).and_return(false)
allow(platform).to receive(:windows?).and_return(false)
allow(platform).to receive(:darwin?).and_return(false)
expect(subject.driver).to be_kind_of(VagrantPlugins::DockerProvider::Driver)
end
end
describe "#state" do
it "returns not_created if no ID" do
allow(machine).to receive(:id).and_return(nil)
expect(subject.state.id).to eq(:not_created)
end
it "calls an action to determine the ID" do
allow(provider_config).to receive(:compose).and_return(false)
allow(platform).to receive(:windows?).and_return(false)
allow(platform).to receive(:darwin?).and_return(false)
allow(machine).to receive(:id).and_return("foo")
expect(subject.state.id).to eq(:not_created)
end
end
describe "#host_vm" do
let(:host_env) { double("host_env", root_path: "/vagrant.d", default_provider: :virtualbox) }
it "returns the host machine object" do
allow(machine.provider_config).to receive(:vagrant_vagrantfile).and_return("/path/to/Vagrantfile")
allow(machine.provider_config).to receive(:vagrant_machine).and_return(:default)
allow(machine).to receive(:env).and_return(true)
allow(machine.env).to receive(:root_path).and_return("/.vagrant.d")
allow(machine.env).to receive(:home_path).and_return("/path/to")
allow(machine.env).to receive(:ui_class).and_return(true)
expect(Vagrant::Environment).to receive(:new).and_return(host_env)
allow(host_env).to receive(:machine).and_return(true)
subject.host_vm
end
end
describe "#ssh_info" do
let(:result) { "127.0.0.1" }
let(:exit_code) { 0 }
let(:ssh_info) {{:host=>result,:port=>22}}
let(:network_settings) { {"NetworkSettings" => {"Bridge"=>"", "SandboxID"=>"randomid", "HairpinMode"=>false, "LinkLocalIPv6Address"=>"", "LinkLocalIPv6PrefixLen"=>0, "Ports"=>{"443/tcp"=>nil, "80/tcp"=>nil}, "SandboxKey"=>"/var/run/docker/netns/158b7024a9e4", "SecondaryIPAddresses"=>nil, "SecondaryIPv6Addresses"=>nil, "EndpointID"=>"randomEndpointID", "Gateway"=>"172.17.0.1", "GlobalIPv6Address"=>"", "GlobalIPv6PrefixLen"=>0, "IPAddress"=>"127.0.0.1", "IPPrefixLen"=>16, "IPv6Gateway"=>"", "MacAddress"=>"02:42:ac:11:00:02", "Networks"=>{"bridge"=>{"IPAMConfig"=>nil, "Links"=>nil, "Aliases"=>nil, "NetworkID"=>"networkIDVar", "EndpointID"=>"endpointIDVar", "Gateway"=>"127.0.0.1", "IPAddress"=>"127.0.0.1", "IPPrefixLen"=>16, "IPv6Gateway"=>"", "GlobalIPv6Address"=>"", "GlobalIPv6PrefixLen"=>0, "MacAddress"=>"02:42:ac:11:00:02", "DriverOpts"=>nil}}}} }
let(:empty_network_settings) { {"NetworkSettings" => {"Bridge"=>"", "SandboxID"=>"randomid", "HairpinMode"=>false, "LinkLocalIPv6Address"=>"", "LinkLocalIPv6PrefixLen"=>0, "Ports"=>"", "SandboxKey"=>"/var/run/docker/netns/158b7024a9e4", "SecondaryIPAddresses"=>nil, "SecondaryIPv6Addresses"=>nil, "EndpointID"=>"randomEndpointID", "Gateway"=>"172.17.0.1", "GlobalIPv6Address"=>"", "GlobalIPv6PrefixLen"=>0, "IPAddress"=>"", "IPPrefixLen"=>16, "IPv6Gateway"=>"", "MacAddress"=>"02:42:ac:11:00:02", "Networks"=>{"bridge"=>{"IPAMConfig"=>nil, "Links"=>nil, "Aliases"=>nil, "NetworkID"=>"networkIDVar", "EndpointID"=>"endpointIDVar", "Gateway"=>"127.0.0.1", "IPAddress"=>"127.0.0.1", "IPPrefixLen"=>16, "IPv6Gateway"=>"", "GlobalIPv6Address"=>"", "GlobalIPv6PrefixLen"=>0, "MacAddress"=>"02:42:ac:11:00:02", "DriverOpts"=>nil}}}} }
before do
allow(VagrantPlugins::DockerProvider::Driver).to receive(:new).and_return(driver_obj)
allow(machine).to receive(:action).with(:read_state).and_return(machine_state_id: :running)
end
it "returns nil if a port info is nil from the driver" do
allow(provider_config).to receive(:compose).and_return(false)
allow(platform).to receive(:windows?).and_return(false)
allow(platform).to receive(:darwin?).and_return(false)
allow(driver_obj).to receive(:created?).and_return(true)
allow(driver_obj).to receive(:state).and_return(:running)
allow(driver_obj).to receive(:inspect_container).and_return(empty_network_settings)
expect(subject.ssh_info).to eq(nil)
end
it "should receive a valid address" do
allow(provider_config).to receive(:compose).and_return(false)
allow(platform).to receive(:windows?).and_return(false)
allow(platform).to receive(:darwin?).and_return(false)
allow(driver_obj).to receive(:created?).and_return(true)
allow(driver_obj).to receive(:state).and_return(:running)
allow(driver_obj).to receive(:execute).with(:get_network_config).and_return(result)
allow(driver_obj).to receive(:inspect_container).and_return(network_settings)
expect(subject.ssh_info).to eq(ssh_info)
end
end
end

View File

@ -0,0 +1,97 @@
require_relative "../../../base"
require Vagrant.source_root.join("plugins/providers/virtualbox/provider")
describe VagrantPlugins::ProviderVirtualBox::Provider do
let(:driver){ double("driver") }
let(:provider){ double("provider", driver: driver) }
let(:provider_config){ double("provider_config") }
let(:uid) { "1000" }
let(:machine){ double("machine", uid: uid, provider: provider, provider_config: provider_config) }
let(:platform) { double("platform") }
subject { described_class.new(machine) }
before do
stub_const("Vagrant::Util::Platform", platform)
allow(platform).to receive(:windows?).and_return(false)
allow(platform).to receive(:cygwin?).and_return(false)
allow(platform).to receive(:wsl?).and_return(false)
allow(platform).to receive(:wsl_windows_access_bypass?).and_return(false)
allow(machine).to receive(:id).and_return("foo")
allow(Process).to receive(:uid).and_return(uid)
end
describe ".usable?" do
subject { described_class }
it "returns true if usable" do
allow(VagrantPlugins::ProviderVirtualBox::Driver::Meta).to receive(:new).and_return(driver)
expect(subject).to be_usable
end
it "raises an exception if virtualbox is not available" do
allow(VagrantPlugins::ProviderVirtualBox::Driver::Meta).to receive(:new).
and_raise(Vagrant::Errors::VirtualBoxNotDetected)
expect { subject.usable?(true) }.
to raise_error(Vagrant::Errors::VirtualBoxNotDetected)
end
it "raises an exception if virtualbox is the wrong version" do
allow(VagrantPlugins::ProviderVirtualBox::Driver::Meta).to receive(:new).
and_raise(Vagrant::Errors::VirtualBoxInvalidVersion, supported_versions: "1,2,3")
expect { subject.usable?(true) }.
to raise_error(Vagrant::Errors::VirtualBoxInvalidVersion)
end
end
describe "#driver" do
it "is initialized" do
allow(VagrantPlugins::ProviderVirtualBox::Driver::Meta).to receive(:new).and_return(driver)
expect(subject.driver).to be(driver)
end
end
describe "#state" do
it "returns not_created if no ID" do
allow(machine).to receive(:id).and_return(nil)
allow(machine).to receive(:data_dir).and_return(".vagrant")
expect(subject.state.id).to eq(:not_created)
end
end
describe "#ssh_info" do
let(:result) { "127.0.0.1" }
let(:exit_code) { 0 }
let(:ssh_info) {{:host=>result,:port=>22}}
let(:ssh) { double("ssh", guest_port: 22) }
let(:config) { double("config", ssh: ssh) }
before do
allow(VagrantPlugins::ProviderVirtualBox::Driver::Meta).to receive(:new).and_return(driver)
allow(machine).to receive(:action).with(:read_state).and_return(machine_state_id: :running)
allow(machine).to receive(:data_dir).and_return(".vagrant")
allow(driver).to receive(:uuid).and_return("1234")
allow(driver).to receive(:read_state).and_return(:running)
allow(driver).to receive(:ssh_port).and_return(22)
allow(machine).to receive(:config).and_return(config)
end
it "returns nil if machine state is not running" do
allow(driver).to receive(:read_state).and_return(:not_created)
expect(subject.ssh_info).to eq(nil)
end
it "should receive a valid address" do
allow(driver).to receive(:execute).with(:get_network_config).and_return(result)
allow(driver).to receive(:read_guest_ip).and_return({"ip" => "127.0.0.1"})
expect(subject.ssh_info).to eq(ssh_info)
end
end
end