lxc-start: check

This commit is contained in:
Fabio Rehm 2013-03-01 22:47:02 -03:00
parent 8b8bfe739b
commit e9a5385c02
2 changed files with 53 additions and 23 deletions

View file

@ -22,13 +22,12 @@ module Vagrant
def create def create
# FIXME: Ruby 1.8 users dont have SecureRandom # FIXME: Ruby 1.8 users dont have SecureRandom
machine_id = SecureRandom.hex(6) machine_id = SecureRandom.hex(6)
log, status = lxc(:create, {'--template' => 'ubuntu-cloud', '--name' => machine_id}, {'-S' => '/home/vagrant/.ssh/id_rsa.pub'}) log, status = lxc :create, '--template', 'ubuntu-cloud', '--name', machine_id, '--', '-S', '/home/vagrant/.ssh/id_rsa.pub'
machine_id machine_id
end end
def start def start
puts 'TODO: Start container' lxc :start, '-d', '--name', @machine.id
update!(:running)
end end
def halt def halt
@ -40,19 +39,8 @@ module Vagrant
File.delete(state_file_path) if state_file_path File.delete(state_file_path) if state_file_path
end end
def state def lxc(command, *args)
# TODO: Grab the real machine state here execute('sudo', "lxc-#{command}", *args)
read_state_from_file
end
private
def lxc(command, params, extra = {})
params = params.map { |opt, val| "#{opt}='#{val}'" }
params << '--' if extra.any?
# Handles extra options passed to templates when using lxc-create
params << extra.map { |opt, val| "#{opt} #{val}" }
execute('sudo', "lxc-#{command}", *params.flatten)
end end
def update!(state) def update!(state)

View file

@ -3,17 +3,38 @@ require 'unit_helper'
require 'vagrant-lxc/container' require 'vagrant-lxc/container'
describe Vagrant::LXC::Container do describe Vagrant::LXC::Container do
# Default subject and machine for specs
let(:machine) { fire_double('Vagrant::Machine') } let(:machine) { fire_double('Vagrant::Machine') }
subject { described_class.new(machine) } subject { described_class.new(machine) }
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 for execution' do
args[0].should == 'sudo'
end
it 'uses the first argument as lxc command suffix' do
args[1].should == 'lxc-command'
end
it 'sends remaining arguments for execution' do
args[2].should == '--state'
args[3].should == 'RUNNING'
end
end
describe 'create' do describe 'create' do
let(:last_command) { @last_command } let(:last_command) { @last_command }
let(:new_machine_id) { 'random-machine-id' } let(:new_machine_id) { 'random-machine-id' }
before do before do
Vagrant::Util::Subprocess.stub(:execute) do |*cmds| subject.stub(:lxc) do |*cmds|
cmds.pop if cmds.last.is_a?(Hash)
@last_command = cmds.join(' ') @last_command = cmds.join(' ')
mock(exit_code: 0, stdout: '') mock(exit_code: 0, stdout: '')
end end
@ -21,10 +42,31 @@ describe Vagrant::LXC::Container do
subject.create subject.create
end end
it 'runs lxc-create with the right arguments' do it 'calls lxc-create with the right arguments' do
last_command.should include "--name='#{new_machine_id}'" last_command.should =~ /^create/
last_command.should include "--template='ubuntu-cloud'" last_command.should include "--name #{new_machine_id}"
last_command.should =~ /\-\- \-S (\w|\/|\.)+\/id_rsa\.pub/ last_command.should include "--template ubuntu-cloud"
last_command.should =~ /\-\- \-S (\w|\/|\.)+\/id_rsa\.pub$/
end
end
describe 'start' 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.start
end
it 'calls lxc-start with the right arguments' do
last_command.should =~ /^start/
last_command.should include "--name #{machine_id}"
last_command.should include '-d'
end end
end end
end end