From 4ec6196b3bcb0298a511ae3fdee0e6823134d835 Mon Sep 17 00:00:00 2001 From: Mitchell Hashimoto Date: Fri, 23 Jul 2010 22:06:17 -0700 Subject: [PATCH] Add CheckBox to up command and make the typical require_box checks --- lib/vagrant/action/builtin.rb | 1 + lib/vagrant/action/vm/check.rb | 21 -------------- lib/vagrant/action/vm/check_box.rb | 25 ++++++++++++++++ lib/vagrant/commands/up.rb | 2 -- test/vagrant/action/vm/check_box_test.rb | 36 ++++++++++++++++++++++++ test/vagrant/action/vm/check_test.rb | 25 ---------------- test/vagrant/commands/up_test.rb | 1 - 7 files changed, 62 insertions(+), 49 deletions(-) delete mode 100644 lib/vagrant/action/vm/check.rb create mode 100644 lib/vagrant/action/vm/check_box.rb create mode 100644 test/vagrant/action/vm/check_box_test.rb delete mode 100644 test/vagrant/action/vm/check_test.rb diff --git a/lib/vagrant/action/builtin.rb b/lib/vagrant/action/builtin.rb index 014155cfe..3e3b7e820 100644 --- a/lib/vagrant/action/builtin.rb +++ b/lib/vagrant/action/builtin.rb @@ -62,6 +62,7 @@ module Vagrant # up - Imports, prepares, then starts a fresh VM. up = Builder.new do + use VM::CheckBox use VM::Import use VM::Persist use VM::MatchMACAddress diff --git a/lib/vagrant/action/vm/check.rb b/lib/vagrant/action/vm/check.rb deleted file mode 100644 index ab062e4bb..000000000 --- a/lib/vagrant/action/vm/check.rb +++ /dev/null @@ -1,21 +0,0 @@ -module Vagrant - class Action - module VM - class Check - def initialize(app, env) - @app = app - end - - def call(env) - box_name = env["config"].vm.box - - env.logger.info "Checking if the box '#{box_name}' was already downloaded" - - box = Vagrant::Box.find(env.env , box_name) - env.logger.info "The box #{box} were found" - @app.call(env) - end - end - end - end -end diff --git a/lib/vagrant/action/vm/check_box.rb b/lib/vagrant/action/vm/check_box.rb new file mode 100644 index 000000000..2e04b17f0 --- /dev/null +++ b/lib/vagrant/action/vm/check_box.rb @@ -0,0 +1,25 @@ +module Vagrant + class Action + module VM + class CheckBox + def initialize(app, env) + @app = app + end + + def call(env) + box_name = env["config"].vm.box + return env.error!(:box_not_specified) if !box_name + + if !Vagrant::Box.find(env.env , box_name) + box_url = env["config"].vm.box_url + return env.error!(:box_specified_doesnt_exist, :box_name => box_name) if !box_url + + env.logger.info "Box #{box_name} not found. Fetching box since URL specified..." + end + + @app.call(env) + end + end + end + end +end diff --git a/lib/vagrant/commands/up.rb b/lib/vagrant/commands/up.rb index 2908adb4b..fad688907 100644 --- a/lib/vagrant/commands/up.rb +++ b/lib/vagrant/commands/up.rb @@ -23,8 +23,6 @@ module Vagrant vm.env.logger.info "VM '#{name}' already created. Booting if its not already running..." vm.start else - vm.env.require_box - vm.env.logger.info "Creating VM '#{name}'" vm.up end diff --git a/test/vagrant/action/vm/check_box_test.rb b/test/vagrant/action/vm/check_box_test.rb new file mode 100644 index 000000000..6b8431cde --- /dev/null +++ b/test/vagrant/action/vm/check_box_test.rb @@ -0,0 +1,36 @@ +require File.join(File.dirname(__FILE__), '..', '..', '..', 'test_helper') + +class CheckBoxVMActionTest < Test::Unit::TestCase + setup do + @klass = Vagrant::Action::VM::CheckBox + @app, @env = mock_action_data + @instance = @klass.new(@app, @env) + end + + context "calling" do + setup do + Vagrant::Box.stubs(:find) + end + + should "return error if box not specified" do + @env.env.config.vm.box = nil + + @app.expects(:call).never + @instance.call(@env) + + assert @env.error? + assert_equal :box_not_specified, @env.error.first + end + + should "error if box does not exist and URL not specified" do + @env.env.config.vm.box_url = nil + Vagrant::Box.expects(:find).with(@env.env, @env["config"].vm.box).returns(nil) + + @app.expects(:call).never + @instance.call(@env) + + assert @env.error? + assert_equal :box_specified_doesnt_exist, @env.error.first + end + end +end diff --git a/test/vagrant/action/vm/check_test.rb b/test/vagrant/action/vm/check_test.rb deleted file mode 100644 index 181595506..000000000 --- a/test/vagrant/action/vm/check_test.rb +++ /dev/null @@ -1,25 +0,0 @@ -require File.join(File.dirname(__FILE__), '..', '..', '..', 'test_helper') - - -class CheckVMActionTest < Test::Unit::TestCase - setup do - @klass = Vagrant::Action::VM::Check - @app, @env = mock_action_data - @instance = @klass.new(@app, @env) - - @env['config'].vm.box = "foo" - end - should "check if the box exist" do - Vagrant::Box.expects("find").with(@env.env, 'foo') - @instance.call(@env) - end - - - should 'continue the chain process' do - Vagrant::Box.stubs('find').with(@env.env, 'foo') - @app.expects(:call).with(@env).once - @instance.call(@env) - end - -end - diff --git a/test/vagrant/commands/up_test.rb b/test/vagrant/commands/up_test.rb index 92bef382f..dbae1486a 100644 --- a/test/vagrant/commands/up_test.rb +++ b/test/vagrant/commands/up_test.rb @@ -38,7 +38,6 @@ class CommandsUpTest < Test::Unit::TestCase 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)