From da15c1d1716e8620ac488aaae8c6eab4bfaf4782 Mon Sep 17 00:00:00 2001 From: Chris Roberts Date: Tue, 8 Oct 2019 16:41:07 -0700 Subject: [PATCH] Add test coverage on host plugin updates for nfs and resove_host_path capabilities --- test/unit/plugins/hosts/bsd/cap/nfs_test.rb | 59 ++++++------ test/unit/plugins/hosts/bsd/cap/path_test.rb | 13 +++ .../plugins/hosts/darwin/cap/path_test.rb | 89 +++++++++++++++++++ 3 files changed, 136 insertions(+), 25 deletions(-) create mode 100644 test/unit/plugins/hosts/bsd/cap/path_test.rb create mode 100644 test/unit/plugins/hosts/darwin/cap/path_test.rb diff --git a/test/unit/plugins/hosts/bsd/cap/nfs_test.rb b/test/unit/plugins/hosts/bsd/cap/nfs_test.rb index c7f6be700..51c125484 100644 --- a/test/unit/plugins/hosts/bsd/cap/nfs_test.rb +++ b/test/unit/plugins/hosts/bsd/cap/nfs_test.rb @@ -1,39 +1,48 @@ require_relative "../../../../base" require_relative "../../../../../../plugins/hosts/bsd/cap/nfs" -require_relative "../../../../../../lib/vagrant/util" describe VagrantPlugins::HostBSD::Cap::NFS do include_context "unit" - describe ".nfs_check_folders_for_apfs" do - it "should prefix host paths that are mounted in /System/Volumes/Data" do - output_from_df = <<-EOH -Filesystem 512-blocks Used Available Capacity iused ifree %iused Mounted on -/dev/disk1s1 976490568 392813584 555082648 42% 1177049 4881275791 0% /System/Volumes/Data -EOH - expect(Vagrant::Util::Subprocess).to receive(:execute).and_return( - Vagrant::Util::Subprocess::Result.new(0, output_from_df, "") - ) + describe ".nfs_export" do + let(:environment) { double("environment", host: host) } + let(:host) { double("host") } + let(:ui) { double("ui") } + let(:id) { "UUID" } + let(:ips) { [] } + let(:folders) { {} } - folders = {"/vagrant"=>{:hostpath=>"/Users/johndoe/vagrant",:bsd__nfs_options=>["rw"]}} - described_class.nfs_check_folders_for_apfs(folders) - expect(folders["/vagrant"][:hostpath]).to eq("/System/Volumes/Data/Users/johndoe/vagrant") + before do + allow(host).to receive(:capability).and_return("") + allow(Vagrant::Util::TemplateRenderer).to receive(:render).and_return("") + allow(described_class).to receive(:sleep) + allow(described_class).to receive(:nfs_cleanup) + allow(described_class).to receive(:system) + allow(File).to receive(:writable?).with("/etc/exports") + allow(ui).to receive(:info) end - it "should not prefix host paths that are mounted in elsewhere" do - output_from_df = <<-EOH -Filesystem 512-blocks Used Available Capacity iused ifree %iused Mounted on -/dev/disk1s5 976490568 20634032 554201072 4% 481588 4881971252 0% / -EOH - expect(Vagrant::Util::Subprocess).to receive(:execute).and_return( - Vagrant::Util::Subprocess::Result.new(0, output_from_df, "") - ) - - folders = {"/vagrant"=>{:hostpath=>"/",:bsd__nfs_options=>["rw"]}} - described_class.nfs_check_folders_for_apfs(folders) - expect(folders["/vagrant"][:hostpath]).to eq("/") + it "should execute successfully when no folders are defined" do + expect { described_class.nfs_export(environment, ui, id, ips, folders) }. + not_to raise_error end + context "with single folder defined" do + let(:folders) { + {"/vagrant" => { + type: :nfs, guestpath: "/vagrant", hostpath: "/Users/vagrant/paths", disabled: false}} + } + + it "should execute successfully" do + expect { described_class.nfs_export(environment, ui, id, ips, folders) }. + not_to raise_error + end + + it "should resolve the host path" do + expect(host).to receive(:capability).with(:resolve_host_path, folders["/vagrant"][:hostpath]).and_return("") + described_class.nfs_export(environment, ui, id, ips, folders) + end + end end end diff --git a/test/unit/plugins/hosts/bsd/cap/path_test.rb b/test/unit/plugins/hosts/bsd/cap/path_test.rb new file mode 100644 index 000000000..ff865f30f --- /dev/null +++ b/test/unit/plugins/hosts/bsd/cap/path_test.rb @@ -0,0 +1,13 @@ +require_relative "../../../../base" +require_relative "../../../../../../plugins/hosts/bsd/cap/path" + +describe VagrantPlugins::HostBSD::Cap::Path do + describe ".resolve_host_path" do + let(:env) { double("environment") } + let(:path) { double("path") } + + it "should return the path object provided" do + expect(described_class.resolve_host_path(env, path)).to eq(path) + end + end +end diff --git a/test/unit/plugins/hosts/darwin/cap/path_test.rb b/test/unit/plugins/hosts/darwin/cap/path_test.rb new file mode 100644 index 000000000..f7e799e6c --- /dev/null +++ b/test/unit/plugins/hosts/darwin/cap/path_test.rb @@ -0,0 +1,89 @@ +require_relative "../../../../base" +require_relative "../../../../../../plugins/hosts/darwin/cap/path" + +describe VagrantPlugins::HostDarwin::Cap::Path do + describe ".resolve_host_path" do + let(:env) { double("environment") } + let(:path) { "/test/vagrant/path" } + let(:firmlink_map) { {} } + + before { allow(described_class).to receive(:firmlink_map).and_return(firmlink_map) } + + it "should not change the path when no firmlinks are defined" do + expect(described_class.resolve_host_path(env, path)).to eq(path) + end + + context "when firmlink map contains non-matching values" do + let(:firmlink_map) { {"/users" => "users", "/system" => "system"} } + + it "should not change the path" do + expect(described_class.resolve_host_path(env, path)).to eq(path) + end + end + + context "when firmlink map contains matching value" do + let(:firmlink_map) { {"/users" => "users", "/test" => "test"} } + + it "should update the path" do + expect(described_class.resolve_host_path(env, path)).not_to eq(path) + end + + it "should prefix the path with the defined data path" do + expect(described_class.resolve_host_path(env, path)).to start_with(described_class.const_get(:FIRMLINK_DATA_PATH)) + end + end + + context "when firmlink map match points to different named target" do + let(:firmlink_map) { {"/users" => "users", "/test" => "other"} } + + it "should update the path" do + expect(described_class.resolve_host_path(env, path)).not_to eq(path) + end + + it "should prefix the path with the defined data path" do + expect(described_class.resolve_host_path(env, path)).to start_with(described_class.const_get(:FIRMLINK_DATA_PATH)) + end + + it "should include the updated path name" do + expect(described_class.resolve_host_path(env, path)).to include("other") + end + end + end + + describe ".firmlink_map" do + before { described_class.reset! } + + context "when firmlink definition file does not exist" do + before { expect(File).to receive(:exist?).with(described_class.const_get(:FIRMLINK_DEFS)).and_return(false) } + + it "should return an empty hash" do + expect(described_class.firmlink_map).to eq({}) + end + end + + context "when firmlink definition file exists with values" do + before do + expect(File).to receive(:exist?).with(described_class.const_get(:FIRMLINK_DEFS)).and_return(true) + expect(File).to receive(:readlines).with.(described_class.const_get(:FIRMLINK_DEFS)). + and_return(["/System\tSystem\n", "/Users\tUsers\n", "/Library/Something\tLibrary/Somethingelse"]) + + it "should generate a non-empty hash" do + expect(described_class.firmlink_map).not_to be_empty + end + + it "should properly create entries" do + result = described_class.firmlink_map + expect(result["/System"]).to eq("System") + expect(result["/Users"]).to eq("Users") + expect(result["/Library/Something"]).to eq("Library/Somethingelse") + end + + it "should only load values once" do + result = describe_class.firmlink_app + expect(File).not_to receive(:readlines) + result = describe_class.firmlink_app + end + end + end + end +end