]> review.fuel-infra Code Review - openstack-build/heat-build.git/commitdiff
Implement runas for cfn-hup
authorTomas Sedovic <tomas@sedovic.cz>
Thu, 26 Apr 2012 15:27:25 +0000 (17:27 +0200)
committerTomas Sedovic <tomas@sedovic.cz>
Thu, 26 Apr 2012 15:27:25 +0000 (17:27 +0200)
As a side effect, this fixes #109. By running everything through `su -c`, all
the piping and redirection issues are outsourced there.

heat/cfntools/cfn_helper.py
heat/tests/test_cfn.py

index 3b6e491a452438171036b5817de3d6614ba3366b..637cdab3e76e80cb4e779cc37fb6b1f9f7efd664 100644 (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()
index 8f6d5237223eb2937ee0d686c3f2b340f28ca949..9e0ee49ac189b0859991d3fe6a090d011f2e7dce 100644 (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()