Use same list_all_networks and filter (#1638)

Ensure the same filtering for networks supported by vagrant-libvirt is
done for both driver and util by moving to call the same function with
filtering.

This avoids calls for the list of host devices from failing to parse
some networks that are not supported.

Fixes: #599
This commit is contained in:
Darragh Bailey
2022-10-11 17:54:41 +01:00
committed by GitHub
parent 736190ae41
commit 59c7d9f18e
6 changed files with 65 additions and 39 deletions

View File

@@ -187,7 +187,7 @@ describe VagrantPlugins::ProviderLibvirt::Driver do
context 'when qemu_use_session is enabled' do
let(:system_connection) { double("system connection") }
let(:networks) { [instance_double(::Fog::Libvirt::Compute::Real)] }
let(:networks) { [instance_double(::Libvirt::Network)] }
let(:dhcp_leases) {
{
"iface" =>"virbr0",
@@ -207,8 +207,7 @@ describe VagrantPlugins::ProviderLibvirt::Driver do
it 'should retrieve the address via the system dhcp-leases API' do
expect(domain).to receive(:mac).and_return("52:54:00:8b:dc:5f")
expect(subject).to receive(:system_connection).and_return(system_connection)
expect(system_connection).to receive(:list_all_networks).and_return(networks)
expect(subject).to receive(:list_all_networks).and_return(networks)
expect(networks[0]).to receive(:dhcp_leases).and_return([dhcp_leases])
expect(subject.get_ipaddress).to eq("192.168.122.43")
@@ -229,6 +228,43 @@ describe VagrantPlugins::ProviderLibvirt::Driver do
end
end
describe '#list_all_networks' do
let(:vagrantfile_providerconfig) do
<<-EOF
libvirt.uri = "qemu:///system"
EOF
end
let(:libvirt_networks) { [
instance_double(::Libvirt::Network),
instance_double(::Libvirt::Network),
instance_double(::Libvirt::Network),
] }
before do
allow(subject).to receive(:system_connection).and_return(libvirt_client)
expect(libvirt_client).to receive(:list_all_networks).and_return(libvirt_networks)
end
it 'should list networks' do
expect(libvirt_networks[0]).to receive(:bridge_name).and_return('')
expect(libvirt_networks[1]).to receive(:bridge_name).and_return('virbr0')
expect(libvirt_networks[2]).to receive(:bridge_name).and_return('virbr1')
expect(subject.list_all_networks).to eq(libvirt_networks)
end
it 'should skip networks missing bridge_name' do
expect(libvirt_networks[0]).to receive(:bridge_name).and_return('')
expect(libvirt_networks[1]).to receive(:bridge_name).and_raise(Libvirt::Error)
expect(libvirt_networks[1]).to receive(:name).and_return('bad_network')
expect(libvirt_networks[2]).to receive(:bridge_name).and_return('virbr1')
expect(subject.list_all_networks).to eq([libvirt_networks[0], libvirt_networks[2]])
end
end
describe '#host_devices' do
let(:vagrantfile_providerconfig) do
<<-EOF
@@ -265,7 +301,7 @@ describe VagrantPlugins::ProviderLibvirt::Driver do
end.and_return(Vagrant::Util::Subprocess::Result.new(exit_code=0, stdout=ip_link_show, stderr=''))
expect(libvirt_client).to receive(:list_all_interfaces).and_return(libvirt_interfaces)
expect(libvirt_client).to receive(:list_all_networks).and_return(libvirt_networks)
expect(subject).to receive(:list_all_networks).and_return(libvirt_networks)
expect(libvirt_interfaces[0]).to receive(:name).and_return('eth0')
expect(libvirt_interfaces[1]).to receive(:name).and_return('virbr0')
expect(libvirt_networks[0]).to receive(:bridge_name).and_return('')

View File

@@ -45,23 +45,12 @@ describe 'VagrantPlugins::ProviderLibvirt::Util::NetworkUtil' do
describe '#libvirt_networks' do
let(:default_network) { create_libvirt_network('default') }
let(:additional_network) { create_libvirt_network('vagrant-libvirt') }
let(:hostdev_network) { create_libvirt_network('hostdev', {:active? => false}) }
it 'should retrieve the list of networks' do
expect(logger).to_not receive(:debug)
expect(libvirt_client).to receive(:list_all_networks).and_return([default_network, additional_network])
expect(driver).to receive(:list_all_networks).and_return([default_network, additional_network])
expect(subject.libvirt_networks(libvirt_client)).to match_array([
hash_including(:name => 'default'),
hash_including(:name => 'vagrant-libvirt'),
])
end
it 'should handle networks without bridge names' do
expect(logger).to receive(:debug).with(/Ignoring hostdev as it does not/)
expect(libvirt_client).to receive(:list_all_networks).and_return([default_network, hostdev_network, additional_network])
expect(subject.libvirt_networks(libvirt_client)).to match_array([
expect(subject.libvirt_networks(driver)).to match_array([
hash_including(:name => 'default'),
hash_including(:name => 'vagrant-libvirt'),
])