[Zope-Checkins] CVS: Zope/lib/python/zdaemon/tests - nokill.py:1.1.2.1 testzdaemon.py:1.5.2.1 testDaemon.py:1.3.26.1
Andreas Jung
andreas@andreas-jung.com
Sat, 16 Nov 2002 11:40:47 -0500
Update of /cvs-repository/Zope/lib/python/zdaemon/tests
In directory cvs.zope.org:/tmp/cvs-serv25283/zdaemon/tests
Modified Files:
Tag: ajung-restructuredtext-integration-branch
testDaemon.py
Added Files:
Tag: ajung-restructuredtext-integration-branch
nokill.py testzdaemon.py
Log Message:
merge from trunk
=== Added File Zope/lib/python/zdaemon/tests/nokill.py ===
#! /usr/bin/env python
import signal
signal.signal(signal.SIGTERM, signal.SIG_IGN)
while 1:
signal.pause()
=== Added File Zope/lib/python/zdaemon/tests/testzdaemon.py ===
"""Test suite for zdaemon.py (the program)."""
import os
import sys
import time
import signal
import tempfile
import unittest
from StringIO import StringIO
from zdaemon import zdaemon
class ZDaemonTests(unittest.TestCase):
python = os.path.abspath(sys.executable)
assert os.path.exists(python)
here = os.path.abspath(os.path.dirname(__file__))
assert os.path.isdir(here)
nokill = os.path.join(here, "nokill.py")
assert os.path.exists(nokill)
parent = os.path.dirname(here)
zdaemon = os.path.join(parent, "zdaemon.py")
assert os.path.exists(zdaemon)
ppath = os.pathsep.join(sys.path)
def setUp(self):
self.zdsock = tempfile.mktemp()
self.new_stdout = StringIO()
self.save_stdout = sys.stdout
sys.stdout = self.new_stdout
self.expect = ""
def tearDown(self):
sys.stdout = self.save_stdout
for sig in (signal.SIGTERM,
signal.SIGHUP,
signal.SIGINT,
signal.SIGCHLD):
signal.signal(sig, signal.SIG_DFL)
try:
os.unlink(self.zdsock)
except os.error:
pass
output = self.new_stdout.getvalue()
self.assertEqual(self.expect, output)
def rundaemon(self, args):
if type(args) is type([]):
args = " ".join(args)
cmd = ("PYTHONPATH=%s %s %s -d -s %s %s" %
(self.ppath, self.python, self.zdaemon, self.zdsock, args))
os.system(cmd)
# When the daemon crashes, the following may help debug it:
##os.system("PYTHONPATH=%s %s %s -s %s %s &" %
## (self.ppath, self.python, self.zdaemon, self.zdsock, args))
def run(self, args):
if type(args) is type(""):
args = args.split()
d = zdaemon.Daemonizer()
try:
d.main(["-s", self.zdsock] + args)
except SystemExit:
pass
def testSystem(self):
self.rundaemon("echo -n")
self.expect = ""
def testInvoke(self):
self.run("echo -n")
self.expect = ""
def testControl(self):
self.rundaemon("sleep 1000")
time.sleep(1)
self.run("-c stop")
time.sleep(1)
self.run("-c exit")
self.expect = "Sent SIGTERM\nExiting now\n"
def testStop(self):
self.rundaemon([self.python, self.nokill])
time.sleep(1)
self.run("-c stop")
time.sleep(1)
self.run("-c exit")
self.expect = "Sent SIGTERM\nSent SIGTERM; will exit later\n"
def testHelp(self):
self.run("-h")
self.expect = zdaemon.__doc__
def testOptionsSysArgv(self):
# Check that options are parsed from sys.argv by default
save_sys_argv = sys.argv
try:
sys.argv = ["A", "-c", "B", "C"]
opts = zdaemon.Options()
finally:
sys.argv = save_sys_argv
self.assertEqual(opts.opts, [("-c", "")])
self.assertEqual(opts.isclient, 1)
self.assertEqual(opts.args, ["B", "C"])
def testOptionsBasic(self):
# Check basic option parsing
opts = zdaemon.Options(["-c", "B", "C"], "foo")
self.assertEqual(opts.opts, [("-c", "")])
self.assertEqual(opts.isclient, 1)
self.assertEqual(opts.args, ["B", "C"])
self.assertEqual(opts.progname, "foo")
def testOptionsHelp(self):
# Check that -h behaves properly
try:
zdaemon.Options(["-h"])
except SystemExit, err:
self.failIf(err.code)
else:
self.fail("SystemExit expected")
self.expect = zdaemon.__doc__
def testOptionsError(self):
# Check that we get an error for an unrecognized option
save_sys_stderr = sys.stderr
try:
sys.stderr = StringIO()
try:
zdaemon.Options(["-/"])
except SystemExit, err:
self.assertEqual(err.code, 2)
else:
self.fail("SystemExit expected")
finally:
sys.stderr = save_sys_stderr
def testSubprocessBasic(self):
# Check basic subprocess management: spawn, kill, wait
opts = zdaemon.Options([])
proc = zdaemon.Subprocess(opts, ["sleep", "100"])
self.assertEqual(proc.pid, 0)
pid = proc.spawn()
self.assertEqual(proc.pid, pid)
msg = proc.kill(signal.SIGTERM)
self.assertEqual(msg, None)
wpid, wsts = os.waitpid(pid, 0)
self.assertEqual(wpid, pid)
self.assertEqual(os.WIFSIGNALED(wsts), 1)
self.assertEqual(os.WTERMSIG(wsts), signal.SIGTERM)
proc.setstatus(wsts)
self.assertEqual(proc.pid, 0)
def test_suite():
suite = unittest.TestSuite()
if os.name == "posix":
suite.addTest(unittest.makeSuite(ZDaemonTests))
return suite
if __name__ == '__main__':
__file__ = sys.argv[0]
unittest.main(defaultTest='test_suite')
=== Zope/lib/python/zdaemon/tests/testDaemon.py 1.3 => 1.3.26.1 ===
--- Zope/lib/python/zdaemon/tests/testDaemon.py:1.3 Wed Aug 14 18:12:52 2002
+++ Zope/lib/python/zdaemon/tests/testDaemon.py Sat Nov 16 11:40:46 2002
@@ -25,14 +25,13 @@
import zdaemon
import zdaemon.tests
import zdaemon.Daemon
-import zdaemon.ZDaemonLogging
import zLOG
class TestDoneError(RuntimeError):
pass
def pstamp(message, sev):
- zdaemon.ZDaemonLogging.pstamp(message, sev)
+ zLOG.LOG("zdaemon", sev, message)
if sev >= zLOG.ERROR:
raise TestDoneError(message)