diff options
author | Andrey Zelenkov <zelenkov@nginx.com> | 2018-01-24 15:43:04 +0300 |
---|---|---|
committer | Andrey Zelenkov <zelenkov@nginx.com> | 2018-01-24 15:43:04 +0300 |
commit | 331514fcf75dde719267efc970979524312d268e (patch) | |
tree | 6a283e37438734661d4afad0e08e95ad6cb1b1f9 | |
parent | 771e9d3cc3a59585a3d8a6ae2baddb18f335c3dd (diff) | |
download | unit-331514fcf75dde719267efc970979524312d268e.tar.gz unit-331514fcf75dde719267efc970979524312d268e.tar.bz2 |
Tests: using "expectedFailure" decorator instead of assertTry().
-rw-r--r-- | test/test_configuration.py | 187 | ||||
-rw-r--r-- | test/test_php_basic.py | 146 | ||||
-rw-r--r-- | test/test_python_application.py | 97 | ||||
-rw-r--r-- | test/test_python_atexit.py | 4 | ||||
-rw-r--r-- | test/test_python_basic.py | 156 | ||||
-rw-r--r-- | test/unit.py | 4 |
6 files changed, 321 insertions, 273 deletions
diff --git a/test/test_configuration.py b/test/test_configuration.py index f10a5e8b..b4fac30e 100644 --- a/test/test_configuration.py +++ b/test/test_configuration.py @@ -6,17 +6,54 @@ class TestUnitConfiguration(unit.TestUnitControl): def setUpClass(): unit.TestUnit().check_modules('python') - def test_json(self): + def test_json_leading_zero(self): self.assertIn('error', self.put('/', '00'), 'leading zero') - def test_json_applications(self): - self.assertIn('error', self.put('/applications', '"{}"'), - 'applications string') - self.assertIn('error', self.put('/applications', '{'), - 'applications miss brace') + def test_json_unicode(self): + self.assertIn('success', self.put('/applications', b""" + { + "ap\u0070": { + "type": "\u0070ython", + "workers": 1, + "path": "\u002Fapp", + "module": "wsgi" + } + } + """), 'unicode') - self.assertTry('assertIn', 'negative workers', 'error', - self.put('/applications', """ + def test_json_unicode_2(self): + self.assertIn('success', self.put('/applications', """ + { + "приложение": { + "type": "python", + "workers": 1, + "path": "/app", + "module": "wsgi" + } + } + """), 'unicode 2') + + def test_json_unicode_number(self): + self.assertIn('error', self.put('/applications', b""" + { + "app": { + "type": "python", + "workers": \u0031, + "path": "/app", + "module": "wsgi" + } + } + """), 'unicode number') + + def test_applications_open_brace(self): + self.assertIn('error', self.put('/applications', '{'), 'open brace') + + def test_applications_string(self): + self.assertIn('error', self.put('/applications', '"{}"'), 'string') + + @unittest.expectedFailure + def test_negative_workers(self): + self.assertIn('error', self.put('/applications', """ { "app": { "type": "python", @@ -25,17 +62,19 @@ class TestUnitConfiguration(unit.TestUnitControl): "module": "wsgi" } } - """)) + """), 'negative workers') - self.assertTry('assertIn', 'application type only', 'error', - self.put('/applications', """ + @unittest.expectedFailure + def test_applications_type_only(self): + self.assertIn('error', self.put('/applications', """ { "app": { "type": "python" } } - """)) + """), 'type only') + def test_applications_miss_quote(self): self.assertIn('error', self.put('/applications', """ { app": { @@ -45,8 +84,9 @@ class TestUnitConfiguration(unit.TestUnitControl): "module": "wsgi" } } - """), 'applications miss quote') + """), 'miss quote') + def test_applications_miss_colon(self): self.assertIn('error', self.put('/applications', """ { "app" { @@ -56,8 +96,9 @@ class TestUnitConfiguration(unit.TestUnitControl): "module": "wsgi" } } - """), 'applications miss colon') + """), 'miss colon') + def test_applications_miss_comma(self): self.assertIn('error', self.put('/applications', """ { "app": { @@ -67,11 +108,13 @@ class TestUnitConfiguration(unit.TestUnitControl): "module": "wsgi" } } - """), 'applications miss comma') + """), 'miss comma') + def test_applications_skip_spaces(self): self.assertIn('success', self.put('/applications', b'{ \n\r\t}'), - 'skip space') + 'skip spaces') + def test_applications_relative_path(self): self.assertIn('success', self.put('/applications', """ { "app": { @@ -83,64 +126,90 @@ class TestUnitConfiguration(unit.TestUnitControl): } """), 'relative path') - self.assertIn('success', self.put('/applications', b""" + @unittest.expectedFailure + def test_listeners_empty(self): + self.assertIn('error', self.put('/listeners', '{"*:7080":{}}'), + 'listener empty') + + def test_listeners_no_app(self): + self.assertIn('error', self.put('/listeners', + '{"*:7080":{"application":"app"}}'), 'listeners no app') + + def test_listeners_wildcard(self): + self.assertIn('success', self.put('/', """ { - "ap\u0070": { - "type": "\u0070ython", - "workers": 1, - "path": "\u002Fapp", - "module": "wsgi" + "listeners": { + "*:7080": { + "application":"app" + } + }, + "applications": { + "app": { + "type": "python", + "workers": 1, + "path": "/app", + "module": "wsgi" + } } } - """), 'unicode') + """), 'listeners wildcard') - self.assertIn('success', self.put('/applications', """ + def test_listeners_explicit(self): + self.assertIn('success', self.put('/', """ { - "приложение": { - "type": "python", - "workers": 1, - "path": "/app", - "module": "wsgi" + "listeners": { + "127.0.0.1:7081": { + "application":"app" + } + }, + "applications": { + "app": { + "type": "python", + "workers": 1, + "path": "/app", + "module": "wsgi" + } } } - """), 'unicode 2') + """), 'explicit') - self.assertIn('error', self.put('/applications', b""" + def test_listeners_explicit_ipv6(self): + self.assertIn('success', self.put('/', """ { - "app": { - "type": "python", - "workers": \u0031, - "path": "/app", - "module": "wsgi" + "listeners": { + "[::1]:7082": { + "application":"app" + } + }, + "applications": { + "app": { + "type": "python", + "workers": 1, + "path": "/app", + "module": "wsgi" + } } } - """), 'unicode number') + """), 'explicit ipv6') - def test_json_listeners(self): - self.assertTry('assertIn', 'listener empty', 'error', - self.put('/listeners', '{"*:7080":{}}')) - self.assertIn('error', self.put('/listeners', - '{"*:7080":{"application":"app"}}'), 'listeners no app') - - self.put('/applications', """ + def test_listeners_no_port(self): + self.assertIn('success', self.put('/', """ { - "app": { - "type": "python", - "workers": 1, - "path": "/app", - "module": "wsgi" + "listeners": { + "[::1]:7082": { + "application":"app" + } + }, + "applications": { + "app": { + "type": "python", + "workers": 1, + "path": "/app", + "module": "wsgi" + } } } - """) - - self.assertIn('success', self.put('/listeners', - '{"*:7080":{"application":"app"}}'), 'listeners wildcard') - self.assertIn('success', self.put('/listeners', - '{"127.0.0.1:7081":{"application":"app"}}'), 'listeners explicit') - self.assertIn('success', self.put('/listeners', - '{"[::1]:7082":{"application":"app"}}'), 'listeners explicit ipv6') - self.assertIn('error', self.put('/listeners', - '{"127.0.0.1":{"application":"app"}}'), 'listeners no port') + """), 'no port') if __name__ == '__main__': unittest.main() diff --git a/test/test_php_basic.py b/test/test_php_basic.py index d37f92f5..3d121a75 100644 --- a/test/test_php_basic.py +++ b/test/test_php_basic.py @@ -6,27 +6,34 @@ class TestUnitBasic(unit.TestUnitControl): def setUpClass(): unit.TestUnit().check_modules('php') - def test_php_get(self): - resp = self.get() - self.assertEqual(resp, {'listeners': {}, 'applications': {}}, 'empty') - self.assertEqual(self.get('/listeners'), {}, 'empty listeners prefix') - self.assertEqual(self.get('/applications'), {}, - 'empty applications prefix') + conf_app = """ + { + "app": { + "type": "php", + "workers": 1, + "root": "/app", + "index": "index.php" + } + } + """ - self.put('/applications', """ - { - "app": { - "type": "php", - "workers": 1, - "root": "/app", - "index": "index.php" + conf_basic = """ + { + "listeners": { + "*:7080": { + "application": "app" } - } - """) + }, + "applications": %s + } + """ % (conf_app) + + def test_php_get_applications(self): + self.put('/applications', self.conf_app) resp = self.get() - self.assertEqual(resp['listeners'], {}, 'php empty listeners') + self.assertEqual(resp['listeners'], {}, 'listeners') self.assertEqual(resp['applications'], { "app": { @@ -36,7 +43,10 @@ class TestUnitBasic(unit.TestUnitControl): "index": "index.php" } }, - 'php applications') + 'applications') + + def test_php_get_applications_prefix(self): + self.put('/applications', self.conf_app) self.assertEqual(self.get('/applications'), { @@ -47,7 +57,10 @@ class TestUnitBasic(unit.TestUnitControl): "index": "index.php" } }, - 'php applications prefix') + 'applications prefix') + + def test_php_get_applications_prefix_2(self): + self.put('/applications', self.conf_app) self.assertEqual(self.get('/applications/app'), { @@ -56,102 +69,67 @@ class TestUnitBasic(unit.TestUnitControl): "root": "/app", "index": "index.php" }, - 'php applications prefix 2') + 'applications prefix 2') + + def test_php_get_applications_prefix_3(self): + self.put('/applications', self.conf_app) - self.assertEqual(self.get('/applications/app/type'), 'php', - 'php applications type') - self.assertEqual(self.get('/applications/app/workers'), 1, - 'php applications workers') + self.assertEqual(self.get('/applications/app/type'), 'php', 'type') + self.assertEqual(self.get('/applications/app/workers'), 1, 'workers') - self.put('/listeners', '{"*:7080":{"application":"app"}}') + def test_php_get_listeners(self): + self.put('/', self.conf_basic) self.assertEqual(self.get()['listeners'], - {"*:7080":{"application":"app"}}, 'php listeners') - self.assertEqual(self.get('/listeners'), - {"*:7080":{"application":"app"}}, 'php listeners prefix') - self.assertEqual(self.get('/listeners/*:7080'), - {"application":"app"}, 'php listeners prefix 2') - self.assertEqual(self.get('/listeners/*:7080/application'), 'app', - 'php listeners application') + {"*:7080":{"application":"app"}}, 'listeners') - def test_php_put(self): - self.put('/', """ - { - "listeners": { - "*:7080": { - "application": "app" - } - }, - "applications": { - "app": { - "type": "php", - "workers": 1, - "root": "/app", - "index": "index.php" - } - } - } - """) + def test_php_get_listeners_prefix(self): + self.put('/', self.conf_basic) - resp = self.get() + self.assertEqual(self.get('/listeners'), + {"*:7080":{"application":"app"}}, 'listeners prefix') - self.assertEqual(resp['listeners'], {"*:7080":{"application":"app"}}, - 'put listeners') + def test_php_get_listeners_prefix_2(self): + self.put('/', self.conf_basic) - self.assertEqual(resp['applications'], - { - "app": { - "type": "php", - "workers": 1, - "root": "/app", - "index": "index.php" - } - }, - 'put applications') + self.assertEqual(self.get('/listeners/*:7080'), + {"application":"app"}, 'listeners prefix 2') + def test_php_change_listener(self): + self.put('/', self.conf_basic) self.put('/listeners', '{"*:7081":{"application":"app"}}') + self.assertEqual(self.get('/listeners'), - {"*:7081": {"application":"app"}}, 'put listeners prefix') + {"*:7081": {"application":"app"}}, 'change listener') + def test_php_add_listener(self): + self.put('/', self.conf_basic) self.put('/listeners/*:7082', '{"application":"app"}') self.assertEqual(self.get('/listeners'), { - "*:7081": { + "*:7080": { "application": "app" }, "*:7082": { "application": "app" } }, - 'put listeners prefix 3') + 'add listener') + + def test_php_change_application(self): + self.put('/', self.conf_basic) self.put('/applications/app/workers', '30') self.assertEqual(self.get('/applications/app/workers'), 30, - 'put applications workers') + 'change application workers') self.put('/applications/app/root', '"/www"') self.assertEqual(self.get('/applications/app/root'), '/www', - 'put applications root') + 'change application root') def test_php_delete(self): - self.put('/', """ - { - "listeners": { - "*:7080": { - "application": "app" - } - }, - "applications": { - "app": { - "type": "php", - "workers": 1, - "root": "/app", - "index": "index.php" - } - } - } - """) + self.put('/', self.conf_basic) self.assertIn('error', self.delete('/applications/app'), 'delete app before listener') diff --git a/test/test_python_application.py b/test/test_python_application.py index 7014036b..d3180d62 100644 --- a/test/test_python_application.py +++ b/test/test_python_application.py @@ -9,7 +9,25 @@ class TestUnitApplication(unit.TestUnitControl): u.check_modules('python') u.check_version('0.4') - def test_python_application(self): + conf = """ + { + "listeners": { + "*:7080": { + "application": "app" + } + }, + "applications": { + "app": { + "type": "python", + "workers": 1, + "path": "%s", + "module": "wsgi" + } + } + } + """ + + def test_python_application_simple(self): code, name = """ def application(environ, start_response): @@ -26,7 +44,6 @@ def application(environ, start_response): ('Http-Host', environ.get('HTTP_HOST')), ('Remote-Addr', environ.get('REMOTE_ADDR')), ('Server-Name', environ.get('SERVER_NAME')), - ('Server-Port', environ.get('SERVER_PORT')), ('Server-Protocol', environ.get('SERVER_PROTOCOL')), ('Custom-Header', environ.get('HTTP_CUSTOM_HEADER')) ]) @@ -35,24 +52,7 @@ def application(environ, start_response): """, 'py_app' self.python_application(name, code) - - self.put('/', """ - { - "listeners": { - "*:7080": { - "application": "app" - } - }, - "applications": { - "app": { - "type": "python", - "workers": 1, - "path": "%s", - "module": "wsgi" - } - } - } - """ % (self.testdir + '/' + name)) + self.put('/', self.conf % (self.testdir + '/' + name)) body = 'Test body string.' @@ -63,28 +63,45 @@ def application(environ, start_response): }, data=body) self.assertEqual(r.status_code, 200, 'status') - self.assertEqual(r.headers['Content-Length'], str(len(body)), - 'header content length') - self.assertEqual(r.headers['Content-Type'], 'text/html', - 'header content type') - self.assertEqual(r.headers['Request-Method'], 'POST', - 'header request method') - self.assertEqual(r.headers['Request-Uri'], '/', 'header request uri') - self.assertEqual(r.headers['Path-Info'], '/', 'header path info') - self.assertEqual(r.headers['Http-Host'], 'localhost', - 'header http host') - self.assertEqual(r.headers['Remote-Addr'], '127.0.0.1', - 'header remote addr') - - self.assertTry('assertEqual', 'header server port', - r.headers['Server-Port'], '7080') - - self.assertEqual(r.headers['Server-Protocol'], 'HTTP/1.1', - 'header server protocol') - self.assertEqual(r.headers['Custom-Header'], 'blah', - 'header custom header') + headers = dict(r.headers) + self.assertRegex(headers.pop('Server'), r'unit/[\d\.]+', + 'server header') + self.assertDictEqual(headers, { + 'Content-Length': str(len(body)), + 'Content-Type': 'text/html', + 'Request-Method': 'POST', + 'Request-Uri': '/', + 'Path-Info': '/', + 'Http-Host': 'localhost', + 'Server-Name': 'localhost', + 'Remote-Addr': '127.0.0.1', + 'Server-Protocol': 'HTTP/1.1', + 'Custom-Header': 'blah' + }, 'headers') self.assertEqual(r.content, str.encode(body), 'body') + @unittest.expectedFailure + def test_python_application_server_port(self): + code, name = """ + +def application(environ, start_response): + + start_response('200 OK', [ + ('Content-Type', 'text/html'), + ('Server-Port', environ.get('SERVER_PORT')) + ]) + return [] + +""", 'py_app' + + self.python_application(name, code) + self.put('/', self.conf % (self.testdir + '/' + name)) + + r = unit.TestUnitHTTP.get(headers={'Host': 'localhost'}) + + self.assertEqual(r.headers.pop('Server-Port'), '7080', + 'Server-Port header') + if __name__ == '__main__': unittest.main() diff --git a/test/test_python_atexit.py b/test/test_python_atexit.py index be45d113..82fa9a39 100644 --- a/test/test_python_atexit.py +++ b/test/test_python_atexit.py @@ -22,7 +22,7 @@ atexit.register(create_file) def application(env, start_response): start_response('200 OK', [('Content-Type','text/html')]) - return [b'body'] + return [] """ % (self.testdir + '/atexit'), 'py_app' @@ -55,7 +55,7 @@ def application(env, start_response): } """) - time.sleep(0.2) + time.sleep(0.2) # wait for 'atexit' file self.assertEqual(os.path.exists(self.testdir + '/atexit'), True, 'python atexit') diff --git a/test/test_python_basic.py b/test/test_python_basic.py index 00460712..8aea5e40 100644 --- a/test/test_python_basic.py +++ b/test/test_python_basic.py @@ -6,27 +6,44 @@ class TestUnitBasic(unit.TestUnitControl): def setUpClass(): unit.TestUnit().check_modules('python') - def test_python_get(self): - resp = self.get() - self.assertEqual(resp, {'listeners': {}, 'applications': {}}, 'empty') - self.assertEqual(self.get('/listeners'), {}, 'empty listeners prefix') - self.assertEqual(self.get('/applications'), {}, - 'empty applications prefix') + conf_app = """ + { + "app": { + "type": "python", + "workers": 1, + "path": "/app", + "module": "wsgi" + } + } + """ - self.put('/applications', """ - { - "app": { - "type": "python", - "workers": 1, - "path": "/app", - "module": "wsgi" + conf_basic = """ + { + "listeners": { + "*:7080": { + "application": "app" } - } - """) + }, + "applications": %s + } + """ % (conf_app) + + def test_python_get_empty(self): + self.assertEqual(self.get(), {'listeners': {}, 'applications': {}}, + 'empty') + + def test_python_get_prefix_listeners(self): + self.assertEqual(self.get('/listeners'), {}, 'listeners prefix') + + def test_python_get_prefix_applications(self): + self.assertEqual(self.get('/applications'), {}, 'applications prefix') + + def test_python_get_applications(self): + self.put('/applications', self.conf_app) resp = self.get() - self.assertEqual(resp['listeners'], {}, 'python empty listeners') + self.assertEqual(resp['listeners'], {}, 'listeners') self.assertEqual(resp['applications'], { "app": { @@ -36,7 +53,10 @@ class TestUnitBasic(unit.TestUnitControl): "module": "wsgi" } }, - 'python applications') + 'applications') + + def test_python_get_applications_prefix(self): + self.put('/applications', self.conf_app) self.assertEqual(self.get('/applications'), { @@ -47,7 +67,10 @@ class TestUnitBasic(unit.TestUnitControl): "module":"wsgi" } }, - 'python applications prefix') + 'applications prefix') + + def test_python_get_applications_prefix_2(self): + self.put('/applications', self.conf_app) self.assertEqual(self.get('/applications/app'), { @@ -56,102 +79,67 @@ class TestUnitBasic(unit.TestUnitControl): "path": "/app", "module": "wsgi" }, - 'python applications prefix 2') + 'applications prefix 2') - self.assertEqual(self.get('/applications/app/type'), 'python', - 'python applications type') - self.assertEqual(self.get('/applications/app/workers'), 1, - 'python applications workers') + def test_python_get_applications_prefix_3(self): + self.put('/applications', self.conf_app) - self.put('/listeners', '{"*:7080":{"application":"app"}}') + self.assertEqual(self.get('/applications/app/type'), 'python', 'type') + self.assertEqual(self.get('/applications/app/workers'), 1, 'workers') + + def test_python_get_listeners(self): + self.put('/', self.conf_basic) self.assertEqual(self.get()['listeners'], - {"*:7080":{"application":"app"}}, 'python listeners') - self.assertEqual(self.get('/listeners'), - {"*:7080":{"application":"app"}}, 'python listeners prefix') - self.assertEqual(self.get('/listeners/*:7080'), - {"application":"app"}, 'python listeners prefix 2') - self.assertEqual(self.get('/listeners/*:7080/application'), 'app', - 'python listeners application') + {"*:7080":{"application":"app"}}, 'listeners') - def test_python_put(self): - self.put('/', """ - { - "listeners": { - "*:7080": { - "application": "app" - } - }, - "applications": { - "app": { - "type": "python", - "workers": 1, - "path": "/app", - "module": "wsgi" - } - } - } - """) + def test_python_get_listeners_prefix(self): + self.put('/', self.conf_basic) - resp = self.get() + self.assertEqual(self.get('/listeners'), + {"*:7080":{"application":"app"}}, 'listeners prefix') - self.assertEqual(resp['listeners'], {"*:7080":{"application":"app"}}, - 'put listeners') + def test_python_get_listeners_prefix_2(self): + self.put('/', self.conf_basic) - self.assertEqual(resp['applications'], - { - "app": { - "type": "python", - "workers": 1, - "path": "/app", - "module": "wsgi" - } - }, - 'put applications') + self.assertEqual(self.get('/listeners/*:7080'), + {"application":"app"}, 'listeners prefix 2') + def test_python_change_listener(self): + self.put('/', self.conf_basic) self.put('/listeners', '{"*:7081":{"application":"app"}}') + self.assertEqual(self.get('/listeners'), - {"*:7081": {"application":"app"}}, 'put listeners prefix') + {"*:7081": {"application":"app"}}, 'change listener') + def test_python_add_listener(self): + self.put('/', self.conf_basic) self.put('/listeners/*:7082', '{"application":"app"}') self.assertEqual(self.get('/listeners'), { - "*:7081": { + "*:7080": { "application": "app" }, "*:7082": { "application": "app" } }, - 'put listeners prefix 3') + 'add listener') + + def test_python_change_application(self): + self.put('/', self.conf_basic) self.put('/applications/app/workers', '30') self.assertEqual(self.get('/applications/app/workers'), 30, - 'put applications workers') + 'change application workers') self.put('/applications/app/path', '"/www"') self.assertEqual(self.get('/applications/app/path'), '/www', - 'put applications path') + 'change application path') def test_python_delete(self): - self.put('/', """ - { - "listeners": { - "*:7080": { - "application": "app" - } - }, - "applications": { - "app": { - "type": "python", - "workers": 1, - "path": "/app", - "module": "wsgi" - } - } - } - """) + self.put('/', self.conf_basic) self.assertIn('error', self.delete('/applications/app'), 'delete app before listener') diff --git a/test/unit.py b/test/unit.py index 87fbd029..38c001ba 100644 --- a/test/unit.py +++ b/test/unit.py @@ -28,10 +28,6 @@ class TestUnit(unittest.TestCase): if '--leave' not in sys.argv: shutil.rmtree(self.testdir) - def assertTry(self, assert_method, description, *args): - try: getattr(self, assert_method)(*args, msg=description) - except AssertionError: print('not yet: ' + description) - def check_modules(self, *modules): self._run() |