`vagrant up` (specifically Actions::VM::Import) now uses a configured box rather than a base VM. Much cleaner!

This commit is contained in:
Mitchell Hashimoto 2010-02-23 00:05:41 -08:00
parent bd551174ba
commit 1a6f838baa
11 changed files with 72 additions and 26 deletions

View File

@ -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)

View File

@ -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

View File

@ -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

View File

@ -40,6 +40,7 @@ run `vagrant down` first.
error
end
Env.require_box
VM.execute!(Actions::VM::Up)
end

View File

@ -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

View File

@ -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)

View File

@ -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'

View File

@ -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

View File

@ -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

View File

@ -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

View File

@ -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