`vagrant resume` with multiple VMs

This commit is contained in:
Mitchell Hashimoto 2010-05-17 10:36:34 -07:00
parent 228327c0ca
commit 38d485c29b
3 changed files with 71 additions and 24 deletions

View File

@ -10,8 +10,33 @@ module Vagrant
description "Resumes a suspend vagrant environment"
def execute(args=[])
env.require_persisted_vm
env.vm.resume
args = parse_options(args)
if args[0]
resume_single(args[0])
else
resume_all
end
end
def resume_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.resume
else
logger.info "VM '#{name}' not created. Ignoring."
end
end
def resume_all
env.vms.keys.each do |name|
resume_single(name)
end
end
def options_spec(opts)

View File

@ -4,30 +4,58 @@ class CommandsResumeTest < Test::Unit::TestCase
setup do
@klass = Vagrant::Commands::Resume
@persisted_vm = mock("persisted_vm")
@persisted_vm.stubs(:execute!)
@env = mock_environment
@env.stubs(:require_persisted_vm)
@env.stubs(:vm).returns(@persisted_vm)
@instance = @klass.new(@env)
end
context "executing" do
should "call on all if no name is given" do
@instance.expects(:resume_all).once
@instance.execute
end
should "call on single if a name is given" do
@instance.expects(:resume_single).with("foo").once
@instance.execute(["foo"])
end
end
context "resume all" do
should "resume each VM" do
vms = { :foo => nil, :bar => nil, :baz => nil }
@env.stubs(:vms).returns(vms)
vms.each do |name, value|
@instance.expects(:resume_single).with(name).once
end
@instance.resume_all
end
end
context "resuming a single VM" do
setup do
@persisted_vm.stubs(:resume)
@persisted_vm.stubs(:saved?).returns(true)
@foo_vm = mock("vm")
vms = { :foo => @foo_vm }
@env.stubs(:vms).returns(vms)
end
should "require a persisted VM" do
@env.expects(:require_persisted_vm).once
@instance.execute
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.resume_single(:foo)
end
should "save the state of the VM" do
@persisted_vm.expects(:resume).once
@instance.execute
should "resume if its created" do
@foo_vm.stubs(:created?).returns(true)
@foo_vm.expects(:resume).once
@instance.execute(["foo"])
end
should "do nothing if its not created" do
@foo_vm.stubs(:created?).returns(false)
@foo_vm.expects(:resume).never
@instance.resume_single(:foo)
end
end
end

View File

@ -46,13 +46,7 @@ class CommandsSuspendTest < Test::Unit::TestCase
@instance.suspend_single(:foo)
end
should "halt if its created" do
@foo_vm.stubs(:created?).returns(true)
@foo_vm.expects(:suspend).once
@instance.execute(["foo"])
end
should "halt and force if specified" do
should "suspend if its created" do
@foo_vm.stubs(:created?).returns(true)
@foo_vm.expects(:suspend).once
@instance.execute(["foo"])