diff --git a/lib/vagrant/errors.rb b/lib/vagrant/errors.rb index ca0a0b9bf..8a66f436b 100644 --- a/lib/vagrant/errors.rb +++ b/lib/vagrant/errors.rb @@ -300,6 +300,10 @@ module Vagrant error_key(:command_unavailable_windows) end + class CommunicatorNotFound < VagrantError + error_key(:communicator_not_found) + end + class ConfigInvalid < VagrantError error_key(:config_invalid) end diff --git a/lib/vagrant/machine.rb b/lib/vagrant/machine.rb index 360d9c4aa..a3426c7b9 100644 --- a/lib/vagrant/machine.rb +++ b/lib/vagrant/machine.rb @@ -173,9 +173,10 @@ module Vagrant # @return [Object] def communicate if !@communicator - # For now, we always return SSH. In the future, we'll abstract - # this and allow plugins to define new methods of communication. - klass = Vagrant.plugin("2").manager.communicators[:ssh] + requested = @config.vm.communicator + requested ||= :ssh + klass = Vagrant.plugin("2").manager.communicators[requested] + raise Errors::CommunicatorNotFound, comm: requested.to_s if !klass @communicator = klass.new(self) end diff --git a/templates/locales/en.yml b/templates/locales/en.yml index e2ea4ede4..8af58badf 100644 --- a/templates/locales/en.yml +++ b/templates/locales/en.yml @@ -579,6 +579,9 @@ en: The executable '%{file}' Vagrant is trying to run was not found in the %PATH% variable. This is an error. Please verify this software is installed and on the path. + communicator_not_found: |- + The requested communicator '%{comm}' could not be found. + Please verify the name is correct and try again. config_invalid: |- There are errors in the configuration of this machine. Please fix the following errors and try again: diff --git a/test/unit/vagrant/machine_test.rb b/test/unit/vagrant/machine_test.rb index 5518e05e9..a4ab1c7ba 100644 --- a/test/unit/vagrant/machine_test.rb +++ b/test/unit/vagrant/machine_test.rb @@ -240,14 +240,27 @@ describe Vagrant::Machine do end end - describe "communicator" do - it "should always return the SSH communicator" do - expect(instance.communicate).to be_kind_of(VagrantPlugins::CommunicatorSSH::Communicator) + describe "#communicate" do + it "should return the SSH communicator by default" do + expect(subject.communicate). + to be_kind_of(VagrantPlugins::CommunicatorSSH::Communicator) + end + + it "should return the specified communicator if given" do + subject.config.vm.communicator = :winrm + expect(subject.communicate). + to be_kind_of(VagrantPlugins::CommunicatorWinRM::Communicator) end it "should memoize the result" do - obj = instance.communicate - expect(instance.communicate).to eql(obj) + obj = subject.communicate + expect(subject.communicate).to equal(obj) + end + + it "raises an exception if an invalid communicator is given" do + subject.config.vm.communicator = :foo + expect { subject.communicate }. + to raise_error(Vagrant::Errors::CommunicatorNotFound) end end