From 09f2009df564ab2063ee4713ba24247039945146 Mon Sep 17 00:00:00 2001 From: Andrey Zelenkov Date: Tue, 20 Feb 2018 20:34:41 +0300 Subject: Tests: reworked python tests with application. --- test/python/204_no_content/wsgi.py | 4 ++ test/python/atexit/wsgi.py | 12 ++++++ test/python/empty/wsgi.py | 3 ++ test/python/mirror/wsgi.py | 10 +++++ test/python/query_string/wsgi.py | 7 ++++ test/python/server_port/wsgi.py | 7 ++++ test/python/variables/wsgi.py | 15 +++++++ test/test_python_application.py | 86 +++----------------------------------- test/test_python_atexit.py | 39 +++-------------- test/test_python_keepalive.py | 35 +--------------- test/test_python_procman.py | 36 ++-------------- test/unit.py | 37 +++++++++++----- 12 files changed, 102 insertions(+), 189 deletions(-) create mode 100644 test/python/204_no_content/wsgi.py create mode 100644 test/python/atexit/wsgi.py create mode 100644 test/python/empty/wsgi.py create mode 100644 test/python/mirror/wsgi.py create mode 100644 test/python/query_string/wsgi.py create mode 100644 test/python/server_port/wsgi.py create mode 100644 test/python/variables/wsgi.py diff --git a/test/python/204_no_content/wsgi.py b/test/python/204_no_content/wsgi.py new file mode 100644 index 00000000..2184ffeb --- /dev/null +++ b/test/python/204_no_content/wsgi.py @@ -0,0 +1,4 @@ +def application(environ, start_response): + + start_response('204 No Content', []) + return [] diff --git a/test/python/atexit/wsgi.py b/test/python/atexit/wsgi.py new file mode 100644 index 00000000..9ad300b9 --- /dev/null +++ b/test/python/atexit/wsgi.py @@ -0,0 +1,12 @@ +import atexit + +def application(environ, start_response): + test_dir = environ.get('HTTP_TEST_DIR') + + def create_file(): + open(test_dir + '/atexit', 'w') + + atexit.register(create_file) + + start_response('200', [('Content-Length', '0')]) + return [] diff --git a/test/python/empty/wsgi.py b/test/python/empty/wsgi.py new file mode 100644 index 00000000..ce03850a --- /dev/null +++ b/test/python/empty/wsgi.py @@ -0,0 +1,3 @@ +def application(env, start_response): + start_response('200', [('Content-Length', '0')]) + return [] diff --git a/test/python/mirror/wsgi.py b/test/python/mirror/wsgi.py new file mode 100644 index 00000000..e4df67ec --- /dev/null +++ b/test/python/mirror/wsgi.py @@ -0,0 +1,10 @@ +def application(environ, start_response): + + content_length = int(environ.get('CONTENT_LENGTH', 0)) + body = bytes(environ['wsgi.input'].read(content_length)) + + start_response('200', [ + ('Content-Type', environ.get('CONTENT_TYPE')), + ('Content-Length', str(len(body))) + ]) + return [body] diff --git a/test/python/query_string/wsgi.py b/test/python/query_string/wsgi.py new file mode 100644 index 00000000..90f1c7ec --- /dev/null +++ b/test/python/query_string/wsgi.py @@ -0,0 +1,7 @@ +def application(environ, start_response): + + start_response('200', [ + ('Content-Length', '0'), + ('Query-String', environ.get('QUERY_STRING')) + ]) + return [] diff --git a/test/python/server_port/wsgi.py b/test/python/server_port/wsgi.py new file mode 100644 index 00000000..89cd82b3 --- /dev/null +++ b/test/python/server_port/wsgi.py @@ -0,0 +1,7 @@ +def application(environ, start_response): + + start_response('200', [ + ('Content-Length', '0'), + ('Server-Port', environ.get('SERVER_PORT')) + ]) + return [] diff --git a/test/python/variables/wsgi.py b/test/python/variables/wsgi.py new file mode 100644 index 00000000..1d4d397d --- /dev/null +++ b/test/python/variables/wsgi.py @@ -0,0 +1,15 @@ +def application(environ, start_response): + + content_length = int(environ.get('CONTENT_LENGTH', 0)) + body = bytes(environ['wsgi.input'].read(content_length)) + + start_response('200', [ + ('Content-Type', environ.get('CONTENT_TYPE')), + ('Content-Length', str(len(body))), + ('Request-Method', environ.get('REQUEST_METHOD')), + ('Request-Uri', environ.get('REQUEST_URI')), + ('Http-Host', environ.get('HTTP_HOST')), + ('Server-Protocol', environ.get('SERVER_PROTOCOL')), + ('Custom-Header', environ.get('HTTP_CUSTOM_HEADER')) + ]) + return [body] diff --git a/test/test_python_application.py b/test/test_python_application.py index 80854eb5..00b901bc 100644 --- a/test/test_python_application.py +++ b/test/test_python_application.py @@ -2,51 +2,13 @@ import time import unittest import unit -class TestUnitPythonApplication(unit.TestUnitControl): +class TestUnitPythonApplication(unit.TestUnitApplicationPython): def setUpClass(): unit.TestUnit().check_modules('python') - def conf_with_name(self, name): - self.conf({ - "listeners": { - "*:7080": { - "application": "app" - } - }, - "applications": { - "app": { - "type": "python", - "processes": { "spare": 0 }, - "path": self.testdir + '/' + name, - "module": "wsgi" - } - } - }) - - def test_python_application_simple(self): - code, name = """ - -def application(environ, start_response): - - content_length = int(environ.get('CONTENT_LENGTH', 0)) - body = bytes(environ['wsgi.input'].read(content_length)) - - start_response('200', [ - ('Content-Type', environ.get('CONTENT_TYPE')), - ('Content-Length', str(len(body))), - ('Request-Method', environ.get('REQUEST_METHOD')), - ('Request-Uri', environ.get('REQUEST_URI')), - ('Http-Host', environ.get('HTTP_HOST')), - ('Server-Protocol', environ.get('SERVER_PROTOCOL')), - ('Custom-Header', environ.get('HTTP_CUSTOM_HEADER')) - ]) - return [body] - -""", 'py_app' - - self.python_application(name, code) - self.conf_with_name(name) + def test_python_application_variables(self): + self.load('variables') body = 'Test body string.' @@ -75,20 +37,7 @@ def application(environ, start_response): self.assertEqual(resp['body'], body, 'body') def test_python_application_query_string(self): - code, name = """ - -def application(environ, start_response): - - start_response('200', [ - ('Content-Length', '0'), - ('Query-String', environ.get('QUERY_STRING')) - ]) - return [] - -""", 'py_app' - - self.python_application(name, code) - self.conf_with_name(name) + self.load('query_string') resp = self.get(url='/?var1=val1&var2=val2') @@ -97,37 +46,14 @@ def application(environ, start_response): @unittest.expectedFailure def test_python_application_server_port(self): - code, name = """ - -def application(environ, start_response): - - start_response('200', [ - ('Content-Length', '0'), - ('Server-Port', environ.get('SERVER_PORT')) - ]) - return [] - -""", 'py_app' - - self.python_application(name, code) - self.conf_with_name(name) + self.load('server_port') self.assertEqual(self.get()['headers']['Server-Port'], '7080', 'Server-Port header') @unittest.expectedFailure def test_python_application_204_transfer_encoding(self): - code, name = """ - -def application(environ, start_response): - - start_response('204 No Content', []) - return [] - -""", 'py_app' - - self.python_application(name, code) - self.conf_with_name(name) + self.load('204_no_content') self.assertNotIn('Transfer-Encoding', self.get()['headers'], '204 header transfer encoding') diff --git a/test/test_python_atexit.py b/test/test_python_atexit.py index b41a80db..a115a192 100644 --- a/test/test_python_atexit.py +++ b/test/test_python_atexit.py @@ -3,46 +3,20 @@ import time import unittest import unit -class TestUnitPythonAtExit(unit.TestUnitControl): +class TestUnitPythonAtExit(unit.TestUnitApplicationPython): def setUpClass(): unit.TestUnit().check_modules('python') def test_python_atexit(self): - code, name = """ -import atexit + self.load('atexit') -def create_file(): - open('%s', 'w') - -atexit.register(create_file) - -def application(env, start_response): - start_response('200', [('Content-Length', '0')]) - return [] - -""" % (self.testdir + '/atexit'), 'py_app' - - self.python_application(name, code) - - self.conf({ - "listeners": { - "*:7080": { - "application": "app" - } - }, - "applications": { - "app": { - "type": "python", - "processes": { "spare": 0 }, - "path": self.testdir + '/' + name, - "module": "wsgi" - } - } + self.get(headers={ + 'Host': 'localhost', + 'Test-Dir': self.testdir, + 'Connection': 'close' }) - self.get() - self.conf({ "listeners": {}, "applications": {} @@ -53,6 +27,5 @@ def application(env, start_response): self.assertEqual(os.path.exists(self.testdir + '/atexit'), True, 'python atexit') - if __name__ == '__main__': unittest.main() diff --git a/test/test_python_keepalive.py b/test/test_python_keepalive.py index eadf922c..0472a15b 100644 --- a/test/test_python_keepalive.py +++ b/test/test_python_keepalive.py @@ -1,44 +1,13 @@ import unittest import unit -class TestUnitPythonKeepalive(unit.TestUnitControl): +class TestUnitPythonKeepalive(unit.TestUnitApplicationPython): def setUpClass(): unit.TestUnit().check_modules('python') def test_python_keepalive_body(self): - code, name = """ - -def application(environ, start_response): - - content_length = int(environ.get('CONTENT_LENGTH', 0)) - body = bytes(environ['wsgi.input'].read(content_length)) - - start_response('200', [ - ('Content-Type', environ.get('CONTENT_TYPE')), - ('Content-Length', str(len(body))) - ]) - return [body] - -""", 'py_app' - - self.python_application(name, code) - - self.conf({ - "listeners": { - "*:7080": { - "application": "app" - } - }, - "applications": { - "app": { - "type": "python", - "processes": { "spare": 0 }, - "path": self.testdir + '/' + name, - "module": "wsgi" - } - } - }) + self.load('mirror') (resp, sock) = self.post(headers={ 'Connection': 'keep-alive', diff --git a/test/test_python_procman.py b/test/test_python_procman.py index 5dea007a..f1187795 100644 --- a/test/test_python_procman.py +++ b/test/test_python_procman.py @@ -4,21 +4,18 @@ import subprocess import unittest import unit -class TestUnitPythonProcman(unit.TestUnitControl): +class TestUnitPythonProcman(unit.TestUnitApplicationPython): def setUpClass(): unit.TestUnit().check_modules('python') - def pids_for_process(self, process=None): - if process is None: - process = self.app_name - + def pids_for_process(self): time.sleep(0.2) output = subprocess.check_output(['ps', 'ax']) pids = set() - for m in re.findall('.*' + process, output.decode()): + for m in re.findall('.*' + self.app_name, output.decode()): pids.add(re.search('^\s*(\d+)', m).group(1)) return pids @@ -26,33 +23,8 @@ class TestUnitPythonProcman(unit.TestUnitControl): def setUp(self): super().setUp() - code, name = """ - -def application(env, start_response): - start_response('200', [('Content-Length', '0')]) - return [] - -""", 'py_app' - self.app_name = "app-" + self.testdir.split('/')[-1] - - self.python_application(name, code) - - self.conf({ - "listeners": { - "*:7080": { - "application": self.app_name - } - }, - "applications": { - self.app_name: { - "type": "python", - "processes": { "spare": 0 }, - "path": self.testdir + '/' + name, - "module": "wsgi" - } - } - }) + self.load('empty', self.app_name) def test_python_processes_access(self): self.conf('1', '/applications/' + self.app_name + '/processes') diff --git a/test/unit.py b/test/unit.py index 43f4d632..2d50d64b 100644 --- a/test/unit.py +++ b/test/unit.py @@ -77,12 +77,6 @@ class TestUnit(unittest.TestCase): self.testdir + '/unit.log', self.testdir + '/control.unit.sock'): exit("Could not start unit") - def python_application(self, name, code): - os.mkdir(self.testdir + '/' + name) - - with open(self.testdir + '/' + name + '/wsgi.py', 'w') as f: - f.write(code) - def _stop(self): with open(self.testdir + '/unit.pid', 'r') as f: pid = f.read().rstrip() @@ -283,20 +277,41 @@ class TestUnitApplicationProto(TestUnitControl): current_dir = os.path.dirname(os.path.abspath(__file__)) +class TestUnitApplicationPython(TestUnitApplicationProto): + def load(self, script, name=None): + if name is None: + name = script + + self.conf({ + "listeners": { + "*:7080": { + "application": name + } + }, + "applications": { + name: { + "type": "python", + "processes": { "spare": 0 }, + "path": self.current_dir + '/python/' + script, + "module": "wsgi" + } + } + }) + class TestUnitApplicationPerl(TestUnitApplicationProto): - def load(self, dir, name='psgi.pl'): + def load(self, script, name='psgi.pl'): self.conf({ "listeners": { "*:7080": { - "application": dir + "application": script } }, "applications": { - dir: { + script: { "type": "perl", "processes": { "spare": 0 }, - "working_directory": self.current_dir + '/perl/' + dir, - "script": self.current_dir + '/perl/' + dir + '/' + name + "working_directory": self.current_dir + '/perl/' + script, + "script": self.current_dir + '/perl/' + script + '/' + name } } }) -- cgit