Implement runas for cfn-hup

As a side effect, this fixes #109. By running everything through `su -c`, all
the piping and redirection issues are outsourced there.
This commit is contained in:
Tomas Sedovic 2012-04-26 17:27:25 +02:00
parent 3802dfbba6
commit 6b0d2d7827
2 changed files with 43 additions and 1 deletions

View File

@ -159,7 +159,7 @@ class CommandRunner(object):
self
"""
logging.debug("Running command: %s" % self._command)
cmd = self._command.split()
cmd = ['su', user, '-c', self._command]
subproc = subprocess.Popen(cmd, stdout=subprocess.PIPE,
stderr=subprocess.PIPE)
output = subproc.communicate()

View File

@ -6,9 +6,11 @@
import io
import sys
import mox
import nose
from nose.plugins.attrib import attr
from nose import with_setup
import unittest
import shutil
from heat.cfntools.cfn_helper import *
@ -113,6 +115,12 @@ def tearDown_metadata_files():
shutil.rmtree('/tmp/_files_test_', ignore_errors=True)
class PopenMock:
def communicate(self):
self.returncode = 0
return ['', None]
@with_setup(None, tearDown_metadata_files)
@attr(tag=['unit', 'cfn-metadata'])
@attr(speed='fast')
@ -161,6 +169,40 @@ def test_metadata_files():
assert(os.stat('/tmp/_files_test_/epel.repo').st_mode & mask == 0644)
assert(os.stat('/tmp/_files_test_/_with/some/dirs/to/make/small.conf').st_mode & mask == 0777)
class CommandRunnerTest(unittest.TestCase):
def setUp(self):
self.m = mox.Mox()
def tearDown(self):
self.m.UnsetStubs()
@attr(tag=['unit', 'cfn-helper'])
@attr(speed='fast')
def test_runas(self):
import subprocess
self.m.StubOutWithMock(subprocess, 'Popen')
subprocess.Popen(['su', 'user', '-c', 'some command'],
stdout=subprocess.PIPE,
stderr=subprocess.PIPE).AndReturn(PopenMock())
self.m.ReplayAll()
CommandRunner('some command').run('user')
self.m.VerifyAll()
@attr(tag=['unit', 'cfn-helper'])
@attr(speed='fast')
def test_default_runas(self):
import subprocess
self.m.StubOutWithMock(subprocess, 'Popen')
subprocess.Popen(['su', 'root', '-c', 'some command'],
stdout=subprocess.PIPE,
stderr=subprocess.PIPE).AndReturn(PopenMock())
self.m.ReplayAll()
CommandRunner('some command').run()
self.m.VerifyAll()
if __name__ == '__main__':
sys.argv.append(__file__)
nose.main()