Add @uri config tests and minor refactor (#1139)

Test settings modifying the `@uri` and `@qemu_use_session` variables to make
it easier to update and perform an initial minor refactor to reduce some
of the code currently in use to set `@uri`.
This commit is contained in:
Darragh Bailey 2020-08-16 16:27:27 +01:00 committed by GitHub
parent 4cff77e1c7
commit d16bdcc1dc
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 131 additions and 10 deletions

View File

@ -647,28 +647,26 @@ module VagrantPlugins
uri << '+ssh://'
uri << @username + '@' if @username
uri << if @host
@host
else
'localhost'
end
uri << ( @host ? @host : 'localhost' )
else
uri << '://'
uri << @host if @host
end
uri << virt_path
uri << '?no_verify=1'
params = {'no_verify' => '1'}
if @id_ssh_key_file
# set ssh key for access to Libvirt host
uri << "\&keyfile="
# if no slash, prepend $HOME/.ssh/
@id_ssh_key_file.prepend("#{`echo ${HOME}`.chomp}/.ssh/") if @id_ssh_key_file !~ /\A\//
uri << @id_ssh_key_file
@id_ssh_key_file.prepend("#{ENV['HOME']}/.ssh/") if @id_ssh_key_file !~ /\A\//
params['keyfile'] = @id_ssh_key_file
end
# set path to Libvirt socket
uri << "\&socket=" + @socket if @socket
params['socket'] = @socket if @socket
uri << "?" + params.map{|pair| pair.join('=')}.join('&')
uri
end

View File

@ -6,6 +6,129 @@ require 'vagrant-libvirt/config'
describe VagrantPlugins::ProviderLibvirt::Config do
include_context 'unit'
let(:fake_env) { Hash.new }
describe '#finalize!' do
it 'is valid with defaults' do
subject.finalize!
end
context '@uri' do
before(:example) do
stub_const("ENV", fake_env)
end
context 'when @driver is defined' do
defaults = {'id_ssh_key_file' => nil}
[
[
{'driver' => 'kvm'},
'qemu:///system?no_verify=1',
false,
],
[
{'driver' => 'qemu'},
'qemu:///system?no_verify=1',
false,
],
[
{'driver' => 'qemu', 'qemu_use_session' => true},
'qemu:///session?no_verify=1',
true,
],
[
{'driver' => 'openvz'},
'openvz:///system?no_verify=1',
false,
],
[
{'driver' => 'vbox'},
'vbox:///session?no_verify=1',
false,
],
].each do |inputs, output_uri, output_session|
it "should detect #{inputs}" do
inputs.merge(defaults).each do |k, v|
subject.instance_variable_set("@#{k}", v)
end
subject.finalize!
expect(subject.uri).to eq(output_uri)
expect(subject.qemu_use_session).to eq(output_session)
end
end
it "should raise exception for unrecognized" do
subject.driver = "bad-driver"
expect { subject.finalize! }.to raise_error("Require specify driver bad-driver")
end
end
context 'when @connect_via_ssh defined' do
defaults = {'driver' => 'qemu', 'id_ssh_key_file' => nil}
[
[
{'connect_via_ssh' => true},
'qemu+ssh://localhost/system?no_verify=1',
],
[
{'connect_via_ssh' => true, 'username' => 'my_user'},
'qemu+ssh://my_user@localhost/system?no_verify=1',
],
[
{'connect_via_ssh' => true, 'host' => 'remote_server'},
'qemu+ssh://remote_server/system?no_verify=1',
],
].each do |inputs, output_uri|
it "should detect #{inputs}" do
inputs.merge(defaults).each do |k, v|
subject.instance_variable_set("@#{k}", v)
end
subject.finalize!
expect(subject.uri).to eq(output_uri)
end
end
end
context 'when @id_ssh_key_file defined' do
defaults = {'driver' => 'qemu'}
[
[
{},
'qemu:///system?no_verify=1&keyfile=/home/user/.ssh/id_rsa',
],
[
{'id_ssh_key_file' => '/path/to/keyfile'},
'qemu:///system?no_verify=1&keyfile=/path/to/keyfile',
],
].each do |inputs, output_uri|
it "should detect #{inputs}" do
inputs.merge(defaults).each do |k, v|
subject.instance_variable_set("@#{k}", v)
end
fake_env['HOME'] = '/home/user'
subject.finalize!
expect(subject.uri).to eq(output_uri)
end
end
end
context 'when @socket defined' do
it "should detect @socket set" do
subject.socket = '/var/run/libvirt/libvirt-sock'
subject.id_ssh_key_file = false
subject.finalize!
expect(subject.uri).to eq('qemu:///system?no_verify=1&socket=/var/run/libvirt/libvirt-sock')
end
end
end
end
def assert_invalid
errors = subject.validate(machine)
raise "No errors: #{errors.inspect}" if errors.values.all?(&:empty?)