2013-03-01 23:45:13 +00:00
|
|
|
require 'unit_helper'
|
|
|
|
|
|
|
|
require 'vagrant-lxc/container'
|
|
|
|
|
|
|
|
describe Vagrant::LXC::Container do
|
2013-03-02 03:55:45 +00:00
|
|
|
# Default subject and container name for specs
|
|
|
|
let(:name) { nil }
|
|
|
|
subject { described_class.new(name) }
|
2013-03-01 23:45:13 +00:00
|
|
|
|
2013-03-02 04:18:38 +00:00
|
|
|
describe 'container name validation' do
|
|
|
|
let(:unknown_container) { described_class.new('unknown') }
|
|
|
|
let(:valid_container) { described_class.new('valid') }
|
|
|
|
let(:new_container) { described_class.new(nil) }
|
|
|
|
|
|
|
|
before do
|
|
|
|
unknown_container.stub(lxc: 'valid')
|
|
|
|
valid_container.stub(lxc: 'valid')
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'raises a NotFound error if an unknown container name gets provided' do
|
|
|
|
expect {
|
|
|
|
unknown_container.validate!
|
|
|
|
}.to raise_error(Vagrant::LXC::Container::NotFound)
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'does not raise a NotFound error if a valid container name gets provided' do
|
|
|
|
expect {
|
|
|
|
valid_container.validate!
|
|
|
|
}.to_not raise_error(Vagrant::LXC::Container::NotFound)
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'does not raise a NotFound error if nil is provider as name' do
|
|
|
|
expect {
|
|
|
|
new_container.validate!
|
|
|
|
}.to_not raise_error(Vagrant::LXC::Container::NotFound)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2013-03-02 01:47:02 +00:00
|
|
|
describe 'lxc commands execution' do
|
|
|
|
let(:args) { @args }
|
|
|
|
|
|
|
|
before do
|
|
|
|
subject.stub(:execute) { |*args| @args = args }
|
|
|
|
subject.lxc :command, '--state', 'RUNNING'
|
|
|
|
end
|
|
|
|
|
2013-03-02 02:08:17 +00:00
|
|
|
it 'prepends sudo' do
|
2013-03-02 01:47:02 +00:00
|
|
|
args[0].should == 'sudo'
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'uses the first argument as lxc command suffix' do
|
|
|
|
args[1].should == 'lxc-command'
|
|
|
|
end
|
|
|
|
|
2013-03-02 02:07:15 +00:00
|
|
|
it 'pass through remaining arguments' do
|
2013-03-02 01:47:02 +00:00
|
|
|
args[2].should == '--state'
|
|
|
|
args[3].should == 'RUNNING'
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2013-03-02 02:07:15 +00:00
|
|
|
describe 'guard for container state' do
|
2013-03-02 03:55:45 +00:00
|
|
|
let(:name) { 'random-container-name' }
|
2013-03-02 02:07:15 +00:00
|
|
|
|
|
|
|
before do
|
2013-03-02 02:27:08 +00:00
|
|
|
subject.stub :lxc
|
2013-03-02 02:07:15 +00:00
|
|
|
subject.wait_until :running
|
|
|
|
end
|
|
|
|
|
2013-03-02 02:27:08 +00:00
|
|
|
it 'runs lxc-wait with the machine id and upcased state' do
|
|
|
|
subject.should have_received(:lxc).with(
|
|
|
|
:wait,
|
2013-03-02 03:55:45 +00:00
|
|
|
'--name', name,
|
2013-03-02 02:27:08 +00:00
|
|
|
'--state', 'RUNNING'
|
|
|
|
)
|
2013-03-02 02:07:15 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
describe 'creation' do
|
2013-03-02 03:55:45 +00:00
|
|
|
let(:name) { 'random-container-name' }
|
2013-03-02 15:17:04 +00:00
|
|
|
let(:template_name) { 'template-name' }
|
|
|
|
let(:tar_cache_path) { '/path/to/tar/cache' }
|
2013-03-03 02:12:26 +00:00
|
|
|
let(:lxc_cache) { '/path/to/cache' }
|
2013-03-02 03:03:48 +00:00
|
|
|
let(:public_key_path) { Vagrant.source_root.join('keys', 'vagrant.pub').expand_path.to_s }
|
2013-03-01 23:45:13 +00:00
|
|
|
|
|
|
|
before do
|
2013-03-02 19:38:53 +00:00
|
|
|
subject.stub(lxc: true)
|
2013-03-02 03:55:45 +00:00
|
|
|
SecureRandom.stub(hex: name)
|
2013-03-03 02:12:26 +00:00
|
|
|
subject.create 'template-name' => template_name, 'tar-cache' => tar_cache_path, 'lxc-cache-path' => lxc_cache
|
2013-03-01 23:45:13 +00:00
|
|
|
end
|
|
|
|
|
2013-03-02 01:47:02 +00:00
|
|
|
it 'calls lxc-create with the right arguments' do
|
2013-03-02 02:27:08 +00:00
|
|
|
subject.should have_received(:lxc).with(
|
|
|
|
:create,
|
2013-03-02 15:17:04 +00:00
|
|
|
'--template', template_name,
|
2013-03-02 03:55:45 +00:00
|
|
|
'--name', name,
|
2013-03-02 02:27:08 +00:00
|
|
|
'--',
|
2013-03-02 15:17:04 +00:00
|
|
|
'-S', public_key_path,
|
2013-03-03 02:12:26 +00:00
|
|
|
'--cache-path', lxc_cache,
|
2013-03-02 15:17:04 +00:00
|
|
|
'-T', tar_cache_path
|
2013-03-02 02:27:08 +00:00
|
|
|
)
|
2013-03-02 01:47:02 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2013-03-02 19:38:53 +00:00
|
|
|
describe 'after create script execution' do
|
|
|
|
let(:name) { 'random-container-name' }
|
|
|
|
let(:after_create_path) { '/path/to/after/create' }
|
|
|
|
let(:execute_cmd) { @execute_cmd }
|
|
|
|
let(:priv_key_path) { Vagrant.source_root.join('keys', 'vagrant').expand_path.to_s }
|
|
|
|
let(:ip) { '10.0.3.234' }
|
|
|
|
|
|
|
|
before do
|
|
|
|
subject.stub(dhcp_ip: ip)
|
|
|
|
subject.stub(:execute) { |*args| @execute_cmd = args.join(' ') }
|
|
|
|
subject.run_after_create_script after_create_path
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'runs after-create-script when present passing required variables' do
|
|
|
|
execute_cmd.should include after_create_path
|
|
|
|
execute_cmd.should include "-r /var/lib/lxc/#{name}/rootfs"
|
|
|
|
execute_cmd.should include "-k #{priv_key_path}"
|
|
|
|
execute_cmd.should include "-i #{ip}"
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2013-03-02 04:34:47 +00:00
|
|
|
describe 'destruction' do
|
|
|
|
let(:name) { 'container-name' }
|
|
|
|
|
|
|
|
before do
|
|
|
|
subject.stub(lxc: true)
|
|
|
|
subject.destroy
|
|
|
|
end
|
|
|
|
|
2013-03-02 19:40:03 +00:00
|
|
|
it 'calls lxc-destroy with the right arguments' do
|
2013-03-02 04:34:47 +00:00
|
|
|
subject.should have_received(:lxc).with(
|
|
|
|
:destroy,
|
|
|
|
'--name', name,
|
|
|
|
)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2013-03-02 01:47:02 +00:00
|
|
|
describe 'start' do
|
2013-03-03 07:37:07 +00:00
|
|
|
let(:config) { mock(:config, start_opts: ['a=1', 'b=2']) }
|
|
|
|
let(:name) { 'container-name' }
|
2013-03-02 01:47:02 +00:00
|
|
|
|
|
|
|
before do
|
2013-03-02 02:08:17 +00:00
|
|
|
subject.stub(lxc: true, wait_until: true)
|
2013-03-03 07:37:07 +00:00
|
|
|
subject.start(config)
|
2013-03-02 01:47:02 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
it 'calls lxc-start with the right arguments' do
|
2013-03-02 02:27:08 +00:00
|
|
|
subject.should have_received(:lxc).with(
|
|
|
|
:start,
|
|
|
|
'-d',
|
2013-03-03 07:37:07 +00:00
|
|
|
'--name', name,
|
|
|
|
'-s', 'a=1',
|
|
|
|
'-s', 'b=2'
|
2013-03-02 02:27:08 +00:00
|
|
|
)
|
2013-03-02 02:08:17 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
it 'waits for container state to be RUNNING' do
|
|
|
|
subject.should have_received(:wait_until).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
|
|
|
|
let(:name) { 'random-container-name' }
|
|
|
|
|
|
|
|
before do
|
|
|
|
subject.stub(lxc: true, wait_until: true)
|
|
|
|
subject.halt
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'calls lxc-shutdown with the right arguments' do
|
|
|
|
subject.should have_received(:lxc).with(
|
|
|
|
:shutdown,
|
|
|
|
'--name', name
|
|
|
|
)
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'waits for container state to be STOPPED' do
|
|
|
|
subject.should have_received(:wait_until).with(:stopped)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2013-03-02 03:05:10 +00:00
|
|
|
describe 'state' do
|
2013-03-02 03:55:45 +00:00
|
|
|
let(:name) { 'random-container-name' }
|
2013-03-02 03:05:10 +00:00
|
|
|
|
|
|
|
before do
|
|
|
|
subject.stub(lxc: "state: STOPPED\npid: 2")
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'calls lxc-info with the right arguments' do
|
|
|
|
subject.state
|
|
|
|
subject.should have_received(:lxc).with(
|
|
|
|
:info,
|
2013-03-02 03:55:45 +00:00
|
|
|
'--name', name
|
2013-03-02 03:05:10 +00:00
|
|
|
)
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'maps the output of lxc-info status out to a symbol' do
|
|
|
|
subject.state.should == :stopped
|
|
|
|
end
|
|
|
|
end
|
2013-03-02 16:55:05 +00:00
|
|
|
|
|
|
|
describe 'dhcp ip' do
|
|
|
|
let(:name) { 'random-container-name' }
|
|
|
|
let(:ip) { "10.0.3.123" }
|
|
|
|
|
|
|
|
before do
|
|
|
|
subject.stub(:raw) {
|
|
|
|
mock(stdout: "#{ip}\n", exit_code: 0)
|
|
|
|
}
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'digs the container ip from lxc dns server' do
|
|
|
|
subject.dhcp_ip.should == ip
|
|
|
|
subject.should have_received(:raw).with('dig', name, '@10.0.3.1', '+short')
|
|
|
|
end
|
|
|
|
end
|
2013-03-01 23:45:13 +00:00
|
|
|
end
|