diff --git a/lib/vagrant/action/box/verify.rb b/lib/vagrant/action/box/verify.rb new file mode 100644 index 000000000..285b92ba5 --- /dev/null +++ b/lib/vagrant/action/box/verify.rb @@ -0,0 +1,23 @@ +module Vagrant + class Action + module Box + class Verify + def initialize(app, env) + @app = app + @env = env + end + + def call(env) + begin + env.logger.info "Verifying box..." + VirtualBox::Appliance.new(env["box"].ovf_file) + rescue Exception + return env.error!(:box_verification_failed) + end + + @app.call(env) + end + end + end + end +end diff --git a/lib/vagrant/action/builtin.rb b/lib/vagrant/action/builtin.rb index bf8a21e83..778cb6b9c 100644 --- a/lib/vagrant/action/builtin.rb +++ b/lib/vagrant/action/builtin.rb @@ -88,6 +88,7 @@ module Vagrant box_add = Builder.new do use Box::Download use Box::Unpackage + use Box::Verify end register :box_add, box_add diff --git a/test/vagrant/action/box/verify_test.rb b/test/vagrant/action/box/verify_test.rb new file mode 100644 index 000000000..82fe97083 --- /dev/null +++ b/test/vagrant/action/box/verify_test.rb @@ -0,0 +1,39 @@ +require File.join(File.dirname(__FILE__), '..', '..', '..', 'test_helper') + +class VerifyBoxActionTest < Test::Unit::TestCase + setup do + @klass = Vagrant::Action::Box::Verify + @app, @env = mock_action_data + + @vm = mock("vm") + @env["vm"] = @vm + @env["box"] = Vagrant::Box.new(mock_environment, "foo") + + @internal_vm = mock("internal") + @vm.stubs(:vm).returns(@internal_vm) + + @instance = @klass.new(@app, @env) + end + + context "calling" do + setup do + @env.logger.stubs(:info) + end + + should "continue fine if verification succeeds" do + seq = sequence("seq") + VirtualBox::Appliance.expects(:new).with(@env["box"].ovf_file).in_sequence(seq) + @app.expects(:call).with(@env).once.in_sequence(seq) + @instance.call(@env) + assert !@env.error? + end + + should "halt chain if verification fails" do + VirtualBox::Appliance.expects(:new).with(@env["box"].ovf_file).raises(Exception) + @app.expects(:call).with(@env).never + @instance.call(@env) + assert @env.error? + assert_equal :box_verification_failed, @env.error.first + end + end +end diff --git a/test/vagrant/actions/box/verify_test.rb b/test/vagrant/actions/box/verify_test.rb index 7e5c6ee9d..e46b25eae 100644 --- a/test/vagrant/actions/box/verify_test.rb +++ b/test/vagrant/actions/box/verify_test.rb @@ -1,6 +1,6 @@ require File.join(File.dirname(__FILE__), '..', '..', '..', 'test_helper') -class VerifyBoxActionTest < Test::Unit::TestCase +class VerifyBoxActionsTest < Test::Unit::TestCase setup do @runner, @vm, @action = mock_action(Vagrant::Actions::Box::Verify) @runner.stubs(:name).returns("foo")