Refactor #8475
Add test coverage for ssh_remote_execute method
| Status: | Needs Decision | Start date: | 07/18/2011 | |
|---|---|---|---|---|
| Priority: | Normal | Due date: | 07/20/2011 | |
| Assignee: | % Done: | 0% | ||
| Category: | cloudpack | Estimated time: | 1.00 hour | |
| Target version: | - | Spent time: | 0.75 hour | |
| Keywords: | Branch: | |||
History
#1
Updated by Jeff McCune almost 2 years ago
- Status changed from Accepted to Needs Decision
Need Decision¶
The ssh_remote_execute method is a thin wrapper to Net::SSH.start which is difficult to mock.
Reading online, I’m not sure if we should be mocking API’s we don’t own. The recommendation seems to be implement a thin wrapper and mock that wrapper, which we’ve already done.
I couldn’t quickly figure out how to add coverage for this code:
# This is the single place to make SSH calls. It will handle collecting STDOUT
# in a line oriented manner, printing it to debug log destination and checking the
# exit code of the remote call. This should also make it much easier to do unit testing on
# all of the other methods that need this functionality. Finally, it should provide
# one place to swap out the back end SSH implementation if need be.
def ssh_remote_execute(server, login, command, keyfile = nil)
Puppet.info "Executing remote command ..."
Puppet.debug "Command: #{command}"
buffer = String.new
exit_code = nil
Net::SSH.start(server, login, :keys => [ keyfile ]) do |session|
session.open_channel do |channel|
channel.on_data do |ch, data|
buffer << data
if buffer =~ /\n/
lines = buffer.split("\n")
buffer = lines.length > 1 ? lines.pop : String.new
lines.each do |line|
Puppet.debug(line)
end
end
end
channel.on_eof do |ch|
# Display anything remaining in the buffer
unless buffer.empty?
Puppet.debug(buffer)
end
end
channel.on_request("exit-status") do |ch, data|
exit_code = data.read_long
Puppet.debug("SSH Command Exit Code: #{exit_code}")
end
# Finally execute the command
channel.exec(command)
end
end
Puppet.info "Executing remote command ... Done"
exit_code
end
#2
Updated by James Turnbull almost 2 years ago
- Target version deleted (
0.6.0)