Merge pull request #4094 from mitchellh/improved-winrm-command-failure-messaging

provisioners/winrm: Better WinRM command failure messaging
This commit is contained in:
Mitchell Hashimoto 2014-08-06 10:44:35 -07:00
commit 94841ef1bc
4 changed files with 33 additions and 8 deletions

View File

@ -60,8 +60,8 @@ module VagrantPlugins
command: command, command: command,
elevated: false, elevated: false,
error_check: true, error_check: true,
error_class: Errors::ExecutionError, error_class: Errors::WinRMBadExitStatus,
error_key: :execution_error, error_key: nil, # use the error_class message key
good_exit: 0, good_exit: 0,
shell: :powershell, shell: :powershell,
}.merge(opts || {}) }.merge(opts || {})
@ -154,10 +154,17 @@ module VagrantPlugins
end end
def raise_execution_error(output, opts) def raise_execution_error(output, opts)
# The error classes expect the translation key to be _key, but that makes for an ugly # WinRM can return multiple stderr and stdout entries
# configuration parameter, so we set it here from `error_key` error_opts = opts.merge(
msg = "Command execution failed with an exit code of #{output[:exitcode]}" stdout: output[:data].collect { |e| e[:stdout] }.join,
error_opts = opts.merge(_key: opts[:error_key], message: msg) stderr: output[:data].collect { |e| e[:stderr] }.join
)
# Use a different error message key if the caller gave us one,
# otherwise use the error's default message
error_opts[:_key] = opts[:error_key] if opts[:error_key]
# 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
end #WinRM class end #WinRM class

View File

@ -18,6 +18,10 @@ module VagrantPlugins
error_key(:invalid_shell) error_key(:invalid_shell)
end end
class WinRMBadExitStatus < WinRMError
error_key(:winrm_bad_exit_status)
end
class WinRMNotReady < WinRMError class WinRMNotReady < WinRMError
error_key(:winrm_not_ready) error_key(:winrm_not_ready)
end end

View File

@ -8,6 +8,19 @@ en:
Password: %{password} Password: %{password}
Endpoint: %{endpoint} Endpoint: %{endpoint}
Message: %{message} Message: %{message}
winrm_bad_exit_status: |-
The following WinRM command responded with a non-zero exit status.
Vagrant assumes that this means the command failed!
%{command}
Stdout from the command:
%{stdout}
Stderr from the command:
%{stderr}
execution_error: |- execution_error: |-
An error occurred executing a remote WinRM command. An error occurred executing a remote WinRM command.

View File

@ -61,9 +61,10 @@ describe VagrantPlugins::CommunicatorWinRM::Communicator do
end end
it "raises error when error_check is true and exit code is non-zero" do it "raises error when error_check is true and exit code is non-zero" do
expect(shell).to receive(:powershell).with(kind_of(String)).and_return({ exitcode: 1 }) expect(shell).to receive(:powershell).with(kind_of(String)).and_return(
{ exitcode: 1, data: [{ stdout: '', stderr: '' }] })
expect { subject.execute("dir") }.to raise_error( expect { subject.execute("dir") }.to raise_error(
VagrantPlugins::CommunicatorWinRM::Errors::ExecutionError) VagrantPlugins::CommunicatorWinRM::Errors::WinRMBadExitStatus)
end end
it "does not raise error when error_check is false and exit code is non-zero" do it "does not raise error when error_check is false and exit code is non-zero" do