Use net.exe to fetch SMB shares when Get-SmbShare is not available

Fixes #9547
This commit is contained in:
Chris Roberts 2018-05-04 14:07:33 -07:00
parent 09be82f1e2
commit 7a4150dc3f
2 changed files with 112 additions and 2 deletions

View File

@ -117,9 +117,21 @@ module VagrantPlugins
# #
# @return [Hash] # @return [Hash]
def self.existing_shares 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") result = Vagrant::Util::PowerShell.execute_cmd("Get-SmbShare|Format-List")
if result.nil? if result.nil?
raise SyncedFolderSMB::Errors::SMBListFailed return nil
end end
shares = {} shares = {}
name = nil name = nil
@ -132,7 +144,33 @@ module VagrantPlugins
next if name.nil? || key.to_s.empty? next if name.nil? || key.to_s.empty?
shares[name][key] = value shares[name][key] = value
end 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 shares
end end

View File

@ -24,6 +24,34 @@ Description : Not Vagrant Owned
EOF 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 before do
@ -62,6 +90,10 @@ Description : Not Vagrant Owned
before do before do
allow(Vagrant::Util::PowerShell).to receive(:execute_cmd).with(/Get-SmbShare/). allow(Vagrant::Util::PowerShell).to receive(:execute_cmd).with(/Get-SmbShare/).
and_return(smblist) 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, "", "")) allow(Vagrant::Util::PowerShell).to receive(:execute).and_return(result.new(0, "", ""))
end end
after{ subject.smb_cleanup(env, machine, options) } after{ subject.smb_cleanup(env, machine, options) }
@ -71,6 +103,21 @@ Description : Not Vagrant Owned
expect(subject).to receive(:sleep) expect(subject).to receive(:sleep)
end 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 it "should remove all shares in single call" do
expect(Vagrant::Util::PowerShell).to receive(:execute).with(any_args, sudo: true).once expect(Vagrant::Util::PowerShell).to receive(:execute).with(any_args, sudo: true).once
end end
@ -89,6 +136,31 @@ Description : Not Vagrant Owned
expect(machine.env.ui).not_to receive(:warn) expect(machine.env.ui).not_to receive(:warn)
end end
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 end
describe ".smb_prepare" do describe ".smb_prepare" do