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

93 lines
2.4 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
2013-03-02 01:47:02 +00:00
# Default subject and machine for specs
2013-03-01 23:45:13 +00:00
let(:machine) { fire_double('Vagrant::Machine') }
subject { described_class.new(machine) }
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(:last_command) { @last_command }
let(:machine_id) { 'random-machine-id' }
let(:machine) { fire_double('Vagrant::Machine', id: machine_id) }
before do
subject.stub(:lxc) do |*cmds|
@last_command = cmds.join(' ')
mock(exit_code: 0, stdout: '')
end
subject.wait_until :running
end
it 'runs lxc-wait with the machine id' do
last_command.should include "--name #{machine_id}"
end
it 'runs lxc-wait with upcased state' do
last_command.should include "--state RUNNING"
end
end
describe 'creation' do
2013-03-01 23:45:13 +00:00
let(:last_command) { @last_command }
let(:new_machine_id) { 'random-machine-id' }
before do
2013-03-02 01:47:02 +00:00
subject.stub(:lxc) do |*cmds|
2013-03-01 23:45:13 +00:00
@last_command = cmds.join(' ')
mock(exit_code: 0, stdout: '')
end
SecureRandom.stub(hex: new_machine_id)
subject.create
end
2013-03-02 01:47:02 +00:00
it 'calls lxc-create with the right arguments' do
last_command.should =~ /^create/
last_command.should include "--name #{new_machine_id}"
last_command.should include "--template ubuntu-cloud"
last_command.should =~ /\-\- \-S (\w|\/|\.)+\/id_rsa\.pub$/
end
end
describe 'start' do
let(:machine_id) { 'random-machine-id' }
let(:machine) { fire_double('Vagrant::Machine', id: machine_id) }
2013-03-02 01:47:02 +00:00
before do
subject.stub(lxc: true, wait_until: true)
2013-03-02 01:47:02 +00:00
subject.start
end
it 'calls lxc-start with the right arguments' do
subject.should have_received(:lxc).with(:start, '-d', '--name', machine.id)
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
end