Merge pull request #12895 from soapy1/validate-box-metadata-json
Raise error if required metadata.json box fields are not present
This commit is contained in:
commit
166abaacf6
@ -18,6 +18,8 @@ module Vagrant
|
||||
|
||||
autoload :Remote, "vagrant/box/remote"
|
||||
|
||||
# The required fields in a boxes `metadata.json` file
|
||||
REQUIRED_METADATA_FIELDS = ["provider"]
|
||||
|
||||
# Number of seconds to wait between checks for box updates
|
||||
BOX_UPDATE_CHECK_INTERVAL = 3600
|
||||
@ -75,6 +77,7 @@ module Vagrant
|
||||
|
||||
begin
|
||||
@metadata = JSON.parse(directory.join("metadata.json").read)
|
||||
validate_metadata_json(@metadata)
|
||||
rescue JSON::ParserError
|
||||
raise Errors::BoxMetadataCorrupted, name: @name
|
||||
end
|
||||
@ -82,6 +85,18 @@ module Vagrant
|
||||
@logger = Log4r::Logger.new("vagrant::box")
|
||||
end
|
||||
|
||||
def validate_metadata_json(metadata)
|
||||
metatdata_fields = metadata.keys
|
||||
REQUIRED_METADATA_FIELDS.each do |field|
|
||||
if !metatdata_fields.include?(field)
|
||||
raise Errors::BoxMetadataMissingRequiredFields,
|
||||
name: @name,
|
||||
required_field: field,
|
||||
all_fields: REQUIRED_METADATA_FIELDS.join(", ")
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
# This deletes the box. This is NOT undoable.
|
||||
def destroy!
|
||||
# Delete the directory to delete the box.
|
||||
|
||||
@ -168,6 +168,10 @@ module Vagrant
|
||||
error_key(:box_metadata_corrupted)
|
||||
end
|
||||
|
||||
class BoxMetadataMissingRequiredFields < VagrantError
|
||||
error_key(:box_metadata_missing_required_fields)
|
||||
end
|
||||
|
||||
class BoxMetadataDownloadError < VagrantError
|
||||
error_key(:box_metadata_download_error)
|
||||
end
|
||||
|
||||
@ -615,6 +615,12 @@ en:
|
||||
The metadata associated with the box '%{name}' appears corrupted.
|
||||
This is most often caused by a disk issue or system crash. Please
|
||||
remove the box, re-add it, and try again.
|
||||
box_metadata_missing_required_fields: |-
|
||||
The metadata associated with the box '%{name}' appears to be missing
|
||||
the required field '%{required_field}'. Please ensure `metadata.json`
|
||||
has all required fields.
|
||||
|
||||
Required fields: %{all_fields}
|
||||
box_metadata_download_error: |-
|
||||
There was an error while downloading the metadata for this box.
|
||||
The error message is shown below:
|
||||
|
||||
@ -44,6 +44,19 @@ describe Vagrant::Box, :skip_windows do
|
||||
f.write(JSON.generate(data))
|
||||
end
|
||||
|
||||
# Verify the metadata
|
||||
expect { subject.metadata }.
|
||||
to raise_error(Vagrant::Errors::BoxMetadataMissingRequiredFields)
|
||||
end
|
||||
|
||||
it "provides the metadata associated with a box" do
|
||||
data = { "provider" => "bar" }
|
||||
|
||||
# Write the metadata
|
||||
directory.join("metadata.json").open("w") do |f|
|
||||
f.write(JSON.generate(data))
|
||||
end
|
||||
|
||||
# Verify the metadata
|
||||
expect(subject.metadata).to eq(data)
|
||||
end
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user