`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"
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)

View File

@ -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

View File

@ -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

View File

@ -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