# frozen_string_literal: true require_relative '../../spec_helper' require 'vagrant-libvirt/errors' require 'vagrant-libvirt/action/create_network_interfaces' require 'vagrant-libvirt/util/unindent' describe VagrantPlugins::ProviderLibvirt::Action::CreateNetworkInterfaces do subject { described_class.new(app, env) } include_context 'unit' include_context 'libvirt' let(:networks) { [ instance_double(::Libvirt::Network), instance_double(::Libvirt::Network), ] } let(:default_network_xml) { <<-EOF default e5f871eb-2899-48b2-83df-78aa43efa360 EOF } let(:management_network_xml) { <<-EOF vagrant-libvirt 46360938-0607-4168-a182-1352fac4a4f9 EOF } let(:default_management_nic_xml) { <<-EOF.unindent EOF } before do allow(app).to receive(:call) allow(libvirt_client).to receive(:lookup_domain_by_uuid).and_return(libvirt_domain) allow(driver).to receive(:list_all_networks).and_return(networks) allow(networks[0]).to receive(:xml_desc).and_return(default_network_xml) allow(networks[0]).to receive(:name).and_return('default') allow(networks[0]).to receive(:bridge_name).and_return('virbr0') allow(networks[0]).to receive(:active?).and_return(true) allow(networks[0]).to receive(:autostart?).and_return(true) allow(networks[1]).to receive(:xml_desc).and_return(management_network_xml) allow(networks[1]).to receive(:name).and_return('vagrant-libvirt') allow(networks[1]).to receive(:bridge_name).and_return('virbr1') allow(networks[1]).to receive(:active?).and_return(true) allow(networks[1]).to receive(:autostart?).and_return(false) allow(logger).to receive(:info) allow(logger).to receive(:debug) end describe '#call' do it 'should inject the management network definition' do expect(driver).to receive(:attach_device).with(default_management_nic_xml) expect(subject.call(env)).to be_nil end context 'private network' do let(:vagrantfile) do <<-EOF Vagrant.configure('2') do |config| config.vm.box = "vagrant-libvirt/test" config.vm.define :test config.vm.provider :libvirt do |libvirt| #{vagrantfile_providerconfig} end config.vm.network :private_network, :ip => "10.20.30.40" end EOF end let(:private_network) { instance_double(::Libvirt::Network) } let(:private_network_xml) { <<-EOF test1 46360938-0607-4168-a182-1352fac4a4f9 EOF } let(:private_nic_xml) { <<-EOF.unindent EOF } before do allow(private_network).to receive(:xml_desc).and_return(private_network_xml) allow(private_network).to receive(:name).and_return('test1') allow(private_network).to receive(:bridge_name).and_return('virbr2') allow(private_network).to receive(:active?).and_return(true) allow(private_network).to receive(:autostart?).and_return(false) end it 'should attach for two networks' do expect(driver).to receive(:list_all_networks).and_return(networks + [private_network]) expect(driver).to receive(:attach_device).with(default_management_nic_xml) expect(driver).to receive(:attach_device).with(private_nic_xml) expect(guest).to receive(:capability).with(:configure_networks, any_args) expect(subject.call(env)).to be_nil end context 'when iface name is set' do let(:private_nic_xml) { <<-EOF.unindent EOF } before do machine.config.vm.networks[0][1][:libvirt__iface_name] = "myvnet0" end it 'should set target appropriately' do expect(driver).to receive(:list_all_networks).and_return(networks + [private_network]) expect(driver).to receive(:attach_device).with(default_management_nic_xml) expect(driver).to receive(:attach_device).with(private_nic_xml) expect(guest).to receive(:capability).with(:configure_networks, any_args) expect(subject.call(env)).to be_nil end end it 'should skip configuring networks in guest without box' do machine.config.vm.box = nil expect(driver).to receive(:list_all_networks).and_return(networks + [private_network]) expect(driver).to receive(:attach_device).with(default_management_nic_xml) expect(driver).to receive(:attach_device).with(private_nic_xml) expect(guest).to_not receive(:capability).with(:configure_networks, any_args) expect(subject.call(env)).to be_nil end end context 'public network' do let(:vagrantfile) do <<-EOF Vagrant.configure('2') do |config| config.vm.box = "vagrant-libvirt/test" config.vm.define :test config.vm.provider :libvirt do |libvirt| #{vagrantfile_providerconfig} end config.vm.network :public_network, :dev => "virbr1", :mode => "bridge", :type => "bridge" end EOF end let(:public_network) { instance_double(::Libvirt::Network) } let(:public_network_xml) { <<-EOF test1 46360938-0607-4168-a182-1352fac4a4f9 EOF } let(:public_nic_xml) { <<-EOF.unindent EOF } let(:domain_xml) { # don't need full domain here, just enough for the network element to work <<-EOF.unindent
EOF } before do allow(public_network).to receive(:xml_desc).and_return(public_network_xml) allow(public_network).to receive(:name).and_return('test1') allow(public_network).to receive(:bridge_name).and_return('virbr2') allow(public_network).to receive(:active?).and_return(true) allow(public_network).to receive(:autostart?).and_return(false) allow(libvirt_domain).to receive(:xml_desc).and_return(domain_xml) end it 'should attach for two networks' do expect(driver).to receive(:list_all_networks).and_return(networks + [public_network]) expect(driver).to receive(:attach_device).with(default_management_nic_xml) expect(driver).to receive(:attach_device).with(public_nic_xml) expect(guest).to receive(:capability).with(:configure_networks, any_args) expect(subject.call(env)).to be_nil end context 'when iface name is set' do let(:public_nic_xml) { <<-EOF.unindent EOF } before do machine.config.vm.networks[0][1][:libvirt__iface_name] = "myvnet0" end it 'should set target appropriately' do expect(driver).to receive(:list_all_networks).and_return(networks + [public_network]) expect(driver).to receive(:attach_device).with(default_management_nic_xml) expect(driver).to receive(:attach_device).with(public_nic_xml) expect(guest).to receive(:capability).with(:configure_networks, any_args) expect(subject.call(env)).to be_nil end end end end end