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 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 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 02:27:08 +00:00
|
|
|
subject.stub(:lxc)
|
2013-03-02 03:55:45 +00:00
|
|
|
SecureRandom.stub(hex: name)
|
2013-03-01 23:45:13 +00:00
|
|
|
subject.create
|
|
|
|
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', 'ubuntu-cloud',
|
2013-03-02 03:55:45 +00:00
|
|
|
'--name', name,
|
2013-03-02 02:27:08 +00:00
|
|
|
'--',
|
|
|
|
'-S', public_key_path
|
|
|
|
)
|
2013-03-02 01:47:02 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
describe 'start' do
|
2013-03-02 03:55:45 +00:00
|
|
|
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-02 01:47:02 +00:00
|
|
|
subject.start
|
|
|
|
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-02 03:55:45 +00:00
|
|
|
'--name', name
|
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
|
|
|
|
|
|
|
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-01 23:45:13 +00:00
|
|
|
end
|