Fix lxc-attach --namespaces support detection and cache the result

This commit is contained in:
Fabio Rehm 2013-10-03 13:26:04 -03:00
parent 8bcb8b5328
commit c194b3a24a
2 changed files with 16 additions and 6 deletions

View file

@ -82,12 +82,14 @@ module Vagrant
opts = cmd.pop opts = cmd.pop
namespaces = Array(opts[:namespaces]).map(&:upcase).join('|') namespaces = Array(opts[:namespaces]).map(&:upcase).join('|')
if run(:attach, '-h').include?('--namespaces') if namespaces
if supports_attach_with_namespaces?
extra = ['--namespaces', namespaces] if namespaces extra = ['--namespaces', namespaces] if namespaces
else else
raise LXC::Errors::NamespacesNotSupported raise LXC::Errors::NamespacesNotSupported
end end
end end
end
run :attach, '--name', @name, *((extra || []) + cmd) run :attach, '--name', @name, *((extra || []) + cmd)
end end
@ -114,6 +116,14 @@ module Vagrant
def run(command, *args) def run(command, *args)
@sudo_wrapper.run("lxc-#{command}", *args) @sudo_wrapper.run("lxc-#{command}", *args)
end end
def supports_attach_with_namespaces?
unless defined?(@supports_attach_with_namespaces)
@supports_attach_with_namespaces = run(:attach, '-h', '2>&1').include?('--namespaces')
end
return @supports_attach_with_namespaces
end
end end
end end
end end

View file

@ -146,13 +146,13 @@ describe Vagrant::LXC::Driver::CLI do
end end
it 'supports a "namespaces" parameter' do it 'supports a "namespaces" parameter' do
subject.stub(:run).with(:attach, '-h').and_return('--namespaces') subject.stub(:run).with(:attach, '-h', '2>&1').and_return('--namespaces')
subject.attach *(command + [{namespaces: ['network', 'mount']}]) subject.attach *(command + [{namespaces: ['network', 'mount']}])
subject.should have_received(:run).with(:attach, '--name', name, '--namespaces', 'NETWORK|MOUNT', '--', *command) subject.should have_received(:run).with(:attach, '--name', name, '--namespaces', 'NETWORK|MOUNT', '--', *command)
end end
it 'raises a NamespacesNotSupported error if not supported' do it 'raises a NamespacesNotSupported error if not supported' do
subject.stub(:run).with(:attach, '-h').and_return('not supported') subject.stub(:run).with(:attach, '-h', '2>&1').and_return('not supported')
expect { expect {
subject.attach *(command + [{namespaces: ['network', 'mount']}]) subject.attach *(command + [{namespaces: ['network', 'mount']}])
}.to raise_error(Vagrant::LXC::Errors::NamespacesNotSupported) }.to raise_error(Vagrant::LXC::Errors::NamespacesNotSupported)