From fd0c863f345f9b62ba523a4159c434106b7d0561 Mon Sep 17 00:00:00 2001 From: Fabio Rehm Date: Mon, 8 Apr 2013 22:06:13 -0300 Subject: [PATCH] Properly handle invalid boxes --- lib/vagrant-lxc/action/handle_box_metadata.rb | 57 ++++++++++------ lib/vagrant-lxc/errors.rb | 8 +++ spec/unit/action/handle_box_metadata_spec.rb | 67 +++++++++++++------ 3 files changed, 94 insertions(+), 38 deletions(-) diff --git a/lib/vagrant-lxc/action/handle_box_metadata.rb b/lib/vagrant-lxc/action/handle_box_metadata.rb index fb9f9dd..1471369 100644 --- a/lib/vagrant-lxc/action/handle_box_metadata.rb +++ b/lib/vagrant-lxc/action/handle_box_metadata.rb @@ -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 diff --git a/lib/vagrant-lxc/errors.rb b/lib/vagrant-lxc/errors.rb index a9cda95..bb056ee 100644 --- a/lib/vagrant-lxc/errors.rb +++ b/lib/vagrant-lxc/errors.rb @@ -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 diff --git a/spec/unit/action/handle_box_metadata_spec.rb b/spec/unit/action/handle_box_metadata_spec.rb index a656d5b..2d3df93 100644 --- a/spec/unit/action/handle_box_metadata_spec.rb +++ b/spec/unit/action/handle_box_metadata_spec.rb @@ -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