Download Install Tutorial Docs FAQ Tools WikiLicense Team IRC Planet Involvement Shop Book

root/branches/cherrypy-2.x/cherrypy/test/modpy.py

Revision 1567 (checked in by fumanchu, 2 years ago)

2.x backports: [1389] Fix for #481 (buildbot quiet mode). Use test.py --dumb to suppress the interactive test output features, as well as the "hit enter" prompt at the end. Also [1392] Final fix for #481 (buildbot). The test suite now exits with a non-zero code if any of the tests fail.

  • Property svn:eol-style set to native
Line 
1 """Wrapper for mod_python, for use as a CherryPy HTTP server.
2
3 To autostart modpython, the "apache" executable or script must be
4 on your system path, or you must override the global APACHE_PATH.
5 On some platforms, "apache" may be called "apachectl" or "apache2ctl"--
6 create a symlink to them if needed.
7
8 You also need the 'modpython_gateway' module at:
9 http://projects.amor.org/misc/wiki/ModPythonGateway
10
11
12 KNOWN BUGS
13 ==========
14
15 1. Apache processes Range headers automatically; CherryPy's truncated
16     output is then truncated again by Apache. See test_core.testRanges.
17     This was worked around in http://www.cherrypy.org/changeset/1319.
18 2. Apache does not allow custom HTTP methods like CONNECT as per the spec.
19     See test_core.testHTTPMethods.
20 3. Max request header and body settings do not work with Apache.
21 4. Apache replaces status "reason phrases" automatically. For example,
22     CherryPy may set "304 Not modified" but Apache will write out
23     "304 Not Modified" (capital "M").
24 5. Apache does not allow custom error codes as per the spec.
25 6. Apache (or perhaps modpython, or modpython_gateway) unquotes %xx in the
26     Request-URI too early.
27 """
28
29 import os
30 curdir = os.path.join(os.getcwd(), os.path.dirname(__file__))
31 import re
32 import time
33
34 import test
35
36
37 def read_process(cmd, args=""):
38     pipein, pipeout = os.popen4("%s %s" % (cmd, args))
39     try:
40         firstline = pipeout.readline()
41         if (re.search(r"(not recognized|No such file|not found)", firstline,
42                       re.IGNORECASE)):
43             raise IOError('%s must be on your system path.' % cmd)
44         output = firstline + pipeout.read()
45     finally:
46         pipeout.close()
47     return output
48
49
50 APACHE_PATH = "apache"
51 CONF_PATH = "test_mp.conf"
52 ready = False
53 interrupt = None
54
55 conf_template = """
56 # Apache2 server configuration file for testing CherryPy with mod_python.
57
58 DocumentRoot "/"
59 Listen %s
60 LoadModule python_module modules/mod_python.so
61
62 SetHandler python-program
63 PythonFixupHandler cherrypy.test.modpy::handler
64 PythonOption testmod %s
65 PythonHandler modpython_gateway::handler
66 PythonOption wsgi.application cherrypy._cpwsgi::wsgiApp
67 PythonDebug On
68 """
69
70 def start(testmod, port):
71     mpconf = CONF_PATH
72     if not os.path.isabs(mpconf):
73         mpconf = os.path.join(curdir, mpconf)
74    
75     f = open(mpconf, 'wb')
76     try:
77         f.write(conf_template % (port, testmod))
78     finally:
79         f.close()
80    
81     result = read_process(APACHE_PATH, "-k start -f %s" % mpconf)
82     if result:
83         print result
84
85 def stop():
86     """Gracefully shutdown a server that is serving forever."""
87     read_process(APACHE_PATH, "-k stop")
88
89
90 loaded = False
91
92 def handler(req):
93     global loaded
94     if not loaded:
95         loaded = True
96         options = req.get_options()
97         testmod = options.get('testmod')
98         m = __import__(('cherrypy.test.%s' % testmod), globals(), locals(), [''])
99         import cherrypy
100         cherrypy.config.update({
101             "server.log_file": os.path.join(curdir, "test.log"),
102             "server.environment": "production",
103             })
104         m.setup_server()
105         cherrypy.server.start(init_only=True, server_class=None, server=None)
106     from mod_python import apache
107     return apache.OK
108
109
110 class ModPythonTestHarness(test.TestHarness):
111     """TestHarness for ModPython and CherryPy."""
112    
113     def _run(self, conf):
114         import webtest
115         webtest.WebCase.PORT = self.port
116         webtest.WebCase.interactive = self.interactive
117         print
118         print "Running tests:", self.server
119        
120         # mod_python, since it runs in the Apache process, must be
121         # started separately for each test, and then *that* process
122         # must run the setup_server() function for the test.
123         # Then our process can run the actual test.
124         test_success = True
125         for testmod in self.tests:
126             try:
127                 start(testmod, self.port)
128                 suite = webtest.ReloadingTestLoader().loadTestsFromName(testmod)
129                 result = webtest.TerseTestRunner(verbosity=2).run(suite)
130                 test_success &= result.wasSuccessful()
131             finally:
132                 stop()
133         if test_success:
134             return 0
135         else:
136             return 1
137
138
Note: See TracBrowser for help on using the browser.

Hosted by WebFaction

Log in as guest/cpguest to create tickets