1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203
|
import sys
import os
import glob
from os.path import join, abspath, dirname, normpath, basename
import unittest
import urllib2
import time
import signal
import re
import gevent
from gevent import socket
import mysubprocess as subprocess
# Ignore tracebacks: KeyboardInterrupt
base_dir = normpath(join(dirname(abspath(__file__)), '..'))
glob_expression = join(base_dir, 'examples', '*.py')
examples = glob.glob(glob_expression)
simple_examples = []
examples_directory = dirname(examples[0])
for example in examples:
if 'serve_forever' not in open(example).read():
simple_examples.append(example)
print '\n'.join(examples)
def make_test(path):
if ' ' in path:
path = '"%s"' % path
class TestExample(unittest.TestCase):
def test(self):
exe = sys.executable
if ' ' in exe:
exe = '"%s"' % exe
cmd = '%s %s' % (exe, path)
print >> sys.stderr, cmd
res = os.system(cmd)
assert not res, '%s failed with %s' % (path, res)
TestExample.__name__ = 'TestExample_' + basename(path).split('.')[0]
return TestExample
for example in simple_examples:
test = make_test(example)
globals()[test.__name__] = test
print 'Added %s' % test.__name__
del test
class BaseTestServer(unittest.TestCase):
def setUp(self):
self.process = subprocess.Popen([sys.executable, join(examples_directory, self.path)], cwd=examples_directory)
time.sleep(1)
def tearDown(self):
self.assertEqual(self.process.poll(), None)
self.process.interrupt()
time.sleep(0.5)
class Test_httpserver(BaseTestServer):
path = 'httpserver.py'
URL = 'http://localhost:8088'
not_found_message = '<h1>Not Found</h1>'
def read(self, path='/'):
url = self.URL + path
try:
response = urllib2.urlopen(url)
except urllib2.HTTPError, response:
pass
return '%s %s' % (response.code, response.msg), response.read()
def _test_hello(self):
status, data = self.read('/')
self.assertEqual(status, '200 OK')
self.assertEqual(data, "<b>hello world</b>")
def _test_not_found(self):
status, data = self.read('/xxx')
self.assertEqual(status, '404 Not Found')
self.assertEqual(data, self.not_found_message)
def test(self):
# running all the test methods now so that we don't set up a server more than once
for method in dir(self):
if method.startswith('_test'):
function = getattr(self, method)
if callable(function):
function()
class Test_wsgiserver(Test_httpserver):
path = 'wsgiserver.py'
if hasattr(socket, 'ssl'):
class Test_wsgiserver_ssl(Test_httpserver):
path = 'wsgiserver_ssl.py'
URL = 'https://localhost:8443'
else:
class Test_wsgiserver_ssl(unittest.TestCase):
path = 'wsgiserver_ssl.py'
def setUp(self):
self.process = subprocess.Popen([sys.executable, join(examples_directory, self.path)],
cwd=examples_directory, stderr=subprocess.PIPE)
time.sleep(1)
def test(self):
self.assertEqual(self.process.poll(), 1)
stderr = self.process.stderr.read().strip()
m = re.match('Traceback \(most recent call last\):.*?ImportError: .*?ssl.*', stderr, re.DOTALL)
assert m is not None, repr(stderr)
def tearDown(self):
if self.process.poll() is None:
try:
SIGINT = getattr(signal, 'SIGINT', None)
if SIGINT is not None:
os.kill(self.process.pid, SIGINT)
time.sleep(0.1)
self.assertEqual(self.process.poll(), 1)
finally:
if self.process.poll() is None:
self.process.kill()
class Test_webpy(Test_httpserver):
path = 'webpy.py'
not_found_message = 'not found'
def _test_hello(self):
status, data = self.read('/')
self.assertEqual(status, '200 OK')
assert "Hello, world" in data, repr(data)
def _test_long(self):
start = time.time()
status, data = self.read('/long')
delay = time.time() - start
assert 10 - 0.1 < delay < 10 + 0.1, delay
self.assertEqual(status, '200 OK')
self.assertEqual(data, 'Hello, 10 seconds later')
class Test_webproxy(Test_httpserver):
path = 'webproxy.py'
def test(self):
status, data = self.read('/')
self.assertEqual(status, '200 OK')
assert "gevent example" in data, repr(data)
status, data = self.read('/http://www.google.com')
self.assertEqual(status, '200 OK')
assert 'google' in data.lower(), repr(data)
class Test_echoserver(BaseTestServer):
path = 'echoserver.py'
def test(self):
def test_client(message):
conn = socket.create_connection(('127.0.0.1', 6000)).makefile(bufsize=1)
welcome = conn.readline()
assert 'Welcome' in welcome, repr(welcome)
conn.write(message)
received = conn.read(len(message))
self.assertEqual(received, message)
conn._sock.settimeout(0.1)
self.assertRaises(socket.timeout, conn.read, 1)
client1 = gevent.spawn_link_exception(test_client, 'hello\r\n')
client2 = gevent.spawn_link_exception(test_client, 'world\r\n')
gevent.joinall([client1, client2])
class TestAllTested(unittest.TestCase):
def test(self):
tests = set()
for klass in globals():
if klass.startswith('Test'):
path = getattr(globals()[klass], 'path', None)
if path is not None:
tests.add(path)
untested = set(examples) - set(simple_examples)
untested = set(basename(path) for path in untested) - tests
assert not untested, 'The following examples have not been tested: %s' % '\n'.join(untested)
if __name__ == '__main__':
unittest.main()
|