mirror of
https://github.com/vagrant-libvirt/vagrant-libvirt.git
synced 2025-02-25 18:55:27 -06:00
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:
parent
4cff77e1c7
commit
d16bdcc1dc
@ -647,28 +647,26 @@ module VagrantPlugins
|
|||||||
uri << '+ssh://'
|
uri << '+ssh://'
|
||||||
uri << @username + '@' if @username
|
uri << @username + '@' if @username
|
||||||
|
|
||||||
uri << if @host
|
uri << ( @host ? @host : 'localhost' )
|
||||||
@host
|
|
||||||
else
|
|
||||||
'localhost'
|
|
||||||
end
|
|
||||||
else
|
else
|
||||||
uri << '://'
|
uri << '://'
|
||||||
uri << @host if @host
|
uri << @host if @host
|
||||||
end
|
end
|
||||||
|
|
||||||
uri << virt_path
|
uri << virt_path
|
||||||
uri << '?no_verify=1'
|
|
||||||
|
params = {'no_verify' => '1'}
|
||||||
|
|
||||||
if @id_ssh_key_file
|
if @id_ssh_key_file
|
||||||
# set ssh key for access to Libvirt host
|
# set ssh key for access to Libvirt host
|
||||||
uri << "\&keyfile="
|
|
||||||
# if no slash, prepend $HOME/.ssh/
|
# if no slash, prepend $HOME/.ssh/
|
||||||
@id_ssh_key_file.prepend("#{`echo ${HOME}`.chomp}/.ssh/") if @id_ssh_key_file !~ /\A\//
|
@id_ssh_key_file.prepend("#{ENV['HOME']}/.ssh/") if @id_ssh_key_file !~ /\A\//
|
||||||
uri << @id_ssh_key_file
|
params['keyfile'] = @id_ssh_key_file
|
||||||
end
|
end
|
||||||
# set path to Libvirt socket
|
# set path to Libvirt socket
|
||||||
uri << "\&socket=" + @socket if @socket
|
params['socket'] = @socket if @socket
|
||||||
|
|
||||||
|
uri << "?" + params.map{|pair| pair.join('=')}.join('&')
|
||||||
uri
|
uri
|
||||||
end
|
end
|
||||||
|
|
||||||
|
@ -6,6 +6,129 @@ require 'vagrant-libvirt/config'
|
|||||||
describe VagrantPlugins::ProviderLibvirt::Config do
|
describe VagrantPlugins::ProviderLibvirt::Config do
|
||||||
include_context 'unit'
|
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
|
def assert_invalid
|
||||||
errors = subject.validate(machine)
|
errors = subject.validate(machine)
|
||||||
raise "No errors: #{errors.inspect}" if errors.values.all?(&:empty?)
|
raise "No errors: #{errors.inspect}" if errors.values.all?(&:empty?)
|
||||||
|
Loading…
Reference in New Issue
Block a user