From 7f5f720e0a3bf7b67fabc46021e187009a708686 Mon Sep 17 00:00:00 2001 From: Mitchell Hashimoto Date: Wed, 6 Aug 2014 16:46:31 -0700 Subject: [PATCH] core: Machine#reload --- lib/vagrant/machine.rb | 17 +++++++++++++---- test/unit/vagrant/machine_test.rb | 22 ++++++++++++++++++++++ 2 files changed, 35 insertions(+), 4 deletions(-) diff --git a/lib/vagrant/machine.rb b/lib/vagrant/machine.rb index 60670ba01..667447297 100644 --- a/lib/vagrant/machine.rb +++ b/lib/vagrant/machine.rb @@ -115,10 +115,7 @@ module Vagrant if base @id = name else - # Read the id file from the data directory if it exists as the - # ID for the pre-existing physical representation of this machine. - id_file = @data_dir.join("id") - @id = id_file.read.chomp if id_file.file? + reload end # Keep track of where our UUID should be placed @@ -340,6 +337,18 @@ module Vagrant "#<#{self.class}: #{@name} (#{@provider.class})>" end + # This reloads the ID of the underlying machine. + def reload + @id = nil + + if @data_dir + # Read the id file from the data directory if it exists as the + # ID for the pre-existing physical representation of this machine. + id_file = @data_dir.join("id") + @id = id_file.read.chomp if id_file.file? + end + end + # This returns the SSH info for accessing this machine. This SSH info # is queried from the underlying provider. This method returns `nil` if # the machine is not ready for SSH communication. diff --git a/test/unit/vagrant/machine_test.rb b/test/unit/vagrant/machine_test.rb index 0c40b713a..3e10d8959 100644 --- a/test/unit/vagrant/machine_test.rb +++ b/test/unit/vagrant/machine_test.rb @@ -448,6 +448,28 @@ describe Vagrant::Machine do end end + describe "#reload" do + before do + allow(provider).to receive(:machine_id_changed) + + subject.id = "foo" + end + + it "should read the ID" do + subject.reload + + expect(subject.id).to eq("foo") + end + + it "should read the updated ID" do + new_instance.id = "bar" + + subject.reload + + expect(subject.id).to eq("bar") + end + end + describe "ssh info" do describe "with the provider returning nil" do it "should return nil if the provider returns nil" do