From b49b1991b9668853f39e0ac9dff4d8cbce696a18 Mon Sep 17 00:00:00 2001 From: Darragh Bailey Date: Sat, 11 Sep 2021 22:00:33 +0100 Subject: [PATCH] Improve handling of cpu model and initrd settings (#1333) Avoid triggering a reconfig of the domain when the cpu model and the initrd haven't been set via the config. --- lib/vagrant-libvirt/action/start_domain.rb | 22 ++++--- spec/unit/action/start_domain_spec.rb | 40 +++++------- .../action/start_domain_spec/existing.xml | 62 +++++++++++++++++++ 3 files changed, 90 insertions(+), 34 deletions(-) create mode 100644 spec/unit/action/start_domain_spec/existing.xml diff --git a/lib/vagrant-libvirt/action/start_domain.rb b/lib/vagrant-libvirt/action/start_domain.rb index 2cfa91c..38d8022 100644 --- a/lib/vagrant-libvirt/action/start_domain.rb +++ b/lib/vagrant-libvirt/action/start_domain.rb @@ -101,11 +101,13 @@ module VagrantPlugins if config.cpu_mode != 'host-passthrough' cpu_model = REXML::XPath.first(xml_descr, '/domain/cpu/model') if cpu_model.nil? - @logger.debug "cpu_model updated from not set to '#{config.cpu_model}'" - descr_changed = true - cpu_model = REXML::Element.new('model', REXML::XPath.first(xml_descr, '/domain/cpu')) - cpu_model.attributes['fallback'] = 'allow' - cpu_model.text = config.cpu_model + if config.cpu_model.strip != '' + @logger.debug "cpu_model updated from not set to '#{config.cpu_model}'" + descr_changed = true + cpu_model = REXML::Element.new('model', REXML::XPath.first(xml_descr, '/domain/cpu')) + cpu_model.attributes['fallback'] = 'allow' + cpu_model.text = config.cpu_model + end else if (cpu_model.text or '').strip != config.cpu_model.strip @logger.debug "cpu_model text updated from #{cpu_model.text} to '#{config.cpu_model}'" @@ -322,10 +324,12 @@ module VagrantPlugins if config.initrd initrd = REXML::XPath.first(xml_descr, '/domain/os/initrd') if initrd.nil? - @logger.debug "initrd updated from not set to '#{config.initrd}'" - descr_changed = true - initrd = REXML::Element.new('initrd', REXML::XPath.first(xml_descr, '/domain/os')) - initrd.text = config.initrd + if config.initrd.strip != '' + @logger.debug "initrd updated from not set to '#{config.initrd}'" + descr_changed = true + initrd = REXML::Element.new('initrd', REXML::XPath.first(xml_descr, '/domain/os')) + initrd.text = config.initrd + end else if (initrd.text or '').strip != config.initrd @logger.debug "initrd updated from '#{initrd.text}' to '#{config.initrd}'" diff --git a/spec/unit/action/start_domain_spec.rb b/spec/unit/action/start_domain_spec.rb index 0913841..6dafaa3 100644 --- a/spec/unit/action/start_domain_spec.rb +++ b/spec/unit/action/start_domain_spec.rb @@ -21,6 +21,8 @@ describe VagrantPlugins::ProviderLibvirt::Action::StartDomain do let(:updated_domain_xml) { File.read(File.join(File.dirname(__FILE__), File.basename(__FILE__, '.rb'), updated_test_file)) } describe '#call' do + let(:test_file) { 'default.xml' } + before do allow_any_instance_of(VagrantPlugins::ProviderLibvirt::Driver) .to receive(:connection).and_return(connection) @@ -33,17 +35,23 @@ describe VagrantPlugins::ProviderLibvirt::Action::StartDomain do allow(logger).to receive(:debug) expect(logger).to receive(:info) expect(ui).to_not receive(:error) + + allow(libvirt_domain).to receive(:xml_desc).and_return(domain_xml) + + allow(libvirt_domain).to receive(:max_memory).and_return(512*1024) + allow(libvirt_domain).to receive(:num_vcpus).and_return(1) end - context 'default config' do - let(:test_file) { 'default.xml' } + it 'should execute without changing' do + allow(libvirt_domain).to receive(:undefine) + expect(libvirt_domain).to receive(:autostart=) + expect(domain).to receive(:start) - before do - allow(libvirt_domain).to receive(:xml_desc).and_return(domain_xml) + expect(subject.call(env)).to be_nil + end - allow(libvirt_domain).to receive(:max_memory).and_return(512*1024) - allow(libvirt_domain).to receive(:num_vcpus).and_return(1) - end + context 'when previously running default config' do + let(:test_file) { 'existing.xml' } it 'should execute without changing' do allow(libvirt_domain).to receive(:undefine) @@ -55,15 +63,6 @@ describe VagrantPlugins::ProviderLibvirt::Action::StartDomain do end context 'tpm' do - let(:test_file) { 'default.xml' } - - before do - allow(libvirt_domain).to receive(:xml_desc).and_return(domain_xml) - - allow(libvirt_domain).to receive(:max_memory).and_return(512*1024) - allow(libvirt_domain).to receive(:num_vcpus).and_return(1) - end - context 'passthrough tpm added' do let(:updated_test_file) { 'default_added_tpm_path.xml' } let(:vagrantfile_providerconfig) do @@ -170,13 +169,6 @@ describe VagrantPlugins::ProviderLibvirt::Action::StartDomain do context 'clock_timers' do let(:test_file) { 'clock_timer_rtc.xml' } - before do - allow(libvirt_domain).to receive(:xml_desc).and_return(domain_xml) - - allow(libvirt_domain).to receive(:max_memory).and_return(512*1024) - allow(libvirt_domain).to receive(:num_vcpus).and_return(1) - end - context 'timers unchanged' do let(:vagrantfile_providerconfig) do <<-EOF @@ -214,8 +206,6 @@ describe VagrantPlugins::ProviderLibvirt::Action::StartDomain do end context 'timers removed' do - let(:updated_test_file) { 'default.xml' } - it 'should modify the domain' do expect(libvirt_domain).to receive(:undefine) expect(logger).to receive(:debug).with('clock timers config changed') diff --git a/spec/unit/action/start_domain_spec/existing.xml b/spec/unit/action/start_domain_spec/existing.xml new file mode 100644 index 0000000..185f13e --- /dev/null +++ b/spec/unit/action/start_domain_spec/existing.xml @@ -0,0 +1,62 @@ + + vagrant-libvirt_default + 881a931b-0110-4d10-81aa-47a1a19f5726 + Source: /home/test/vagrant-libvirt/Vagrantfile + 2097152 + 2097152 + 2 + + hvm + + + + + + + + + + destroy + restart + destroy + + /usr/bin/qemu-system-x86_64 + + + + +
+ + +
+ + + + + + +
+ + + + + + + + + + + + + + +