2021-06-30 13:27:03 +01:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
|
2022-11-08 16:28:32 +00:00
|
|
|
require_relative '../../spec_helper'
|
2016-11-24 17:54:15 +00:00
|
|
|
|
2016-12-06 23:20:29 +01:00
|
|
|
require 'vagrant-libvirt/config'
|
|
|
|
|
require 'vagrant-libvirt/util/erb_template'
|
2016-11-24 17:54:15 +00:00
|
|
|
|
2016-12-06 23:20:29 +01:00
|
|
|
describe 'templates/domain' do
|
|
|
|
|
include_context 'unit'
|
2016-11-24 17:54:15 +00:00
|
|
|
|
|
|
|
|
class DomainTemplateHelper < VagrantPlugins::ProviderLibvirt::Config
|
|
|
|
|
include VagrantPlugins::ProviderLibvirt::Util::ErbTemplate
|
2017-05-29 14:01:11 +01:00
|
|
|
|
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
|
|
|
attr_accessor :domain_volumes
|
|
|
|
|
|
|
|
|
|
def initialize
|
|
|
|
|
super
|
|
|
|
|
@domain_volumes = []
|
2022-08-16 18:44:11 +02:00
|
|
|
@sysinfo_blocks = {
|
|
|
|
|
'bios' => {:section => "BIOS", :xml => "bios"},
|
|
|
|
|
'system' => {:section => "System", :xml => "system"},
|
|
|
|
|
'base board' => {:section => "Base Board", :xml => "baseBoard"},
|
|
|
|
|
'chassis' => {:section => "Chassis", :xml => "chassis"},
|
|
|
|
|
'oem strings' => {:section => "OEM Strings", :xml => "oemStrings"},
|
|
|
|
|
}
|
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
|
|
|
end
|
|
|
|
|
|
2017-05-29 14:01:11 +01:00
|
|
|
def finalize!
|
|
|
|
|
super
|
2022-06-24 18:02:05 +01:00
|
|
|
|
|
|
|
|
disks.each do |disk|
|
|
|
|
|
disk[:absolute_path] = '/var/lib/libvirt/images/' + disk[:path]
|
|
|
|
|
end
|
2017-05-29 14:01:11 +01:00
|
|
|
end
|
2016-11-24 17:54:15 +00:00
|
|
|
end
|
|
|
|
|
|
2022-06-24 18:02:05 +01:00
|
|
|
def resolve
|
|
|
|
|
# resolving is now done during create domain, so need to recreate
|
|
|
|
|
# the same behaviour before calling the template until that
|
|
|
|
|
# is separated out from create domain.
|
|
|
|
|
resolver = ::VagrantPlugins::ProviderLibvirt::Util::DiskDeviceResolver.new(prefix=domain.disk_device[0..1])
|
|
|
|
|
resolver.resolve!(domain.domain_volumes.dup.each { |volume| volume[:device] = volume[:dev] })
|
|
|
|
|
resolver.resolve!(domain.disks)
|
|
|
|
|
end
|
|
|
|
|
|
2016-11-24 17:54:15 +00:00
|
|
|
let(:domain) { DomainTemplateHelper.new }
|
|
|
|
|
let(:xml_expected) { File.read(File.join(File.dirname(__FILE__), test_file)) }
|
|
|
|
|
|
2016-12-06 23:20:29 +01:00
|
|
|
context 'when only defaults used' do
|
2016-11-24 17:54:15 +00:00
|
|
|
let(:test_file) { 'domain_defaults.xml' }
|
2016-12-06 23:20:29 +01:00
|
|
|
it 'renders template' do
|
2016-11-24 17:54:15 +00:00
|
|
|
domain.finalize!
|
|
|
|
|
expect(domain.to_xml('domain')).to eq xml_expected
|
|
|
|
|
end
|
|
|
|
|
end
|
|
|
|
|
|
2016-12-06 23:20:29 +01:00
|
|
|
context 'when all settings enabled' do
|
2016-11-24 17:54:15 +00:00
|
|
|
before do
|
2020-08-12 21:26:36 +01:00
|
|
|
domain.title = 'title'
|
|
|
|
|
domain.description = 'description'
|
2016-11-24 17:54:15 +00:00
|
|
|
domain.instance_variable_set('@domain_type', 'kvm')
|
|
|
|
|
domain.cpu_mode = 'custom'
|
2016-12-06 23:20:29 +01:00
|
|
|
domain.cpu_feature(name: 'AAA', policy: 'required')
|
2018-03-21 19:00:28 +01:00
|
|
|
domain.hyperv_feature(name: 'BBB', state: 'on')
|
2020-12-16 20:19:24 +01:00
|
|
|
domain.clock_offset = 'variable'
|
|
|
|
|
domain.clock_timer(name: 't1')
|
|
|
|
|
domain.clock_timer(name: 't2', track: 'b', tickpolicy: 'c', frequency: 'd', mode: 'e', present: 'yes')
|
2019-09-04 20:57:15 +02:00
|
|
|
domain.hyperv_feature(name: 'spinlocks', state: 'on', retries: '4096')
|
2018-01-12 19:47:49 +07:00
|
|
|
domain.cputopology(sockets: '1', cores: '3', threads: '2')
|
2022-11-10 12:22:37 +00:00
|
|
|
domain.memtune(type: 'hard_limit', value: '250000')
|
|
|
|
|
domain.memtune(type: 'soft_limit', value: '200000')
|
2022-10-08 05:14:08 -05:00
|
|
|
domain.cpuaffinitiy(0 => '0')
|
2016-11-24 17:54:15 +00:00
|
|
|
domain.machine_type = 'pc-compatible'
|
|
|
|
|
domain.machine_arch = 'x86_64'
|
|
|
|
|
domain.loader = '/efi/loader'
|
|
|
|
|
domain.boot('network')
|
|
|
|
|
domain.boot('cdrom')
|
|
|
|
|
domain.boot('hd')
|
|
|
|
|
domain.emulator_path = '/usr/bin/kvm-spice'
|
2020-12-17 22:03:47 +11:00
|
|
|
domain.instance_variable_set('@domain_volume_cache', 'deprecated')
|
2016-11-24 17:54:15 +00:00
|
|
|
domain.disk_bus = 'ide'
|
2017-01-30 17:06:31 +01:00
|
|
|
domain.disk_device = 'vda'
|
2020-12-17 22:03:47 +11:00
|
|
|
domain.disk_driver(:cache => 'unsafe', :io => 'threads', :copy_on_read => 'on', :discard => 'unmap', :detect_zeroes => 'on')
|
2016-12-06 23:20:29 +01:00
|
|
|
domain.storage(:file, path: 'test-disk1.qcow2')
|
2020-12-17 22:03:47 +11:00
|
|
|
domain.storage(:file, path: 'test-disk2.qcow2', io: 'threads', copy_on_read: 'on', discard: 'unmap', detect_zeroes: 'on')
|
2022-10-08 13:59:06 +03:00
|
|
|
domain.storage(:file, device: :floppy)
|
2016-12-06 23:20:29 +01:00
|
|
|
domain.storage(:file, device: :cdrom)
|
|
|
|
|
domain.storage(:file, device: :cdrom)
|
|
|
|
|
domain.channel(type: 'unix',
|
|
|
|
|
target_name: 'org.qemu.guest_agent.0',
|
|
|
|
|
target_type: 'virtio')
|
2017-11-28 18:23:47 +01:00
|
|
|
domain.channel(type: 'spicevmc',
|
|
|
|
|
target_name: 'com.redhat.spice.0',
|
|
|
|
|
target_type: 'virtio')
|
2016-12-06 23:20:29 +01:00
|
|
|
domain.channel(type: 'unix',
|
|
|
|
|
target_type: 'guestfwd',
|
|
|
|
|
target_address: '192.0.2.42',
|
|
|
|
|
target_port: '4242',
|
|
|
|
|
source_path: '/tmp/foo')
|
|
|
|
|
domain.random(model: 'random')
|
2021-10-19 16:37:46 +02:00
|
|
|
domain.serial(:type => 'file', :source => {:path => '/var/log/vm_consoles/machine.log'})
|
2016-12-06 23:20:29 +01:00
|
|
|
domain.pci(bus: '0x06', slot: '0x12', function: '0x5')
|
2020-05-10 05:40:29 -05:00
|
|
|
domain.pci(domain: '0x0001', bus: '0x03', slot: '0x00', function: '0x0')
|
2022-05-04 10:11:50 +01:00
|
|
|
domain.pci(domain: '0x0002', bus: '0x04', slot: '0x00', function: '0x0', guest_domain: '0x0000', guest_bus: '0x01', guest_slot: '0x01', guest_function: '0x0')
|
2018-03-18 19:25:01 +11:00
|
|
|
domain.usb_controller(model: 'nec-xhci', ports: '4')
|
2016-12-06 23:20:29 +01:00
|
|
|
domain.usb(bus: '1', device: '2', vendor: '0x1234', product: '0xabcd')
|
|
|
|
|
domain.redirdev(type: 'tcp', host: 'localhost', port: '4000')
|
|
|
|
|
domain.redirfilter(class: '0x0b', vendor: '0x08e6',
|
|
|
|
|
product: '0x3437', version: '2.00', allow: 'yes')
|
2017-01-10 00:10:56 +01:00
|
|
|
domain.watchdog(model: 'i6300esb', action: 'reset')
|
2017-01-12 09:31:12 +01:00
|
|
|
domain.smartcard(mode: 'passthrough')
|
2016-11-24 17:54:15 +00:00
|
|
|
domain.tpm_path = '/dev/tpm0'
|
2017-01-10 20:07:15 +03:00
|
|
|
|
2022-08-16 18:44:11 +02:00
|
|
|
domain.sysinfo = {
|
|
|
|
|
'system' => {
|
|
|
|
|
'serial' => 'AAAAAAAA',
|
|
|
|
|
},
|
|
|
|
|
'oem strings' => [
|
|
|
|
|
'AAAAAAAA',
|
|
|
|
|
],
|
|
|
|
|
}
|
|
|
|
|
|
2017-01-10 20:07:15 +03:00
|
|
|
domain.qemuargs(value: '-device')
|
|
|
|
|
domain.qemuargs(value: 'dummy-device')
|
2020-05-10 09:37:31 -04:00
|
|
|
|
|
|
|
|
domain.qemuenv(QEMU_AUDIO_DRV: 'pa')
|
|
|
|
|
domain.qemuenv(QEMU_AUDIO_TIMER_PERIOD: '150')
|
|
|
|
|
domain.qemuenv(QEMU_PA_SAMPLES: '1024')
|
|
|
|
|
domain.qemuenv(QEMU_PA_SERVER: '/run/user/1000/pulse/native')
|
|
|
|
|
|
2020-05-10 05:24:06 -05:00
|
|
|
domain.shares = '1024'
|
2020-04-29 13:08:01 -05:00
|
|
|
domain.cpuset = '1-4,^3,6'
|
2020-05-10 04:52:46 -05:00
|
|
|
domain.nodeset = '1-4,^3,6'
|
2021-10-26 15:11:30 +06:00
|
|
|
|
|
|
|
|
domain.video_accel3d = true
|
2016-11-24 17:54:15 +00:00
|
|
|
end
|
|
|
|
|
let(:test_file) { 'domain_all_settings.xml' }
|
2016-12-06 23:20:29 +01:00
|
|
|
it 'renders template' do
|
2016-11-24 17:54:15 +00:00
|
|
|
domain.finalize!
|
2022-06-24 18:02:05 +01:00
|
|
|
|
|
|
|
|
domain.domain_volumes.push({
|
|
|
|
|
:cache => 'unsafe',
|
|
|
|
|
:bus => domain.disk_bus,
|
|
|
|
|
:absolute_path => '/var/lib/libvirt/images/test.qcow2'
|
|
|
|
|
})
|
|
|
|
|
domain.domain_volumes.push({
|
|
|
|
|
:cache => 'unsafe',
|
|
|
|
|
:bus => domain.disk_bus,
|
|
|
|
|
:absolute_path => '/var/lib/libvirt/images/test2.qcow2'
|
|
|
|
|
})
|
|
|
|
|
resolve
|
2021-11-22 10:02:18 +00:00
|
|
|
|
2016-11-24 17:54:15 +00:00
|
|
|
expect(domain.to_xml('domain')).to eq xml_expected
|
|
|
|
|
end
|
|
|
|
|
end
|
2018-02-14 13:13:41 -05:00
|
|
|
|
2021-12-08 19:48:31 +00:00
|
|
|
context 'when cpu mode is set' do
|
|
|
|
|
context 'to host-passthrough' do
|
|
|
|
|
before do
|
|
|
|
|
domain.cpu_mode = 'host-passthrough'
|
|
|
|
|
domain.cpu_model = 'SandyBridge'
|
|
|
|
|
domain.cputopology :sockets => '1', :cores => '2', :threads => '1'
|
|
|
|
|
domain.nested = true
|
|
|
|
|
end
|
|
|
|
|
let(:test_file) { 'domain_cpu_mode_passthrough.xml' }
|
|
|
|
|
it 'should allow features and topology and ignore model' do
|
|
|
|
|
domain.finalize!
|
|
|
|
|
expect(domain.to_xml('domain')).to eq xml_expected
|
|
|
|
|
end
|
2018-02-14 13:13:41 -05:00
|
|
|
end
|
2021-12-08 19:48:31 +00:00
|
|
|
|
|
|
|
|
context 'to custom and model is set' do
|
|
|
|
|
before do
|
|
|
|
|
domain.cpu_mode = 'custom'
|
|
|
|
|
domain.cpu_model = 'SandyBridge'
|
|
|
|
|
end
|
|
|
|
|
let(:test_file) { 'domain_custom_cpu_model.xml' }
|
|
|
|
|
it 'renders template' do
|
|
|
|
|
domain.finalize!
|
|
|
|
|
expect(domain.to_xml('domain')).to eq xml_expected
|
|
|
|
|
end
|
2018-02-14 13:13:41 -05:00
|
|
|
end
|
|
|
|
|
end
|
2020-12-15 06:43:46 -06:00
|
|
|
|
|
|
|
|
context 'when tpm 2.0 device is specified' do
|
|
|
|
|
before do
|
|
|
|
|
domain.tpm_version = '2.0'
|
|
|
|
|
domain.tpm_type = 'emulator'
|
|
|
|
|
domain.tpm_model = 'tpm-crb'
|
|
|
|
|
end
|
|
|
|
|
let(:test_file) { 'tpm/version_2.0.xml' }
|
|
|
|
|
it 'renders template' do
|
|
|
|
|
domain.finalize!
|
|
|
|
|
expect(domain.to_xml('domain')).to eq xml_expected
|
|
|
|
|
end
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
context 'when tpm 1.2 device is implicitly used' do
|
|
|
|
|
before do
|
|
|
|
|
domain.tpm_path = '/dev/tpm0'
|
|
|
|
|
end
|
|
|
|
|
let(:test_file) { 'tpm/version_1.2.xml' }
|
|
|
|
|
it 'renders template' do
|
|
|
|
|
domain.finalize!
|
|
|
|
|
expect(domain.to_xml('domain')).to eq xml_expected
|
|
|
|
|
end
|
|
|
|
|
end
|
2020-12-15 12:40:05 -05:00
|
|
|
|
|
|
|
|
context 'memballoon' do
|
|
|
|
|
context 'default' do
|
|
|
|
|
it 'renders without specifying the xml tag' do
|
|
|
|
|
domain.finalize!
|
|
|
|
|
|
|
|
|
|
expect(domain.to_xml('domain')).to_not match(/memballoon/)
|
|
|
|
|
end
|
|
|
|
|
end
|
|
|
|
|
|
2022-01-17 15:45:24 +01:00
|
|
|
context 'memballoon enabled' do
|
2020-12-15 12:40:05 -05:00
|
|
|
before do
|
|
|
|
|
domain.memballoon_enabled = true
|
|
|
|
|
end
|
|
|
|
|
|
2022-01-17 15:45:24 +01:00
|
|
|
it 'renders with memballoon element' do
|
2020-12-15 12:40:05 -05:00
|
|
|
domain.finalize!
|
|
|
|
|
|
|
|
|
|
expect(domain.to_xml('domain')).to match(/<memballoon model='virtio'>/)
|
|
|
|
|
expect(domain.to_xml('domain')).to match(/<address type='pci' domain='0x0000' bus='0x00' slot='0x0f' function='0x0'\/>/)
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
context 'all settings specified' do
|
|
|
|
|
before do
|
|
|
|
|
domain.memballoon_model = "virtio-non-transitional"
|
|
|
|
|
domain.memballoon_pci_bus = "0x01"
|
|
|
|
|
domain.memballoon_pci_slot = "0x05"
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
it 'renders with specified values' do
|
|
|
|
|
domain.finalize!
|
|
|
|
|
|
|
|
|
|
expect(domain.to_xml('domain')).to match(/<memballoon model='virtio-non-transitional'>/)
|
|
|
|
|
expect(domain.to_xml('domain')).to match(/<address type='pci' domain='0x0000' bus='0x01' slot='0x05' function='0x0'\/>/)
|
|
|
|
|
end
|
|
|
|
|
end
|
|
|
|
|
end
|
|
|
|
|
|
2022-01-17 15:45:24 +01:00
|
|
|
context 'memballoon disabled' do
|
2020-12-15 12:40:05 -05:00
|
|
|
before do
|
|
|
|
|
domain.memballoon_enabled = false
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
it 'renders the memballoon element with model none' do
|
|
|
|
|
domain.finalize!
|
|
|
|
|
|
|
|
|
|
expect(domain.to_xml('domain')).to match(/<memballoon model='none'\/>/)
|
|
|
|
|
end
|
|
|
|
|
end
|
|
|
|
|
end
|
2015-06-02 10:00:38 +01:00
|
|
|
|
|
|
|
|
context 'scsi controller' do
|
|
|
|
|
context 'when disk device suggests scsi' do
|
|
|
|
|
let(:test_file) { 'domain_scsi_device_storage.xml' }
|
|
|
|
|
|
|
|
|
|
before do
|
|
|
|
|
domain.disk_device = 'sda'
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
it 'renders scsi controller in template' do
|
|
|
|
|
domain.finalize!
|
|
|
|
|
domain.domain_volumes.push({
|
|
|
|
|
:cache => 'unsafe',
|
|
|
|
|
:bus => domain.disk_bus,
|
|
|
|
|
:absolute_path => '/var/lib/libvirt/images/test.qcow2'
|
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
resolve
|
|
|
|
|
expect(domain.to_xml('domain')).to eq xml_expected
|
|
|
|
|
end
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
context 'when disk bus is scsi' do
|
|
|
|
|
let(:test_file) { 'domain_scsi_bus_storage.xml' }
|
|
|
|
|
|
|
|
|
|
before do
|
|
|
|
|
domain.disk_bus = 'scsi'
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
it 'renders scsi controller in template based on bus' do
|
|
|
|
|
domain.finalize!
|
|
|
|
|
domain.domain_volumes.push({
|
|
|
|
|
:dev => 'vda',
|
|
|
|
|
:cache => 'unsafe',
|
|
|
|
|
:bus => domain.disk_bus,
|
|
|
|
|
:absolute_path => '/var/lib/libvirt/images/test.qcow2'
|
|
|
|
|
})
|
|
|
|
|
resolve
|
|
|
|
|
expect(domain.to_xml('domain')).to eq xml_expected
|
|
|
|
|
end
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
context 'when enough scsi disks are added' do
|
|
|
|
|
let(:test_file) { 'domain_scsi_multiple_controllers_storage.xml' }
|
|
|
|
|
|
|
|
|
|
before do
|
|
|
|
|
domain.disk_bus = 'scsi'
|
|
|
|
|
domain.disk_controller_model = 'virtio-scsi'
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
it 'should render with multiple scsi controllers' do
|
|
|
|
|
domain.finalize!
|
|
|
|
|
for idx in 1..15 do
|
|
|
|
|
domain.domain_volumes.push({
|
|
|
|
|
:cache => 'unsafe',
|
|
|
|
|
:bus => domain.disk_bus,
|
|
|
|
|
:absolute_path => "/var/lib/libvirt/images/test-#{idx}.img"
|
|
|
|
|
})
|
|
|
|
|
end
|
|
|
|
|
resolve
|
|
|
|
|
expect(domain.to_xml('domain')).to eq xml_expected
|
|
|
|
|
end
|
|
|
|
|
end
|
|
|
|
|
end
|
2016-11-24 17:54:15 +00:00
|
|
|
end
|