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

root/branches/cp3-wsgi-remix/test/modpy.py

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

First inclusion of jamwt's mpcp into CP distro as _cpmodpy.py. Some tests fail, especially tutorials.

  • 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 If you wish to use the WSGI interface instead of our _cpmodpy interface,
9 you also need the 'modpython_gateway' module at:
10 http://projects.amor.org/misc/wiki/ModPythonGateway
11
12
13 KNOWN BUGS
14 ==========
15
16 1. Apache processes Range headers automatically; CherryPy's truncated
17     output is then truncated again by Apache. See test_core.testRanges.
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
53 conf_modpython_gateway = """
54 # Apache2 server conf file for testing CherryPy with modpython_gateway.
55
56 DocumentRoot "/"
57 Listen %s
58 LoadModule python_module modules/mod_python.so
59
60 SetHandler python-program
61 PythonFixupHandler cherrypy.test.modpy::wsgisetup
62 PythonOption testmod %s
63 PythonHandler modpython_gateway::handler
64 PythonOption wsgi.application cherrypy._cpwsgi::wsgiApp
65 PythonDebug On
66 """
67
68 conf_cpmodpy = """
69 # Apache2 server conf file for testing CherryPy with _cpmodpy.
70
71 DocumentRoot "/"
72 Listen %s
73 LoadModule python_module modules/mod_python.so
74
75 SetHandler python-program
76 PythonHandler cherrypy._cpmodpy::handler
77 PythonOption cherrypy.setup cherrypy.test.%s::setup_server
78 PythonDebug On
79 """
80
81 def start(testmod, port, conf_template):
82     mpconf = CONF_PATH
83     if not os.path.isabs(mpconf):
84         mpconf = os.path.join(curdir, mpconf)
85    
86     f = open(mpconf, 'wb')
87     try:
88         f.write(conf_template % (port, testmod))
89     finally:
90         f.close()
91    
92     result = read_process(APACHE_PATH, "-k start -f %s" % mpconf)
93     if result:
94         print result
95
96 def stop():
97     """Gracefully shutdown a server that is serving forever."""
98     read_process(APACHE_PATH, "-k stop")
99
100
101 loaded = False
102 def wsgisetup(req):
103     global loaded
104     if not loaded:
105         loaded = True
106         options = req.get_options()
107         modname = options['testmod']
108         mod = __import__(modname, globals(), locals(), [''])
109         mod.setup_server()
110        
111         import cherrypy
112         cherrypy.config.update({
113             "log_file": os.path.join(curdir, "test.log"),
114             "environment": "production",
115             })
116         cherrypy.engine.start(blocking=False)
117     from mod_python import apache
118     return apache.OK
119
120
121 class ModPythonTestHarness(test.TestHarness):
122     """TestHarness for ModPython and CherryPy."""
123    
124     use_wsgi = False
125    
126     def _run(self, conf):
127         import webtest
128         webtest.WebCase.PORT = self.port
129         print
130         print "Running tests:", self.server
131        
132         if self.use_wsgi:
133             conf_template = conf_modpython_gateway
134         else:
135             conf_template = conf_cpmodpy
136        
137         # mod_python, since it runs in the Apache process, must be
138         # started separately for each test, and then *that* process
139         # must run the setup_server() function for the test.
140         # Then our process can run the actual test.
141         for testmod in self.tests:
142             try:
143                 start(testmod, self.port, conf_template)
144                 suite = webtest.ReloadingTestLoader().loadTestsFromName(testmod)
145                 webtest.TerseTestRunner(verbosity=2).run(suite)
146             finally:
147                 stop()
148
Note: See TracBrowser for help on using the browser.

Hosted by WebFaction

Log in as guest/cpguest to create tickets