diff --git a/lib/vagrant/actions/box/add.rb b/lib/vagrant/actions/box/add.rb index 2d3184119..fbce7fb40 100644 --- a/lib/vagrant/actions/box/add.rb +++ b/lib/vagrant/actions/box/add.rb @@ -15,6 +15,7 @@ module Vagrant @runner.add_action(Download) @runner.add_action(Unpackage) + @runner.add_action(Verify) end end end diff --git a/lib/vagrant/actions/box/unpackage.rb b/lib/vagrant/actions/box/unpackage.rb index 8f6bf5288..89df68fb4 100644 --- a/lib/vagrant/actions/box/unpackage.rb +++ b/lib/vagrant/actions/box/unpackage.rb @@ -4,7 +4,6 @@ module Vagrant # This action unpackages a downloaded box file into its final # box destination within the vagrant home folder. class Unpackage < Base - def execute! @runner.invoke_around_callback(:unpackage) do setup_box_dir diff --git a/lib/vagrant/actions/box/verify.rb b/lib/vagrant/actions/box/verify.rb new file mode 100644 index 000000000..a4e6ffba6 --- /dev/null +++ b/lib/vagrant/actions/box/verify.rb @@ -0,0 +1,32 @@ +module Vagrant + module Actions + module Box + # This action verifies that a given box is valid. This works by attempting + # to read/interpret the appliance file (OVF). If the reading succeeds, then + # the box is assumed to be valid. + class Verify < Base + def execute! + reload_configuration + verify_appliance + end + + def reload_configuration + # We have to reload the environment config since we _just_ added the + # box. We do this by setting the current box to the recently added box, + # then reloading + @runner.env.config.vm.box = @runner.name + @runner.env.load_box! + @runner.env.load_config! + end + + def verify_appliance + # We now try to read the applince. If it succeeds, we return true. + VirtualBox::Appliance.new(@runner.env.box.ovf_file) + true + rescue VirtualBox::Exceptions::FileErrorException + false + end + end + end + end +end diff --git a/test/vagrant/actions/box/add_test.rb b/test/vagrant/actions/box/add_test.rb index ef42fb4a2..75bc0a84f 100644 --- a/test/vagrant/actions/box/add_test.rb +++ b/test/vagrant/actions/box/add_test.rb @@ -7,7 +7,7 @@ class AddBoxActionTest < Test::Unit::TestCase context "prepare" do setup do - @default_order = [Vagrant::Actions::Box::Download, Vagrant::Actions::Box::Unpackage] + @default_order = [Vagrant::Actions::Box::Download, Vagrant::Actions::Box::Unpackage, Vagrant::Actions::Box::Verify] @runner.stubs(:directory).returns("foo") File.stubs(:exists?).returns(false) end diff --git a/test/vagrant/actions/box/verify_test.rb b/test/vagrant/actions/box/verify_test.rb new file mode 100644 index 000000000..29d0b0399 --- /dev/null +++ b/test/vagrant/actions/box/verify_test.rb @@ -0,0 +1,47 @@ +require File.join(File.dirname(__FILE__), '..', '..', '..', 'test_helper') + +class VerifyBoxActionTest < Test::Unit::TestCase + setup do + @runner, @vm, @action = mock_action(Vagrant::Actions::Box::Verify) + @runner.stubs(:name).returns("foo") + @runner.stubs(:temp_path).returns("bar") + end + + context "executing" do + should "execute the proper actions in the proper order" do + exec_seq = sequence("exec_seq") + @action.expects(:reload_configuration).in_sequence(exec_seq) + @action.expects(:verify_appliance).in_sequence(exec_seq) + @action.execute! + end + end + + context "reloading configuration" do + should "set the new box, load box, then load config" do + reload_seq = sequence("reload_seq") + @runner.env.config.vm.expects(:box=).with(@runner.name).in_sequence(reload_seq) + @runner.env.expects(:load_box!).in_sequence(reload_seq) + @runner.env.expects(:load_config!).in_sequence(reload_seq) + @action.reload_configuration + end + end + + context "verifying appliance" do + setup do + @box = mock("box") + @box.stubs(:ovf_file).returns("foo") + + @runner.env.stubs(:box).returns(@box) + end + + should "create new appliance and return true if succeeds" do + VirtualBox::Appliance.expects(:new).with(@box.ovf_file) + assert @action.verify_appliance + end + + should "return false if an exception is raised" do + VirtualBox::Appliance.expects(:new).with(@box.ovf_file).raises(VirtualBox::Exceptions::FileErrorException) + assert !@action.verify_appliance + end + end +end