diff options
| author | christian <christian@980ebf18-57e1-0310-9a29-db15c13687c0> | 2007-08-09 08:45:50 +0000 |
|---|---|---|
| committer | christian <christian@980ebf18-57e1-0310-9a29-db15c13687c0> | 2007-08-09 08:45:50 +0000 |
| commit | 3de4829cf0b607624b8a5f5f1e6055141d85af2f (patch) | |
| tree | 16cb9ef447771dace252003abba400a40d8b520d /test/puppet | |
| parent | 5a25701723431e0ebe2d7134ab65d56bee2c5244 (diff) | |
| download | puppet-3de4829cf0b607624b8a5f5f1e6055141d85af2f.tar.gz puppet-3de4829cf0b607624b8a5f5f1e6055141d85af2f.tar.xz puppet-3de4829cf0b607624b8a5f5f1e6055141d85af2f.zip | |
Refactor SUIDManager tests to run without root, change SUIDManager's behavior to not silently fail when it's not root and fix all other tests that broke as a result.
git-svn-id: https://reductivelabs.com/svn/puppet/trunk@2759 980ebf18-57e1-0310-9a29-db15c13687c0
Diffstat (limited to 'test/puppet')
| -rwxr-xr-x | test/puppet/tc_suidmanager.rb | 107 |
1 files changed, 48 insertions, 59 deletions
diff --git a/test/puppet/tc_suidmanager.rb b/test/puppet/tc_suidmanager.rb index 2a9eabe30..6db59d165 100755 --- a/test/puppet/tc_suidmanager.rb +++ b/test/puppet/tc_suidmanager.rb @@ -5,17 +5,13 @@ $:.unshift("../lib").unshift("../../lib") if __FILE__ =~ /\.rb$/ require 'puppet' require 'puppettest' require 'test/unit' +require 'mocha' class TestSUIDManager < Test::Unit::TestCase include PuppetTest def setup - if Process.uid != 0 - warn "Process tests must be run as root" - @run = false - else - @run = true - end + @user = nonrootuser super end @@ -24,6 +20,8 @@ class TestSUIDManager < Test::Unit::TestCase # SUIDManager for the UID/GID calls was causing problems due to the # modification of a closure. Should the bug rear itself again, this # test will fail. + Process.expects(:uid).times(2) + assert_nothing_raised do Puppet::Util::SUIDManager.uid Puppet::Util::SUIDManager.uid @@ -31,77 +29,68 @@ class TestSUIDManager < Test::Unit::TestCase end def test_id_set - if @run - user = nonrootuser - assert_nothing_raised do - Puppet::Util::SUIDManager.egid = user.gid - Puppet::Util::SUIDManager.euid = user.uid - end - - assert_equal(Puppet::Util::SUIDManager.euid, Process.euid) - assert_equal(Puppet::Util::SUIDManager.egid, Process.egid) - - assert_nothing_raised do - Puppet::Util::SUIDManager.euid = 0 - Puppet::Util::SUIDManager.egid = 0 - end + Process.expects(:euid=).with(@user.uid) + Process.expects(:egid=).with(@user.gid) + + assert_nothing_raised do + Puppet::Util::SUIDManager.egid = @user.gid + Puppet::Util::SUIDManager.euid = @user.uid end end def test_utiluid - user = nonrootuser.name - if @run - assert_not_equal(nil, Puppet::Util.uid(user)) - end + assert_not_equal(nil, Puppet::Util.uid(@user.name)) end def test_asuser - if @run - user = nonrootuser - uid, gid = [nil, nil] - - assert_nothing_raised do - Puppet::Util::SUIDManager.asuser(user.uid, user.gid) do - uid = Process.euid - gid = Process.egid - end - end - assert_equal(user.uid, uid) - assert_equal(user.gid, gid) - end + expects_id_set_and_revert @user.uid, @user.gid + Puppet::Util::SUIDManager.asuser @user.uid, @user.gid do end end + def test_system - # NOTE: not sure what shells this will work on.. - if @run - user = nonrootuser - status = Puppet::Util::SUIDManager.system("exit $EUID", user.uid, user.gid) - assert_equal(user.uid, status.exitstatus, "EUID does not seem to be inherited. This test consistently fails on RedHat-like machines.") - end + expects_id_set_and_revert @user.uid, @user.gid + Kernel.expects(:system).with('blah') + Puppet::Util::SUIDManager.system('blah', @user.uid, @user.gid) end def test_run_and_capture if (RUBY_VERSION <=> "1.8.4") < 0 warn "Cannot run this test on ruby < 1.8.4" else - # NOTE: because of the way that run_and_capture currently - # works, we cannot just blindly echo to stderr. This little - # hack gets around our problem, but the real problem is the - # way that run_and_capture works. - user = nil - uid = nil - if Puppet::Util::SUIDManager.uid == 0 - userobj = nonrootuser() - user = userobj.name - uid = userobj.uid - else - uid = Process.uid - end - cmd = [%{/bin/echo $EUID}] - output = Puppet::Util::SUIDManager.run_and_capture(cmd, uid)[0].chomp - assert_equal(uid.to_s, output) + Puppet::Util.expects(:execute).with( 'yay', + { :failonfail => false, + :uid => @user.uid, + :gid => @user.gid } + ).returns('output') + + + output = Puppet::Util::SUIDManager.run_and_capture 'yay', + @user.uid, + @user.gid + + assert_equal 'output', output.first + assert_kind_of Process::Status, output.last end end + + private + def expects_id_set_and_revert uid, gid + Process.expects(:uid).returns(99999) + Process.expects(:gid).returns(99998) + Process.expects(:euid).returns(99997) + Process.expects(:egid).returns(99996) + + Process.expects(:uid=).with(uid) + Process.expects(:gid=).with(gid) + Process.expects(:euid=).with(uid) + Process.expects(:egid=).with(gid) + + Process.expects(:uid=).with(99999) + Process.expects(:gid=).with(99998) + Process.expects(:euid=).with(99997) + Process.expects(:egid=).with(99996) + end end # $Id$ |
