From 81e1e8932a563ab87bc0f1852c606f3984fe3828 Mon Sep 17 00:00:00 2001 From: Mitchell Hashimoto Date: Sun, 16 May 2010 18:34:35 -0700 Subject: [PATCH] Can now specify which VMs to up with `vagrant up` or will up all by default --- lib/vagrant/commands/up.rb | 38 ++++++++++++--- lib/vagrant/vm.rb | 4 ++ test/vagrant/commands/halt_test.rb | 6 --- test/vagrant/commands/up_test.rb | 78 +++++++++++++++++------------- test/vagrant/vm_test.rb | 7 +++ 5 files changed, 86 insertions(+), 47 deletions(-) diff --git a/lib/vagrant/commands/up.rb b/lib/vagrant/commands/up.rb index 6a15a52e2..41336ece7 100644 --- a/lib/vagrant/commands/up.rb +++ b/lib/vagrant/commands/up.rb @@ -9,18 +9,40 @@ module Vagrant description "Creates the vagrant environment" def execute(args=[]) + args = parse_options(args) + + if args[0] + up_single(args[0]) + else + up_all + end + end + + def up_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? + logger.info "VM '#{name}' already created. Booting if its not already running..." + vm.start + else + vm.env.require_box + + logger.info "Creating VM '#{name}'" + vm.up + end + end + + def up_all # First verify that all VMs have valid boxes env.vms.each { |name, vm| vm.env.require_box unless vm.created? } # Next, handle each VM - env.vms.each do |name, vm| - if vm.created? - logger.info "VM '#{name}' already created. Booting if its not already running..." - vm.start - else - logger.info "Creating VM '#{name}'" - vm.execute!(Actions::VM::Up) - end + env.vms.keys.each do |name| + up_single(name) end end diff --git a/lib/vagrant/vm.rb b/lib/vagrant/vm.rb index 618213519..15e2f2415 100644 --- a/lib/vagrant/vm.rb +++ b/lib/vagrant/vm.rb @@ -99,6 +99,10 @@ module Vagrant execute! end + def up + execute!(Actions::VM::Up) + end + def start return if @vm.running? diff --git a/test/vagrant/commands/halt_test.rb b/test/vagrant/commands/halt_test.rb index 08ef2b069..3cbe61b45 100644 --- a/test/vagrant/commands/halt_test.rb +++ b/test/vagrant/commands/halt_test.rb @@ -4,13 +4,7 @@ class CommandsHaltTest < Test::Unit::TestCase setup do @klass = Vagrant::Commands::Halt - @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 diff --git a/test/vagrant/commands/up_test.rb b/test/vagrant/commands/up_test.rb index ba2aed82b..9a0d0c5f0 100644 --- a/test/vagrant/commands/up_test.rb +++ b/test/vagrant/commands/up_test.rb @@ -6,29 +6,59 @@ class CommandsUpTest < Test::Unit::TestCase @env = mock_environment @instance = @klass.new(@env) - - @persisted_vm = mock("persisted_vm") - @persisted_vm.stubs(:execute!) end context "executing" do + should "call up_all if no name is given" do + @instance.expects(:up_all).once + @instance.execute + end + + should "call up_single if a name is given" do + @instance.expects(:up_single).with("foo").once + @instance.execute(["foo"]) + end + end + + context "upping a single VM" do setup do - @new_vm = mock("vm") - @new_vm.stubs(:execute!) - - @vms = {} + @vm = mock("vm") + @vm.stubs(:env).returns(@env) + @vms = {: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.up_single(:foo) + end + + should "start created VMs" do + @vm.stubs(:created?).returns(true) + @vm.expects(:start).once + @instance.up_single(:foo) + end + + should "up non-created VMs" do + @vm.stubs(:created?).returns(false) + @vm.env.expects(:require_box).once + @vm.expects(:up).once + @vm.expects(:start).never + @instance.up_single(:foo) + end + end + + context "upping all VMs" do + setup do + @vms = {} @env.stubs(:vms).returns(@vms) - @env.stubs(:require_box) end def create_vm - env = mock_environment - env.stubs(:require_box) - vm = mock("vm") - vm.stubs(:env).returns(env) - vm.stubs(:execute!) + vm.stubs(:env).returns(mock_environment) vm.stubs(:created?).returns(false) vm end @@ -39,28 +69,10 @@ class CommandsUpTest < Test::Unit::TestCase @vms.each do |name, vm| vm.env.expects(:require_box).once + @instance.expects(:up_single).with(name).once end - @instance.execute - end - - should "start created VMs" do - vm = create_vm - vm.stubs(:created?).returns(true) - - @vms[:foo] = vm - - vm.expects(:start).once - @instance.execute - end - - should "up non-created VMs" do - vm = create_vm - vm.expects(:execute!).with(Vagrant::Actions::VM::Up).once - vm.expects(:start).never - - @vms[:foo] = vm - @instance.execute + @instance.up_all end end end diff --git a/test/vagrant/vm_test.rb b/test/vagrant/vm_test.rb index 597f95e9c..075cb3c7c 100644 --- a/test/vagrant/vm_test.rb +++ b/test/vagrant/vm_test.rb @@ -161,6 +161,13 @@ class VMTest < Test::Unit::TestCase end end + context "upping" do + should "execute the up action" do + @vm.expects(:execute!).with(Vagrant::Actions::VM::Up).once + @vm.up + end + end + context "halting" do should "execute the halt action" do @vm.expects(:execute!).with(Vagrant::Actions::VM::Halt, false).once