Skip to content

Commit

Permalink
Merge pull request sous-chefs#1114 from chef-cookbooks/Kapil/Github-1…
Browse files Browse the repository at this point in the history
…081_docker_exec_does_not_check_the_return_code_of_the_command_it_runs

Fix for docker_exec does not check the return code of the command it runs
  • Loading branch information
tas50 authored Jun 3, 2020
2 parents a1de487 + 4e2e46d commit 6bfb235
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 2 deletions.
9 changes: 8 additions & 1 deletion libraries/docker_exec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,20 @@ class DockerExec < DockerBase
property :container, String
property :timeout, Numeric, default: 60, desired_state: false
property :container_obj, Docker::Container, desired_state: false
property :returns, [ Integer, Array ], coerce: proc { |v| Array(v) }, default: [0],
description: 'The return value for a command. This may be an array of accepted values. An exception is raised when the return value(s) do not match.'

alias_method :cmd, :command

action :run do
converge_by "executing #{new_resource.command} on #{new_resource.container}" do
with_retries { new_resource.container_obj Docker::Container.get(new_resource.container, {}, connection) }
new_resource.container_obj.exec(new_resource.command, wait: new_resource.timeout)
stdout, stderr, exit_code = new_resource.container_obj.exec(new_resource.command, wait: new_resource.timeout)
Chef::Log.trace(stdout)
Chef::Log.trace(stderr)
unless new_resource.returns.include?(exit_code)
raise "Expected process to exit with 0, but received #{exit_code}"
end
end
end
end
Expand Down
13 changes: 13 additions & 0 deletions spec/docker_test/exec_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,19 @@
)
end

context 'testing default properties' do
it 'docker_exec[default]' do
expect(chef_run).to run_docker_exec('default').with(
host: nil,
command: nil,
container: nil,
timeout: 60,
container_obj: nil,
returns: [0]
)
end
end

context 'testing run action' do
it 'run docker_exec[touch_it]' do
expect(chef_run).to run_docker_exec('touch_it').with(
Expand Down
2 changes: 1 addition & 1 deletion spec/docker_test/installation_package_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@

context 'testing default action, default properties' do
it 'installs docker' do
expect(chef_run).to create_docker_installation_package('default').with(version: '19.03.5')
expect(chef_run).to create_docker_installation_package('default').with(version: '19.03.8')
end
end

Expand Down
2 changes: 2 additions & 0 deletions test/cookbooks/docker_test/recipes/exec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -23,3 +23,5 @@
end

file '/marker_busybox_exec_twofile'

docker_exec 'default'

0 comments on commit 6bfb235

Please sign in to comment.