diff --git a/lib/vagrant-libvirt/action/package_domain.rb b/lib/vagrant-libvirt/action/package_domain.rb index 09b1bd6..eafa435 100644 --- a/lib/vagrant-libvirt/action/package_domain.rb +++ b/lib/vagrant-libvirt/action/package_domain.rb @@ -3,11 +3,7 @@ require 'fileutils' require 'log4r' -class String - def unindent - gsub(/^#{scan(/^\s*/).min_by{|l|l.length}}/, "") - end -end +require 'vagrant-libvirt/util/unindent' module VagrantPlugins module ProviderLibvirt diff --git a/lib/vagrant-libvirt/cap/synced_folder_9p.rb b/lib/vagrant-libvirt/cap/synced_folder_9p.rb index 2e54408..53b1810 100644 --- a/lib/vagrant-libvirt/cap/synced_folder_9p.rb +++ b/lib/vagrant-libvirt/cap/synced_folder_9p.rb @@ -34,6 +34,8 @@ module VagrantPlugins raise Vagrant::Errors::Error('No Libvirt connection') if machine.provider.driver.connection.nil? @conn = machine.provider.driver.connection.client + machine.ui.info I18n.t("vagrant_libvirt.cap.9p.preparing") + begin # loop through folders folders.each do |id, folder_opts| @@ -45,8 +47,6 @@ module VagrantPlugins mount_tag = Digest::MD5.new.update(folder_opts[:hostpath]).to_s[0, 31] folder_opts[:mount_tag] = mount_tag - machine.ui.info I18n.t("vagrant_libvirt.cap.9p.preparing") - xml = Nokogiri::XML::Builder.new do |xml| xml.filesystem(type: 'mount', accessmode: folder_opts[:accessmode]) do xml.driver(type: 'path', wrpolicy: 'immediate') diff --git a/lib/vagrant-libvirt/util/unindent.rb b/lib/vagrant-libvirt/util/unindent.rb new file mode 100644 index 0000000..dd9f876 --- /dev/null +++ b/lib/vagrant-libvirt/util/unindent.rb @@ -0,0 +1,7 @@ +# frozen_string_literal: true + +class String + def unindent + gsub(/^#{scan(/^\s*/).min_by{|l|l.length}}/, "") + end +end diff --git a/locales/en.yml b/locales/en.yml index d8f5a5f..c3d52ad 100644 --- a/locales/en.yml +++ b/locales/en.yml @@ -217,10 +217,10 @@ en: cap: virtiofs: - preparing: Configuring virtiofs device for shared folders... + preparing: Configuring virtiofs devices for shared folders... mounting: Mounting virtiofs shared folders... - cleanup: Removing virtiofs device for shared folders... + cleanup: Removing virtiofs devices for shared folders... 9p: - preparing: Configuring 9p device for shared folders... + preparing: Configuring 9p devices for shared folders... mounting: Mounting 9p shared folders... - cleanup: Removing 9p device for shared folders... + cleanup: Removing 9p devices for shared folders... diff --git a/spec/support/sharedcontext.rb b/spec/support/sharedcontext.rb index 9558384..dd03d57 100644 --- a/spec/support/sharedcontext.rb +++ b/spec/support/sharedcontext.rb @@ -37,5 +37,6 @@ shared_context 'unit' do before (:each) do allow(machine).to receive(:guest).and_return(guest) allow(machine).to receive(:communicate).and_return(communicator) + allow(machine).to receive(:ui).and_return(ui) end end diff --git a/spec/unit/action/forward_ports_spec.rb b/spec/unit/action/forward_ports_spec.rb index 5854a30..a1d3d35 100644 --- a/spec/unit/action/forward_ports_spec.rb +++ b/spec/unit/action/forward_ports_spec.rb @@ -195,7 +195,6 @@ describe VagrantPlugins::ProviderLibvirt::Action::ClearForwardedPorts do end end it 'should terminate each of the processes' do - expect(logger).to receive(:info).with(no_args) # don't know how to test translations from vagrant expect(subject).to receive(:ssh_pid?).with("10001").and_return(true) expect(subject).to receive(:ssh_pid?).with("10002").and_return(true) expect(logger).to receive(:debug).with(/Killing pid/).twice() diff --git a/spec/unit/cap/synced_folder_9p_spec.rb b/spec/unit/cap/synced_folder_9p_spec.rb index 9bb21cf..d3126fc 100644 --- a/spec/unit/cap/synced_folder_9p_spec.rb +++ b/spec/unit/cap/synced_folder_9p_spec.rb @@ -4,6 +4,7 @@ require 'spec_helper' require 'support/sharedcontext' require 'vagrant-libvirt/cap/synced_folder_9p' +require 'vagrant-libvirt/util/unindent' describe VagrantPlugins::SyncedFolder9P::SyncedFolder do include_context 'unit' @@ -11,6 +12,79 @@ describe VagrantPlugins::SyncedFolder9P::SyncedFolder do subject { described_class.new } + describe '#prepare' do + let(:synced_folders) { { + "/vagrant" => { + :hostpath => '/home/test/default', + :disabled=>false, + :guestpath=>'/vagrant', + :type => :"9p", + }, + } } + + let(:expected_xml) { + <<-EOF.unindent + + + + + + EOF + } + + before do + allow(libvirt_client).to receive(:lookup_domain_by_uuid).and_return(libvirt_domain) + allow(logger).to receive(:debug) + end + + it 'should attach device xml' do + expect(libvirt_domain).to receive(:attach_device).with(expected_xml, 0) + + subject.prepare(machine, synced_folders, {}) + end + + context 'multiple folders' do + let(:synced_folders) { { + "/vagrant" => { + :hostpath => '/home/test/default', + :disabled=>false, + :guestpath=>'/vagrant', + :type => :"9p", + }, + "/custom" => { + :hostpath => '/home/test/custom', + :disabled=>false, + :guestpath=>'/custom', + :type => :"9p", + }, + } } + + let(:expected_xml) { + [ <<-XML1.unindent, <<-XML2.unindent ] + + + + + + XML1 + + + + + + XML2 + } + + it 'should attach all device xml' do + expect(libvirt_domain).to receive(:attach_device).with(expected_xml[0], 0) + expect(libvirt_domain).to receive(:attach_device).with(expected_xml[1], 0) + expect(ui).to receive(:info).with(/Configuring 9p devices for shared folders/).once + + subject.prepare(machine, synced_folders, {}) + end + end + end + describe '#usable?' do context 'with libvirt provider' do before do diff --git a/spec/unit/cap/synced_folder_virtiofs_spec.rb b/spec/unit/cap/synced_folder_virtiofs_spec.rb index 7162a89..1b2e609 100644 --- a/spec/unit/cap/synced_folder_virtiofs_spec.rb +++ b/spec/unit/cap/synced_folder_virtiofs_spec.rb @@ -4,6 +4,7 @@ require 'spec_helper' require 'support/sharedcontext' require 'vagrant-libvirt/cap/synced_folder_virtiofs' +require 'vagrant-libvirt/util/unindent' describe VagrantPlugins::SyncedFolderVirtioFS::SyncedFolder do include_context 'unit' @@ -11,6 +12,79 @@ describe VagrantPlugins::SyncedFolderVirtioFS::SyncedFolder do subject { described_class.new } + describe '#prepare' do + let(:synced_folders) { { + "/vagrant" => { + :hostpath => '/home/test/default', + :disabled=>false, + :guestpath=>'/vagrant', + :type => :virtiofs, + }, + } } + + let(:expected_xml) { + <<-EOF.unindent + + + + + + EOF + } + + before do + allow(libvirt_client).to receive(:lookup_domain_by_uuid).and_return(libvirt_domain) + allow(logger).to receive(:debug) + end + + it 'should attach device xml' do + expect(libvirt_domain).to receive(:attach_device).with(expected_xml, 0) + + subject.prepare(machine, synced_folders, {}) + end + + context 'multiple folders' do + let(:synced_folders) { { + "/vagrant" => { + :hostpath => '/home/test/default', + :disabled=>false, + :guestpath=>'/vagrant', + :type => :virtiofs, + }, + "/custom" => { + :hostpath => '/home/test/custom', + :disabled=>false, + :guestpath=>'/custom', + :type => :virtiofs, + }, + } } + + let(:expected_xml) { + [ <<-XML1.unindent, <<-XML2.unindent ] + + + + + + XML1 + + + + + + XML2 + } + + it 'should attach all device xml' do + expect(libvirt_domain).to receive(:attach_device).with(expected_xml[0], 0) + expect(libvirt_domain).to receive(:attach_device).with(expected_xml[1], 0) + expect(ui).to receive(:info).with(/Configuring virtiofs devices for shared folders/).once + + subject.prepare(machine, synced_folders, {}) + end + end + end + describe '#usable?' do context 'with libvirt provider' do before do