Handle autoport when port explicit set (#1693)

Better handle setting the autoport value when the port is explicitly set
to ensure that the XML sent to update the VM is correct and will be the
XML that is reflected in the defined machine.

By prioritizing checking if the port is provided, graphics_autoport =
"yes" is ignored.

Fixes: #1687
This commit is contained in:
Darragh Bailey
2022-12-09 07:48:08 +00:00
committed by GitHub
parent 1733e1cc19
commit 7a8306745b
2 changed files with 59 additions and 5 deletions

View File

@@ -272,13 +272,25 @@ module VagrantPlugins
graphics.attributes['listen'] = config.graphics_ip
graphics.delete_element('//listen')
end
if graphics.attributes['autoport'] != config.graphics_autoport
descr_changed = true
graphics.attributes['autoport'] = config.graphics_autoport
if config.graphics_autoport == 'no'
graphics.attributes.delete('autoport')
unless config.graphics_port.nil? or config.graphics_port == -1
if graphics.attributes['autoport'] != 'no'
descr_changed = true
graphics.attributes['autoport'] = 'no'
end
if graphics.attributes['port'] != config.graphics_port
descr_changed = true
graphics.attributes['port'] = config.graphics_port
end
else
if graphics.attributes['autoport'] != config.graphics_autoport
descr_changed = true
graphics.attributes['autoport'] = config.graphics_autoport
if config.graphics_autoport == 'no'
graphics.attributes['port'] = config.graphics_port
else
graphics.attributes['port'] = '-1'
end
end
end
if graphics.attributes['websocket'] != config.graphics_websocket.to_s
descr_changed = true

View File

@@ -349,6 +349,48 @@ describe VagrantPlugins::ProviderLibvirt::Action::StartDomain do
expect(subject.call(env)).to be_nil
end
end
[
[
'when port explicitly set, should set autoport=no',
proc { |config|
config.graphics_port = 5901
},
"<graphics autoport='yes' keymap='en-us' listen='127.0.0.1' port='-1' type='vnc' websocket='-1'/>",
"<graphics autoport='no' keymap='en-us' listen='127.0.0.1' port='5901' type='vnc' websocket='-1'/>",
],
[
'when port updated, should set autoport=no and update port',
proc { |config|
config.graphics_port = 5902
},
"<graphics autoport='no' keymap='en-us' listen='127.0.0.1' port='5901' type='vnc' websocket='-1'/>",
"<graphics autoport='no' keymap='en-us' listen='127.0.0.1' port='5902' type='vnc' websocket='-1'/>",
],
[
'when autoport set and no port, should set autoport=yes and update port to -1',
proc { |config|
config.graphics_autoport = 'yes'
},
"<graphics autoport='no' keymap='en-us' listen='127.0.0.1' port='5901' type='vnc' websocket='-1'/>",
"<graphics autoport='yes' keymap='en-us' listen='127.0.0.1' port='-1' type='vnc' websocket='-1'/>",
],
].each do |description, config_proc, graphics_xml_start, graphics_xml_output|
it "#{description}" do
config_proc.call(machine.provider_config)
initial_domain_xml = domain_xml.gsub(/<graphics .*\/>/, graphics_xml_start)
updated_domain_xml = domain_xml.gsub(/<graphics .*\/>/, graphics_xml_output)
expect(ui).to_not receive(:warn)
expect(connection).to receive(:define_domain).with(match(graphics_xml_output)).and_return(libvirt_domain)
expect(libvirt_domain).to receive(:xml_desc).and_return(initial_domain_xml, updated_domain_xml, updated_domain_xml)
expect(libvirt_domain).to receive(:autostart=)
expect(domain).to receive(:start)
expect(subject.call(env)).to be_nil
end
end
end
context 'nvram' do