diff --git a/plugins/hosts/windows/cap/smb.rb b/plugins/hosts/windows/cap/smb.rb index 639cd2b53..e767eb202 100644 --- a/plugins/hosts/windows/cap/smb.rb +++ b/plugins/hosts/windows/cap/smb.rb @@ -117,9 +117,21 @@ module VagrantPlugins # # @return [Hash] def self.existing_shares + shares = get_smbshares || get_netshares + if shares.nil? + raise SyncedFolderSMB::Errors::SMBListFailed + end + @@logger.debug("local share listing: #{shares}") + shares + end + + # Get current SMB share list using Get-SmbShare + # + # @return [Hash] + def self.get_smbshares result = Vagrant::Util::PowerShell.execute_cmd("Get-SmbShare|Format-List") if result.nil? - raise SyncedFolderSMB::Errors::SMBListFailed + return nil end shares = {} name = nil @@ -132,7 +144,33 @@ module VagrantPlugins next if name.nil? || key.to_s.empty? shares[name][key] = value end - @@logger.debug("local share listing: #{shares}") + shares + end + + # Get current SMB share list using net.exe + # + # @return [Hash] + def self.get_netshares + result = Vagrant::Util::PowerShell.execute_cmd("net share") + if result.nil? + return nil + end + result.sub!(/^.+?\-+/m, "") + share_names = result.strip.split("\n").map do |line| + line.strip.split(/\s+/).first + end + shares = {} + share_names.each do |share_name| + shares[share_name] = {} + result = Vagrant::Util::PowerShell.execute_cmd("net share #{share_name}") + next if result.nil? + result.each_line do |line| + key, value = line.strip.split(/\s+/, 2) + next if key == "Share name" + key = "Description" if key == "Remark" + shares[share_name][key] = value + end + end shares end diff --git a/test/unit/plugins/hosts/windows/cap/smb_test.rb b/test/unit/plugins/hosts/windows/cap/smb_test.rb index a1822e643..d482ad0dd 100644 --- a/test/unit/plugins/hosts/windows/cap/smb_test.rb +++ b/test/unit/plugins/hosts/windows/cap/smb_test.rb @@ -24,6 +24,34 @@ Description : Not Vagrant Owned EOF } + let(:netsharelist){ <<-EOF + +Share name Resource Remark +----------------------------------------------- +vgt-CUSTOM_ID-1 /a/path vgt-CUSTOM_ID-1 +vgt-CUSTOM_ID-2 /other/path vgt-CUSTOM_ID-2 +my-share /my/path Not Vagran... + + EOF + } + let(:netshare1){ <<-EOF +Share name vgt-CUSTOM_ID-1 +Path /a/path +Remark vgt-CUSTOM_ID-1 + EOF + } + let(:netshare2){ <<-EOF +Share name vgt-CUSTOM_ID-2 +Path /other/path +Remark vgt-CUSTOM_ID-2 + EOF + } + let(:netshare_my){ <<-EOF +Share name my-share +Path /my/path +Remark Not Vagrant Owned + EOF + } before do @@ -62,6 +90,10 @@ Description : Not Vagrant Owned before do allow(Vagrant::Util::PowerShell).to receive(:execute_cmd).with(/Get-SmbShare/). and_return(smblist) + allow(Vagrant::Util::PowerShell).to receive(:execute_cmd).with(/net share/).and_return(netsharelist) + allow(Vagrant::Util::PowerShell).to receive(:execute_cmd).with(/net share vgt-CUSTOM_ID-1/).and_return(netshare1) + allow(Vagrant::Util::PowerShell).to receive(:execute_cmd).with(/net share vgt-CUSTOM_ID-2/).and_return(netshare2) + allow(Vagrant::Util::PowerShell).to receive(:execute_cmd).with(/net share my/).and_return(netshare_my) allow(Vagrant::Util::PowerShell).to receive(:execute).and_return(result.new(0, "", "")) end after{ subject.smb_cleanup(env, machine, options) } @@ -71,6 +103,21 @@ Description : Not Vagrant Owned expect(subject).to receive(:sleep) end + it "should remove owned shares" do + expect(Vagrant::Util::PowerShell).to receive(:execute) do |*args| + expect(args).to include("vgt-CUSTOM_ID-1") + expect(args).to include("vgt-CUSTOM_ID-2") + result.new(0, "", "") + end + end + + it "should not remove owned shares" do + expect(Vagrant::Util::PowerShell).to receive(:execute) do |*args| + expect(args).not_to include("my-share") + result.new(0, "", "") + end + end + it "should remove all shares in single call" do expect(Vagrant::Util::PowerShell).to receive(:execute).with(any_args, sudo: true).once end @@ -89,6 +136,31 @@ Description : Not Vagrant Owned expect(machine.env.ui).not_to receive(:warn) end end + + context "when Get-SmbShare is not available" do + before do + expect(Vagrant::Util::PowerShell).to receive(:execute_cmd).with(/Get-SmbShare/).and_return(nil) + end + + it "should fetch list using net.exe" do + expect(Vagrant::Util::PowerShell).to receive(:execute_cmd).with(/net share/).and_return("") + end + + it "should remove owned shares" do + expect(Vagrant::Util::PowerShell).to receive(:execute) do |*args| + expect(args).to include("vgt-CUSTOM_ID-1") + expect(args).to include("vgt-CUSTOM_ID-2") + result.new(0, "", "") + end + end + + it "should not remove owned shares" do + expect(Vagrant::Util::PowerShell).to receive(:execute) do |*args| + expect(args).not_to include("my-share") + result.new(0, "", "") + end + end + end end describe ".smb_prepare" do