From 4a583f373b2a3073f4e758f4aaf502f98a23cb45 Mon Sep 17 00:00:00 2001 From: Anders Kaseorg Date: Tue, 10 Sep 2019 14:15:41 -0700 Subject: [PATCH] providers/docker: Fix usability check MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit In commit 7980178d194bb21bde2e21858fbc969e4cc7eb3f (#10879) I added a `usable?` class method to `VagrantPlugins::DockerProvider::Provider`. However, commit 34e53a5a4b208edcc3e25c1f0e3aa0ef56e8d8d9 (#10890) incorrectly changed it to an instance method. This rendered it ineffective because it’s called on the class, not an instance. Change it back to a class method. Signed-off-by: Anders Kaseorg --- plugins/providers/docker/provider.rb | 16 ++++++++-------- .../docker/action/destroy_network_test.rb | 1 + .../plugins/providers/docker/provider_test.rb | 5 ++++- test/unit/support/shared/virtualbox_context.rb | 5 +++++ 4 files changed, 18 insertions(+), 9 deletions(-) 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/destroy_network_test.rb b/test/unit/plugins/providers/docker/action/destroy_network_test.rb index e6856f022..27957cbaa 100644 --- a/test/unit/plugins/providers/docker/action/destroy_network_test.rb +++ b/test/unit/plugins/providers/docker/action/destroy_network_test.rb @@ -3,6 +3,7 @@ require_relative "../../../../../../plugins/providers/docker/action/destroy_netw describe VagrantPlugins::DockerProvider::Action::DestroyNetwork do include_context "unit" + include_context "virtualbox" let(:sandbox) { isolated_environment } 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 diff --git a/test/unit/support/shared/virtualbox_context.rb b/test/unit/support/shared/virtualbox_context.rb index 1909ccdc9..f28ec8420 100644 --- a/test/unit/support/shared/virtualbox_context.rb +++ b/test/unit/support/shared/virtualbox_context.rb @@ -28,6 +28,11 @@ shared_context "virtualbox" do allow(subprocess).to receive(:execute). with("VBoxManage", "showvminfo", kind_of(String), kind_of(Hash)). and_return(subprocess_result(exit_code: 0)) + + # apparently this is also used in docker tests + allow(subprocess).to receive(:execute). + with("docker", "version", an_instance_of(Hash)). + and_return(subprocess_result(exit_code: 0)) end around do |example|