Skip to content
This repository has been archived by the owner on Nov 24, 2022. It is now read-only.

Commit

Permalink
driver: little cleanup after LXC 1.0+ requirement bump
Browse files Browse the repository at this point in the history
The conditional `lxc-version` and `lxc-config` mechanisms aren't needed
anymore. They were for pre-1.0 LXC versions.
  • Loading branch information
Virgil Dupras committed Jan 14, 2018
1 parent aa77765 commit 2b08ae1
Show file tree
Hide file tree
Showing 4 changed files with 8 additions and 55 deletions.
4 changes: 1 addition & 3 deletions lib/vagrant-lxc/driver.rb
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,6 @@ class Driver
class ContainerNotFound < StandardError; end

# Default root folder where container configs are stored
DEFAULT_CONTAINERS_PATH = '/var/lib/lxc'

attr_reader :container_name,
:customizations

Expand All @@ -36,7 +34,7 @@ def validate!

# Root folder where container configs are stored
def containers_path
@containers_path ||= @cli.support_config_command? ? @cli.config('lxc.lxcpath') : DEFAULT_CONTAINERS_PATH
@containers_path ||= @cli.config('lxc.lxcpath')
end

def all_containers
Expand Down
18 changes: 2 additions & 16 deletions lib/vagrant-lxc/driver/cli.rb
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ def list

def version
return @version if @version
@version = support_version_command? ? run(:version) : run(:create, '--version')
@version = run(:create, '--version')
if @version =~ /(lxc version:\s+|)(.+)\s*$/
@version = $2.downcase
else
Expand All @@ -39,11 +39,7 @@ def version
end

def config(param)
if support_config_command?
run(:config, param).gsub("\n", '')
else
raise Errors::CommandNotSupported, name: 'config', available_version: '> 1.x.x', version: version
end
run(:config, param).gsub("\n", '')
end

def state
Expand Down Expand Up @@ -155,16 +151,6 @@ def supports_attach?
return @supports_attach
end

def support_config_command?
version[0].to_i >= 1
end

def support_version_command?
@sudo_wrapper.run('which', 'lxc-version').strip.chomp != ''
rescue Vagrant::LXC::Errors::ExecuteError
return false
end

private

def run(command, *args)
Expand Down
26 changes: 2 additions & 24 deletions spec/unit/driver/cli_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -30,16 +30,7 @@

describe 'version' do
before do
allow(subject).to receive(:support_version_command?).and_return(true)
allow(subject).to receive(:run).with(:version).and_return(lxc_version_out)
end

describe 'lxc version before 1.x.x' do
let(:lxc_version_out) { "lxc version: 0.x.y-rc1\n" }

it 'parses the version from the output' do
expect(subject.version).to eq('0.x.y-rc1')
end
allow(subject).to receive(:run).with(:create, '--version').and_return(lxc_version_out)
end

describe 'lxc version after 1.x.x' do
Expand All @@ -53,24 +44,11 @@

describe 'config' do
before do
allow(subject).to receive(:support_version_command?).and_return(support_version_command?)
allow(subject).to receive(:run).with(:config, 'lxc.lxcpath').and_return(lxc_config_out)
allow(subject).to receive(:run).with(:version).and_return(lxc_version_out)
allow(subject).to receive(:run).with(:create, '--version').and_return(lxc_version_out)
end

describe 'lxc version before 1.x.x' do
let(:support_version_command?) { true }
let(:lxc_config_out) { "/var/lib/lxc\n" }
let(:lxc_version_out) { "lxc version: 0.x.y-rc1\n" }

it 'not supported' do
expect{subject.config('lxc.lxcpath')}.to raise_error(Vagrant::LXC::Errors::CommandNotSupported)
end
end

describe 'lxc version before after 1.x.x'do
let(:support_version_command?) { false }
describe 'lxc version after 1.x.x'do
let(:lxc_config_out) { "/var/lib/lxc\n" }
let(:lxc_version_out) { "1.0.0\n" }

Expand Down
15 changes: 3 additions & 12 deletions spec/unit/driver_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@
describe 'start' do
let(:customizations) { [['a', '1'], ['b', '2']] }
let(:internal_customization) { ['internal', 'customization'] }
let(:cli) { double(Vagrant::LXC::Driver::CLI, start: true, support_config_command?: false) }
let(:cli) { double(Vagrant::LXC::Driver::CLI, start: true) }
let(:sudo) { double(Vagrant::LXC::SudoWrapper) }

subject { described_class.new('name', sudo, cli) }
Expand All @@ -99,6 +99,7 @@
and_return('# CONFIGURATION')
sudo.should_receive(:run).twice.with('cp', '-f', %r{/(run|tmp)/.*}, '/var/lib/lxc/name/config')
sudo.should_receive(:run).twice.with('chown', 'root:root', '/var/lib/lxc/name/config')
expect(cli).to receive(:config).with("lxc.lxcpath").and_return("/var/lib/lxc")

subject.customizations << internal_customization
subject.start(customizations)
Expand Down Expand Up @@ -152,21 +153,11 @@
end

describe 'containers_path' do
let(:cli) { double(Vagrant::LXC::Driver::CLI, config: cli_config_value, support_config_command?: cli_support_config_command_value) }
let(:cli) { double(Vagrant::LXC::Driver::CLI, config: cli_config_value) }

subject { described_class.new('name', nil, cli) }

describe 'lxc version before 1.x.x' do
let(:cli_support_config_command_value) { false }
let(:cli_config_value) { '/var/lib/lxc' }

it 'delegates to cli' do
expect(subject.containers_path).to eq(cli_config_value)
end
end

describe 'lxc version after 1.x.x' do
let(:cli_support_config_command_value) { true }
let(:cli_config_value) { '/etc/lxc' }

it 'delegates to cli' do
Expand Down

0 comments on commit 2b08ae1

Please sign in to comment.