Avoid setting cpu element on unsupported architectures (#1633)

The CPU element to manage the mode, model, features (including nested),
is only available on some architectures. To allow this plugin to
generate XML valid for other architectures such as RISC-V, the CPU
element needs to be optional and only enabled when the architecture
specified supports it.

Include checks in the validation section to help prevent the setting of
an unsupported architecture with any of the CPU features that require
the CPU element to be available.

Fixes: #1538
This commit is contained in:
Darragh Bailey
2022-10-30 14:29:21 +00:00
committed by GitHub
parent 199b3a07e6
commit ddb6dbd076
5 changed files with 240 additions and 83 deletions

View File

@@ -161,6 +161,71 @@ describe VagrantPlugins::ProviderLibvirt::Action::StartDomain do
end
end
context 'cpu' do
let(:test_file) { 'existing.xml' }
let(:updated_domain_xml) {
new_xml = domain_xml.dup
new_xml.gsub!(
/<cpu .*\/>/,
<<-EOF
<cpu check='partial' mode='custom'>
<model fallback='allow'>Haswell</model>
<feature name='vmx' policy='optional'/>
<feature name='svm' policy='optional'/>
</cpu>
EOF
)
new_xml
}
let(:vagrantfile_providerconfig) {
<<-EOF
libvirt.cpu_mode = 'custom'
libvirt.cpu_model = 'Haswell'
libvirt.nested = true
EOF
}
it 'should set cpu related settings when changed' do
expect(ui).to_not receive(:warn)
expect(connection).to receive(:define_domain).and_return(libvirt_domain)
expect(libvirt_domain).to receive(:xml_desc).and_return(domain_xml, updated_domain_xml)
expect(libvirt_domain).to receive(:autostart=)
expect(domain).to receive(:start)
expect(subject.call(env)).to be_nil
end
let(:domain_xml_no_cpu) {
new_xml = domain_xml.dup
new_xml.gsub!(/<cpu .*\/>/, '')
new_xml
}
let(:updated_domain_xml_new_cpu) {
new_xml = domain_xml.dup
new_xml.gsub!(
/<cpu .*\/>/,
<<-EOF
<cpu mode='custom'>
<model fallback='allow'>Haswell</model>
<feature name='vmx' policy='optional'/>
<feature name='svm' policy='optional'/>
</cpu>
EOF
)
new_xml
}
it 'should add cpu settings if not already present' do
expect(ui).to_not receive(:warn)
expect(connection).to receive(:define_domain).and_return(libvirt_domain)
expect(libvirt_domain).to receive(:xml_desc).and_return(domain_xml_no_cpu, updated_domain_xml_new_cpu)
expect(libvirt_domain).to receive(:autostart=)
expect(domain).to receive(:start)
expect(subject.call(env)).to be_nil
end
end
context 'graphics' do
context 'autoport not disabled' do
let(:test_file) { 'existing.xml' }

View File

@@ -676,6 +676,33 @@ describe VagrantPlugins::ProviderLibvirt::Config do
expect(subject.channels).to match([a_hash_including({:target_name => 'com.redhat.spice.0'})])
end
end
context '@machine_arch and @cpu_*' do
context 'should set @cpu_mode based on @machine_arch support' do
# it's possible when this is unset that the host arch should be read
it 'should default to host-model if machine_arch unset' do
subject.finalize!
expect(subject.cpu_mode).to eq('host-model')
end
it 'should default to host-model if supported' do
subject.machine_arch = 'aarch64'
subject.finalize!
expect(subject.cpu_mode).to eq('host-model')
end
it 'should default to nil if unsupported' do
subject.machine_arch = 'ppc'
subject.finalize!
expect(subject.cpu_mode).to be_nil
end
end
end
end
def assert_invalid
@@ -804,6 +831,32 @@ describe VagrantPlugins::ProviderLibvirt::Config do
end
end
context '@machine_arch and @cpu_*' do
it 'should be valid if cpu_* settings and no arch set' do
subject.cpu_mode = 'host-passthrough'
subject.nested = true
assert_valid
end
it 'should be valid if cpu_* settings and supported' do
subject.machine_arch = 'aarch64'
subject.cpu_mode = 'host-passthrough'
subject.nested = true
assert_valid
end
it 'should flag settings invalid if unsupported' do
subject.machine_arch = 'ppc'
subject.cpu_mode = 'host-passthrough'
subject.nested = true
errors = assert_invalid
expect(errors).to include(match(/Architecture ppc does not support .* cpu_mode, nested/))
end
end
context 'with cpu_mode and cpu_model defined' do
it 'should discard model if mode is passthrough' do
subject.cpu_mode = 'host-passthrough'