Merge pull request #11068 from briancain/DOCKER-USABLE

Fix Docker providers usable? check
This commit is contained in:
Brian Cain 2019-09-12 08:40:44 -07:00 committed by GitHub
commit bab66df318
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
6 changed files with 112 additions and 16 deletions

View File

@ -11,6 +11,14 @@ module VagrantPlugins
class Provider < Vagrant.plugin("2", :provider) class Provider < Vagrant.plugin("2", :provider)
@@host_vm_mutex = Mutex.new @@host_vm_mutex = Mutex.new
def self.usable?(raise_error=false)
Driver.new.execute("docker", "version")
true
rescue Vagrant::Errors::CommandUnavailable, Errors::ExecuteError
raise if raise_error
return false
end
def initialize(machine) def initialize(machine)
@logger = Log4r::Logger.new("vagrant::provider::docker") @logger = Log4r::Logger.new("vagrant::provider::docker")
@machine = machine @machine = machine
@ -45,14 +53,6 @@ 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

@ -4,7 +4,6 @@ require_relative "../../../../../../plugins/providers/docker/action/connect_netw
describe VagrantPlugins::DockerProvider::Action::ConnectNetworks do describe VagrantPlugins::DockerProvider::Action::ConnectNetworks do
include_context "unit" include_context "unit"
include_context "virtualbox"
let(:sandbox) { isolated_environment } let(:sandbox) { isolated_environment }
@ -14,8 +13,18 @@ describe VagrantPlugins::DockerProvider::Action::ConnectNetworks do
sandbox.create_vagrant_env sandbox.create_vagrant_env
end end
let(:vm_config) { double("machine_vm_config") }
let(:machine_config) do
double("machine_config").tap do |top_config|
allow(top_config).to receive(:vm).and_return(vm_config)
end
end
let(:machine) do let(:machine) do
iso_env.machine(iso_env.machine_names[0], :docker).tap do |m| iso_env.machine(iso_env.machine_names[0], :docker).tap do |m|
allow(m).to receive(:vagrantfile).and_return(vagrantfile)
allow(m).to receive(:config).and_return(machine_config)
allow(m).to receive(:id).and_return("12345") allow(m).to receive(:id).and_return("12345")
allow(m.provider).to receive(:driver).and_return(driver) allow(m.provider).to receive(:driver).and_return(driver)
allow(m.provider).to receive(:host_vm?).and_return(false) allow(m.provider).to receive(:host_vm?).and_return(false)
@ -25,8 +34,10 @@ describe VagrantPlugins::DockerProvider::Action::ConnectNetworks do
let(:docker_connects) { {0=>"vagrant_network_172.20.0.0/16", 1=>"vagrant_network_public_wlp4s0", 2=>"vagrant_network_2a02:6b8:b010:9020:1::/80"} } let(:docker_connects) { {0=>"vagrant_network_172.20.0.0/16", 1=>"vagrant_network_public_wlp4s0", 2=>"vagrant_network_2a02:6b8:b010:9020:1::/80"} }
let(:vagrantfile) { double("vagrantfile") }
let(:env) {{ machine: machine, ui: machine.ui, root_path: Pathname.new("."), let(:env) {{ machine: machine, ui: machine.ui, root_path: Pathname.new("."),
docker_connects: docker_connects }} docker_connects: docker_connects, vagrantfile: vagrantfile }}
let(:app) { lambda { |*args| }} let(:app) { lambda { |*args| }}
let(:driver) { double("driver", create: "abcd1234") } let(:driver) { double("driver", create: "abcd1234") }
@ -55,6 +66,18 @@ describe VagrantPlugins::DockerProvider::Action::ConnectNetworks do
subject { described_class.new(app, env) } subject { described_class.new(app, env) }
let(:subprocess_result) do
double("subprocess_result").tap do |result|
allow(result).to receive(:exit_code).and_return(0)
allow(result).to receive(:stdout).and_return("")
allow(result).to receive(:stderr).and_return("")
end
end
before do
allow(Vagrant::Util::Subprocess).to receive(:execute).with("docker", "version", an_instance_of(Hash)).and_return(subprocess_result)
end
after do after do
sandbox.close sandbox.close
end end

