From e9a5385c02f71e5aa3905c66e97fd0b36c5f3882 Mon Sep 17 00:00:00 2001 From: Fabio Rehm Date: Fri, 1 Mar 2013 22:47:02 -0300 Subject: [PATCH] lxc-start: check --- lib/vagrant-lxc/container.rb | 20 +++---------- spec/unit/container_spec.rb | 56 +++++++++++++++++++++++++++++++----- 2 files changed, 53 insertions(+), 23 deletions(-) diff --git a/lib/vagrant-lxc/container.rb b/lib/vagrant-lxc/container.rb index 71a8190..be9ff21 100644 --- a/lib/vagrant-lxc/container.rb +++ b/lib/vagrant-lxc/container.rb @@ -22,13 +22,12 @@ module Vagrant def create # FIXME: Ruby 1.8 users dont have SecureRandom 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 end def start - puts 'TODO: Start container' - update!(:running) + lxc :start, '-d', '--name', @machine.id end def halt @@ -40,19 +39,8 @@ module Vagrant File.delete(state_file_path) if state_file_path end - def state - # TODO: Grab the real machine state here - 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) + def lxc(command, *args) + execute('sudo', "lxc-#{command}", *args) end def update!(state) diff --git a/spec/unit/container_spec.rb b/spec/unit/container_spec.rb index 63fafed..1be33ec 100644 --- a/spec/unit/container_spec.rb +++ b/spec/unit/container_spec.rb @@ -3,17 +3,38 @@ require 'unit_helper' require 'vagrant-lxc/container' describe Vagrant::LXC::Container do + # Default subject and machine for specs let(:machine) { fire_double('Vagrant::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 let(:last_command) { @last_command } let(:new_machine_id) { 'random-machine-id' } before do - Vagrant::Util::Subprocess.stub(:execute) do |*cmds| - cmds.pop if cmds.last.is_a?(Hash) + subject.stub(:lxc) do |*cmds| @last_command = cmds.join(' ') mock(exit_code: 0, stdout: '') end @@ -21,10 +42,31 @@ describe Vagrant::LXC::Container do subject.create end - it 'runs lxc-create with the right arguments' do - last_command.should include "--name='#{new_machine_id}'" - last_command.should include "--template='ubuntu-cloud'" - last_command.should =~ /\-\- \-S (\w|\/|\.)+\/id_rsa\.pub/ + 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(: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