From 8a69c1205c7b991619b7e042ff1c71b5ccb0647f Mon Sep 17 00:00:00 2001 From: Jeff Bonhag <6937257+jbonhag@users.noreply.github.com> Date: Wed, 6 Nov 2019 16:10:24 -0500 Subject: [PATCH] Fixes #10170: Check for modinfo in /sbin (#11178) Check for modinfo in /sbin if it doesn't appear on the PATH. If it's not found on the PATH or in /sbin, the command will default back to modinfo so the user sees the error message about adding it to their PATH. --- plugins/hosts/linux/cap/nfs.rb | 20 +++++++- test/unit/plugins/hosts/linux/cap/nfs_test.rb | 46 +++++++++++++++++++ 2 files changed, 65 insertions(+), 1 deletion(-) diff --git a/plugins/hosts/linux/cap/nfs.rb b/plugins/hosts/linux/cap/nfs.rb index 630dc0f4e..addc7ee89 100644 --- a/plugins/hosts/linux/cap/nfs.rb +++ b/plugins/hosts/linux/cap/nfs.rb @@ -93,7 +93,7 @@ module VagrantPlugins "systemctl --no-pager --no-legend --plain list-unit-files --all --type=service " \ "| grep #{nfs_service_name_systemd}").exit_code == 0 else - Vagrant::Util::Subprocess.execute("modinfo", "nfsd").exit_code == 0 || + Vagrant::Util::Subprocess.execute(modinfo_path, "nfsd").exit_code == 0 || Vagrant::Util::Subprocess.execute("grep", "nfsd", "/proc/filesystems").exit_code == 0 end end @@ -261,6 +261,24 @@ module VagrantPlugins Vagrant::Util::Subprocess.execute(*Shellwords.split(check_command)).exit_code == 0 end + def self.modinfo_path + if !defined?(@_modinfo_path) + @_modinfo_path = Vagrant::Util::Which.which("modinfo") + + if @_modinfo_path.to_s.empty? + path = "/sbin/modinfo" + if File.file?(path) + @_modinfo_path = path + end + end + + if @_modinfo_path.to_s.empty? + @_modinfo_path = "modinfo" + end + end + @_modinfo_path + end + # @private # Reset the cached values for capability. This is not considered a public # API and should only be used for testing. diff --git a/test/unit/plugins/hosts/linux/cap/nfs_test.rb b/test/unit/plugins/hosts/linux/cap/nfs_test.rb index 575035ab9..860c9b172 100644 --- a/test/unit/plugins/hosts/linux/cap/nfs_test.rb +++ b/test/unit/plugins/hosts/linux/cap/nfs_test.rb @@ -337,4 +337,50 @@ EOH end end end + + describe ".modinfo_path" do + let(:cap){ VagrantPlugins::HostLinux::Cap::NFS } + + context "with modinfo on PATH" do + before do + expect(Vagrant::Util::Which).to receive(:which).with("modinfo").and_return("/usr/bin/modinfo") + end + + it "should use full path to modinfo" do + expect(cap.modinfo_path).to eq("/usr/bin/modinfo") + end + end + + context "with modinfo at /sbin/modinfo" do + before do + expect(Vagrant::Util::Which).to receive(:which).with("modinfo").and_return(nil) + expect(File).to receive(:file?).with("/sbin/modinfo").and_return(true) + end + + it "should use /sbin/modinfo" do + expect(cap.modinfo_path).to eq("/sbin/modinfo") + end + end + + context "modinfo not found" do + before do + expect(Vagrant::Util::Which).to receive(:which).with("modinfo").and_return(nil) + expect(File).to receive(:file?).with("/sbin/modinfo").and_return(false) + end + + it "should use modinfo" do + expect(cap.modinfo_path).to eq("modinfo") + end + end + + context "with cached value for modinfo_path" do + before do + cap.instance_variable_set(:@_modinfo_path, "/usr/local/bin/modinfo") + end + + it "should use cached value" do + expect(cap.modinfo_path).to eq("/usr/local/bin/modinfo") + end + end + end end