2013-03-01 23:45:13 +00:00
|
|
|
require 'unit_helper'
|
|
|
|
|
2013-03-10 03:45:27 +00:00
|
|
|
require "vendored_vagrant"
|
2013-03-01 23:45:13 +00:00
|
|
|
require 'vagrant-lxc/container'
|
|
|
|
|
|
|
|
describe Vagrant::LXC::Container do
|
2013-03-02 03:55:45 +00:00
|
|
|
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' }
|
2013-03-08 03:54:15 +00:00
|
|
|
let(:rootfs_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-08 03:54:15 +00:00
|
|
|
subject.create 'template-name' => template_name, 'rootfs-cache-path' => rootfs_cache, 'template-opts' => { '--foo' => 'bar'}
|
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-07 05:09:09 +00:00
|
|
|
'--auth-key', public_key_path,
|
2013-03-08 03:54:15 +00:00
|
|
|
'--cache', rootfs_cache,
|
2013-03-07 05:09:09 +00:00
|
|
|
'--foo', 'bar'
|
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
|
|
|
|
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
|
|
|
|
2013-03-10 04:54:33 +00:00
|
|
|
describe 'assigned ip' do
|
|
|
|
# This ip is set on the sample-arp-output based on mac address from sample-config
|
|
|
|
let(:ip) { "10.0.3.30" }
|
|
|
|
let(:conf_file_contents) { File.read('spec/fixtures/sample-config') }
|
|
|
|
let(:name) { 'random-container-name' }
|
2013-03-02 16:55:05 +00:00
|
|
|
|
|
|
|
before do
|
2013-03-10 04:54:33 +00:00
|
|
|
@arp_output = File.read('spec/fixtures/sample-arp-output')
|
2013-03-02 16:55:05 +00:00
|
|
|
subject.stub(:raw) {
|
2013-03-10 04:54:33 +00:00
|
|
|
mock(stdout: "#{@arp_output}\n", exit_code: 0)
|
2013-03-02 16:55:05 +00:00
|
|
|
}
|
2013-03-10 04:54:33 +00:00
|
|
|
File.stub(read: conf_file_contents)
|
2013-03-02 16:55:05 +00:00
|
|
|
end
|
|
|
|
|
2013-03-10 04:54:33 +00:00
|
|
|
it 'gets parsed from `arp` based on lxc mac address' do
|
|
|
|
subject.assigned_ip.should == ip
|
|
|
|
subject.should have_received(:raw).with('arp', '-n')
|
2013-03-02 16:55:05 +00:00
|
|
|
end
|
|
|
|
end
|
2013-03-01 23:45:13 +00:00
|
|
|
end
|