From 89a4a29d65cace4d970d0f220ad01815883ab8aa Mon Sep 17 00:00:00 2001 From: Gilles Cornu Date: Mon, 20 Oct 2014 17:45:02 +0200 Subject: [PATCH] Memoize machine.ssh_info when ready for connection --- lib/vagrant/machine.rb | 13 +++++++++++-- test/unit/vagrant/machine_test.rb | 11 +++++++++++ 2 files changed, 22 insertions(+), 2 deletions(-) diff --git a/lib/vagrant/machine.rb b/lib/vagrant/machine.rb index c38b983b2..4e3638c9b 100644 --- a/lib/vagrant/machine.rb +++ b/lib/vagrant/machine.rb @@ -72,6 +72,12 @@ module Vagrant # @return [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. # # @param [String] name Name of the virtual machine. @@ -377,6 +383,9 @@ module Vagrant # # @return [Hash] SSH information. def ssh_info + + return @ssh_info unless @ssh_info.nil? + # First, ask the provider for their information. If the provider # returns nil, then the machine is simply not ready for SSH, and # we return nil as well. @@ -444,8 +453,8 @@ module Vagrant end end - # Return the final compiled SSH info data - info + # Memoize the final compiled SSH info data and return it + @ssh_info = info end # Returns the state of this machine. The state is queried from the diff --git a/test/unit/vagrant/machine_test.rb b/test/unit/vagrant/machine_test.rb index ec7c67f53..f24b4bd42 100644 --- a/test/unit/vagrant/machine_test.rb +++ b/test/unit/vagrant/machine_test.rb @@ -583,6 +583,17 @@ describe Vagrant::Machine do instance.ssh_info 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 it "should with the provider key path" do provider_ssh_info[:private_key_path] = "~/foo"