From 9cb0015b6e0ccd2ba95eb60e90355a455952e6d2 Mon Sep 17 00:00:00 2001 From: Mitchell Hashimoto Date: Sun, 1 Jul 2012 21:41:57 -0700 Subject: [PATCH] Test that an added box provider must match expected. --- lib/vagrant/box_collection2.rb | 18 +++++++++++++++++- lib/vagrant/errors.rb | 4 ++++ templates/locales/en.yml | 5 +++++ test/unit/support/isolated_environment.rb | 5 ++--- test/unit/vagrant/box_collection2_test.rb | 19 ++++++++++++++++++- 5 files changed, 46 insertions(+), 5 deletions(-) diff --git a/lib/vagrant/box_collection2.rb b/lib/vagrant/box_collection2.rb index 77fbd0f60..eca92d5b2 100644 --- a/lib/vagrant/box_collection2.rb +++ b/lib/vagrant/box_collection2.rb @@ -46,8 +46,24 @@ module Vagrant Archive::Tar::Minitar.unpack(path.to_s, box_dir.to_s) end + # Find the box we just added + box = find(name, provider) + + # Verify that the provider matches. If not, then we need to rollback + box_provider = box.metadata["provider"] + if box_provider.to_sym != provider + @logger.error("Added box provider doesnt match expected: #{box_provider}") + + # Delete the directory + @logger.debug("Deleting the added box directory...") + box_dir.rmtree + + # Raise an exception + raise Errors::BoxProviderDoesntMatch, :expected => provider, :actual => box_provider + end + # Return the box - find(name, provider) + box end # This returns an array of all the boxes on the system, given by diff --git a/lib/vagrant/errors.rb b/lib/vagrant/errors.rb index 0aae5c519..0b57d7c45 100644 --- a/lib/vagrant/errors.rb +++ b/lib/vagrant/errors.rb @@ -113,6 +113,10 @@ module Vagrant error_key(:not_specified, "vagrant.actions.vm.check_box") end + class BoxProviderDoesntMatch < VagrantError + error_key(:box_provider_doesnt_match) + end + class BoxSpecifiedDoesntExist < VagrantError status_code(23) error_key(:does_not_exist, "vagrant.actions.vm.check_box") diff --git a/templates/locales/en.yml b/templates/locales/en.yml index 4b727f9bc..98031702c 100644 --- a/templates/locales/en.yml +++ b/templates/locales/en.yml @@ -16,6 +16,11 @@ en: errors: base_vm_not_found: The base VM with the name '%{name}' was not found. box_not_found: Box '%{name}' could not be found. + box_provider_doesnt_match: |- + The box you attempted to add doesn't match the provider you specified. + + Provider expected: %{expected} + Provider of box: %{actual} box_upgrade_required: |- The box '%{name}' is still stored on disk in the Vagrant 1.0.x format. This box must be upgraded in order to work properly with diff --git a/test/unit/support/isolated_environment.rb b/test/unit/support/isolated_environment.rb index 84d752c0a..3de02f847 100644 --- a/test/unit/support/isolated_environment.rb +++ b/test/unit/support/isolated_environment.rb @@ -75,12 +75,11 @@ module Unit box_dir end - # This creates a "box" file with the given name and provider. + # This creates a "box" file with the given provider. # - # @param [String] name Name of the box. # @param [Symbol] provider Provider for the box. # @return [Pathname] Path to the newly created box. - def box2_file(name, provider) + def box2_file(provider) # This is the metadata we want to store in our file metadata = { "type" => "v2_box", diff --git a/test/unit/vagrant/box_collection2_test.rb b/test/unit/vagrant/box_collection2_test.rb index 52abf797d..e7701040c 100644 --- a/test/unit/vagrant/box_collection2_test.rb +++ b/test/unit/vagrant/box_collection2_test.rb @@ -11,7 +11,7 @@ describe Vagrant::BoxCollection2 do describe "adding" do it "should add a valid box to the system" do - box_path = environment.box2_file("foo", :virtualbox) + box_path = environment.box2_file(:virtualbox) # Add the box box = instance.add(box_path, "foo", :virtualbox) @@ -23,6 +23,23 @@ describe Vagrant::BoxCollection2 do box = instance.find("foo", :virtualbox) box.should_not be_nil end + + it "should raise an exception and not add the box if the provider doesn't match" do + box_name = "foo" + good_provider = :virtualbox + bad_provider = :vmware + + # Create a VirtualBox box file + box_path = environment.box2_file(good_provider) + + # Add the box but with an invalid provider, verify we get the proper + # error. + expect { instance.add(box_path, box_name, bad_provider) }. + to raise_error(Vagrant::Errors::BoxProviderDoesntMatch) + + # Verify the box doesn't exist + instance.find(box_name, bad_provider).should be_nil + end end describe "listing all" do