View File

@ -12,14 +12,26 @@ describe VagrantPlugins::DockerProvider::Action::DestroyNetwork do
sandbox.create_vagrant_env sandbox.create_vagrant_env
end end
let(:vm_config) { double("machine_vm_config") }
let(:machine_config) do
double("machine_config").tap do |top_config|
allow(top_config).to receive(:vm).and_return(vm_config)
end
end
let(:machine) do let(:machine) do
iso_env.machine(iso_env.machine_names[0], :docker).tap do |m| iso_env.machine(iso_env.machine_names[0], :docker).tap do |m|
allow(m).to receive(:vagrantfile).and_return(vagrantfile)
allow(m).to receive(:config).and_return(machine_config)
allow(m.provider).to receive(:driver).and_return(driver) allow(m.provider).to receive(:driver).and_return(driver)
allow(m.config.vm).to receive(:networks).and_return(networks) allow(m.config.vm).to receive(:networks).and_return(networks)
end end
end end
let(:env) {{ machine: machine, ui: machine.ui, root_path: Pathname.new(".") }} let(:vagrantfile) { double("vagrantfile") }
let(:env) {{ machine: machine, ui: machine.ui, root_path: Pathname.new("."), vagrantfile: vagrantfile }}
let(:app) { lambda { |*args| }} let(:app) { lambda { |*args| }}
let(:driver) { double("driver", create: "abcd1234") } let(:driver) { double("driver", create: "abcd1234") }
@ -43,6 +55,18 @@ describe VagrantPlugins::DockerProvider::Action::DestroyNetwork do
subject { described_class.new(app, env) } subject { described_class.new(app, env) }
let(:subprocess_result) do
double("subprocess_result").tap do |result|
allow(result).to receive(:exit_code).and_return(0)
allow(result).to receive(:stdout).and_return("")
allow(result).to receive(:stderr).and_return("")
end
end
before do
allow(Vagrant::Util::Subprocess).to receive(:execute).with("docker", "version", an_instance_of(Hash)).and_return(subprocess_result)
end
after do after do
sandbox.close sandbox.close
end end

View File

@ -4,7 +4,6 @@ require_relative "../../../../../../plugins/providers/docker/action/login"
describe VagrantPlugins::DockerProvider::Action::Login do describe VagrantPlugins::DockerProvider::Action::Login do
include_context "unit" include_context "unit"
include_context "virtualbox"
let(:sandbox) { isolated_environment } let(:sandbox) { isolated_environment }
@ -16,22 +15,46 @@ describe VagrantPlugins::DockerProvider::Action::Login do
let(:provider_config) { double("provider_config", username: "docker", password: "") } let(:provider_config) { double("provider_config", username: "docker", password: "") }
let(:vm_config) { double("machine_vm_config") }
let(:machine_config) do
double("machine_config").tap do |top_config|
allow(top_config).to receive(:vm).and_return(vm_config)
end
end
let(:machine) do let(:machine) do
iso_env.machine(iso_env.machine_names[0], :docker).tap do |m| iso_env.machine(iso_env.machine_names[0], :docker).tap do |m|
allow(m).to receive(:id).and_return("12345") allow(m).to receive(:id).and_return("12345")
allow(m).to receive(:config).and_return(machine_config)
allow(m).to receive(:provider_config).and_return(provider_config) allow(m).to receive(:provider_config).and_return(provider_config)
allow(m).to receive(:vagrantfile).and_return(vagrantfile)
allow(m.provider).to receive(:driver).and_return(driver) allow(m.provider).to receive(:driver).and_return(driver)
allow(m.provider).to receive(:host_vm?).and_return(false) allow(m.provider).to receive(:host_vm?).and_return(false)
end end
end end
let(:env) {{ machine: machine, ui: machine.ui, root_path: Pathname.new(".") }} let(:vagrantfile) { double("vagrantfile") }
let(:env) {{ machine: machine, ui: machine.ui, root_path: Pathname.new("."), vagrantfile: vagrantfile }}
let(:app) { lambda { |*args| }} let(:app) { lambda { |*args| }}
let(:driver) { double("driver", create: "abcd1234") } let(:driver) { double("driver", create: "abcd1234") }
subject { described_class.new(app, env) } subject { described_class.new(app, env) }
let(:subprocess_result) do
double("subprocess_result").tap do |result|
allow(result).to receive(:exit_code).and_return(0)
allow(result).to receive(:stdout).and_return("")
allow(result).to receive(:stderr).and_return("")
end
end
before do
allow(Vagrant::Util::Subprocess).to receive(:execute).with("docker", "version", an_instance_of(Hash)).and_return(subprocess_result)
end
after do after do
sandbox.close sandbox.close
end end

