From 5d94ab9e6092ff170748f44aae725d8988bd7e32 Mon Sep 17 00:00:00 2001 From: Mitchell Hashimoto Date: Mon, 5 May 2014 21:44:34 -0700 Subject: [PATCH] core: if state ID is NOT_CREATED_ID, destroy machine state --- lib/vagrant/machine.rb | 6 ++++++ lib/vagrant/machine_state.rb | 6 ++++++ test/unit/vagrant/machine_test.rb | 12 ++++++++++++ 3 files changed, 24 insertions(+) diff --git a/lib/vagrant/machine.rb b/lib/vagrant/machine.rb index 6c05d9966..7f2862ada 100644 --- a/lib/vagrant/machine.rb +++ b/lib/vagrant/machine.rb @@ -421,6 +421,12 @@ module Vagrant result = @provider.state raise Errors::MachineStateInvalid if !result.is_a?(MachineState) + # If the ID is the special not created ID, then set our ID to + # nil so that we destroy all our data. + if result.id == MachineState::NOT_CREATED_ID + self.id = nil + end + # Update our state cache if we have a UUID and an entry in the # master index. uuid = index_uuid diff --git a/lib/vagrant/machine_state.rb b/lib/vagrant/machine_state.rb index 6da2996e9..2800df390 100644 --- a/lib/vagrant/machine_state.rb +++ b/lib/vagrant/machine_state.rb @@ -14,6 +14,12 @@ module Vagrant # The long description can span multiple lines describing what the # state actually means. class MachineState + # This is a special ID that can be set for the state ID that + # tells Vagrant that the machine is not created. If this is the + # case, then Vagrant will set the ID to nil which will automatically + # clean out the machine data directory. + NOT_CREATED_ID = :not_created + # Unique ID for this state. # # @return [Symbol] diff --git a/test/unit/vagrant/machine_test.rb b/test/unit/vagrant/machine_test.rb index 9e8f6e5df..e483a8191 100644 --- a/test/unit/vagrant/machine_test.rb +++ b/test/unit/vagrant/machine_test.rb @@ -627,6 +627,18 @@ describe Vagrant::Machine do expect(entry.state).to eq("short") env.machine_index.release(entry) end + + it "should set the ID to nil if the state is not created" do + state = Vagrant::MachineState.new( + Vagrant::MachineState::NOT_CREATED_ID, "short", "long") + + allow(provider).to receive(:machine_id_changed) + subject.id = "foo" + + expect(provider).to receive(:state).and_return(state) + expect(subject.state.id).to eq(Vagrant::MachineState::NOT_CREATED_ID) + expect(subject.id).to be_nil + end end describe "#with_ui" do