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

144 lines
4.3 KiB
Ruby
Raw Normal View History

2013-03-01 23:45:13 +00:00
require 'unit_helper'
require 'vagrant'
2013-03-01 23:45:13 +00:00
require 'vagrant-lxc/container'
describe Vagrant::LXC::Container do
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', cli) }
let(:valid_container) { described_class.new('valid', cli) }
let(:new_container) { described_class.new(nil) }
let(:cli) { fire_double('Vagrant::LXC::Container::CLI', list: ['valid']) }
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 02:07:15 +00:00
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 }
let(:cli) { fire_double('Vagrant::LXC::Container::CLI', :create => true, :name= => true) }
subject { described_class.new(name, cli) }
2013-03-01 23:45:13 +00:00
before do
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
it 'creates container with the right arguments' do
cli.should have_received(:create).with(
template_name,
'--auth-key' => public_key_path,
'--cache' => rootfs_cache,
'--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' }
let(:cli) { fire_double('Vagrant::LXC::Container::CLI', destroy: true) }
2013-03-02 04:34:47 +00:00
subject { described_class.new(name, cli) }
2013-03-02 04:34:47 +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-03-12 18:29:18 +00:00
let(:config) { mock(:config, start_opts: ['a=1', 'b=2']) }
let(:name) { 'container-name' }
let(:cli) { fire_double('Vagrant::LXC::Container::CLI', start: true) }
subject { described_class.new(name, cli) }
2013-03-02 01:47:02 +00:00
before do
cli.stub(:transition_to).and_yield(cli)
2013-03-02 01:47:02 +00:00
end
it 'starts container with configured lxc settings' do
2013-03-12 18:29:18 +00:00
cli.should_receive(:start).with(['a=1', 'b=2'], nil)
subject.start(config)
end
it 'expects a transition to running state to take place' do
cli.should_receive(:transition_to).with(:running)
subject.start(config)
2013-03-01 23:45:13 +00:00
end
end
2013-03-02 04:20:27 +00:00
describe 'halt' do
let(:name) { 'container-name' }
let(:cli) { fire_double('Vagrant::LXC::Container::CLI', shutdown: true) }
subject { described_class.new(name, cli) }
2013-03-02 04:20:27 +00:00
before do
cli.stub(:transition_to).and_yield(cli)
2013-03-02 04:20:27 +00:00
end
it 'delegates to cli shutdown' do
cli.should_receive(:shutdown)
subject.halt
2013-03-02 04:20:27 +00:00
end
it 'expects a transition to running state to take place' do
cli.should_receive(:transition_to).with(:stopped)
subject.halt
2013-03-02 04:20:27 +00:00
end
end
describe 'state' do
let(:name) { 'random-container-name' }
let(:cli_state) { :something }
let(:cli) { fire_double('Vagrant::LXC::Container::CLI', state: cli_state) }
subject { described_class.new(name, cli) }
it 'delegates to cli' do
subject.state.should == cli_state
end
end
describe 'assigned ip' do
# This ip is set on the sample-ifconfig-output fixture
let(:ip) { "10.0.3.109" }
let(:ifconfig_output) { File.read('spec/fixtures/sample-ifconfig-output') }
let(:name) { 'random-container-name' }
let(:cli) { fire_double('Vagrant::LXC::Container::CLI', :attach => ifconfig_output) }
2013-03-11 00:59:45 +00:00
subject { described_class.new(name, cli) }
context 'when ip for eth0 gets returned from lxc-attach call' do
it 'gets parsed from ifconfig output' do
2013-03-11 00:59:45 +00:00
subject.assigned_ip.should == ip
cli.should have_received(:attach).with('/sbin/ifconfig', '-v', 'eth0', namespaces: 'network')
2013-03-11 00:59:45 +00:00
end
end
end
2013-03-01 23:45:13 +00:00
end