2020-10-23 14:31:29 +01:00
|
|
|
require 'spec_helper'
|
|
|
|
|
require 'support/sharedcontext'
|
|
|
|
|
require 'support/libvirt_context'
|
|
|
|
|
|
|
|
|
|
require 'vagrant-libvirt/errors'
|
|
|
|
|
require 'vagrant-libvirt/action/create_domain'
|
|
|
|
|
|
|
|
|
|
describe VagrantPlugins::ProviderLibvirt::Action::CreateDomain do
|
|
|
|
|
subject { described_class.new(app, env) }
|
|
|
|
|
|
|
|
|
|
include_context 'unit'
|
|
|
|
|
include_context 'libvirt'
|
|
|
|
|
|
|
|
|
|
let(:libvirt_client) { double('libvirt_client') }
|
|
|
|
|
let(:servers) { double('servers') }
|
|
|
|
|
let(:volumes) { double('volumes') }
|
|
|
|
|
|
|
|
|
|
let(:storage_pool_xml) { File.read(File.join(File.dirname(__FILE__), File.basename(__FILE__, '.rb'), test_file)) }
|
|
|
|
|
let(:libvirt_storage_pool) { double('storage_pool') }
|
|
|
|
|
|
|
|
|
|
describe '#call' do
|
|
|
|
|
before do
|
|
|
|
|
allow_any_instance_of(VagrantPlugins::ProviderLibvirt::Driver)
|
|
|
|
|
.to receive(:connection).and_return(connection)
|
|
|
|
|
allow(connection).to receive(:client).and_return(libvirt_client)
|
|
|
|
|
|
|
|
|
|
allow(connection).to receive(:servers).and_return(servers)
|
|
|
|
|
allow(connection).to receive(:volumes).and_return(volumes)
|
2020-12-05 15:49:25 +00:00
|
|
|
|
2020-12-05 15:24:42 +00:00
|
|
|
allow(logger).to receive(:info)
|
2020-12-05 15:49:25 +00:00
|
|
|
|
|
|
|
|
env[:domain_name] = "vagrant-test_default"
|
|
|
|
|
|
Allow to use many disks in vagrant box for libvirt provider
Adds support for a new multi disk box format and handling to upload the
multiple disks to the storage pool.
New format is:
{
'disks': [
{
'name': 'disk1.img',
'virtual_size': 10,
'format': 'qcow2'
},
{
'name': 'disk2.img',
'virtual_size': 15,
'format': 'qcow2'
},
{
'name': 'disk3.img',
}
],
'provider': 'libvirt',
'format': 'qcow2'
}
It is expected to remove format from being set at the top level when
using the new format, with the assuming that qcow2 should be the default
format, and other formats should be permitted to be specified as needed.
Includes tests for handling the box images and creation of domain
volumes. Additionally includes an integration test to ensure a box with
2 disks will work as expected.
Partially fixes: #602
2020-09-10 10:03:00 +02:00
|
|
|
env[:box_volumes] = []
|
|
|
|
|
env[:box_volumes].push({
|
|
|
|
|
:path=>"/test/box.img",
|
|
|
|
|
:name=>"test_vagrant_box_image_1.1.1_0.img",
|
|
|
|
|
:virtual_size=>5
|
|
|
|
|
})
|
2020-12-05 15:49:25 +00:00
|
|
|
# should be ignored for system session and used for user session
|
|
|
|
|
allow(Process).to receive(:uid).and_return(9999)
|
|
|
|
|
allow(Process).to receive(:gid).and_return(9999)
|
2020-10-23 14:31:29 +01:00
|
|
|
end
|
|
|
|
|
|
2020-12-05 15:49:25 +00:00
|
|
|
context 'connection => qemu:///system' do
|
|
|
|
|
context 'default pool' do
|
|
|
|
|
let(:test_file) { 'default_system_storage_pool.xml' }
|
2020-10-23 14:31:29 +01:00
|
|
|
|
2020-12-05 15:49:25 +00:00
|
|
|
it 'should execute correctly' do
|
|
|
|
|
expect(libvirt_client).to receive(:lookup_storage_pool_by_name).and_return(libvirt_storage_pool)
|
|
|
|
|
expect(libvirt_storage_pool).to receive(:xml_desc).and_return(storage_pool_xml)
|
|
|
|
|
expect(servers).to receive(:create).and_return(machine)
|
|
|
|
|
expect(volumes).to_not receive(:create) # additional disks only
|
2020-10-23 14:31:29 +01:00
|
|
|
|
2020-12-05 15:49:25 +00:00
|
|
|
expect(subject.call(env)).to be_nil
|
|
|
|
|
end
|
2020-10-23 14:31:29 +01:00
|
|
|
|
2020-12-05 15:49:25 +00:00
|
|
|
context 'additional disks' do
|
|
|
|
|
let(:vagrantfile) do
|
|
|
|
|
<<-EOF
|
|
|
|
|
Vagrant.configure('2') do |config|
|
|
|
|
|
config.vm.define :test
|
|
|
|
|
config.vm.provider :libvirt do |libvirt|
|
|
|
|
|
libvirt.storage :file, :size => '20G'
|
|
|
|
|
end
|
2020-10-23 14:31:29 +01:00
|
|
|
end
|
2020-12-05 15:49:25 +00:00
|
|
|
EOF
|
2020-10-23 14:31:29 +01:00
|
|
|
end
|
|
|
|
|
|
2020-12-05 15:49:25 +00:00
|
|
|
context 'volume create failed' do
|
|
|
|
|
it 'should raise an exception' do
|
|
|
|
|
expect(libvirt_client).to receive(:lookup_storage_pool_by_name).and_return(libvirt_storage_pool)
|
|
|
|
|
expect(libvirt_storage_pool).to receive(:xml_desc).and_return(storage_pool_xml)
|
|
|
|
|
expect(volumes).to receive(:create).and_raise(Libvirt::Error)
|
2020-10-23 14:31:29 +01:00
|
|
|
|
2020-12-05 15:49:25 +00:00
|
|
|
expect{ subject.call(env) }.to raise_error(VagrantPlugins::ProviderLibvirt::Errors::FogCreateDomainVolumeError)
|
|
|
|
|
end
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
context 'volume create succeeded' do
|
|
|
|
|
it 'should complete' do
|
|
|
|
|
expect(libvirt_client).to receive(:lookup_storage_pool_by_name).and_return(libvirt_storage_pool)
|
|
|
|
|
expect(libvirt_storage_pool).to receive(:xml_desc).and_return(storage_pool_xml)
|
|
|
|
|
expect(volumes).to receive(:create).with(
|
|
|
|
|
hash_including(
|
|
|
|
|
:path => "/var/lib/libvirt/images/vagrant-test_default-vdb.qcow2",
|
|
|
|
|
:owner => 0,
|
|
|
|
|
:group => 0,
|
|
|
|
|
:pool_name => "default",
|
|
|
|
|
)
|
|
|
|
|
)
|
|
|
|
|
expect(servers).to receive(:create).and_return(machine)
|
|
|
|
|
|
|
|
|
|
expect(subject.call(env)).to be_nil
|
|
|
|
|
end
|
2020-10-23 14:31:29 +01:00
|
|
|
end
|
|
|
|
|
end
|
2020-12-05 15:49:25 +00:00
|
|
|
end
|
2020-10-23 14:31:29 +01:00
|
|
|
|
2020-12-05 15:49:25 +00:00
|
|
|
context 'no default pool' do
|
|
|
|
|
it 'should raise an exception' do
|
|
|
|
|
expect(libvirt_client).to receive(:lookup_storage_pool_by_name).and_return(nil)
|
2020-10-23 14:31:29 +01:00
|
|
|
|
2020-12-05 15:49:25 +00:00
|
|
|
expect{ subject.call(env) }.to raise_error(VagrantPlugins::ProviderLibvirt::Errors::NoStoragePool)
|
2020-10-23 14:31:29 +01:00
|
|
|
end
|
|
|
|
|
end
|
|
|
|
|
end
|
|
|
|
|
|
2020-12-05 15:49:25 +00:00
|
|
|
context 'connection => qemu:///session' do
|
|
|
|
|
let(:vagrantfile) do
|
|
|
|
|
<<-EOF
|
|
|
|
|
Vagrant.configure('2') do |config|
|
|
|
|
|
config.vm.define :test
|
|
|
|
|
config.vm.provider :libvirt do |libvirt|
|
|
|
|
|
libvirt.qemu_use_session = true
|
|
|
|
|
end
|
|
|
|
|
end
|
|
|
|
|
EOF
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
context 'default pool' do
|
|
|
|
|
let(:test_file) { 'default_user_storage_pool.xml' }
|
|
|
|
|
|
|
|
|
|
it 'should execute correctly' do
|
|
|
|
|
expect(libvirt_client).to receive(:lookup_storage_pool_by_name).and_return(libvirt_storage_pool)
|
|
|
|
|
expect(libvirt_storage_pool).to receive(:xml_desc).and_return(storage_pool_xml)
|
|
|
|
|
expect(servers).to receive(:create).and_return(machine)
|
|
|
|
|
|
|
|
|
|
expect(subject.call(env)).to be_nil
|
|
|
|
|
end
|
2020-10-23 14:31:29 +01:00
|
|
|
|
2020-12-05 15:49:25 +00:00
|
|
|
context 'additional disks' do
|
|
|
|
|
let(:vagrantfile) do
|
|
|
|
|
<<-EOF
|
|
|
|
|
Vagrant.configure('2') do |config|
|
|
|
|
|
config.vm.define :test
|
|
|
|
|
config.vm.provider :libvirt do |libvirt|
|
|
|
|
|
libvirt.qemu_use_session = true
|
|
|
|
|
libvirt.storage :file, :size => '20G'
|
|
|
|
|
end
|
|
|
|
|
end
|
|
|
|
|
EOF
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
context 'volume create succeeded' do
|
|
|
|
|
it 'should complete' do
|
|
|
|
|
expect(libvirt_client).to receive(:lookup_storage_pool_by_name).and_return(libvirt_storage_pool)
|
|
|
|
|
expect(libvirt_storage_pool).to receive(:xml_desc).and_return(storage_pool_xml)
|
|
|
|
|
expect(volumes).to receive(:create).with(
|
|
|
|
|
hash_including(
|
|
|
|
|
:path => "/var/lib/libvirt/images/vagrant-test_default-vdb.qcow2",
|
|
|
|
|
:owner => 9999,
|
|
|
|
|
:group => 9999,
|
|
|
|
|
:pool_name => "default",
|
|
|
|
|
)
|
|
|
|
|
)
|
|
|
|
|
expect(servers).to receive(:create).and_return(machine)
|
|
|
|
|
|
|
|
|
|
expect(subject.call(env)).to be_nil
|
|
|
|
|
end
|
|
|
|
|
end
|
|
|
|
|
end
|
2020-10-23 14:31:29 +01:00
|
|
|
end
|
|
|
|
|
end
|
|
|
|
|
end
|
|
|
|
|
end
|