2013-03-02 15:17:04 +00:00
|
|
|
require 'unit_helper'
|
|
|
|
|
2013-04-05 06:10:38 +00:00
|
|
|
require 'vagrant'
|
2013-04-09 01:06:13 +00:00
|
|
|
require 'vagrant-lxc/errors'
|
2013-03-02 15:17:04 +00:00
|
|
|
require 'vagrant-lxc/action/handle_box_metadata'
|
|
|
|
|
|
|
|
describe Vagrant::LXC::Action::HandleBoxMetadata do
|
2013-07-13 16:41:39 +00:00
|
|
|
let(:app) { double(:app, call: true) }
|
2014-03-09 21:11:40 +00:00
|
|
|
let(:env) { {machine: machine, ui: double(info: true, warn: true)} }
|
2013-07-13 16:41:39 +00:00
|
|
|
let(:machine) { double(:machine, box: box) }
|
|
|
|
let(:box) { double(:box, name: 'box-name', metadata: metadata, directory: box_directory) }
|
2013-04-05 06:10:38 +00:00
|
|
|
let(:box_directory) { Pathname.new('/path/to/box') }
|
2013-04-09 01:06:13 +00:00
|
|
|
let(:version) { '2' }
|
|
|
|
let(:metadata) { {'template-opts' => {'--foo' => 'bar'}, 'version' => version} }
|
2013-04-05 06:10:38 +00:00
|
|
|
let(:vagrant_key) { Vagrant.source_root.join('keys', 'vagrant.pub').expand_path.to_s }
|
2013-03-02 15:17:04 +00:00
|
|
|
|
|
|
|
subject { described_class.new(app, env) }
|
|
|
|
|
2014-03-09 21:11:40 +00:00
|
|
|
context 'with 1.0.0 box' do
|
|
|
|
let(:version) { '1.0.0' }
|
|
|
|
|
|
|
|
before do
|
|
|
|
File.stub(exists?: true)
|
|
|
|
# REFACTOR: This is pretty bad
|
|
|
|
subject.stub_chain(:template_config_file, :exist?).and_return(true)
|
|
|
|
subject.stub_chain(:template_config_file, :to_s).and_return(box_directory.join('lxc-config').to_s)
|
|
|
|
subject.call(env)
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'sets the tarball argument for the template' do
|
2014-03-14 14:37:50 +00:00
|
|
|
expect(env[:lxc_template_opts]).to include(
|
2014-03-09 21:11:40 +00:00
|
|
|
'--tarball' => box_directory.join('rootfs.tar.gz').to_s
|
|
|
|
)
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'sets the template --config parameter' do
|
2014-03-14 14:37:50 +00:00
|
|
|
expect(env[:lxc_template_opts]).to include(
|
2014-03-09 21:11:40 +00:00
|
|
|
'--config' => box_directory.join('lxc-config').to_s
|
|
|
|
)
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'does not set the auth key argument for the template' do
|
2014-03-14 14:37:50 +00:00
|
|
|
expect(env[:lxc_template_opts]).not_to include(
|
2014-03-09 21:11:40 +00:00
|
|
|
'--auth-key' => vagrant_key
|
|
|
|
)
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'sets the template options from metadata on env hash' do
|
2014-03-14 14:37:50 +00:00
|
|
|
expect(env[:lxc_template_opts]).to include(metadata['template-opts'])
|
2014-03-09 21:11:40 +00:00
|
|
|
end
|
|
|
|
|
2014-03-14 14:20:41 +00:00
|
|
|
xit 'sets the template source path on env hash' do
|
2014-03-14 14:37:50 +00:00
|
|
|
expect(env[:lxc_template_src]).to eq(box_directory.join('lxc-template').to_s)
|
2014-03-09 21:11:40 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
it 'does not warn about deprecation' do
|
2014-03-14 14:37:50 +00:00
|
|
|
expect(env[:ui]).not_to have_received(:warn)
|
2014-03-09 21:11:40 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'with valid pre 1.0.0 box' do
|
2013-04-09 01:06:13 +00:00
|
|
|
before do
|
|
|
|
File.stub(exists?: true)
|
2014-03-09 21:11:40 +00:00
|
|
|
# REFACTOR: This is pretty bad
|
|
|
|
subject.stub_chain(:old_template_config_file, :exist?).and_return(true)
|
|
|
|
subject.stub_chain(:old_template_config_file, :to_s).and_return(box_directory.join('lxc.conf').to_s)
|
2013-04-09 01:06:13 +00:00
|
|
|
subject.call(env)
|
|
|
|
end
|
2013-03-02 15:17:04 +00:00
|
|
|
|
2013-04-09 01:06:13 +00:00
|
|
|
it 'sets the tarball argument for the template' do
|
2014-03-14 14:37:50 +00:00
|
|
|
expect(env[:lxc_template_opts]).to include(
|
2013-04-09 01:06:13 +00:00
|
|
|
'--tarball' => box_directory.join('rootfs.tar.gz').to_s
|
|
|
|
)
|
|
|
|
end
|
2013-03-02 15:17:04 +00:00
|
|
|
|
2013-04-09 01:06:13 +00:00
|
|
|
it 'sets the auth key argument for the template' do
|
2014-03-14 14:37:50 +00:00
|
|
|
expect(env[:lxc_template_opts]).to include(
|
2013-04-09 01:06:13 +00:00
|
|
|
'--auth-key' => vagrant_key
|
|
|
|
)
|
|
|
|
end
|
|
|
|
|
2014-03-09 21:11:40 +00:00
|
|
|
it 'sets the lxc config file parameter' do
|
2014-03-14 14:37:50 +00:00
|
|
|
expect(env[:lxc_template_config]).to eq(box_directory.join('lxc.conf').to_s)
|
2014-03-09 21:11:40 +00:00
|
|
|
end
|
|
|
|
|
2013-04-09 01:06:13 +00:00
|
|
|
it 'sets the template options from metadata on env hash' do
|
2014-03-14 14:37:50 +00:00
|
|
|
expect(env[:lxc_template_opts]).to include(metadata['template-opts'])
|
2013-04-09 01:06:13 +00:00
|
|
|
end
|
2013-03-02 15:17:04 +00:00
|
|
|
|
2014-03-14 14:20:41 +00:00
|
|
|
xit 'sets the template source path on env hash' do
|
2014-03-14 14:37:50 +00:00
|
|
|
expect(env[:lxc_template_src]).to eq(box_directory.join('lxc-template').to_s)
|
2013-04-09 01:06:13 +00:00
|
|
|
end
|
2014-03-09 21:11:40 +00:00
|
|
|
|
|
|
|
it 'warns about deprecation' do
|
2014-03-14 14:37:50 +00:00
|
|
|
expect(env[:ui]).to have_received(:warn)
|
2014-03-09 21:11:40 +00:00
|
|
|
end
|
2013-04-05 06:10:38 +00:00
|
|
|
end
|
2013-03-08 03:54:15 +00:00
|
|
|
|
2013-04-09 01:06:13 +00:00
|
|
|
describe 'with invalid contents' do
|
|
|
|
before { File.stub(exists?: true) }
|
|
|
|
|
2014-03-09 21:11:40 +00:00
|
|
|
it 'validates box versions' do
|
|
|
|
%w( 2 3 1.0.0 ).each do |v|
|
|
|
|
metadata['version'] = v
|
2014-03-11 22:50:28 +00:00
|
|
|
expect { subject.call(env) }.to_not raise_error
|
2014-03-09 21:11:40 +00:00
|
|
|
end
|
|
|
|
|
2013-04-09 01:06:13 +00:00
|
|
|
metadata['version'] = '1'
|
2014-03-11 22:50:28 +00:00
|
|
|
expect { subject.call(env) }.to raise_error
|
2013-04-09 01:06:13 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
it 'raises an error if the rootfs tarball cant be found' do
|
2014-03-14 14:37:50 +00:00
|
|
|
allow(File).to receive(:exists?).with(box_directory.join('rootfs.tar.gz').to_s).and_return(false)
|
2013-04-09 01:06:13 +00:00
|
|
|
expect {
|
|
|
|
subject.call(env)
|
|
|
|
}.to raise_error(Vagrant::LXC::Errors::RootFSTarballMissing)
|
|
|
|
end
|
|
|
|
|
2014-03-14 14:18:39 +00:00
|
|
|
it 'does not raise an error if the lxc-template script cant be found' do
|
2014-03-14 14:37:50 +00:00
|
|
|
allow(File).to receive(:exists?).with(box_directory.join('lxc-template').to_s).and_return(false)
|
2013-04-09 01:06:13 +00:00
|
|
|
expect {
|
|
|
|
subject.call(env)
|
2014-03-14 14:18:39 +00:00
|
|
|
}.to_not raise_error
|
2013-04-09 01:06:13 +00:00
|
|
|
end
|
2013-03-02 15:17:04 +00:00
|
|
|
end
|
|
|
|
end
|