Properly handle invalid boxes

This commit is contained in:
Fabio Rehm 2013-04-08 22:06:13 -03:00
parent a8825b0b28
commit fd0c863f34
3 changed files with 94 additions and 38 deletions

View file

@ -9,31 +9,50 @@ module Vagrant
end
def call(env)
env[:ui].info I18n.t("vagrant.actions.vm.import.importing",
:name => env[:machine].box.name)
@env = env
@box = @env[:machine].box
box = env[:machine].box
@env[:ui].info I18n.t("vagrant.actions.vm.import.importing",
:name => @env[:machine].box.name)
template_src = box.directory.join('lxc-template').to_s
unless File.exists?(template_src)
raise Errors::TemplateFileMissing.new name: box.name
end
@logger.debug 'Validating box contents'
validate_box
# TODO: Validate box version
@logger.debug('Merging metadata with template name and rootfs tarball')
template_opts = box.metadata.fetch('template-opts', {}).dup
template_opts.merge!(
'--tarball' => box.directory.join('rootfs.tar.gz').to_s,
'--auth-key' => Vagrant.source_root.join('keys', 'vagrant.pub').expand_path.to_s
)
env[:lxc_template_opts] = template_opts
env[:lxc_template_src] = template_src
@logger.debug 'Setting box options on environment'
@env[:lxc_template_opts] = template_opts
@env[:lxc_template_src] = template_src
@app.call env
end
def template_src
@template_src ||= @box.directory.join('lxc-template').to_s
end
def template_opts
@template_opts ||= @box.metadata.fetch('template-opts', {}).dup.merge!(
'--tarball' => rootfs_tarball,
'--auth-key' => Vagrant.source_root.join('keys', 'vagrant.pub').expand_path.to_s
)
end
def rootfs_tarball
@rootfs_tarball ||= @box.directory.join('rootfs.tar.gz').to_s
end
def validate_box
if @box.metadata.fetch('version').to_i != 2
raise Errors::InvalidBoxVersion.new name: @box.name
end
unless File.exists?(template_src)
raise Errors::TemplateFileMissing.new name: @box.name
end
unless File.exists?(rootfs_tarball)
raise Errors::RootFSTarballMissing.new name: @box.name
end
end
end
end
end

View file

@ -6,9 +6,17 @@ module Vagrant
class ExecuteError < Vagrant::Errors::VagrantError
error_key(:lxc_execute_error)
end
# Box related errors
class TemplateFileMissing < Vagrant::Errors::VagrantError
error_key(:lxc_template_file_missing)
end
class RootFSTarballMissing < Vagrant::Errors::VagrantError
error_key(:lxc_invalid_box_version)
end
class InvalidBoxVersion < Vagrant::Errors::VagrantError
error_key(:lxc_invalid_box_version)
end
end
end
end

View file

@ -1,6 +1,7 @@
require 'unit_helper'
require 'vagrant'
require 'vagrant-lxc/errors'
require 'vagrant-lxc/action/handle_box_metadata'
describe Vagrant::LXC::Action::HandleBoxMetadata do
@ -9,33 +10,61 @@ describe Vagrant::LXC::Action::HandleBoxMetadata do
let(:machine) { mock(:machine, box: box) }
let(:box) { mock(:box, name: 'box-name', metadata: metadata, directory: box_directory) }
let(:box_directory) { Pathname.new('/path/to/box') }
let(:metadata) { {'template-opts' => {'--foo' => 'bar'}} }
let(:version) { '2' }
let(:metadata) { {'template-opts' => {'--foo' => 'bar'}, 'version' => version} }
let(:vagrant_key) { Vagrant.source_root.join('keys', 'vagrant.pub').expand_path.to_s }
subject { described_class.new(app, env) }
before do
File.stub(exists?: true)
subject.call(env)
context 'with valid contents' do
before do
File.stub(exists?: true)
subject.call(env)
end
it 'sets the tarball argument for the template' do
env[:lxc_template_opts].should include(
'--tarball' => box_directory.join('rootfs.tar.gz').to_s
)
end
it 'sets the auth key argument for the template' do
env[:lxc_template_opts].should include(
'--auth-key' => vagrant_key
)
end
it 'sets the template options from metadata on env hash' do
env[:lxc_template_opts].should include(metadata['template-opts'])
end
it 'sets the template source path on env hash' do
env[:lxc_template_src].should == box_directory.join('lxc-template').to_s
end
end
it 'sets the tarball argument for the template' do
env[:lxc_template_opts].should include(
'--tarball' => box_directory.join('rootfs.tar.gz').to_s
)
end
describe 'with invalid contents' do
before { File.stub(exists?: true) }
it 'sets the auth key argument for the template' do
env[:lxc_template_opts].should include(
'--auth-key' => vagrant_key
)
end
it 'raises an error if the version is != 2' do
metadata['version'] = '1'
expect {
subject.call(env)
}.to raise_error(Vagrant::LXC::Errors::InvalidBoxVersion)
end
it 'sets the template options from metadata on env hash' do
env[:lxc_template_opts].should include(metadata['template-opts'])
end
it 'raises an error if the rootfs tarball cant be found' do
File.stub(:exists?).with(box_directory.join('rootfs.tar.gz').to_s).and_return(false)
expect {
subject.call(env)
}.to raise_error(Vagrant::LXC::Errors::RootFSTarballMissing)
end
it 'sets the template source path on env hash' do
env[:lxc_template_src].should == box_directory.join('lxc-template').to_s
it 'raises an error if the lxc-template script cant be found' do
File.stub(:exists?).with(box_directory.join('lxc-template').to_s).and_return(false)
expect {
subject.call(env)
}.to raise_error(Vagrant::LXC::Errors::TemplateFileMissing)
end
end
end