2013-03-10 22:15:31 +00:00
|
|
|
require 'unit_helper'
|
|
|
|
|
2013-04-05 05:17:19 +00:00
|
|
|
require 'vagrant-lxc/driver/cli'
|
2013-03-10 22:15:31 +00:00
|
|
|
|
2013-04-05 05:17:19 +00:00
|
|
|
describe Vagrant::LXC::Driver::CLI do
|
2013-03-10 22:15:31 +00:00
|
|
|
describe 'list' do
|
|
|
|
let(:lxc_ls_out) { "dup-container\na-container dup-container" }
|
2013-03-10 23:31:32 +00:00
|
|
|
let(:result) { @result }
|
2013-03-10 22:15:31 +00:00
|
|
|
|
|
|
|
before do
|
2013-03-10 23:31:32 +00:00
|
|
|
subject.stub(:run).with(:ls).and_return(lxc_ls_out)
|
|
|
|
@result = subject.list
|
2013-03-10 22:15:31 +00:00
|
|
|
end
|
|
|
|
|
2013-03-10 23:31:32 +00:00
|
|
|
it 'grabs previously created containers from lxc-ls output' do
|
|
|
|
result.should be_an Enumerable
|
|
|
|
result.should include 'a-container'
|
|
|
|
result.should include 'dup-container'
|
2013-03-10 22:15:31 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
it 'removes duplicates from lxc-ls output' do
|
|
|
|
result.uniq.should == result
|
|
|
|
end
|
|
|
|
end
|
2013-03-10 23:31:32 +00:00
|
|
|
|
2013-04-09 01:27:29 +00:00
|
|
|
describe 'version' do
|
|
|
|
let(:lxc_version_out) { "lxc version: 0.x.y-rc1\n" }
|
|
|
|
|
|
|
|
before do
|
|
|
|
subject.stub(:run).with(:version).and_return(lxc_version_out)
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'parses the version from the output' do
|
|
|
|
subject.version.should == '0.x.y-rc1'
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2013-03-10 23:31:32 +00:00
|
|
|
describe 'create' do
|
|
|
|
let(:template) { 'quantal-64' }
|
|
|
|
let(:name) { 'quantal-container' }
|
|
|
|
let(:template_args) { { '--extra-param' => 'param', '--other' => 'value' } }
|
|
|
|
|
|
|
|
subject { described_class.new(name) }
|
|
|
|
|
|
|
|
before do
|
2013-03-29 15:24:37 +00:00
|
|
|
subject.stub(:run) { |*args| @run_args = args }
|
2013-04-05 05:28:25 +00:00
|
|
|
subject.create(template, template_args)
|
2013-03-10 23:31:32 +00:00
|
|
|
end
|
|
|
|
|
2013-03-29 15:24:37 +00:00
|
|
|
it 'issues a lxc-create with provided template, container name and hash of arguments' do
|
|
|
|
subject.should have_received(:run).with(
|
|
|
|
:create,
|
|
|
|
'--template', template,
|
|
|
|
'--name', name,
|
|
|
|
'--',
|
|
|
|
'--extra-param', 'param',
|
|
|
|
'--other', 'value'
|
|
|
|
)
|
|
|
|
end
|
2013-03-10 23:31:32 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
describe 'destroy' do
|
|
|
|
let(:name) { 'a-container-for-destruction' }
|
|
|
|
|
|
|
|
subject { described_class.new(name) }
|
|
|
|
|
|
|
|
before do
|
|
|
|
subject.stub(:run)
|
|
|
|
subject.destroy
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'issues a lxc-destroy with container name' do
|
|
|
|
subject.should have_received(:run).with(:destroy, '--name', name)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
describe 'start' do
|
|
|
|
let(:name) { 'a-container' }
|
|
|
|
subject { described_class.new(name) }
|
|
|
|
|
|
|
|
before do
|
|
|
|
subject.stub(:run)
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'starts container on the background' do
|
|
|
|
subject.start
|
|
|
|
subject.should have_received(:run).with(
|
|
|
|
:start,
|
|
|
|
'-d',
|
|
|
|
'--name', name
|
|
|
|
)
|
|
|
|
end
|
|
|
|
|
2013-04-06 01:08:02 +00:00
|
|
|
it 'uses provided array to override container configs' do
|
|
|
|
subject.start([['config', 'value'], ['other', 'value']])
|
2013-03-10 23:31:32 +00:00
|
|
|
subject.should have_received(:run).with(:start, '-d', '--name', name,
|
|
|
|
'-s', 'lxc.config=value',
|
|
|
|
'-s', 'lxc.other=value'
|
|
|
|
)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
describe 'shutdown' do
|
|
|
|
let(:name) { 'a-running-container' }
|
|
|
|
subject { described_class.new(name) }
|
|
|
|
|
|
|
|
before do
|
|
|
|
subject.stub(:run)
|
|
|
|
subject.shutdown
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'issues a lxc-shutdown with provided container name' do
|
|
|
|
subject.should have_received(:run).with(:shutdown, '--name', name)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
describe 'state' do
|
|
|
|
let(:name) { 'a-container' }
|
|
|
|
subject { described_class.new(name) }
|
|
|
|
|
|
|
|
before do
|
|
|
|
subject.stub(:run).and_return("state: STOPPED\npid: 2")
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'calls lxc-info with the right arguments' do
|
|
|
|
subject.state
|
|
|
|
subject.should have_received(:run).with(:info, '--name', name)
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'maps the output of lxc-info status out to a symbol' do
|
|
|
|
subject.state.should == :stopped
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2013-03-19 04:25:27 +00:00
|
|
|
describe 'attach' do
|
|
|
|
let(:name) { 'a-running-container' }
|
|
|
|
let(:command) { ['ls', 'cat /tmp/file'] }
|
|
|
|
let(:command_output) { 'folders list' }
|
|
|
|
subject { described_class.new(name) }
|
|
|
|
|
|
|
|
before do
|
|
|
|
subject.stub(run: command_output)
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'calls lxc-attach with specified command' do
|
|
|
|
subject.attach(*command)
|
|
|
|
subject.should have_received(:run).with(:attach, '--name', name, '--', *command)
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'supports a "namespaces" parameter' do
|
|
|
|
subject.attach *(command + [{namespaces: ['network', 'mount']}])
|
|
|
|
subject.should have_received(:run).with(:attach, '--name', name, '--namespaces', 'NETWORK|MOUNT', '--', *command)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2013-03-10 23:31:32 +00:00
|
|
|
describe 'transition block' do
|
|
|
|
let(:name) { 'a-running-container' }
|
|
|
|
subject { described_class.new(name) }
|
|
|
|
|
2013-05-07 14:07:35 +00:00
|
|
|
before do
|
|
|
|
subject.stub(run: true, sleep: true, state: :stopped)
|
|
|
|
end
|
2013-03-10 23:31:32 +00:00
|
|
|
|
2013-05-07 14:07:35 +00:00
|
|
|
it 'yields a cli object' do
|
2013-03-10 23:31:32 +00:00
|
|
|
subject.stub(:shutdown)
|
|
|
|
subject.transition_to(:stopped) { |c| c.shutdown }
|
|
|
|
subject.should have_received(:shutdown)
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'throws an exception if block is not provided' do
|
|
|
|
expect {
|
|
|
|
subject.transition_to(:running)
|
|
|
|
}.to raise_error(described_class::TransitionBlockNotProvided)
|
|
|
|
end
|
|
|
|
|
2013-05-07 14:07:35 +00:00
|
|
|
pending 'waits for the expected container state'
|
2013-03-10 23:31:32 +00:00
|
|
|
end
|
2013-03-10 22:15:31 +00:00
|
|
|
end
|