1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91
|
#!/usr/bin/env ruby
require File.dirname(__FILE__) + '/../../spec_helper'
module ExecModuleTesting
def create_resource(command, output, exitstatus)
@user_name = 'some_user_name'
@group_name = 'some_group_name'
Puppet.features.stubs(:root?).returns(true)
@execer = Puppet::Type.type(:exec).create(:name => command, :path => %w{/usr/bin /bin}, :user => @user_name, :group => @group_name)
status = stub "process"
status.stubs(:exitstatus).returns(exitstatus)
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.type(:exec), " when execing" do
include ExecModuleTesting
it "should use the 'run_and_capture' method to exec" do
command = "true"
create_resource(command, "", 0)
@execer.refresh.should == :executed_command
end
it "should report a failure" do
command = "false"
create_resource(command, "", 1)
proc { @execer.refresh }.should raise_error(Puppet::Error)
end
it "should log the output on success" do
#Puppet::Util::Log.newdestination :console
command = "false"
output = "output1\noutput2\n"
create_logging_resource(command, output, 0, true, :err)
expect_output(output, :err)
@execer.refresh
end
it "should log the output on failure" do
#Puppet::Util::Log.newdestination :console
command = "false"
output = "output1\noutput2\n"
create_logging_resource(command, output, 1, true, :err)
expect_output(output, :err)
proc { @execer.refresh }.should raise_error(Puppet::Error)
end
end
describe Puppet::Type.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 = "false"
output = "output1\noutput2\n"
create_logging_resource(command, output, 1, :on_failure, :err)
expect_output(output, :err)
proc { @execer.refresh }.should raise_error(Puppet::Error)
end
it "shouldn't log the output on success" do
#Puppet::Util::Log.newdestination :console
command = "true"
output = "output1\noutput2\n"
create_logging_resource(command, output, 0, :on_failure, :err)
@execer.property(:returns).expects(:err).never
@execer.refresh
end
end
|