View File

@ -3,7 +3,6 @@ require_relative "../../../../../../plugins/providers/docker/action/prepare_netw
describe VagrantPlugins::DockerProvider::Action::PrepareNetworks do describe VagrantPlugins::DockerProvider::Action::PrepareNetworks do
include_context "unit" include_context "unit"
include_context "virtualbox"
let(:sandbox) { isolated_environment } let(:sandbox) { isolated_environment }
@ -13,14 +12,26 @@ describe VagrantPlugins::DockerProvider::Action::PrepareNetworks do
sandbox.create_vagrant_env sandbox.create_vagrant_env
end end
let(:vm_config) { double("machine_vm_config") }
let(:machine_config) do
double("machine_config").tap do |top_config|
allow(top_config).to receive(:vm).and_return(vm_config)
end
end
let(:machine) do let(:machine) do
iso_env.machine(iso_env.machine_names[0], :docker).tap do |m| iso_env.machine(iso_env.machine_names[0], :docker).tap do |m|
allow(m).to receive(:vagrantfile).and_return(vagrantfile)
allow(m).to receive(:config).and_return(machine_config)
allow(m.provider).to receive(:driver).and_return(driver) allow(m.provider).to receive(:driver).and_return(driver)
allow(m.config.vm).to receive(:networks).and_return(networks) allow(m.config.vm).to receive(:networks).and_return(networks)
end end
end end
let(:env) {{ machine: machine, ui: machine.ui, root_path: Pathname.new(".") }} let(:vagrantfile) { double("vagrantfile") }
let(:env) {{ machine: machine, ui: machine.ui, root_path: Pathname.new("."), vagrantfile: vagrantfile }}
let(:app) { lambda { |*args| }} let(:app) { lambda { |*args| }}
let(:driver) { double("driver", create: "abcd1234") } let(:driver) { double("driver", create: "abcd1234") }
@ -56,6 +67,18 @@ describe VagrantPlugins::DockerProvider::Action::PrepareNetworks do
subject { described_class.new(app, env) } subject { described_class.new(app, env) }
let(:subprocess_result) do
double("subprocess_result").tap do |result|
allow(result).to receive(:exit_code).and_return(0)
allow(result).to receive(:stdout).and_return("")
allow(result).to receive(:stderr).and_return("")
end
end
before do
allow(Vagrant::Util::Subprocess).to receive(:execute).with("docker", "version", an_instance_of(Hash)).and_return(subprocess_result)
end
after do after do
sandbox.close sandbox.close
end end

View File

@ -21,9 +21,12 @@ describe VagrantPlugins::DockerProvider::Provider do
end end
describe ".usable?" do describe ".usable?" do
subject { described_class }
it "returns true if usable" do it "returns true if usable" do
allow(VagrantPlugins::DockerProvider::Driver).to receive(:new).and_return(driver_obj)
allow(provider_config).to receive(:compose).and_return(false) allow(provider_config).to receive(:compose).and_return(false)
allow(subject.driver).to receive(:execute).with("docker", "version").and_return(true) allow(driver_obj).to receive(:execute).with("docker", "version").and_return(true)
expect(subject).to be_usable expect(subject).to be_usable
end end