| 1 |
import os |
| 2 |
import sys |
| 3 |
import subprocess |
| 4 |
import time |
| 5 |
|
| 6 |
from pecan.compat import urlopen, URLError |
| 7 |
from pecan.tests import PecanTestCase |
| 8 |
|
| 9 |
import unittest |
| 10 |
|
| 11 |
|
| 12 |
if __name__ == '__main__': |
| 13 |
|
| 14 |
class TestTemplateBuilds(PecanTestCase): |
| 15 |
""" |
| 16 |
Used to test the templated quickstart project(s). |
| 17 |
""" |
| 18 |
|
| 19 |
@property |
| 20 |
def bin(self): |
| 21 |
return os.path.dirname(sys.executable) |
| 22 |
|
| 23 |
def poll(self, proc): |
| 24 |
limit = 30 |
| 25 |
for i in range(limit): |
| 26 |
proc.poll() |
| 27 |
|
| 28 |
# Make sure it's running |
| 29 |
if proc.returncode is None: |
| 30 |
break |
| 31 |
elif i == limit: # pragma: no cover |
| 32 |
raise RuntimeError("Server process didn't start.") |
| 33 |
time.sleep(.1) |
| 34 |
|
| 35 |
def test_project_pecan_serve_command(self): |
| 36 |
# Start the server |
| 37 |
proc = subprocess.Popen([ |
| 38 |
os.path.join(self.bin, 'pecan'), |
| 39 |
'serve', |
| 40 |
'testing123/config.py' |
| 41 |
]) |
| 42 |
|
| 43 |
try: |
| 44 |
self.poll(proc) |
| 45 |
retries = 30 |
| 46 |
while True: |
| 47 |
retries -= 1 |
| 48 |
if retries < 0: # pragma: nocover |
| 49 |
raise RuntimeError( |
| 50 |
"The HTTP server has not replied within 3 seconds." |
| 51 |
) |
| 52 |
try: |
| 53 |
# ...and that it's serving (valid) content... |
| 54 |
resp = urlopen('http://localhost:8080/') |
| 55 |
assert resp.getcode() |
| 56 |
assert len(resp.read().decode()) |
| 57 |
except URLError: |
| 58 |
pass |
| 59 |
else: |
| 60 |
break |
| 61 |
time.sleep(.1) |
| 62 |
finally: |
| 63 |
proc.terminate() |
| 64 |
|
| 65 |
def test_project_pecan_shell_command(self): |
| 66 |
# Start the server |
| 67 |
proc = subprocess.Popen([ |
| 68 |
os.path.join(self.bin, 'pecan'), |
| 69 |
'shell', |
| 70 |
'testing123/config.py' |
| 71 |
], |
| 72 |
stdout=subprocess.PIPE, |
| 73 |
stderr=subprocess.PIPE, |
| 74 |
stdin=subprocess.PIPE |
| 75 |
) |
| 76 |
|
| 77 |
self.poll(proc) |
| 78 |
|
| 79 |
out, _ = proc.communicate( |
| 80 |
b'{"model" : model, "conf" : conf, "app" : app}' |
| 81 |
) |
| 82 |
assert 'testing123.model' in out.decode(), out |
| 83 |
assert 'Config(' in out.decode(), out |
| 84 |
assert 'webtest.app.TestApp' in out.decode(), out |
| 85 |
|
| 86 |
try: |
| 87 |
# just in case stdin doesn't close |
| 88 |
proc.terminate() |
| 89 |
except Exception: |
| 90 |
pass |
| 91 |
|
| 92 |
class TestThirdPartyServe(TestTemplateBuilds): |
| 93 |
|
| 94 |
def poll_http(self, name, proc, port): |
| 95 |
try: |
| 96 |
self.poll(proc) |
| 97 |
retries = 30 |
| 98 |
while True: |
| 99 |
retries -= 1 |
| 100 |
if retries < 0: # pragma: nocover |
| 101 |
raise RuntimeError( |
| 102 |
"The %s server has not replied within" |
| 103 |
" 3 seconds." % name |
| 104 |
) |
| 105 |
try: |
| 106 |
# ...and that it's serving (valid) content... |
| 107 |
resp = urlopen('http://localhost:%d/' % port) |
| 108 |
assert resp.getcode() |
| 109 |
assert len(resp.read().decode()) |
| 110 |
except URLError: |
| 111 |
pass |
| 112 |
else: |
| 113 |
break |
| 114 |
time.sleep(.1) |
| 115 |
finally: |
| 116 |
proc.terminate() |
| 117 |
|
| 118 |
class TestUWSGIServiceCommand(TestThirdPartyServe): |
| 119 |
|
| 120 |
def test_serve_from_config(self): |
| 121 |
# Start the server |
| 122 |
proc = subprocess.Popen([ |
| 123 |
os.path.join(self.bin, 'uwsgi'), |
| 124 |
'--http-socket', |
| 125 |
':8080', |
| 126 |
'--venv', |
| 127 |
sys.prefix, |
| 128 |
'--pecan', |
| 129 |
'testing123/config.py' |
| 130 |
]) |
| 131 |
|
| 132 |
self.poll_http('uwsgi', proc, 8080) |
| 133 |
|
| 134 |
# First, ensure that the `testing123` package has been installed |
| 135 |
args = [ |
| 136 |
os.path.join(os.path.dirname(sys.executable), 'pip'), |
| 137 |
'install', |
| 138 |
'-U', |
| 139 |
'-e', |
| 140 |
'./testing123' |
| 141 |
] |
| 142 |
process = subprocess.Popen(args) |
| 143 |
_, unused_err = process.communicate() |
| 144 |
assert not process.poll() |
| 145 |
|
| 146 |
unittest.main() |