From 3b4d2ab7951b296bdc52ea5d61efadd03fc70c64 Mon Sep 17 00:00:00 2001 From: Mitchell Hashimoto Date: Sun, 16 May 2010 18:22:28 -0700 Subject: [PATCH] `vagrant halt` updated to work with multi-VM --- lib/vagrant/commands/halt.rb | 31 ++++++++++++++--- lib/vagrant/vm.rb | 4 +++ test/vagrant/commands/halt_test.rb | 56 +++++++++++++++++++++++++----- test/vagrant/vm_test.rb | 12 +++++++ 4 files changed, 90 insertions(+), 13 deletions(-) diff --git a/lib/vagrant/commands/halt.rb b/lib/vagrant/commands/halt.rb index 4572aa2b8..337dff2b5 100644 --- a/lib/vagrant/commands/halt.rb +++ b/lib/vagrant/commands/halt.rb @@ -11,10 +11,33 @@ module Vagrant description "Halts the currently running vagrant environment" def execute(args=[]) - parse_options(args) + args = parse_options(args) - env.require_persisted_vm - env.vm.execute!(Actions::VM::Halt, options[:force]) + if args[0] + halt_single(args[0]) + else + halt_all + end + end + + def halt_single(name) + vm = env.vms[name.to_sym] + if vm.nil? + error_and_exit(:unknown_vm, :vm => name) + return # for tests + end + + if vm.created? + vm.halt(options[:force]) + else + logger.info "VM '#{name}' not created. Ignoring." + end + end + + def halt_all + env.vms.keys.each do |name| + halt_single(name) + end end def options_spec(opts) @@ -29,4 +52,4 @@ module Vagrant end end end -end \ No newline at end of file +end diff --git a/lib/vagrant/vm.rb b/lib/vagrant/vm.rb index 51059bd72..618213519 100644 --- a/lib/vagrant/vm.rb +++ b/lib/vagrant/vm.rb @@ -105,6 +105,10 @@ module Vagrant execute!(Actions::VM::Start) end + def halt(force=false) + execute!(Actions::VM::Halt, force) + end + def destroy execute!(Actions::VM::Down) end diff --git a/test/vagrant/commands/halt_test.rb b/test/vagrant/commands/halt_test.rb index 5b97f2273..08ef2b069 100644 --- a/test/vagrant/commands/halt_test.rb +++ b/test/vagrant/commands/halt_test.rb @@ -15,21 +15,59 @@ class CommandsHaltTest < Test::Unit::TestCase end context "executing" do - should "require a persisted VM" do - @env.expects(:require_persisted_vm).once + should "call halt_all if no name is given" do + @instance.expects(:halt_all).once @instance.execute end - should "call the `halt` action on the VM" do - @persisted_vm.expects(:execute!).with(Vagrant::Actions::VM::Halt, false).once - @instance.execute + should "call halt_single if a name is given" do + @instance.expects(:halt_single).with("foo").once + @instance.execute(["foo"]) end + end - should "be forceful if -f flag is sent" do - %w{--force -f}.each do |flag| - @persisted_vm.expects(:execute!).with(Vagrant::Actions::VM::Halt, true).once - @instance.execute([flag]) + context "halting all" do + should "halt each VM" do + vms = { :foo => nil, :bar => nil, :baz => nil } + @env.stubs(:vms).returns(vms) + + vms.each do |name, value| + @instance.expects(:halt_single).with(name).once end + + @instance.halt_all + end + end + + context "halting a single VM" do + setup do + @foo_vm = mock("vm") + vms = { :foo => @foo_vm } + @env.stubs(:vms).returns(vms) + end + + should "error and exit if the VM doesn't exist" do + @env.stubs(:vms).returns({}) + @instance.expects(:error_and_exit).with(:unknown_vm, :vm => :foo).once + @instance.halt_single(:foo) + end + + should "halt if its created" do + @foo_vm.stubs(:created?).returns(true) + @foo_vm.expects(:halt).with(false).once + @instance.execute(["foo"]) + end + + should "halt and force if specified" do + @foo_vm.stubs(:created?).returns(true) + @foo_vm.expects(:halt).with(true).once + @instance.execute(["foo", "--force"]) + end + + should "do nothing if its not created" do + @foo_vm.stubs(:created?).returns(false) + @foo_vm.expects(:halt).never + @instance.halt_single(:foo) end end end diff --git a/test/vagrant/vm_test.rb b/test/vagrant/vm_test.rb index c62548f51..597f95e9c 100644 --- a/test/vagrant/vm_test.rb +++ b/test/vagrant/vm_test.rb @@ -161,6 +161,18 @@ class VMTest < Test::Unit::TestCase end end + context "halting" do + should "execute the halt action" do + @vm.expects(:execute!).with(Vagrant::Actions::VM::Halt, false).once + @vm.halt + end + + should "force if specified" do + @vm.expects(:execute!).with(Vagrant::Actions::VM::Halt, true).once + @vm.halt(true) + end + end + context "destroying" do should "execute the down action" do @vm.expects(:execute!).with(Vagrant::Actions::VM::Down).once