2021-06-30 13:27:03 +01:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
|
2020-10-23 14:31:29 +01:00
|
|
|
require 'spec_helper'
|
|
|
|
|
require 'support/sharedcontext'
|
|
|
|
|
require 'support/libvirt_context'
|
|
|
|
|
|
|
|
|
|
require 'vagrant-libvirt/errors'
|
2021-09-28 13:17:22 +01:00
|
|
|
require 'vagrant-libvirt/util/byte_number'
|
2020-10-23 14:31:29 +01:00
|
|
|
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') }
|
2021-09-28 13:17:22 +01:00
|
|
|
let(:domain_volume) { double('domain_volume') }
|
2020-10-23 14:31:29 +01:00
|
|
|
|
2021-05-28 14:46:57 +01:00
|
|
|
let(:domain_xml) { File.read(File.join(File.dirname(__FILE__), File.basename(__FILE__, '.rb'), domain_xml_file)) }
|
|
|
|
|
let(:storage_pool_xml) { File.read(File.join(File.dirname(__FILE__), File.basename(__FILE__, '.rb'), storage_pool_xml_file)) }
|
2020-10-23 14:31:29 +01:00
|
|
|
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)
|
2021-09-28 13:17:22 +01:00
|
|
|
allow(volumes).to receive(:all).and_return([domain_volume])
|
|
|
|
|
allow(domain_volume).to receive(:pool_name).and_return('default')
|
|
|
|
|
allow(domain_volume).to receive(:[]).with('name').and_return('vagrant-test_default.img')
|
|
|
|
|
allow(domain_volume).to receive(:path).and_return('/var/lib/libvirt/images/vagrant-test_default.img')
|
|
|
|
|
allow(machine).to receive_message_chain("box.name") { 'vagrant-libvirt/test' }
|
2020-12-05 15:49:25 +00:00
|
|
|
|
2020-12-05 15:24:42 +00:00
|
|
|
allow(logger).to receive(:info)
|
2021-09-11 19:29:45 +01:00
|
|
|
allow(logger).to receive(:debug)
|
2021-09-28 13:17:22 +01:00
|
|
|
allow(ui).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",
|
2021-09-28 13:17:22 +01:00
|
|
|
:virtual_size=> ByteNumber.new(5),
|
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
|
|
|
})
|
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
|
2021-09-28 13:17:22 +01:00
|
|
|
let(:domain_xml_file) { 'default_domain.xml' }
|
2020-10-23 14:31:29 +01:00
|
|
|
|
2021-09-28 13:17:22 +01:00
|
|
|
context 'default pool' do
|
2020-12-05 15:49:25 +00:00
|
|
|
it 'should execute correctly' do
|
2021-05-28 14:46:57 +01:00
|
|
|
expect(servers).to receive(:create).with(xml: domain_xml).and_return(machine)
|
2020-12-05 15:49:25 +00:00
|
|
|
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
|
|
|
|
2021-09-28 13:17:22 +01:00
|
|
|
context 'with no box' do
|
|
|
|
|
let(:storage_pool_xml_file) { 'default_system_storage_pool.xml' }
|
2020-12-05 15:49:25 +00:00
|
|
|
let(:vagrantfile) do
|
|
|
|
|
<<-EOF
|
|
|
|
|
Vagrant.configure('2') do |config|
|
|
|
|
|
config.vm.define :test
|
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
|
|
|
|
|
|
2021-09-28 13:17:22 +01:00
|
|
|
it 'should query for the storage pool path' 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
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
context 'additional disks' do
|
|
|
|
|
let(:vagrantfile_providerconfig) do
|
|
|
|
|
<<-EOF
|
|
|
|
|
libvirt.storage :file, :size => '20G'
|
|
|
|
|
EOF
|
|
|
|
|
end
|
|
|
|
|
|
2020-12-05 15:49:25 +00:00
|
|
|
context 'volume create failed' do
|
|
|
|
|
it 'should raise an exception' do
|
|
|
|
|
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
|
2021-05-28 14:46:57 +01:00
|
|
|
let(:domain_xml_file) { 'additional_disks_domain.xml' }
|
|
|
|
|
|
2020-12-05 15:49:25 +00:00
|
|
|
it 'should complete' do
|
|
|
|
|
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",
|
|
|
|
|
)
|
|
|
|
|
)
|
2021-05-28 14:46:57 +01:00
|
|
|
expect(servers).to receive(:create).with(xml: domain_xml).and_return(machine)
|
2020-12-05 15:49:25 +00:00
|
|
|
|
|
|
|
|
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
|
2021-09-28 13:17:22 +01:00
|
|
|
let(:vagrantfile) do
|
|
|
|
|
<<-EOF
|
|
|
|
|
Vagrant.configure('2') do |config|
|
|
|
|
|
config.vm.define :test
|
|
|
|
|
end
|
|
|
|
|
EOF
|
|
|
|
|
end
|
|
|
|
|
|
2020-12-05 15:49:25 +00:00
|
|
|
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
|
2021-09-28 13:17:22 +01:00
|
|
|
let(:vagrantfile_providerconfig) do
|
2020-12-05 15:49:25 +00:00
|
|
|
<<-EOF
|
2021-09-28 13:17:22 +01:00
|
|
|
libvirt.qemu_use_session = true
|
2020-12-05 15:49:25 +00:00
|
|
|
EOF
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
context 'default pool' do
|
|
|
|
|
it 'should execute correctly' do
|
|
|
|
|
expect(servers).to receive(:create).and_return(machine)
|
|
|
|
|
|
|
|
|
|
expect(subject.call(env)).to be_nil
|
|
|
|
|
end
|
2020-10-23 14:31:29 +01:00
|
|
|
|
2021-09-28 13:17:22 +01:00
|
|
|
context 'with no box' do
|
|
|
|
|
let(:storage_pool_xml_file) { 'default_user_storage_pool.xml' }
|
2020-12-05 15:49:25 +00:00
|
|
|
let(:vagrantfile) do
|
|
|
|
|
<<-EOF
|
|
|
|
|
Vagrant.configure('2') do |config|
|
|
|
|
|
config.vm.define :test
|
|
|
|
|
config.vm.provider :libvirt do |libvirt|
|
2021-09-28 13:17:22 +01:00
|
|
|
#{vagrantfile_providerconfig}
|
2020-12-05 15:49:25 +00:00
|
|
|
end
|
|
|
|
|
end
|
|
|
|
|
EOF
|
|
|
|
|
end
|
|
|
|
|
|
2021-09-28 13:17:22 +01:00
|
|
|
it 'should query for the storage pool path' 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
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
context 'additional disks' do
|
|
|
|
|
let(:vagrantfile_providerconfig) do
|
|
|
|
|
<<-EOF
|
|
|
|
|
libvirt.qemu_use_session = true
|
|
|
|
|
libvirt.storage :file, :size => '20G'
|
|
|
|
|
EOF
|
|
|
|
|
end
|
|
|
|
|
|
2020-12-05 15:49:25 +00:00
|
|
|
context 'volume create succeeded' do
|
|
|
|
|
it 'should complete' do
|
|
|
|
|
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
|