Simple prepare test for 9p and virtiofs synced_folders (#1529)

Provide basic tests for 9p and virtiofs synced_folder capabilities to
validate prepare behaviour.
This commit is contained in:
Darragh Bailey
2022-07-13 17:36:17 +01:00
committed by GitHub
parent 2dc704e1f9
commit 206a9244a8
8 changed files with 163 additions and 12 deletions

View File

@@ -3,11 +3,7 @@
require 'fileutils' require 'fileutils'
require 'log4r' require 'log4r'
class String require 'vagrant-libvirt/util/unindent'
def unindent
gsub(/^#{scan(/^\s*/).min_by{|l|l.length}}/, "")
end
end
module VagrantPlugins module VagrantPlugins
module ProviderLibvirt module ProviderLibvirt

View File

@@ -34,6 +34,8 @@ module VagrantPlugins
raise Vagrant::Errors::Error('No Libvirt connection') if machine.provider.driver.connection.nil? raise Vagrant::Errors::Error('No Libvirt connection') if machine.provider.driver.connection.nil?
@conn = machine.provider.driver.connection.client @conn = machine.provider.driver.connection.client
machine.ui.info I18n.t("vagrant_libvirt.cap.9p.preparing")
begin begin
# loop through folders # loop through folders
folders.each do |id, folder_opts| 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] mount_tag = Digest::MD5.new.update(folder_opts[:hostpath]).to_s[0, 31]
folder_opts[:mount_tag] = mount_tag folder_opts[:mount_tag] = mount_tag
machine.ui.info I18n.t("vagrant_libvirt.cap.9p.preparing")
xml = Nokogiri::XML::Builder.new do |xml| xml = Nokogiri::XML::Builder.new do |xml|
xml.filesystem(type: 'mount', accessmode: folder_opts[:accessmode]) do xml.filesystem(type: 'mount', accessmode: folder_opts[:accessmode]) do
xml.driver(type: 'path', wrpolicy: 'immediate') xml.driver(type: 'path', wrpolicy: 'immediate')

View File

@@ -0,0 +1,7 @@
# frozen_string_literal: true
class String
def unindent
gsub(/^#{scan(/^\s*/).min_by{|l|l.length}}/, "")
end
end

View File

@@ -217,10 +217,10 @@ en:
cap: cap:
virtiofs: virtiofs:
preparing: Configuring virtiofs device for shared folders... preparing: Configuring virtiofs devices for shared folders...
mounting: Mounting virtiofs shared folders... mounting: Mounting virtiofs shared folders...
cleanup: Removing virtiofs device for shared folders... cleanup: Removing virtiofs devices for shared folders...
9p: 9p:
preparing: Configuring 9p device for shared folders... preparing: Configuring 9p devices for shared folders...
mounting: Mounting 9p shared folders... mounting: Mounting 9p shared folders...
cleanup: Removing 9p device for shared folders... cleanup: Removing 9p devices for shared folders...

View File

@@ -37,5 +37,6 @@ shared_context 'unit' do
before (:each) do before (:each) do
allow(machine).to receive(:guest).and_return(guest) allow(machine).to receive(:guest).and_return(guest)
allow(machine).to receive(:communicate).and_return(communicator) allow(machine).to receive(:communicate).and_return(communicator)
allow(machine).to receive(:ui).and_return(ui)
end end
end end

View File

@@ -195,7 +195,6 @@ describe VagrantPlugins::ProviderLibvirt::Action::ClearForwardedPorts do
end end
end end
it 'should terminate each of the processes' do 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("10001").and_return(true)
expect(subject).to receive(:ssh_pid?).with("10002").and_return(true) expect(subject).to receive(:ssh_pid?).with("10002").and_return(true)
expect(logger).to receive(:debug).with(/Killing pid/).twice() expect(logger).to receive(:debug).with(/Killing pid/).twice()

View File

@@ -4,6 +4,7 @@ require 'spec_helper'
require 'support/sharedcontext' require 'support/sharedcontext'
require 'vagrant-libvirt/cap/synced_folder_9p' require 'vagrant-libvirt/cap/synced_folder_9p'
require 'vagrant-libvirt/util/unindent'
describe VagrantPlugins::SyncedFolder9P::SyncedFolder do describe VagrantPlugins::SyncedFolder9P::SyncedFolder do
include_context 'unit' include_context 'unit'
@@ -11,6 +12,79 @@ describe VagrantPlugins::SyncedFolder9P::SyncedFolder do
subject { described_class.new } 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
<filesystem type="mount" accessmode="passthrough">
<driver type="path" wrpolicy="immediate"></driver>
<source dir="/home/test/default"></source>
<target dir="1ef53e1c9b6a5a0c695c80665331652\"></target>
</filesystem>
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 ]
<filesystem type="mount" accessmode="passthrough">
<driver type="path" wrpolicy="immediate"></driver>
<source dir="/home/test/default"></source>
<target dir="1ef53e1c9b6a5a0c695c80665331652\"></target>
</filesystem>
XML1
<filesystem type="mount" accessmode="passthrough">
<driver type="path" wrpolicy="immediate"></driver>
<source dir="/home/test/custom"></source>
<target dir=\"a2a1a8b6d98be8f790f3c987e006d13\"></target>
</filesystem>
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 describe '#usable?' do
context 'with libvirt provider' do context 'with libvirt provider' do
before do before do

View File

@@ -4,6 +4,7 @@ require 'spec_helper'
require 'support/sharedcontext' require 'support/sharedcontext'
require 'vagrant-libvirt/cap/synced_folder_virtiofs' require 'vagrant-libvirt/cap/synced_folder_virtiofs'
require 'vagrant-libvirt/util/unindent'
describe VagrantPlugins::SyncedFolderVirtioFS::SyncedFolder do describe VagrantPlugins::SyncedFolderVirtioFS::SyncedFolder do
include_context 'unit' include_context 'unit'
@@ -11,6 +12,79 @@ describe VagrantPlugins::SyncedFolderVirtioFS::SyncedFolder do
subject { described_class.new } 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
<filesystem type="mount" accessmode="passthrough">
<driver type="virtiofs"></driver>
<source dir="/home/test/default"></source>
<target dir="1ef53e1c9b6a5a0c695c80665331652\"></target>
</filesystem>
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 ]
<filesystem type="mount" accessmode="passthrough">
<driver type="virtiofs"></driver>
<source dir="/home/test/default"></source>
<target dir="1ef53e1c9b6a5a0c695c80665331652\"></target>
</filesystem>
XML1
<filesystem type="mount" accessmode="passthrough">
<driver type="virtiofs"></driver>
<source dir="/home/test/custom"></source>
<target dir=\"a2a1a8b6d98be8f790f3c987e006d13\"></target>
</filesystem>
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 describe '#usable?' do
context 'with libvirt provider' do context 'with libvirt provider' do
before do before do