2013-03-01 23:45:13 +00:00
|
|
|
require 'unit_helper'
|
|
|
|
|
2013-03-15 01:39:57 +00:00
|
|
|
require 'vagrant'
|
2013-04-05 05:17:19 +00:00
|
|
|
require 'vagrant-lxc/driver'
|
2013-05-07 14:07:35 +00:00
|
|
|
require 'vagrant-lxc/driver/cli'
|
2013-03-01 23:45:13 +00:00
|
|
|
|
2013-04-05 05:17:19 +00:00
|
|
|
describe Vagrant::LXC::Driver do
|
2013-03-02 04:18:38 +00:00
|
|
|
describe 'container name validation' do
|
2013-03-10 22:22:58 +00:00
|
|
|
let(:unknown_container) { described_class.new('unknown', cli) }
|
|
|
|
let(:valid_container) { described_class.new('valid', cli) }
|
2013-03-02 04:18:38 +00:00
|
|
|
let(:new_container) { described_class.new(nil) }
|
2013-04-05 05:17:19 +00:00
|
|
|
let(:cli) { fire_double('Vagrant::LXC::Driver::CLI', list: ['valid']) }
|
2013-03-02 04:18:38 +00:00
|
|
|
|
2013-04-05 05:17:19 +00:00
|
|
|
it 'raises a ContainerNotFound error if an unknown container name gets provided' do
|
2013-03-02 04:18:38 +00:00
|
|
|
expect {
|
|
|
|
unknown_container.validate!
|
2013-04-05 05:17:19 +00:00
|
|
|
}.to raise_error(Vagrant::LXC::Driver::ContainerNotFound)
|
2013-03-02 04:18:38 +00:00
|
|
|
end
|
|
|
|
|
2013-04-05 05:17:19 +00:00
|
|
|
it 'does not raise a ContainerNotFound error if a valid container name gets provided' do
|
2013-03-02 04:18:38 +00:00
|
|
|
expect {
|
|
|
|
valid_container.validate!
|
2013-04-05 05:17:19 +00:00
|
|
|
}.to_not raise_error(Vagrant::LXC::Driver::ContainerNotFound)
|
2013-03-02 04:18:38 +00:00
|
|
|
end
|
|
|
|
|
2013-04-05 05:17:19 +00:00
|
|
|
it 'does not raise a ContainerNotFound error if nil is provider as name' do
|
2013-03-02 04:18:38 +00:00
|
|
|
expect {
|
|
|
|
new_container.validate!
|
2013-04-05 05:17:19 +00:00
|
|
|
}.to_not raise_error(Vagrant::LXC::Driver::ContainerNotFound)
|
2013-03-02 04:18:38 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2013-03-02 02:07:15 +00:00
|
|
|
describe 'creation' do
|
2013-04-05 06:10:38 +00:00
|
|
|
let(:name) { 'container-name' }
|
|
|
|
let(:template_name) { 'auto-assigned-template-id' }
|
|
|
|
let(:template_path) { '/path/to/lxc-template-from-box' }
|
|
|
|
let(:template_opts) { {'--some' => 'random-option'} }
|
2013-06-08 05:01:50 +00:00
|
|
|
let(:config_file) { '/path/to/lxc-config-from-box' }
|
2013-04-05 06:10:38 +00:00
|
|
|
let(:rootfs_tarball) { '/path/to/cache/rootfs.tar.gz' }
|
|
|
|
let(:cli) { fire_double('Vagrant::LXC::Driver::CLI', :create => true, :name= => true) }
|
2013-03-11 00:13:29 +00:00
|
|
|
|
2013-04-05 06:10:38 +00:00
|
|
|
subject { described_class.new(nil, cli) }
|
2013-03-01 23:45:13 +00:00
|
|
|
|
|
|
|
before do
|
2013-04-05 06:10:38 +00:00
|
|
|
subject.stub(:import_template).and_yield(template_name)
|
2013-06-08 05:01:50 +00:00
|
|
|
subject.create name, template_path, config_file, template_opts
|
2013-04-05 06:10:38 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
it 'sets the cli object container name' do
|
|
|
|
cli.should have_received(:name=).with(name)
|
2013-03-01 23:45:13 +00:00
|
|
|
end
|
|
|
|
|
2013-03-11 00:13:29 +00:00
|
|
|
it 'creates container with the right arguments' do
|
|
|
|
cli.should have_received(:create).with(
|
|
|
|
template_name,
|
2013-06-08 05:01:50 +00:00
|
|
|
config_file,
|
2013-04-05 06:10:38 +00:00
|
|
|
template_opts
|
2013-03-02 02:27:08 +00:00
|
|
|
)
|
2013-03-02 01:47:02 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2013-03-02 04:34:47 +00:00
|
|
|
describe 'destruction' do
|
2013-04-10 02:56:08 +00:00
|
|
|
let(:cli) { fire_double('Vagrant::LXC::Driver::CLI', destroy: true) }
|
2013-03-02 04:34:47 +00:00
|
|
|
|
2013-04-10 02:56:08 +00:00
|
|
|
subject { described_class.new('name', cli) }
|
2013-03-02 04:34:47 +00:00
|
|
|
|
2013-03-11 00:13:29 +00:00
|
|
|
before { subject.destroy }
|
|
|
|
|
|
|
|
it 'delegates to cli object' do
|
|
|
|
cli.should have_received(:destroy)
|
2013-03-02 04:34:47 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2013-03-02 01:47:02 +00:00
|
|
|
describe 'start' do
|
2013-04-10 02:54:28 +00:00
|
|
|
let(:customizations) { [['a', '1'], ['b', '2']] }
|
|
|
|
let(:internal_customization) { ['internal', 'customization'] }
|
2013-06-08 05:01:50 +00:00
|
|
|
let(:rootfs) { ['rootfs', subject.rootfs_path.to_s] }
|
2013-04-10 02:54:28 +00:00
|
|
|
let(:cli) { fire_double('Vagrant::LXC::Driver::CLI', start: true) }
|
2013-03-11 00:13:29 +00:00
|
|
|
|
2013-04-10 02:54:28 +00:00
|
|
|
subject { described_class.new('name', cli) }
|
2013-03-02 01:47:02 +00:00
|
|
|
|
|
|
|
before do
|
2013-03-11 00:13:29 +00:00
|
|
|
cli.stub(:transition_to).and_yield(cli)
|
2013-04-10 02:54:28 +00:00
|
|
|
subject.customizations << internal_customization
|
|
|
|
subject.start(customizations)
|
2013-03-02 01:47:02 +00:00
|
|
|
end
|
|
|
|
|
2013-04-10 02:54:28 +00:00
|
|
|
it 'starts container with configured customizations' do
|
2013-06-08 05:01:50 +00:00
|
|
|
cli.should have_received(:start).with(customizations + [internal_customization, rootfs], nil)
|
2013-03-02 02:08:17 +00:00
|
|
|
end
|
|
|
|
|
2013-03-11 00:13:29 +00:00
|
|
|
it 'expects a transition to running state to take place' do
|
2013-04-10 02:54:28 +00:00
|
|
|
cli.should have_received(:transition_to).with(:running)
|
2013-03-01 23:45:13 +00:00
|
|
|
end
|
|
|
|
end
|
2013-03-02 03:05:10 +00:00
|
|
|
|
2013-03-02 04:20:27 +00:00
|
|
|
describe 'halt' do
|
2013-04-10 02:56:08 +00:00
|
|
|
let(:cli) { fire_double('Vagrant::LXC::Driver::CLI', shutdown: true) }
|
2013-03-11 00:13:29 +00:00
|
|
|
|
2013-04-10 02:56:08 +00:00
|
|
|
subject { described_class.new('name', cli) }
|
2013-03-02 04:20:27 +00:00
|
|
|
|
|
|
|
before do
|
2013-03-11 00:13:29 +00:00
|
|
|
cli.stub(:transition_to).and_yield(cli)
|
2013-03-02 04:20:27 +00:00
|
|
|
end
|
|
|
|
|
2013-03-11 00:13:29 +00:00
|
|
|
it 'delegates to cli shutdown' do
|
|
|
|
cli.should_receive(:shutdown)
|
2013-05-07 14:07:35 +00:00
|
|
|
subject.forced_halt
|
2013-03-02 04:20:27 +00:00
|
|
|
end
|
|
|
|
|
2013-03-11 00:13:29 +00:00
|
|
|
it 'expects a transition to running state to take place' do
|
|
|
|
cli.should_receive(:transition_to).with(:stopped)
|
2013-05-07 14:07:35 +00:00
|
|
|
subject.forced_halt
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'attempts to force the container to stop in case a shutdown doesnt work' do
|
|
|
|
cli.stub(:shutdown).and_raise(Vagrant::LXC::Driver::CLI::TargetStateNotReached.new :target, :source)
|
|
|
|
cli.should_receive(:transition_to).with(:stopped).twice
|
|
|
|
cli.should_receive(:stop)
|
|
|
|
subject.forced_halt
|
2013-03-02 04:20:27 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2013-03-02 03:05:10 +00:00
|
|
|
describe 'state' do
|
2013-03-11 00:13:29 +00:00
|
|
|
let(:cli_state) { :something }
|
2013-04-05 05:17:19 +00:00
|
|
|
let(:cli) { fire_double('Vagrant::LXC::Driver::CLI', state: cli_state) }
|
2013-03-02 03:05:10 +00:00
|
|
|
|
2013-04-10 02:56:08 +00:00
|
|
|
subject { described_class.new('name', cli) }
|
2013-03-02 03:05:10 +00:00
|
|
|
|
2013-03-11 00:13:29 +00:00
|
|
|
it 'delegates to cli' do
|
|
|
|
subject.state.should == cli_state
|
2013-03-02 03:05:10 +00:00
|
|
|
end
|
|
|
|
end
|
2013-03-02 16:55:05 +00:00
|
|
|
|
2013-04-10 05:02:36 +00:00
|
|
|
pending 'assigned ip' do
|
2013-04-05 06:23:03 +00:00
|
|
|
# This ip is set on the sample-ip-addr-output fixture
|
|
|
|
let(:ip) { "10.0.254.137" }
|
|
|
|
let(:ifconfig_output) { File.read('spec/fixtures/sample-ip-addr-output') }
|
2013-04-05 05:17:19 +00:00
|
|
|
let(:cli) { fire_double('Vagrant::LXC::Driver::CLI', :attach => ifconfig_output) }
|
2013-03-11 00:59:45 +00:00
|
|
|
|
2013-04-10 02:56:08 +00:00
|
|
|
subject { described_class.new('name', cli) }
|
2013-03-19 04:46:44 +00:00
|
|
|
|
|
|
|
context 'when ip for eth0 gets returned from lxc-attach call' do
|
2013-04-10 01:06:55 +00:00
|
|
|
it 'gets parsed from `ip addr` output' do
|
2013-03-11 00:59:45 +00:00
|
|
|
subject.assigned_ip.should == ip
|
2013-04-05 06:23:03 +00:00
|
|
|
cli.should have_received(:attach).with(
|
|
|
|
'/sbin/ip',
|
|
|
|
'-4',
|
|
|
|
'addr',
|
|
|
|
'show',
|
|
|
|
'scope',
|
|
|
|
'global',
|
|
|
|
'eth0',
|
|
|
|
namespaces: 'network'
|
|
|
|
)
|
2013-03-11 00:59:45 +00:00
|
|
|
end
|
2013-03-02 16:55:05 +00:00
|
|
|
end
|
|
|
|
end
|
2013-04-10 02:54:28 +00:00
|
|
|
|
|
|
|
describe 'folder sharing' do
|
|
|
|
let(:shared_folder) { {guestpath: '/vagrant', hostpath: '/path/to/host/dir'} }
|
|
|
|
let(:folders) { [shared_folder] }
|
|
|
|
let(:rootfs_path) { Pathname('/path/to/rootfs') }
|
|
|
|
let(:expected_guest_path) { "#{rootfs_path}/vagrant" }
|
|
|
|
|
2013-04-10 02:56:08 +00:00
|
|
|
subject { described_class.new('name') }
|
2013-04-10 02:54:28 +00:00
|
|
|
|
|
|
|
before do
|
|
|
|
subject.stub(rootfs_path: rootfs_path, system: true)
|
|
|
|
subject.share_folders(folders)
|
|
|
|
end
|
|
|
|
|
|
|
|
it "creates guest folder under container's rootfs" do
|
|
|
|
subject.should have_received(:system).with("sudo mkdir -p #{expected_guest_path}")
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'adds a mount.entry to its local customizations' do
|
|
|
|
subject.customizations.should include [
|
|
|
|
'mount.entry',
|
|
|
|
"#{shared_folder[:hostpath]} #{expected_guest_path} none bind 0 0"
|
|
|
|
]
|
|
|
|
end
|
|
|
|
end
|
2013-03-01 23:45:13 +00:00
|
|
|
end
|