Improve `sh' task action error handling:
Handle exit status code in Remote#execute.
This commit is contained in:
parent
f10914c7d7
commit
825bdec74d
@ -24,3 +24,16 @@ Feature: sh task action
|
||||
"""
|
||||
When I execute the recipe
|
||||
Then the output must contain "from remote"
|
||||
|
||||
Scenario: aborts on failed command execution
|
||||
Given a recipe with:
|
||||
"""
|
||||
target 'some_host.test'
|
||||
|
||||
task :some_task do
|
||||
sh '\false'
|
||||
sh '\echo after_fail'
|
||||
end
|
||||
"""
|
||||
When I execute the recipe
|
||||
Then the output must not contain "after_fail"
|
||||
|
@ -4,5 +4,6 @@ module Producer
|
||||
ConditionNotMetError = Class.new(Error)
|
||||
RecipeEvaluationError = Class.new(StandardError)
|
||||
TaskEvaluationError = Class.new(RecipeEvaluationError)
|
||||
RemoteCommandExecutionError = Class.new(Error)
|
||||
end
|
||||
end
|
||||
|
@ -16,9 +16,18 @@ module Producer
|
||||
|
||||
def execute(command)
|
||||
output = ''
|
||||
session.exec command do |ch, stream, data|
|
||||
session.open_channel do |channel|
|
||||
channel.exec command do |ch, success|
|
||||
ch.on_data do |c, data|
|
||||
output << data
|
||||
end
|
||||
|
||||
ch.on_request('exit-status') do |c, data|
|
||||
exit_status = data.read_long
|
||||
raise RemoteCommandExecutionError if exit_status != 0
|
||||
end
|
||||
end
|
||||
end
|
||||
session.loop
|
||||
output
|
||||
end
|
||||
|
@ -49,6 +49,16 @@ module Producer::Core
|
||||
end
|
||||
expect(remote.execute(command)).to eq arguments
|
||||
end
|
||||
|
||||
it 'raises an exception when the exit status code is not 0' do
|
||||
story_with_new_channel do |ch|
|
||||
ch.sends_exec command
|
||||
ch.gets_data arguments
|
||||
ch.gets_exit_status 1
|
||||
end
|
||||
expect { remote.execute(command) }
|
||||
.to raise_error(RemoteCommandExecutionError)
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
|
Loading…
x
Reference in New Issue
Block a user