From b21e4b0602c9782b01f7cf586bd33d57c77a3ab8 Mon Sep 17 00:00:00 2001 From: Chris Roberts Date: Wed, 21 Sep 2022 10:34:27 -0700 Subject: [PATCH] Use static file path when creating new exports file --- plugins/hosts/linux/cap/nfs.rb | 7 ++++--- test/unit/plugins/hosts/linux/cap/nfs_test.rb | 13 ++++++++++--- 2 files changed, 14 insertions(+), 6 deletions(-) diff --git a/plugins/hosts/linux/cap/nfs.rb b/plugins/hosts/linux/cap/nfs.rb index 0cf5fb26b..4a5516065 100644 --- a/plugins/hosts/linux/cap/nfs.rb +++ b/plugins/hosts/linux/cap/nfs.rb @@ -177,10 +177,11 @@ module VagrantPlugins exports_path = Pathname.new(NFS_EXPORTS_PATH) # Write contents out to temporary file - new_exports_file = Tempfile.create('vagrant') + new_exports_path = File.join(Dir.tmpdir, "vagrant-exports") + FileUtils.rm_f(new_exports_path) + new_exports_file = File.open(new_exports_path, "w+") new_exports_file.puts(new_exports_content) new_exports_file.close - new_exports_path = new_exports_file.path # Ensure new file mode and uid/gid match existing file to replace existing_stat = File.stat(NFS_EXPORTS_PATH) @@ -209,7 +210,7 @@ module VagrantPlugins stdout: result.stdout end ensure - if File.exist?(new_exports_path) + if !new_exports_path.nil? && File.exist?(new_exports_path) File.unlink(new_exports_path) end end diff --git a/test/unit/plugins/hosts/linux/cap/nfs_test.rb b/test/unit/plugins/hosts/linux/cap/nfs_test.rb index ab94f7497..849ffe997 100644 --- a/test/unit/plugins/hosts/linux/cap/nfs_test.rb +++ b/test/unit/plugins/hosts/linux/cap/nfs_test.rb @@ -270,19 +270,26 @@ EOH context "exports file modification" do let(:tmp_stat) { double("tmp_stat", uid: 100, gid: 100, mode: tmp_mode) } let(:tmp_mode) { 0 } - let(:exports_stat) { double("stat", uid: exports_uid, gid: exports_gid, mode: exports_mode) } + let(:exports_stat) { + double("stat", uid: exports_uid, gid: exports_gid, + mode: exports_mode, :directory? => true, :writable? => true, + :world_writable? => true, :sticky? => true) + } let(:exports_uid) { -1 } let(:exports_gid) { -1 } let(:exports_mode) { 0 } let(:new_exports_file) { double("new_exports_file", path: "/dev/null/exports") } + let(:new_exports_path) { new_exports_file.path } before do - allow(File).to receive(:stat).with(new_exports_file.path).and_return(tmp_stat) + allow(File).to receive(:stat).and_call_original + allow(File).to receive(:join).with(Dir.tmpdir, "vagrant-exports").and_return(new_exports_path) + allow(File).to receive(:open).with(new_exports_path, "w+").and_return(new_exports_file) + allow(File).to receive(:stat).with(new_exports_path).and_return(tmp_stat) allow(File).to receive(:stat).with(tmp_exports_path.to_s).and_return(exports_stat) allow(new_exports_file).to receive(:puts) allow(new_exports_file).to receive(:close) allow(Vagrant::Util::Subprocess).to receive(:execute).and_return(Vagrant::Util::Subprocess::Result.new(0, "", "")) - allow(Tempfile).to receive(:create).with("vagrant").and_return(new_exports_file) end it "should retain existing file owner and group IDs" do