summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorEric Saxby <sax@livinginthepast.org>2014-10-01 14:00:37 -0700
committerEric Saxby <sax@livinginthepast.org>2014-10-01 14:00:37 -0700
commitd34d1a91a429ef460847e584f311a798727cc30b (patch)
tree109580acf31d8744d279b753945affc4b5b09e88
parent601481ebc362eaf9418cbd12eef38952404c7675 (diff)
downloadchef-d34d1a91a429ef460847e584f311a798727cc30b.tar.gz
Log output of crontab -l when non-zero exit status
When reading a unix crontab and we receive a non-zero exit status, log the stdout and stderr to debug. We expect exit status 1 to happen when the user does not have a crontab, but let's log anyways for help in troubleshooting unexpected situations.
-rw-r--r--lib/chef/provider/cron/unix.rb2
-rw-r--r--spec/unit/provider/cron/unix_spec.rb12
2 files changed, 14 insertions, 0 deletions
diff --git a/lib/chef/provider/cron/unix.rb b/lib/chef/provider/cron/unix.rb
index 8e81ff6706..350f8bda18 100644
--- a/lib/chef/provider/cron/unix.rb
+++ b/lib/chef/provider/cron/unix.rb
@@ -33,6 +33,8 @@ class Chef
crontab = shell_out('/usr/bin/crontab -l', :user => @new_resource.user)
status = crontab.status.exitstatus
+ Chef::Log.debug crontab.format_for_exception if status > 0
+
if status > 1
raise Chef::Exceptions::Cron, "Error determining state of #{@new_resource.name}, exit: #{status}"
end
diff --git a/spec/unit/provider/cron/unix_spec.rb b/spec/unit/provider/cron/unix_spec.rb
index 6562890b4d..3d7a5675fc 100644
--- a/spec/unit/provider/cron/unix_spec.rb
+++ b/spec/unit/provider/cron/unix_spec.rb
@@ -59,6 +59,8 @@ describe Chef::Provider::Cron::Unix do
end
before do
+ allow(Chef::Log).to receive(:debug)
+ allow(shell_out).to receive(:format_for_exception).and_return("formatted command output")
allow(provider).to receive(:shell_out).with('/usr/bin/crontab -l', :user => username).and_return(shell_out)
end
@@ -78,6 +80,11 @@ describe Chef::Provider::Cron::Unix do
it "should return nil if the user has no crontab" do
expect(provider.send(:read_crontab)).to be_nil
end
+
+ it "logs the crontab output to debug" do
+ provider.send(:read_crontab)
+ expect(Chef::Log).to have_received(:debug).with("formatted command output")
+ end
end
context "when any other error occurs" do
@@ -88,6 +95,11 @@ describe Chef::Provider::Cron::Unix do
provider.send(:read_crontab)
}.to raise_error(Chef::Exceptions::Cron, "Error determining state of #{new_resource.name}, exit: 2")
end
+
+ it "logs the crontab output to debug" do
+ provider.send(:read_crontab) rescue nil
+ expect(Chef::Log).to have_received(:debug).with("formatted command output")
+ end
end
end