Merge pull request #4996 from mitchellh/4976-fix-guest-autodetection-with-windows-guests

4976 fix guest autodetection with windows guests
This commit is contained in:
Shawn Neal 2014-12-16 11:47:05 -08:00
commit 2785fd14db
2 changed files with 38 additions and 7 deletions

View File

@ -79,12 +79,21 @@ module VagrantPlugins
alias_method :sudo, :execute alias_method :sudo, :execute
def test(command, opts=nil) def test(command, opts=nil)
# If this is a *nix command with no Windows equivilant, assume failure # If this is a *nix command (which we know about) with no Windows
# equivilant, assume failure
command = @cmd_filter.filter(command) command = @cmd_filter.filter(command)
return false if command.empty? return false if command.empty?
opts = { error_check: false }.merge(opts || {}) opts = {
execute(command, opts) == 0 command: command,
elevated: false,
error_check: false,
}.merge(opts || {})
# If we're passed a *nix command which PS can't parse we get exit code
# 0, but output in stderr. We need to check both exit code and stderr.
output = shell.send(:powershell, command)
return output[:exitcode] == 0 && flatten_stderr(output).length == 0
end end
def upload(from, to) def upload(from, to)
@ -156,8 +165,8 @@ module VagrantPlugins
def raise_execution_error(output, opts) def raise_execution_error(output, opts)
# WinRM can return multiple stderr and stdout entries # WinRM can return multiple stderr and stdout entries
error_opts = opts.merge( error_opts = opts.merge(
stdout: output[:data].collect { |e| e[:stdout] }.join, stdout: flatten_stdout(output),
stderr: output[:data].collect { |e| e[:stderr] }.join stderr: flatten_stderr(output)
) )
# Use a different error message key if the caller gave us one, # Use a different error message key if the caller gave us one,
@ -167,6 +176,20 @@ module VagrantPlugins
# Raise the error, use the type the caller gave us or the comm default # Raise the error, use the type the caller gave us or the comm default
raise opts[:error_class], error_opts raise opts[:error_class], error_opts
end end
# TODO: Replace with WinRM Output class when WinRM 1.3 is released
def flatten_stderr(output)
output[:data].map do | line |
line[:stderr]
end.compact.join
end
def flatten_stdout(output)
output[:data].map do | line |
line[:flatten_stdout]
end.compact.join
end
end #WinRM class end #WinRM class
end end
end end

View File

@ -75,15 +75,23 @@ describe VagrantPlugins::CommunicatorWinRM::Communicator do
describe ".test" do describe ".test" do
it "returns true when exit code is zero" do it "returns true when exit code is zero" do
expect(shell).to receive(:powershell).with(kind_of(String)).and_return({ exitcode: 0 }) output = { exitcode: 0, data:[{ stderr: '' }] }
expect(shell).to receive(:powershell).with(kind_of(String)).and_return(output)
expect(subject.test("test -d c:/windows")).to be_true expect(subject.test("test -d c:/windows")).to be_true
end end
it "returns false when exit code is non-zero" do it "returns false when exit code is non-zero" do
expect(shell).to receive(:powershell).with(kind_of(String)).and_return({ exitcode: 1 }) output = { exitcode: 1, data:[{ stderr: '' }] }
expect(shell).to receive(:powershell).with(kind_of(String)).and_return(output)
expect(subject.test("test -d /tmp/foobar")).to be_false expect(subject.test("test -d /tmp/foobar")).to be_false
end end
it "returns false when stderr contains output" do
output = { exitcode: 0, data:[{ stderr: 'this is an error' }] }
expect(shell).to receive(:powershell).with(kind_of(String)).and_return(output)
expect(subject.test("[-x stuff] && foo")).to be_false
end
it "returns false when command is testing for linux OS" do it "returns false when command is testing for linux OS" do
expect(subject.test("uname -s | grep Debian")).to be_false expect(subject.test("uname -s | grep Debian")).to be_false
end end