`vagrant ssh-config` is now multi-vm friendly

This commit is contained in:
Mitchell Hashimoto 2010-05-17 10:52:07 -07:00
parent 586a56b5b5
commit f30645a967
3 changed files with 69 additions and 26 deletions

View File

@ -9,12 +9,27 @@ module Vagrant
def execute(args=[])
env.require_root_path
parse_options(args)
args = parse_options(args)
show_single(args[0])
end
def show_single(name)
if name.nil? && env.multivm?
error_and_exit(:ssh_config_multivm)
return # for tests
end
vm = name.nil? ? env.vms.values.first : env.vms[name.to_sym]
if vm.nil?
error_and_exit(:unknown_vm, :vm => name)
return # for tests
end
puts TemplateRenderer.render("ssh_config", {
:host_key => options[:host] || "vagrant",
:ssh_user => env.config.ssh.username,
:ssh_port => env.ssh.port,
:private_key_path => env.config.ssh.private_key_path
:ssh_user => vm.env.config.ssh.username,
:ssh_port => vm.ssh.port,
:private_key_path => vm.env.config.ssh.private_key_path
})
end
@ -27,4 +42,4 @@ module Vagrant
end
end
end
end
end

View File

@ -148,6 +148,11 @@
specific VM must be specified. This can be done by calling
`vagrant ssh NAME` where NAME is a valid VM represented by
your Vagrantfile.
:ssh_config_multivm: |-
Because this Vagrant environment represents multiple VMs, a
specific VM must be specified. This can be done by calling
`vagrant ssh-config NAME` where NAME is a valid VM represented by
your Vagrantfile.
:system_invalid_class: |-
The specified system does not inherit from `Vagrant::Systems::Base`. The
specified system class must inherit from this class.

View File

@ -4,31 +4,14 @@ class CommandsSSHConfigTest < Test::Unit::TestCase
setup do
@klass = Vagrant::Commands::SSHConfig
@persisted_vm = mock("persisted_vm")
@persisted_vm.stubs(:execute!)
@env = mock_environment
@env.stubs(:require_persisted_vm)
@env.stubs(:vm).returns(@persisted_vm)
@env.stubs(:require_root_path)
@instance = @klass.new(@env)
end
context "executing" do
setup do
@ssh = mock("ssh")
@ssh.stubs(:port).returns(2197)
@env.stubs(:ssh).returns(@ssh)
@env.stubs(:require_root_path)
@instance.stubs(:puts)
@data = {
:host_key => "vagrant",
:ssh_user => @env.config.ssh.username,
:ssh_port => @env.ssh.port,
:private_key_path => @env.config.ssh.private_key_path
}
@instance.stubs(:show_single)
end
should "require root path" do
@ -36,19 +19,59 @@ class CommandsSSHConfigTest < Test::Unit::TestCase
@instance.execute
end
should "call show_single with given argument" do
@instance.expects(:show_single).with("foo").once
@instance.execute(["foo"])
end
end
context "showing a single entry" do
setup do
@ssh = mock("ssh")
@ssh.stubs(:port).returns(2197)
@bar = mock("vm")
@bar.stubs(:env).returns(mock_environment)
@bar.stubs(:ssh).returns(@ssh)
@vms = {:bar => @bar}
@env.stubs(:multivm?).returns(true)
@env.stubs(:vms).returns(@vms)
@data = {
:host_key => "vagrant",
:ssh_user => @bar.env.config.ssh.username,
:ssh_port => @bar.ssh.port,
:private_key_path => @bar.env.config.ssh.private_key_path
}
@instance.stubs(:puts)
end
should "error if name is nil and multivm" do
@env.stubs(:multivm?).returns(true)
@instance.expects(:error_and_exit).with(:ssh_config_multivm).once
@instance.show_single(nil)
end
should "error if the VM is not found" do
@instance.expects(:error_and_exit).with(:unknown_vm, :vm => "foo").once
@instance.show_single("foo")
end
should "output rendered template" do
result = mock("result")
Vagrant::Util::TemplateRenderer.expects(:render).with("ssh_config", @data).returns(result)
@instance.expects(:puts).with(result).once
@instance.execute
@instance.show_single(:bar)
end
should "render with the given host name if given" do
host = "foo"
@data[:host_key] = host
Vagrant::Util::TemplateRenderer.expects(:render).with("ssh_config", @data)
@instance.execute(["--host", host])
@instance.execute(["bar", "--host", host])
end
end
end