`vagrant halt` updated to work with multi-VM

This commit is contained in:
Mitchell Hashimoto 2010-05-16 18:22:28 -07:00
parent 50086423ee
commit 3b4d2ab795
4 changed files with 90 additions and 13 deletions

View File

@ -11,10 +11,33 @@ module Vagrant
description "Halts the currently running vagrant environment" description "Halts the currently running vagrant environment"
def execute(args=[]) def execute(args=[])
parse_options(args) args = parse_options(args)
env.require_persisted_vm if args[0]
env.vm.execute!(Actions::VM::Halt, options[:force]) 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 end
def options_spec(opts) def options_spec(opts)

View File

@ -105,6 +105,10 @@ module Vagrant
execute!(Actions::VM::Start) execute!(Actions::VM::Start)
end end
def halt(force=false)
execute!(Actions::VM::Halt, force)
end
def destroy def destroy
execute!(Actions::VM::Down) execute!(Actions::VM::Down)
end end

View File

@ -15,21 +15,59 @@ class CommandsHaltTest < Test::Unit::TestCase
end end
context "executing" do context "executing" do
should "require a persisted VM" do should "call halt_all if no name is given" do
@env.expects(:require_persisted_vm).once @instance.expects(:halt_all).once
@instance.execute @instance.execute
end end
should "call the `halt` action on the VM" do should "call halt_single if a name is given" do
@persisted_vm.expects(:execute!).with(Vagrant::Actions::VM::Halt, false).once @instance.expects(:halt_single).with("foo").once
@instance.execute @instance.execute(["foo"])
end end
end
should "be forceful if -f flag is sent" do context "halting all" do
%w{--force -f}.each do |flag| should "halt each VM" do
@persisted_vm.expects(:execute!).with(Vagrant::Actions::VM::Halt, true).once vms = { :foo => nil, :bar => nil, :baz => nil }
@instance.execute([flag]) @env.stubs(:vms).returns(vms)
vms.each do |name, value|
@instance.expects(:halt_single).with(name).once
end 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 end
end end

View File

@ -161,6 +161,18 @@ class VMTest < Test::Unit::TestCase
end end
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 context "destroying" do
should "execute the down action" do should "execute the down action" do
@vm.expects(:execute!).with(Vagrant::Actions::VM::Down).once @vm.expects(:execute!).with(Vagrant::Actions::VM::Down).once