diff --git a/lib/vagrant/machine.rb b/lib/vagrant/machine.rb index cff5a59f6..9ed774a9b 100644 --- a/lib/vagrant/machine.rb +++ b/lib/vagrant/machine.rb @@ -378,6 +378,19 @@ module Vagrant def state result = @provider.state raise Errors::MachineStateInvalid if !result.is_a?(MachineState) + + # Update our state cache if we have a UUID and an entry in the + # master index. + uuid = index_uuid + if uuid + entry = @env.machine_index.get(uuid) + if entry + entry.state = result.short_description + @env.machine_index.set(entry) + @env.machine_index.release(entry) + end + end + result end diff --git a/test/unit/vagrant/machine_test.rb b/test/unit/vagrant/machine_test.rb index 215f9c94d..784f205fa 100644 --- a/test/unit/vagrant/machine_test.rb +++ b/test/unit/vagrant/machine_test.rb @@ -517,7 +517,7 @@ describe Vagrant::Machine do end end - describe "state" do + describe "#state" do it "should query state from the provider" do state = Vagrant::MachineState.new(:id, "short", "long") @@ -530,6 +530,21 @@ describe Vagrant::Machine do expect { instance.state }. to raise_error(Vagrant::Errors::MachineStateInvalid) end + + it "should save the state with the index" do + allow(provider).to receive(:machine_id_changed) + subject.id = "foo" + + state = Vagrant::MachineState.new(:id, "short", "long") + expect(provider).to receive(:state).and_return(state) + + subject.state + + entry = env.machine_index.get(subject.index_uuid) + expect(entry).to_not be_nil + expect(entry.state).to eq("short") + env.machine_index.release(entry) + end end describe "#with_ui" do