vagrant-lxc-ng/spec/unit/container_spec.rb

199 lines
4.8 KiB
Ruby
Raw Normal View History

2013-03-01 23:45:13 +00:00
require 'unit_helper'
require 'vagrant-lxc/container'
describe Vagrant::LXC::Container do
# Default subject and container name for specs
let(:name) { nil }
subject { described_class.new(name) }
2013-03-01 23:45:13 +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
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
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,
'--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
let(:name) { 'random-container-name' }
let(:template_name) { 'template-name' }
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
subject.stub(lxc: true)
SecureRandom.stub(hex: name)
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,
'--template', template_name,
'--name', name,
2013-03-02 02:27:08 +00:00
'--',
2013-03-07 05:09:09 +00:00
'--auth-key', public_key_path,
'--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
let(:config) { mock(:config, start_opts: ['a=1', 'b=2']) }
let(:name) { 'container-name' }
2013-03-02 01:47:02 +00:00
before do
subject.stub(lxc: true, wait_until: true)
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',
'--name', name,
'-s', 'a=1',
'-s', 'b=2'
2013-03-02 02:27:08 +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 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
describe 'state' do
let(:name) { 'random-container-name' }
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,
'--name', name
)
end
it 'maps the output of lxc-info status out to a symbol' do
subject.state.should == :stopped
end
end
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