2013-03-01 23:45:13 +00:00
|
|
|
require 'unit_helper'
|
|
|
|
|
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'
|
2014-03-11 22:50:28 +00:00
|
|
|
require 'vagrant-lxc/sudo_wrapper'
|
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-07-28 05:17:05 +00:00
|
|
|
let(:unknown_container) { described_class.new('unknown', nil, cli) }
|
|
|
|
let(:valid_container) { described_class.new('valid', nil, cli) }
|
|
|
|
let(:new_container) { described_class.new(nil, nil) }
|
2014-03-11 22:50:28 +00:00
|
|
|
let(:cli) { 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-07-13 16:42:03 +00:00
|
|
|
}.to raise_error
|
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-07-13 16:42:03 +00:00
|
|
|
}.not_to raise_error
|
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-07-13 16:42:03 +00:00
|
|
|
}.not_to raise_error
|
2013-03-02 04:18:38 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2013-03-02 02:07:15 +00:00
|
|
|
describe 'creation' do
|
2014-05-04 23:50:04 +00:00
|
|
|
let(:name) { 'container-name' }
|
|
|
|
let(:backingstore) { 'btrfs' }
|
|
|
|
let(:backingstore_opts) { [['--dir', '/tmp/foo'], ['--foo', 'bar']] }
|
|
|
|
let(:template_name) { 'auto-assigned-template-id' }
|
|
|
|
let(:template_path) { '/path/to/lxc-template-from-box' }
|
|
|
|
let(:template_opts) { {'--some' => 'random-option'} }
|
|
|
|
let(:config_file) { '/path/to/lxc-config-from-box' }
|
|
|
|
let(:rootfs_tarball) { '/path/to/cache/rootfs.tar.gz' }
|
|
|
|
let(:cli) { double(Vagrant::LXC::Driver::CLI, :create => true, :name= => true) }
|
2013-03-11 00:13:29 +00:00
|
|
|
|
2013-07-28 05:17:05 +00:00
|
|
|
subject { described_class.new(nil, nil, cli) }
|
2013-03-01 23:45:13 +00:00
|
|
|
|
|
|
|
before do
|
2014-03-14 14:37:50 +00:00
|
|
|
allow(subject).to receive(:import_template).and_yield(template_name)
|
2014-05-04 23:50:04 +00:00
|
|
|
subject.create name, backingstore, backingstore_opts, template_path, config_file, template_opts
|
2013-04-05 06:10:38 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
it 'sets the cli object container name' do
|
2014-03-14 14:37:50 +00:00
|
|
|
expect(cli).to 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
|
2014-03-14 14:37:50 +00:00
|
|
|
expect(cli).to have_received(:create).with(
|
2013-03-11 00:13:29 +00:00
|
|
|
template_name,
|
2014-05-04 23:50:04 +00:00
|
|
|
backingstore,
|
|
|
|
backingstore_opts,
|
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
|
2014-03-11 22:50:28 +00:00
|
|
|
let(:cli) { double(Vagrant::LXC::Driver::CLI, destroy: true) }
|
2013-03-02 04:34:47 +00:00
|
|
|
|
2013-07-28 05:17:05 +00:00
|
|
|
subject { described_class.new('name', nil, 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
|
2014-03-14 14:37:50 +00:00
|
|
|
expect(cli).to have_received(:destroy)
|
2013-03-02 04:34:47 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2014-06-09 01:50:30 +00:00
|
|
|
describe 'supports_attach?' do
|
|
|
|
let(:cli) { double(Vagrant::LXC::Driver::CLI, supports_attach?: true) }
|
|
|
|
|
|
|
|
subject { described_class.new('name', nil, cli) }
|
|
|
|
|
|
|
|
it 'delegates to cli object' do
|
|
|
|
expect(subject.supports_attach?).to be_truthy
|
|
|
|
expect(cli).to have_received(:supports_attach?)
|
|
|
|
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'] }
|
2014-03-11 22:50:28 +00:00
|
|
|
let(:cli) { double(Vagrant::LXC::Driver::CLI, start: true) }
|
2014-04-17 10:56:56 +00:00
|
|
|
let(:sudo) { double(Vagrant::LXC::SudoWrapper) }
|
2013-03-11 00:13:29 +00:00
|
|
|
|
2013-08-01 01:35:49 +00:00
|
|
|
subject { described_class.new('name', sudo, cli) }
|
2013-03-02 01:47:02 +00:00
|
|
|
|
|
|
|
before do
|
2014-05-13 02:17:04 +00:00
|
|
|
sudo.should_receive(:run).with('cat', '/var/lib/lxc/name/config').exactly(2).times.
|
2014-04-17 10:56:56 +00:00
|
|
|
and_return('# CONFIGURATION')
|
|
|
|
sudo.should_receive(:run).twice.with('cp', '-f', %r{/tmp/.*}, '/var/lib/lxc/name/config')
|
|
|
|
sudo.should_receive(:run).twice.with('chown', 'root:root', '/var/lib/lxc/name/config')
|
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-08-01 01:35:49 +00:00
|
|
|
it 'prunes previous customizations before writing'
|
|
|
|
|
|
|
|
it 'writes configurations to config file'
|
|
|
|
|
2013-04-10 02:54:28 +00:00
|
|
|
it 'starts container with configured customizations' do
|
2014-03-14 14:37:50 +00:00
|
|
|
expect(cli).to have_received(:start)
|
2013-03-02 02:08:17 +00:00
|
|
|
end
|
2013-03-01 23:45:13 +00:00
|
|
|
end
|
2013-03-02 03:05:10 +00:00
|
|
|
|
2013-03-02 04:20:27 +00:00
|
|
|
describe 'halt' do
|
2014-03-11 22:50:28 +00:00
|
|
|
let(:cli) { double(Vagrant::LXC::Driver::CLI, shutdown: true) }
|
2013-03-11 00:13:29 +00:00
|
|
|
|
2013-07-28 05:17:05 +00:00
|
|
|
subject { described_class.new('name', nil, cli) }
|
2013-03-02 04:20:27 +00:00
|
|
|
|
|
|
|
before do
|
2014-03-14 14:37:50 +00:00
|
|
|
allow(cli).to receive(: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
|
2014-03-14 14:37:50 +00:00
|
|
|
expect(cli).to 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
|
2014-03-14 14:37:50 +00:00
|
|
|
expect(cli).to 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
|
2014-03-14 14:37:50 +00:00
|
|
|
allow(cli).to receive(:shutdown).and_raise(Vagrant::LXC::Driver::CLI::TargetStateNotReached.new :target, :source)
|
|
|
|
expect(cli).to receive(:transition_to).with(:stopped).twice
|
|
|
|
expect(cli).to receive(:stop)
|
2013-05-07 14:07:35 +00:00
|
|
|
subject.forced_halt
|
2013-03-02 04:20:27 +00:00
|
|
|
end
|
2013-10-24 13:44:45 +00:00
|
|
|
|
|
|
|
it 'attempts to force the container to stop in case lxc-shutdown is not supported' do
|
2014-03-14 14:37:50 +00:00
|
|
|
allow(cli).to receive(:shutdown).and_raise(Vagrant::LXC::Driver::CLI::ShutdownNotSupported)
|
|
|
|
expect(cli).to receive(:transition_to).with(:stopped).twice
|
|
|
|
expect(cli).to receive(:stop)
|
2013-10-24 13:44:45 +00:00
|
|
|
subject.forced_halt
|
|
|
|
end
|
2013-03-02 04:20:27 +00:00
|
|
|
end
|
|
|
|
|
2013-03-02 03:05:10 +00:00
|
|
|
describe 'state' do
|
2013-03-11 00:13:29 +00:00
|
|
|
let(:cli_state) { :something }
|
2014-03-11 22:50:28 +00:00
|
|
|
let(:cli) { double(Vagrant::LXC::Driver::CLI, state: cli_state) }
|
2013-03-02 03:05:10 +00:00
|
|
|
|
2013-07-28 05:17:05 +00:00
|
|
|
subject { described_class.new('name', nil, cli) }
|
2013-03-02 03:05:10 +00:00
|
|
|
|
2013-03-11 00:13:29 +00:00
|
|
|
it 'delegates to cli' do
|
2014-03-14 14:37:50 +00:00
|
|
|
expect(subject.state).to eq(cli_state)
|
2013-03-02 03:05:10 +00:00
|
|
|
end
|
|
|
|
end
|
2013-03-02 16:55:05 +00:00
|
|
|
|
2013-04-10 02:54:28 +00:00
|
|
|
describe 'folder sharing' do
|
|
|
|
let(:shared_folder) { {guestpath: '/vagrant', hostpath: '/path/to/host/dir'} }
|
2014-03-14 02:36:15 +00:00
|
|
|
let(:ro_rw_folder) { {guestpath: '/vagrant/ro_rw', hostpath: '/path/to/host/dir', mount_options: ['ro', 'rw']} }
|
|
|
|
let(:folders) { [shared_folder, ro_rw_folder] }
|
2013-04-10 02:54:28 +00:00
|
|
|
let(:rootfs_path) { Pathname('/path/to/rootfs') }
|
2014-05-04 23:51:53 +00:00
|
|
|
let(:expected_guest_path) { "vagrant" }
|
2014-03-11 22:50:28 +00:00
|
|
|
let(:sudo_wrapper) { double(Vagrant::LXC::SudoWrapper, run: true) }
|
2013-04-10 02:54:28 +00:00
|
|
|
|
2013-07-28 05:17:05 +00:00
|
|
|
subject { described_class.new('name', sudo_wrapper) }
|
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
|
2014-05-04 23:51:53 +00:00
|
|
|
expect(sudo_wrapper).to have_received(:run).with("mkdir", "-p", "#{rootfs_path}/#{expected_guest_path}")
|
2013-04-10 02:54:28 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
it 'adds a mount.entry to its local customizations' do
|
2014-03-14 14:37:50 +00:00
|
|
|
expect(subject.customizations).to include [
|
2013-04-10 02:54:28 +00:00
|
|
|
'mount.entry',
|
|
|
|
"#{shared_folder[:hostpath]} #{expected_guest_path} none bind 0 0"
|
|
|
|
]
|
|
|
|
end
|
2014-03-14 02:36:15 +00:00
|
|
|
|
|
|
|
it 'supports additional mount options' do
|
2014-03-14 14:37:50 +00:00
|
|
|
expect(subject.customizations).to include [
|
2014-03-14 02:36:15 +00:00
|
|
|
'mount.entry',
|
2014-05-04 23:51:53 +00:00
|
|
|
"#{ro_rw_folder[:hostpath]} vagrant/ro_rw none ro,rw 0 0"
|
2014-03-14 02:36:15 +00:00
|
|
|
]
|
|
|
|
end
|
2013-04-10 02:54:28 +00:00
|
|
|
end
|
2013-03-01 23:45:13 +00:00
|
|
|
end
|