Add a test case for #22.

This commit is contained in:
Ilya Otyutskiy
2014-05-28 21:51:57 +04:00
parent 0abd31c147
commit 0a627b1b68
2 changed files with 51 additions and 0 deletions

View File

@@ -0,0 +1,29 @@
import logging
from sys import argv
from daemonize import Daemonize
pid = argv[1]
log = argv[2]
logger = logging.getLogger(__name__)
logger.setLevel(logging.DEBUG)
logger.propagate = False
fh = logging.FileHandler(log, "w")
fh.setLevel(logging.DEBUG)
logger.addHandler(fh)
keep_fds = [fh.stream.fileno()]
def privileged_action():
logger.info("Privileged action.")
def action():
logger.info("Action.")
daemon = Daemonize(app="issue-22", pid=pid, action=action,
privileged_action=privileged_action, logger=logger, keep_fds=keep_fds)
daemon.start()

View File

@@ -109,5 +109,27 @@ class UidGidTest(unittest.TestCase):
with open(self.logfile, "r") as f:
self.assertEqual(f.read(), self.expected)
class PrivilegedActionTest(unittest.TestCase):
def setUp(self):
self.correct_log = """Privileged action.
Starting daemon.
Action.
Stopping daemon.
"""
self.pidfile = mkstemp()[1]
self.logfile = mkstemp()[1]
os.system("python tests/daemon_privileged_action.py %s %s" % (self.pidfile, self.logfile))
sleep(.1)
def tearDown(self):
os.system("kill `cat %s`" % self.pidfile)
sleep(.1)
def test_privileged_action(self):
sleep(5)
with open(self.logfile, "r") as contents:
self.assertEqual(contents.read(), self.correct_log)
if __name__ == '__main__':
unittest.main()