diff --git a/plugins/providers/docker/errors.rb b/plugins/providers/docker/errors.rb index 77824b265..97b895f39 100644 --- a/plugins/providers/docker/errors.rb +++ b/plugins/providers/docker/errors.rb @@ -1,12 +1,21 @@ module VagrantPlugins module DockerProvider module Errors - class ImageNotConfiguredError < Vagrant::Errors::VagrantError + class DockerError < Vagrant::Errors::VagrantError + error_namespace("docker_provider.errors") + end + + class ImageNotConfiguredError < DockerError error_key(:docker_provider_image_not_configured) end - class NfsWithoutPrivilegedError < Vagrant::Errors::VagrantError + + class NfsWithoutPrivilegedError < DockerError error_key(:docker_provider_nfs_without_privileged) end + + class SyncedFolderNonDocker < DockerError + error_key(:synced_folder_non_docker) + end end end end diff --git a/plugins/providers/docker/synced_folder.rb b/plugins/providers/docker/synced_folder.rb index 9aad24761..1416b6c39 100644 --- a/plugins/providers/docker/synced_folder.rb +++ b/plugins/providers/docker/synced_folder.rb @@ -1,9 +1,18 @@ module VagrantPlugins module DockerProvider class SyncedFolder < Vagrant.plugin("2", :synced_folder) - def usable?(machine) + def usable?(machine, raise_error=false) # These synced folders only work if the provider is Docker - machine.provider_name == :docker + if machine.provider_name != :docker + if raise_error + raise Errors::SyncedFolderNonDocker, + provider: machine.provider_name.to_s + end + + return false + end + + true end def prepare(machine, folders, _opts) diff --git a/templates/locales/providers_docker.yml b/templates/locales/providers_docker.yml index 62b30a2c6..a45524dc3 100644 --- a/templates/locales/providers_docker.yml +++ b/templates/locales/providers_docker.yml @@ -18,14 +18,14 @@ en: config: cmd_not_set: |- The Docker command has not been set! - - vagrant: - errors: docker_provider_nfs_without_privileged: |- You've configured a NFS synced folder but didn't enable privileged mode for the container. Please set the `privileged` option to true on the provider block from your Vagrantfile, recreate the container and try again. - docker_provider_image_not_configured: |- The base Docker image has not been set for the '%{name}' VM! + synced_folder_non_docker: |- + The "docker" synced folder type can't be used because the provider + in use is not Docker. This synced folder type only works with the + Docker provider. The provider this machine is using is: %{provider} diff --git a/test/unit/plugins/providers/docker/synced_folder_spec.rb b/test/unit/plugins/providers/docker/synced_folder_spec.rb new file mode 100644 index 000000000..dfbcaed0c --- /dev/null +++ b/test/unit/plugins/providers/docker/synced_folder_spec.rb @@ -0,0 +1,30 @@ +require_relative "../../../base" + +require Vagrant.source_root.join("plugins/providers/docker/synced_folder") + +describe VagrantPlugins::DockerProvider::SyncedFolder do + subject { described_class.new } + + describe "#usable?" do + let(:machine) { double("machine") } + + before do + machine.stub(provider_name: :docker) + end + + it "is usable" do + expect(subject).to be_usable(machine) + end + + it "is not usable if provider isn't docker" do + machine.stub(provider_name: :virtualbox) + expect(subject).to_not be_usable(machine) + end + + it "raises an error if bad provider if specified" do + machine.stub(provider_name: :virtualbox) + expect { subject.usable?(machine, true) }. + to raise_error(VagrantPlugins::DockerProvider::Errors::SyncedFolderNonDocker) + end + end +end