`vagrant suspend` works with multi-vms

This commit is contained in:
Mitchell Hashimoto 2010-05-17 01:45:57 -07:00
parent fd12018114
commit 228327c0ca
2 changed files with 76 additions and 17 deletions

View File

@ -11,8 +11,33 @@ module Vagrant
description "Suspends the currently running vagrant environment" description "Suspends the currently running vagrant environment"
def execute(args=[]) def execute(args=[])
env.require_persisted_vm args = parse_options(args)
env.vm.suspend
if args[0]
suspend_single(args[0])
else
suspend_all
end
end
def suspend_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.suspend
else
logger.info "VM '#{name}' not created. Ignoring."
end
end
def suspend_all
env.vms.keys.each do |name|
suspend_single(name)
end
end end
def options_spec(opts) def options_spec(opts)
@ -20,4 +45,4 @@ module Vagrant
end end
end end
end end
end end

View File

@ -4,30 +4,64 @@ class CommandsSuspendTest < Test::Unit::TestCase
setup do setup do
@klass = Vagrant::Commands::Suspend @klass = Vagrant::Commands::Suspend
@persisted_vm = mock("persisted_vm")
@persisted_vm.stubs(:execute!)
@env = mock_environment @env = mock_environment
@env.stubs(:require_persisted_vm)
@env.stubs(:vm).returns(@persisted_vm)
@instance = @klass.new(@env) @instance = @klass.new(@env)
end end
context "executing" do context "executing" do
should "call on all if no name is given" do
@instance.expects(:suspend_all).once
@instance.execute
end
should "call on single if a name is given" do
@instance.expects(:suspend_single).with("foo").once
@instance.execute(["foo"])
end
end
context "suspending all" do
should "suspend each VM" do
vms = { :foo => nil, :bar => nil, :baz => nil }
@env.stubs(:vms).returns(vms)
vms.each do |name, value|
@instance.expects(:suspend_single).with(name).once
end
@instance.suspend_all
end
end
context "suspending a single VM" do
setup do setup do
@persisted_vm.stubs(:suspend) @foo_vm = mock("vm")
@persisted_vm.stubs(:saved?).returns(false) vms = { :foo => @foo_vm }
@env.stubs(:vms).returns(vms)
end end
should "require a persisted VM" do should "error and exit if the VM doesn't exist" do
@env.expects(:require_persisted_vm).once @env.stubs(:vms).returns({})
@instance.execute @instance.expects(:error_and_exit).with(:unknown_vm, :vm => :foo).once
@instance.suspend_single(:foo)
end end
should "suspend the VM" do should "halt if its created" do
@persisted_vm.expects(:suspend).once @foo_vm.stubs(:created?).returns(true)
@instance.execute @foo_vm.expects(:suspend).once
@instance.execute(["foo"])
end
should "halt and force if specified" do
@foo_vm.stubs(:created?).returns(true)
@foo_vm.expects(:suspend).once
@instance.execute(["foo"])
end
should "do nothing if its not created" do
@foo_vm.stubs(:created?).returns(false)
@foo_vm.expects(:suspend).never
@instance.suspend_single(:foo)
end end
end end
end end