diff --git a/plugins/providers/docker/provider.rb b/plugins/providers/docker/provider.rb index acef00498..5a4779a08 100644 --- a/plugins/providers/docker/provider.rb +++ b/plugins/providers/docker/provider.rb @@ -11,6 +11,14 @@ module VagrantPlugins class Provider < Vagrant.plugin("2", :provider) @@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) @logger = Log4r::Logger.new("vagrant::provider::docker") @machine = machine @@ -45,14 +53,6 @@ module VagrantPlugins @driver 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. # It does not perform any action on the machine or verify it is # running. diff --git a/test/unit/plugins/providers/docker/action/connect_networks_test.rb b/test/unit/plugins/providers/docker/action/connect_networks_test.rb index 8d57f1d4b..3b7dbd27b 100644 --- a/test/unit/plugins/providers/docker/action/connect_networks_test.rb +++ b/test/unit/plugins/providers/docker/action/connect_networks_test.rb @@ -4,7 +4,6 @@ require_relative "../../../../../../plugins/providers/docker/action/connect_netw describe VagrantPlugins::DockerProvider::Action::ConnectNetworks do include_context "unit" - include_context "virtualbox" let(:sandbox) { isolated_environment } @@ -14,8 +13,18 @@ describe VagrantPlugins::DockerProvider::Action::ConnectNetworks do sandbox.create_vagrant_env 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 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.provider).to receive(:driver).and_return(driver) 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(:vagrantfile) { double("vagrantfile") } + 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(:driver) { double("driver", create: "abcd1234") } @@ -55,6 +66,18 @@ describe VagrantPlugins::DockerProvider::Action::ConnectNetworks do 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 sandbox.close end diff --git a/test/unit/plugins/providers/docker/action/destroy_network_test.rb b/test/unit/plugins/providers/docker/action/destroy_network_test.rb index e6856f022..dad664dd3 100644 --- a/test/unit/plugins/providers/docker/action/destroy_network_test.rb +++ b/test/unit/plugins/providers/docker/action/destroy_network_test.rb @@ -12,14 +12,26 @@ describe VagrantPlugins::DockerProvider::Action::DestroyNetwork do sandbox.create_vagrant_env 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 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.config.vm).to receive(:networks).and_return(networks) 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(:driver) { double("driver", create: "abcd1234") } @@ -43,6 +55,18 @@ describe VagrantPlugins::DockerProvider::Action::DestroyNetwork do 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 sandbox.close end diff --git a/test/unit/plugins/providers/docker/action/login_test.rb b/test/unit/plugins/providers/docker/action/login_test.rb index 85b68897b..48daf10c9 100644 --- a/test/unit/plugins/providers/docker/action/login_test.rb +++ b/test/unit/plugins/providers/docker/action/login_test.rb @@ -4,7 +4,6 @@ require_relative "../../../../../../plugins/providers/docker/action/login" describe VagrantPlugins::DockerProvider::Action::Login do include_context "unit" - include_context "virtualbox" let(:sandbox) { isolated_environment } @@ -16,22 +15,46 @@ describe VagrantPlugins::DockerProvider::Action::Login do 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 iso_env.machine(iso_env.machine_names[0], :docker).tap do |m| 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(:vagrantfile).and_return(vagrantfile) allow(m.provider).to receive(:driver).and_return(driver) allow(m.provider).to receive(:host_vm?).and_return(false) 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(:driver) { double("driver", create: "abcd1234") } 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 sandbox.close end 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 1a2ccb7eb..095ca4335 100644 --- a/test/unit/plugins/providers/docker/action/prepare_networks_test.rb +++ b/test/unit/plugins/providers/docker/action/prepare_networks_test.rb @@ -3,7 +3,6 @@ require_relative "../../../../../../plugins/providers/docker/action/prepare_netw describe VagrantPlugins::DockerProvider::Action::PrepareNetworks do include_context "unit" - include_context "virtualbox" let(:sandbox) { isolated_environment } @@ -13,14 +12,26 @@ describe VagrantPlugins::DockerProvider::Action::PrepareNetworks do sandbox.create_vagrant_env 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 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.config.vm).to receive(:networks).and_return(networks) 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(:driver) { double("driver", create: "abcd1234") } @@ -56,6 +67,18 @@ describe VagrantPlugins::DockerProvider::Action::PrepareNetworks do 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 sandbox.close end diff --git a/test/unit/plugins/providers/docker/provider_test.rb b/test/unit/plugins/providers/docker/provider_test.rb index 64d1f4d6d..3cb6e03cb 100644 --- a/test/unit/plugins/providers/docker/provider_test.rb +++ b/test/unit/plugins/providers/docker/provider_test.rb @@ -21,9 +21,12 @@ describe VagrantPlugins::DockerProvider::Provider do end describe ".usable?" do + subject { described_class } + 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(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 end