Properly discard VM state if saved on various commands such as destroy [closes GH-123]

This commit is contained in:
Mitchell Hashimoto 2010-07-29 20:15:38 -07:00
parent d17765c3dd
commit 8ea6d2bc40
4 changed files with 63 additions and 1 deletions

View File

@ -1,11 +1,14 @@
## 0.5.1 (unreleased)
- Fixed `halt`, `destroy`, `reload` to where they failed if the VM was
in a saved state. [GH-123]
- Added `config.chef.recipe_url` which allows you to specify a URL to
a gzipped tar file for chef solo to download cookbooks. See the
[chef-solo docs](http://wiki.opscode.com/display/chef/Chef+Solo#ChefSolo-RunningfromaURL) for more information.
[GH-121]
- Added `vagrant box repackage` which repackages boxes which have
been added. This is useful in case you want to redistribute a base
box you have but may have lost the actual "box" file.
box you have but may have lost the actual "box" file. [GH-120]
## Previous

View File

@ -32,6 +32,7 @@ module Vagrant
# halt - Halts the VM, attempting gracefully but then forcing
# a restart if fails.
halt = Builder.new do
use VM::DiscardState
use VM::Halt
use VM::DisableNetworks
end

View File

@ -0,0 +1,22 @@
module Vagrant
class Action
module VM
# Discards the saved state of the VM if its saved. If its
# not saved, does nothing.
class DiscardState
def initialize(app, env)
@app = app
end
def call(env)
if env["vm"].vm.saved?
env.logger.info "Discarding saved state of VM..."
env["vm"].vm.discard_state
end
@app.call(env)
end
end
end
end
end

View File

@ -0,0 +1,36 @@
require "test_helper"
class DiscardStateVMActionTest < Test::Unit::TestCase
setup do
@klass = Vagrant::Action::VM::DiscardState
@app, @env = mock_action_data
@vm = mock("vm")
@env["vm"] = @vm
@internal_vm = mock("internal")
@vm.stubs(:vm).returns(@internal_vm)
@instance = @klass.new(@app, @env)
end
context "calling" do
setup do
@internal_vm.stubs(:saved?).returns(false)
end
should "do nothing if not saved and continue chain" do
@internal_vm.expects(:saved?).returns(false)
@app.expects(:call).with(@env).once
@instance.call(@env)
end
should "discard state and continue chain" do
seq = sequence("sequence")
@internal_vm.expects(:saved?).returns(true).in_sequence(seq)
@internal_vm.expects(:discard_state).in_sequence(seq)
@app.expects(:call).with(@env).once.in_sequence(seq)
@instance.call(@env)
end
end
end