From 8e7621061f2e69f5fb6c58cd43edd8309deac415 Mon Sep 17 00:00:00 2001 From: Mitchell Hashimoto Date: Tue, 2 Mar 2010 22:19:13 -0800 Subject: [PATCH] vagrant-up and vagrant-halt no longer raise exceptions when the VM is not in the proper state. --- lib/vagrant/actions/vm/halt.rb | 2 ++ lib/vagrant/vm.rb | 2 ++ test/vagrant/actions/vm/halt_test.rb | 12 ++++++++++++ test/vagrant/vm_test.rb | 10 ++++++++++ 4 files changed, 26 insertions(+) diff --git a/lib/vagrant/actions/vm/halt.rb b/lib/vagrant/actions/vm/halt.rb index 4da6ec03a..650b4a3ca 100644 --- a/lib/vagrant/actions/vm/halt.rb +++ b/lib/vagrant/actions/vm/halt.rb @@ -3,6 +3,8 @@ module Vagrant module VM class Halt < Base def execute! + raise ActionException.new("VM is not running! Nothing to shut down!") unless @runner.vm.running? + logger.info "Forcing shutdown of VM..." @runner.vm.stop(true) end diff --git a/lib/vagrant/vm.rb b/lib/vagrant/vm.rb index 49b5403ae..9502fd70c 100644 --- a/lib/vagrant/vm.rb +++ b/lib/vagrant/vm.rb @@ -26,6 +26,8 @@ module Vagrant end def start + return if @vm.running? + actions = [Actions::VM::ForwardPorts, Actions::VM::SharedFolders, Actions::VM::Start] actions.each do |action| add_action(action) diff --git a/test/vagrant/actions/vm/halt_test.rb b/test/vagrant/actions/vm/halt_test.rb index 39c0abdf2..313c3f53c 100644 --- a/test/vagrant/actions/vm/halt_test.rb +++ b/test/vagrant/actions/vm/halt_test.rb @@ -7,9 +7,21 @@ class HaltActionTest < Test::Unit::TestCase end context "executing" do + setup do + @vm.stubs(:running?).returns(true) + end + should "force the VM to stop" do @vm.expects(:stop).with(true).once @action.execute! end + + should "raise an ActionException if VM is not running" do + @vm.stubs(:running?).returns(false) + @vm.expects(:stop).never + assert_raises(Vagrant::Actions::ActionException) { + @action.execute! + } + end end end diff --git a/test/vagrant/vm_test.rb b/test/vagrant/vm_test.rb index d954076bb..f493f30e5 100644 --- a/test/vagrant/vm_test.rb +++ b/test/vagrant/vm_test.rb @@ -80,6 +80,16 @@ class VMTest < Test::Unit::TestCase end context "starting" do + setup do + @mock_vm.stubs(:running?).returns(false) + end + + should "not do anything if the VM is already running" do + @mock_vm.stubs(:running?).returns(true) + @vm.expects(:execute!).never + @vm.start + end + should "add and execute the proper actions" do actions = [Vagrant::Actions::VM::ForwardPorts, Vagrant::Actions::VM::SharedFolders, Vagrant::Actions::VM::Start]