From 38d485c29b8659fa42228234af59185042ac1853 Mon Sep 17 00:00:00 2001 From: Mitchell Hashimoto Date: Mon, 17 May 2010 10:36:34 -0700 Subject: [PATCH] `vagrant resume` with multiple VMs --- lib/vagrant/commands/resume.rb | 31 +++++++++++++-- test/vagrant/commands/resume_test.rb | 56 ++++++++++++++++++++------- test/vagrant/commands/suspend_test.rb | 8 +--- 3 files changed, 71 insertions(+), 24 deletions(-) diff --git a/lib/vagrant/commands/resume.rb b/lib/vagrant/commands/resume.rb index afb8a51ca..937c3c5cc 100644 --- a/lib/vagrant/commands/resume.rb +++ b/lib/vagrant/commands/resume.rb @@ -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) @@ -19,4 +44,4 @@ module Vagrant end end end -end \ No newline at end of file +end diff --git a/test/vagrant/commands/resume_test.rb b/test/vagrant/commands/resume_test.rb index 336f86ec7..0671a34ff 100644 --- a/test/vagrant/commands/resume_test.rb +++ b/test/vagrant/commands/resume_test.rb @@ -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 diff --git a/test/vagrant/commands/suspend_test.rb b/test/vagrant/commands/suspend_test.rb index 20703c54d..84a9feaad 100644 --- a/test/vagrant/commands/suspend_test.rb +++ b/test/vagrant/commands/suspend_test.rb @@ -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"])