core: Machine updates state cache when querying state

This commit is contained in:
Mitchell Hashimoto 2014-03-14 15:49:08 -07:00
parent 44e379e961
commit 13a4db391a
2 changed files with 29 additions and 1 deletions

View File

@ -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

View File

@ -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