From 1a6f838baacc37dac486b6b7d042edeab6bd8938 Mon Sep 17 00:00:00 2001 From: Mitchell Hashimoto Date: Tue, 23 Feb 2010 00:05:41 -0800 Subject: [PATCH] `vagrant up` (specifically Actions::VM::Import) now uses a configured box rather than a base VM. Much cleaner! --- config/default.rb | 2 +- lib/vagrant/actions/vm/import.rb | 4 ++-- lib/vagrant/box.rb | 4 ++++ lib/vagrant/commands.rb | 1 + lib/vagrant/config.rb | 3 ++- lib/vagrant/env.rb | 17 ++++++++++++++++ test/test_helper.rb | 3 ++- test/vagrant/actions/vm/import_test.rb | 27 ++++++-------------------- test/vagrant/box_test.rb | 10 ++++++++++ test/vagrant/commands_test.rb | 6 ++++++ test/vagrant/env_test.rb | 21 ++++++++++++++++++++ 11 files changed, 72 insertions(+), 26 deletions(-) diff --git a/config/default.rb b/config/default.rb index 6a019f2bf..c47a14902 100644 --- a/config/default.rb +++ b/config/default.rb @@ -11,7 +11,7 @@ Vagrant::Config.run do |config| config.dotfile_name = ".vagrant" - config.vm.base = "~/.vagrant/base/base.ovf" + config.vm.box_ovf = "box.ovf" config.vm.base_mac = "0800279C2E41" config.vm.project_directory = "/vagrant" config.vm.forward_port("ssh", 22, 2222) diff --git a/lib/vagrant/actions/vm/import.rb b/lib/vagrant/actions/vm/import.rb index 802678e5d..56a8e8d2e 100644 --- a/lib/vagrant/actions/vm/import.rb +++ b/lib/vagrant/actions/vm/import.rb @@ -5,9 +5,9 @@ module Vagrant def execute! @runner.invoke_around_callback(:import) do Busy.busy do - logger.info "Importing base VM (#{Vagrant.config[:vm][:base]})..." + logger.info "Importing base VM (#{Vagrant::Env.box.ovf_file})..." # Use the first argument passed to the action - @runner.vm = VirtualBox::VM.import(Vagrant.config[:vm][:base]) + @runner.vm = VirtualBox::VM.import(Vagrant::Env.box.ovf_file) end end end diff --git a/lib/vagrant/box.rb b/lib/vagrant/box.rb index 6cc8d2842..bc9a9e9e8 100644 --- a/lib/vagrant/box.rb +++ b/lib/vagrant/box.rb @@ -27,6 +27,10 @@ module Vagrant @name = name end + def ovf_file + File.join(directory, Vagrant.config.vm.box_ovf) + end + def add execute!(Actions::Box::Add) end diff --git a/lib/vagrant/commands.rb b/lib/vagrant/commands.rb index 1ee4e1dc5..0ed0ab215 100644 --- a/lib/vagrant/commands.rb +++ b/lib/vagrant/commands.rb @@ -40,6 +40,7 @@ run `vagrant down` first. error end + Env.require_box VM.execute!(Actions::VM::Up) end diff --git a/lib/vagrant/config.rb b/lib/vagrant/config.rb index 0e99474f1..62008c05e 100644 --- a/lib/vagrant/config.rb +++ b/lib/vagrant/config.rb @@ -46,7 +46,8 @@ module Vagrant end class VMConfig < Base - attr_accessor :base + attr_accessor :box + attr_accessor :box_ovf attr_accessor :base_mac attr_accessor :project_directory attr_reader :forwarded_ports diff --git a/lib/vagrant/env.rb b/lib/vagrant/env.rb index 3972eb1c5..c7b908819 100644 --- a/lib/vagrant/env.rb +++ b/lib/vagrant/env.rb @@ -6,10 +6,12 @@ module Vagrant # Initialize class variables used @@persisted_vm = nil @@root_path = nil + @@box = nil extend Vagrant::Util class << self + def box; @@box; end def persisted_vm; @@persisted_vm; end def root_path; @@root_path; end def dotfile_path; File.join(root_path, Vagrant.config.dotfile_name); end @@ -21,6 +23,7 @@ module Vagrant load_root_path! load_config! load_home_directory! + load_box! load_vm! end @@ -53,6 +56,10 @@ module Vagrant end end + def load_box! + @@box = Box.find(Vagrant.config.vm.box) if Vagrant.config.vm.box + end + def load_vm! File.open(dotfile_path) do |f| @@persisted_vm = Vagrant::VM.find(f.read) @@ -88,6 +95,16 @@ msg load_root_path!(path.parent, opts) end + def require_box + if !box + error_and_exit(<<-msg) +No base box was specified! A base box is required as a staring point +for every vagrant virtual machine. Please specify one in your Vagrantfile +using `config.vm.box` +msg + end + end + def require_persisted_vm if !persisted_vm error_and_exit(<<-error) diff --git a/test/test_helper.rb b/test/test_helper.rb index 3b21f60d2..ce3e2f4e1 100644 --- a/test/test_helper.rb +++ b/test/test_helper.rb @@ -32,7 +32,8 @@ class Test::Unit::TestCase config.ssh.forwarded_port_key = "ssh" config.ssh.max_tries = 10 - config.vm.base = "foo" + config.vm.box = "foo" + config.vm.box_ovf = "box.ovf" config.vm.base_mac = "42" config.vm.project_directory = "/hobo" config.vm.disk_image_format = 'VMDK' diff --git a/test/vagrant/actions/vm/import_test.rb b/test/vagrant/actions/vm/import_test.rb index a4b608b7e..451aaabe3 100644 --- a/test/vagrant/actions/vm/import_test.rb +++ b/test/vagrant/actions/vm/import_test.rb @@ -4,6 +4,11 @@ class ImportActionTest < Test::Unit::TestCase setup do @mock_vm, @vm, @import = mock_action(Vagrant::Actions::VM::Import) + @ovf_file = "foo" + @box = mock("box") + @box.stubs(:ovf_file).returns(@ovf_file) + Vagrant::Env.stubs(:box).returns(@box) + VirtualBox::VM.stubs(:import) end @@ -18,7 +23,7 @@ class ImportActionTest < Test::Unit::TestCase end should "call import on VirtualBox::VM with the proper base" do - VirtualBox::VM.expects(:import).once + VirtualBox::VM.expects(:import).once.with(@ovf_file) @import.execute! end @@ -28,24 +33,4 @@ class ImportActionTest < Test::Unit::TestCase VirtualBox::VM.expects(:import).returns(new_vm) @import.execute! end - - context "when importing with or without an ovf file as an argument" do - # NOTE for both tests File.expects(:expand_path) makes mocha recurse and vomit - - should "default the ovf_file value to the vagrant base when not passed as an init argument" do\ - File.stubs(:expand_path) - File.expand_path do |n| - assert_equal n, Vagrant.config.vm.base - end - Vagrant::Actions::VM::Import.new(@vm) - end - - should "expand the ovf path and assign it when passed as a parameter" do - File.stubs(:expand_path) - File.expand_path do |n| - assert_equal n, 'foo' - end - Vagrant::Actions::VM::Import.new(@vm, 'foo') - end - end end diff --git a/test/vagrant/box_test.rb b/test/vagrant/box_test.rb index 66697d171..93effccf1 100644 --- a/test/vagrant/box_test.rb +++ b/test/vagrant/box_test.rb @@ -78,5 +78,15 @@ class BoxTest < Test::Unit::TestCase @box.destroy end end + + context "ovf file" do + setup do + @box.stubs(:directory).returns("foo") + end + + should "be the directory joined with the config ovf file" do + assert_equal File.join(@box.directory, Vagrant.config.vm.box_ovf), @box.ovf_file + end + end end end diff --git a/test/vagrant/commands_test.rb b/test/vagrant/commands_test.rb index 07f03dcc5..9c6b981e2 100644 --- a/test/vagrant/commands_test.rb +++ b/test/vagrant/commands_test.rb @@ -33,6 +33,7 @@ class CommandsTest < Test::Unit::TestCase setup do Vagrant::Env.stubs(:persisted_vm).returns(nil) Vagrant::VM.stubs(:execute!) + Vagrant::Env.stubs(:require_box) end should "require load the environment" do @@ -40,6 +41,11 @@ class CommandsTest < Test::Unit::TestCase Vagrant::Commands.up end + should "require a box" do + Vagrant::Env.expects(:require_box).once + Vagrant::Commands.up + end + should "error if a persisted VM already exists" do Vagrant::Env.expects(:persisted_vm).returns(true) Vagrant::Commands.expects(:error_and_exit).once diff --git a/test/vagrant/env_test.rb b/test/vagrant/env_test.rb index dc98a77ab..1a9c1da04 100644 --- a/test/vagrant/env_test.rb +++ b/test/vagrant/env_test.rb @@ -11,6 +11,7 @@ class EnvTest < Test::Unit::TestCase setup do mock_config + Vagrant::Box.stubs(:find).returns("foo") end context "requiring a VM" do @@ -198,4 +199,24 @@ class EnvTest < Test::Unit::TestCase assert_equal File.join(@home_path, "boxes"), Vagrant::Env.boxes_path end end + + context "loading box" do + setup do + @box = mock("box") + end + + should "set the box to what is found by the Box class" do + Vagrant::Box.expects(:find).with(Vagrant.config.vm.box).once.returns(@box) + Vagrant::Env.load_box! + assert @box.equal?(Vagrant::Env.box) + end + end + + context "requiring boxes" do + should "error and exit if no box is found" do + Vagrant::Env.expects(:box).returns(nil) + Vagrant::Env.expects(:error_and_exit).once + Vagrant::Env.require_box + end + end end