diff options
-rwxr-xr-x | lib/puppet/type/exec.rb | 16 | ||||
-rwxr-xr-x | spec/unit/ral/exec.rb | 64 |
2 files changed, 64 insertions, 16 deletions
diff --git a/lib/puppet/type/exec.rb b/lib/puppet/type/exec.rb index b76426495..5bb3158c4 100755 --- a/lib/puppet/type/exec.rb +++ b/lib/puppet/type/exec.rb @@ -114,8 +114,15 @@ module Puppet end if log = @resource[:logoutput] - if log == :true + case log + when :true log = @resource[:loglevel] + when :on_failure + if status.exitstatus.to_s != self.should.to_s + log = @resource[:loglevel] + else + log = :false + end end unless log == :false @output.split(/\n/).each { |line| @@ -200,10 +207,11 @@ module Puppet newparam(:logoutput) do desc "Whether to log output. Defaults to logging output at the - loglevel for the ``exec`` resource. Values are **true**, *false*, - and any legal log level." + loglevel for the ``exec`` resource. Use *on_failure* to only + log the output when the command reports an error. Values are + **true**, *false*, *on_failure*, and any legal log level." - values = [:true, :false] + values = [:true, :false, :on_failure] # And all of the log levels Puppet::Util::Log.eachlevel { |level| values << level } newvalues(*values) diff --git a/spec/unit/ral/exec.rb b/spec/unit/ral/exec.rb index b45b3e70b..b465123f8 100755 --- a/spec/unit/ral/exec.rb +++ b/spec/unit/ral/exec.rb @@ -16,6 +16,18 @@ module ExecModuleTesting Puppet::Util::SUIDManager.expects(:run_and_capture).with([command], @user_name, @group_name).returns([output, status]) end + + def create_logging_resource(command, output, exitstatus, logoutput, loglevel) + create_resource(command, output, exitstatus) + @execer[:logoutput] = logoutput + @execer[:loglevel] = loglevel + end + + def expect_output(output, loglevel) + output.split(/\n/).each do |line| + @execer.property(:returns).expects(loglevel).with(line) + end + end end describe Puppet::Type::Exec, " when execing" do @@ -48,13 +60,8 @@ describe Puppet::Type::Exec, " when execing" do #Puppet::Util::Log.newdestination :console command = "/bin/false" output = "output1\noutput2\n" - create_resource(command, output, 0) - @execer[:logoutput] = true - @execer[:loglevel] = :err - output.split(/\n/).each do |line| - @execer.property(:returns).expects(:err).with(line) - end - + create_logging_resource(command, output, 0, true, :err) + expect_output(output, :err) @execer.refresh end @@ -62,12 +69,33 @@ describe Puppet::Type::Exec, " when execing" do #Puppet::Util::Log.newdestination :console command = "/bin/false" output = "output1\noutput2\n" - create_resource(command, output, 1) - @execer[:logoutput] = true - @execer[:loglevel] = :err - output.split(/\n/).each do |line| - @execer.property(:returns).expects(:err).with(line) + create_logging_resource(command, output, 1, true, :err) + expect_output(output, :err) + + # no idea, why should raise_error doesn't work here + raised = false + begin + @execer.refresh.should raise_error(Puppet::Error) + rescue Puppet::Error + raised = true + end + unless raised + raise "didn't receive Puppet::Error" end + end + +end + + +describe Puppet::Type::Exec, " when logoutput=>on_failure is set," do + include ExecModuleTesting + + it "should log the output on failure" do + #Puppet::Util::Log.newdestination :console + command = "/bin/false" + output = "output1\noutput2\n" + create_logging_resource(command, output, 1, :on_failure, :err) + expect_output(output, :err) # no idea, why should raise_error doesn't work here raised = false @@ -80,5 +108,17 @@ describe Puppet::Type::Exec, " when execing" do raise "didn't receive Puppet::Error" end end + + it "shouldn't log the output on success" do + #Puppet::Util::Log.newdestination :console + command = "/bin/true" + output = "output1\noutput2\n" + create_logging_resource(command, output, 0, :on_failure, :err) + @execer.property(:returns).stubs(:err) do + fail "logging, where non was expected" + end + @execer.refresh + end + end |