From 228327c0ca82437e4376e1922d807468342e4d5e Mon Sep 17 00:00:00 2001 From: Mitchell Hashimoto Date: Mon, 17 May 2010 01:45:57 -0700 Subject: [PATCH] `vagrant suspend` works with multi-vms --- lib/vagrant/commands/suspend.rb | 31 ++++++++++++-- test/vagrant/commands/suspend_test.rb | 62 +++++++++++++++++++++------ 2 files changed, 76 insertions(+), 17 deletions(-) diff --git a/lib/vagrant/commands/suspend.rb b/lib/vagrant/commands/suspend.rb index 9bdaa3d11..f45410cbd 100644 --- a/lib/vagrant/commands/suspend.rb +++ b/lib/vagrant/commands/suspend.rb @@ -11,8 +11,33 @@ module Vagrant description "Suspends the currently running vagrant environment" def execute(args=[]) - env.require_persisted_vm - env.vm.suspend + args = parse_options(args) + + 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 def options_spec(opts) @@ -20,4 +45,4 @@ module Vagrant end end end -end \ No newline at end of file +end diff --git a/test/vagrant/commands/suspend_test.rb b/test/vagrant/commands/suspend_test.rb index ec0f037f1..20703c54d 100644 --- a/test/vagrant/commands/suspend_test.rb +++ b/test/vagrant/commands/suspend_test.rb @@ -4,30 +4,64 @@ class CommandsSuspendTest < Test::Unit::TestCase setup do @klass = Vagrant::Commands::Suspend - @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(: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 - @persisted_vm.stubs(:suspend) - @persisted_vm.stubs(:saved?).returns(false) + @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.suspend_single(:foo) end - should "suspend the VM" do - @persisted_vm.expects(:suspend).once - @instance.execute + 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 + @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