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 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162
|
#!/usr/bin/env ruby
require File.dirname(__FILE__) + '/../../spec_helper'
describe Puppet::Type.type(:exec) do
def create_resource(command, output, exitstatus, returns = 0)
@user_name = 'some_user_name'
@group_name = 'some_group_name'
Puppet.features.stubs(:root?).returns(true)
@execer = Puppet::Type.type(:exec).new(:name => command, :path => @example_path, :user => @user_name, :group => @group_name, :returns => returns)
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, returns = 0)
create_resource(command, output, exitstatus, returns)
@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
before do
@executable = Puppet.features.posix? ? '/bin/true' : 'C:/Program Files/something.exe'
@command = Puppet.features.posix? ? '/bin/true whatever' : '"C:/Program Files/something.exe" whatever'
File.stubs(:exists?).returns false
File.stubs(:exists?).with(@executable).returns true
@example_path = Puppet.features.posix? ? %w{/usr/bin /bin} : [ "C:/Program Files/something/bin", "C:/Ruby/bin" ]
File.stubs(:exists?).with(File.join(@example_path[0],"true")).returns true
File.stubs(:exists?).with(File.join(@example_path[0],"false")).returns true
end
it "should return :executed_command as its event" do
resource = Puppet::Type.type(:exec).new :command => @command
resource.parameter(:returns).event.name.should == :executed_command
end
describe "when execing" do
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 not report a failure if the exit status is specified in a returns array" do
command = "false"
create_resource(command, "", 1, [0,1])
proc { @execer.refresh }.should_not raise_error(Puppet::Error)
end
it "should report a failure if the exit status is not specified in a returns array" do
command = "false"
create_resource(command, "", 1, [0,100])
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 "when logoutput=>on_failure is set" do
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 "should log the output on failure when returns is specified as an array" do
#Puppet::Util::Log.newdestination :console
command = "false"
output = "output1\noutput2\n"
create_logging_resource(command, output, 1, :on_failure, :err, [0, 100])
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
it "shouldn't log the output on success when non-zero exit status is in a returns array" do
#Puppet::Util::Log.newdestination :console
command = "true"
output = "output1\noutput2\n"
create_logging_resource(command, output, 100, :on_failure, :err, [1,100])
@execer.property(:returns).expects(:err).never
@execer.refresh
end
describe " when multiple tries are set," do
it "should repeat the command attempt 'tries' times on failure and produce an error" do
Puppet.features.stubs(:root?).returns(true)
command = "false"
user = "user"
group = "group"
tries = 5
retry_exec = Puppet::Type.type(:exec).new(:name => command, :path => %w{/usr/bin /bin}, :user => user, :group => group, :returns => 0, :tries => tries, :try_sleep => 0)
status = stub "process"
status.stubs(:exitstatus).returns(1)
Puppet::Util::SUIDManager.expects(:run_and_capture).with([command], user, group).times(tries).returns(["", status])
proc { retry_exec.refresh }.should raise_error(Puppet::Error)
end
end
it "should be able to autorequire files mentioned in the command" do
catalog = Puppet::Resource::Catalog.new
catalog.add_resource Puppet::Type.type(:file).new(:name => @executable)
@execer = Puppet::Type.type(:exec).new(:name => @command)
catalog.add_resource @execer
rels = @execer.autorequire
rels[0].should be_instance_of(Puppet::Relationship)
rels[0].target.should equal(@execer)
end
end
|