core: Configurable communicator

This commit is contained in:
Mitchell Hashimoto 2014-03-10 23:12:14 -07:00
parent 40d89cf5ea
commit f07ee5754a
4 changed files with 29 additions and 8 deletions

View File

@ -300,6 +300,10 @@ module Vagrant
error_key(:command_unavailable_windows) error_key(:command_unavailable_windows)
end end
class CommunicatorNotFound < VagrantError
error_key(:communicator_not_found)
end
class ConfigInvalid < VagrantError class ConfigInvalid < VagrantError
error_key(:config_invalid) error_key(:config_invalid)
end end

View File

@ -173,9 +173,10 @@ module Vagrant
# @return [Object] # @return [Object]
def communicate def communicate
if !@communicator if !@communicator
# For now, we always return SSH. In the future, we'll abstract requested = @config.vm.communicator
# this and allow plugins to define new methods of communication. requested ||= :ssh
klass = Vagrant.plugin("2").manager.communicators[:ssh] klass = Vagrant.plugin("2").manager.communicators[requested]
raise Errors::CommunicatorNotFound, comm: requested.to_s if !klass
@communicator = klass.new(self) @communicator = klass.new(self)
end end

View File

@ -579,6 +579,9 @@ en:
The executable '%{file}' Vagrant is trying to run was not The executable '%{file}' Vagrant is trying to run was not
found in the %PATH% variable. This is an error. Please verify found in the %PATH% variable. This is an error. Please verify
this software is installed and on the path. 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: |- config_invalid: |-
There are errors in the configuration of this machine. Please fix There are errors in the configuration of this machine. Please fix
the following errors and try again: the following errors and try again:

View File

@ -240,14 +240,27 @@ describe Vagrant::Machine do
end end
end end
describe "communicator" do describe "#communicate" do
it "should always return the SSH communicator" do it "should return the SSH communicator by default" do
expect(instance.communicate).to be_kind_of(VagrantPlugins::CommunicatorSSH::Communicator) 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 end
it "should memoize the result" do it "should memoize the result" do
obj = instance.communicate obj = subject.communicate
expect(instance.communicate).to eql(obj) 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
end end