Test that an added box provider must match expected.

This commit is contained in:
Mitchell Hashimoto 2012-07-01 21:41:57 -07:00
parent c2151681ec
commit 9cb0015b6e
5 changed files with 46 additions and 5 deletions

View File

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

View File

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

View File

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

View File

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

View File

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