Memoize machine.ssh_info when ready for connection

This commit is contained in:
Gilles Cornu 2014-10-20 17:45:02 +02:00
parent 4e81be879c
commit 89a4a29d65
2 changed files with 22 additions and 2 deletions

View File

@ -72,6 +72,12 @@ module Vagrant
# @return [Vagrantfile] # @return [Vagrantfile]
attr_reader :vagrantfile attr_reader :vagrantfile
# The SSH information for accessing this machine.
# This attribute is set only when the machine is ready for SSH communication.
#
# @return [Hash]
attr_reader :ssh_info
# Initialize a new machine. # Initialize a new machine.
# #
# @param [String] name Name of the virtual machine. # @param [String] name Name of the virtual machine.
@ -377,6 +383,9 @@ module Vagrant
# #
# @return [Hash] SSH information. # @return [Hash] SSH information.
def ssh_info def ssh_info
return @ssh_info unless @ssh_info.nil?
# First, ask the provider for their information. If the provider # First, ask the provider for their information. If the provider
# returns nil, then the machine is simply not ready for SSH, and # returns nil, then the machine is simply not ready for SSH, and
# we return nil as well. # we return nil as well.
@ -444,8 +453,8 @@ module Vagrant
end end
end end
# Return the final compiled SSH info data # Memoize the final compiled SSH info data and return it
info @ssh_info = info
end end
# Returns the state of this machine. The state is queried from the # Returns the state of this machine. The state is queried from the

View File

@ -583,6 +583,17 @@ describe Vagrant::Machine do
instance.ssh_info instance.ssh_info
end end
# It is not possible to test the memoization of a Ruby Hash with object equality,
# but we can verify that some code of ssh_info method is not executed again.
it "should check and try to fix the permissions of the private key file only once" do
provider_ssh_info[:private_key_path] = nil
instance.config.ssh.private_key_path = nil
expect(ssh_klass).to receive(:check_key_permissions).once.with(Pathname.new(instance.env.default_private_key_path.to_s))
instance.ssh_info
instance.ssh_info
end
context "expanding path relative to the root path" do context "expanding path relative to the root path" do
it "should with the provider key path" do it "should with the provider key path" do
provider_ssh_info[:private_key_path] = "~/foo" provider_ssh_info[:private_key_path] = "~/foo"