Tests: using "expectedFailure" decorator instead of assertTry().

This commit is contained in:
Andrey Zelenkov
2018-01-24 15:43:04 +03:00
parent 771e9d3cc3
commit 331514fcf7
6 changed files with 359 additions and 311 deletions

View File

@@ -6,83 +6,10 @@ class TestUnitConfiguration(unit.TestUnitControl):
def setUpClass(): def setUpClass():
unit.TestUnit().check_modules('python') unit.TestUnit().check_modules('python')
def test_json(self): def test_json_leading_zero(self):
self.assertIn('error', self.put('/', '00'), 'leading zero') self.assertIn('error', self.put('/', '00'), 'leading zero')
def test_json_applications(self): def test_json_unicode(self):
self.assertIn('error', self.put('/applications', '"{}"'),
'applications string')
self.assertIn('error', self.put('/applications', '{'),
'applications miss brace')
self.assertTry('assertIn', 'negative workers', 'error',
self.put('/applications', """
{
"app": {
"type": "python",
"workers": -1,
"path": "/app",
"module": "wsgi"
}
}
"""))
self.assertTry('assertIn', 'application type only', 'error',
self.put('/applications', """
{
"app": {
"type": "python"
}
}
"""))
self.assertIn('error', self.put('/applications', """
{
app": {
"type": "python",
"workers": 1,
"path": "/app",
"module": "wsgi"
}
}
"""), 'applications miss quote')
self.assertIn('error', self.put('/applications', """
{
"app" {
"type": "python",
"workers": 1,
"path": "/app",
"module": "wsgi"
}
}
"""), 'applications miss colon')
self.assertIn('error', self.put('/applications', """
{
"app": {
"type": "python"
"workers": 1,
"path": "/app",
"module": "wsgi"
}
}
"""), 'applications miss comma')
self.assertIn('success', self.put('/applications', b'{ \n\r\t}'),
'skip space')
self.assertIn('success', self.put('/applications', """
{
"app": {
"type": "python",
"workers": 1,
"path": "../app",
"module": "wsgi"
}
}
"""), 'relative path')
self.assertIn('success', self.put('/applications', b""" self.assertIn('success', self.put('/applications', b"""
{ {
"ap\u0070": { "ap\u0070": {
@@ -94,6 +21,7 @@ class TestUnitConfiguration(unit.TestUnitControl):
} }
"""), 'unicode') """), 'unicode')
def test_json_unicode_2(self):
self.assertIn('success', self.put('/applications', """ self.assertIn('success', self.put('/applications', """
{ {
"приложение": { "приложение": {
@@ -105,6 +33,7 @@ class TestUnitConfiguration(unit.TestUnitControl):
} }
"""), 'unicode 2') """), 'unicode 2')
def test_json_unicode_number(self):
self.assertIn('error', self.put('/applications', b""" self.assertIn('error', self.put('/applications', b"""
{ {
"app": { "app": {
@@ -116,31 +45,171 @@ class TestUnitConfiguration(unit.TestUnitControl):
} }
"""), 'unicode number') """), 'unicode number')
def test_json_listeners(self): def test_applications_open_brace(self):
self.assertTry('assertIn', 'listener empty', 'error', self.assertIn('error', self.put('/applications', '{'), 'open brace')
self.put('/listeners', '{"*:7080":{}}'))
self.assertIn('error', self.put('/listeners',
'{"*:7080":{"application":"app"}}'), 'listeners no app')
self.put('/applications', """ 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": { "app": {
"type": "python",
"workers": -1,
"path": "/app",
"module": "wsgi"
}
}
"""), 'negative workers')
@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": {
"type": "python", "type": "python",
"workers": 1, "workers": 1,
"path": "/app", "path": "/app",
"module": "wsgi" "module": "wsgi"
} }
} }
""") """), 'miss quote')
self.assertIn('success', self.put('/listeners', def test_applications_miss_colon(self):
'{"*:7080":{"application":"app"}}'), 'listeners wildcard') self.assertIn('error', self.put('/applications', """
self.assertIn('success', self.put('/listeners', {
'{"127.0.0.1:7081":{"application":"app"}}'), 'listeners explicit') "app" {
self.assertIn('success', self.put('/listeners', "type": "python",
'{"[::1]:7082":{"application":"app"}}'), 'listeners explicit ipv6') "workers": 1,
"path": "/app",
"module": "wsgi"
}
}
"""), 'miss colon')
def test_applications_miss_comma(self):
self.assertIn('error', self.put('/applications', """
{
"app": {
"type": "python"
"workers": 1,
"path": "/app",
"module": "wsgi"
}
}
"""), 'miss comma')
def test_applications_skip_spaces(self):
self.assertIn('success', self.put('/applications', b'{ \n\r\t}'),
'skip spaces')
def test_applications_relative_path(self):
self.assertIn('success', self.put('/applications', """
{
"app": {
"type": "python",
"workers": 1,
"path": "../app",
"module": "wsgi"
}
}
"""), 'relative path')
@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', self.assertIn('error', self.put('/listeners',
'{"127.0.0.1":{"application":"app"}}'), 'listeners no port') '{"*:7080":{"application":"app"}}'), 'listeners no app')
def test_listeners_wildcard(self):
self.assertIn('success', self.put('/', """
{
"listeners": {
"*:7080": {
"application":"app"
}
},
"applications": {
"app": {
"type": "python",
"workers": 1,
"path": "/app",
"module": "wsgi"
}
}
}
"""), 'listeners wildcard')
def test_listeners_explicit(self):
self.assertIn('success', self.put('/', """
{
"listeners": {
"127.0.0.1:7081": {
"application":"app"
}
},
"applications": {
"app": {
"type": "python",
"workers": 1,
"path": "/app",
"module": "wsgi"
}
}
}
"""), 'explicit')
def test_listeners_explicit_ipv6(self):
self.assertIn('success', self.put('/', """
{
"listeners": {
"[::1]:7082": {
"application":"app"
}
},
"applications": {
"app": {
"type": "python",
"workers": 1,
"path": "/app",
"module": "wsgi"
}
}
}
"""), 'explicit ipv6')
def test_listeners_no_port(self):
self.assertIn('success', self.put('/', """
{
"listeners": {
"[::1]:7082": {
"application":"app"
}
},
"applications": {
"app": {
"type": "python",
"workers": 1,
"path": "/app",
"module": "wsgi"
}
}
}
"""), 'no port')
if __name__ == '__main__': if __name__ == '__main__':
unittest.main() unittest.main()

View File

@@ -6,27 +6,34 @@ class TestUnitBasic(unit.TestUnitControl):
def setUpClass(): def setUpClass():
unit.TestUnit().check_modules('php') unit.TestUnit().check_modules('php')
def test_php_get(self): conf_app = """
resp = self.get() {
self.assertEqual(resp, {'listeners': {}, 'applications': {}}, 'empty') "app": {
self.assertEqual(self.get('/listeners'), {}, 'empty listeners prefix') "type": "php",
self.assertEqual(self.get('/applications'), {}, "workers": 1,
'empty applications prefix') "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() resp = self.get()
self.assertEqual(resp['listeners'], {}, 'php empty listeners') self.assertEqual(resp['listeners'], {}, 'listeners')
self.assertEqual(resp['applications'], self.assertEqual(resp['applications'],
{ {
"app": { "app": {
@@ -36,7 +43,10 @@ class TestUnitBasic(unit.TestUnitControl):
"index": "index.php" "index": "index.php"
} }
}, },
'php applications') 'applications')
def test_php_get_applications_prefix(self):
self.put('/applications', self.conf_app)
self.assertEqual(self.get('/applications'), self.assertEqual(self.get('/applications'),
{ {
@@ -47,7 +57,10 @@ class TestUnitBasic(unit.TestUnitControl):
"index": "index.php" "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'), self.assertEqual(self.get('/applications/app'),
{ {
@@ -56,102 +69,67 @@ class TestUnitBasic(unit.TestUnitControl):
"root": "/app", "root": "/app",
"index": "index.php" "index": "index.php"
}, },
'php applications prefix 2') 'applications prefix 2')
self.assertEqual(self.get('/applications/app/type'), 'php', def test_php_get_applications_prefix_3(self):
'php applications type') self.put('/applications', self.conf_app)
self.assertEqual(self.get('/applications/app/workers'), 1,
'php applications workers')
self.put('/listeners', '{"*:7080":{"application":"app"}}') self.assertEqual(self.get('/applications/app/type'), 'php', 'type')
self.assertEqual(self.get('/applications/app/workers'), 1, 'workers')
def test_php_get_listeners(self):
self.put('/', self.conf_basic)
self.assertEqual(self.get()['listeners'], self.assertEqual(self.get()['listeners'],
{"*:7080":{"application":"app"}}, 'php listeners') {"*:7080":{"application":"app"}}, 'listeners')
def test_php_get_listeners_prefix(self):
self.put('/', self.conf_basic)
self.assertEqual(self.get('/listeners'), self.assertEqual(self.get('/listeners'),
{"*:7080":{"application":"app"}}, 'php listeners prefix') {"*:7080":{"application":"app"}}, 'listeners prefix')
def test_php_get_listeners_prefix_2(self):
self.put('/', self.conf_basic)
self.assertEqual(self.get('/listeners/*:7080'), self.assertEqual(self.get('/listeners/*:7080'),
{"application":"app"}, 'php listeners prefix 2') {"application":"app"}, 'listeners prefix 2')
self.assertEqual(self.get('/listeners/*:7080/application'), 'app',
'php listeners application')
def test_php_put(self):
self.put('/', """
{
"listeners": {
"*:7080": {
"application": "app"
}
},
"applications": {
"app": {
"type": "php",
"workers": 1,
"root": "/app",
"index": "index.php"
}
}
}
""")
resp = self.get()
self.assertEqual(resp['listeners'], {"*:7080":{"application":"app"}},
'put listeners')
self.assertEqual(resp['applications'],
{
"app": {
"type": "php",
"workers": 1,
"root": "/app",
"index": "index.php"
}
},
'put applications')
def test_php_change_listener(self):
self.put('/', self.conf_basic)
self.put('/listeners', '{"*:7081":{"application":"app"}}') self.put('/listeners', '{"*:7081":{"application":"app"}}')
self.assertEqual(self.get('/listeners'),
{"*:7081": {"application":"app"}}, 'put listeners prefix')
self.assertEqual(self.get('/listeners'),
{"*:7081": {"application":"app"}}, 'change listener')
def test_php_add_listener(self):
self.put('/', self.conf_basic)
self.put('/listeners/*:7082', '{"application":"app"}') self.put('/listeners/*:7082', '{"application":"app"}')
self.assertEqual(self.get('/listeners'), self.assertEqual(self.get('/listeners'),
{ {
"*:7081": { "*:7080": {
"application": "app" "application": "app"
}, },
"*:7082": { "*:7082": {
"application": "app" "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.put('/applications/app/workers', '30')
self.assertEqual(self.get('/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.put('/applications/app/root', '"/www"')
self.assertEqual(self.get('/applications/app/root'), '/www', self.assertEqual(self.get('/applications/app/root'), '/www',
'put applications root') 'change application root')
def test_php_delete(self): def test_php_delete(self):
self.put('/', """ self.put('/', self.conf_basic)
{
"listeners": {
"*:7080": {
"application": "app"
}
},
"applications": {
"app": {
"type": "php",
"workers": 1,
"root": "/app",
"index": "index.php"
}
}
}
""")
self.assertIn('error', self.delete('/applications/app'), self.assertIn('error', self.delete('/applications/app'),
'delete app before listener') 'delete app before listener')

View File

@@ -9,7 +9,25 @@ class TestUnitApplication(unit.TestUnitControl):
u.check_modules('python') u.check_modules('python')
u.check_version('0.4') 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 = """ code, name = """
def application(environ, start_response): def application(environ, start_response):
@@ -26,7 +44,6 @@ def application(environ, start_response):
('Http-Host', environ.get('HTTP_HOST')), ('Http-Host', environ.get('HTTP_HOST')),
('Remote-Addr', environ.get('REMOTE_ADDR')), ('Remote-Addr', environ.get('REMOTE_ADDR')),
('Server-Name', environ.get('SERVER_NAME')), ('Server-Name', environ.get('SERVER_NAME')),
('Server-Port', environ.get('SERVER_PORT')),
('Server-Protocol', environ.get('SERVER_PROTOCOL')), ('Server-Protocol', environ.get('SERVER_PROTOCOL')),
('Custom-Header', environ.get('HTTP_CUSTOM_HEADER')) ('Custom-Header', environ.get('HTTP_CUSTOM_HEADER'))
]) ])
@@ -35,24 +52,7 @@ def application(environ, start_response):
""", 'py_app' """, 'py_app'
self.python_application(name, code) self.python_application(name, code)
self.put('/', self.conf % (self.testdir + '/' + name))
self.put('/', """
{
"listeners": {
"*:7080": {
"application": "app"
}
},
"applications": {
"app": {
"type": "python",
"workers": 1,
"path": "%s",
"module": "wsgi"
}
}
}
""" % (self.testdir + '/' + name))
body = 'Test body string.' body = 'Test body string.'
@@ -63,28 +63,45 @@ def application(environ, start_response):
}, data=body) }, data=body)
self.assertEqual(r.status_code, 200, 'status') self.assertEqual(r.status_code, 200, 'status')
self.assertEqual(r.headers['Content-Length'], str(len(body)), headers = dict(r.headers)
'header content length') self.assertRegex(headers.pop('Server'), r'unit/[\d\.]+',
self.assertEqual(r.headers['Content-Type'], 'text/html', 'server header')
'header content type') self.assertDictEqual(headers, {
self.assertEqual(r.headers['Request-Method'], 'POST', 'Content-Length': str(len(body)),
'header request method') 'Content-Type': 'text/html',
self.assertEqual(r.headers['Request-Uri'], '/', 'header request uri') 'Request-Method': 'POST',
self.assertEqual(r.headers['Path-Info'], '/', 'header path info') 'Request-Uri': '/',
self.assertEqual(r.headers['Http-Host'], 'localhost', 'Path-Info': '/',
'header http host') 'Http-Host': 'localhost',
self.assertEqual(r.headers['Remote-Addr'], '127.0.0.1', 'Server-Name': 'localhost',
'header remote addr') 'Remote-Addr': '127.0.0.1',
'Server-Protocol': 'HTTP/1.1',
self.assertTry('assertEqual', 'header server port', 'Custom-Header': 'blah'
r.headers['Server-Port'], '7080') }, 'headers')
self.assertEqual(r.headers['Server-Protocol'], 'HTTP/1.1',
'header server protocol')
self.assertEqual(r.headers['Custom-Header'], 'blah',
'header custom header')
self.assertEqual(r.content, str.encode(body), 'body') 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__': if __name__ == '__main__':
unittest.main() unittest.main()

View File

@@ -22,7 +22,7 @@ atexit.register(create_file)
def application(env, start_response): def application(env, start_response):
start_response('200 OK', [('Content-Type','text/html')]) start_response('200 OK', [('Content-Type','text/html')])
return [b'body'] return []
""" % (self.testdir + '/atexit'), 'py_app' """ % (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, self.assertEqual(os.path.exists(self.testdir + '/atexit'), True,
'python atexit') 'python atexit')

View File

@@ -6,27 +6,44 @@ class TestUnitBasic(unit.TestUnitControl):
def setUpClass(): def setUpClass():
unit.TestUnit().check_modules('python') unit.TestUnit().check_modules('python')
def test_python_get(self): conf_app = """
resp = self.get() {
self.assertEqual(resp, {'listeners': {}, 'applications': {}}, 'empty') "app": {
self.assertEqual(self.get('/listeners'), {}, 'empty listeners prefix') "type": "python",
self.assertEqual(self.get('/applications'), {}, "workers": 1,
'empty applications prefix') "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() resp = self.get()
self.assertEqual(resp['listeners'], {}, 'python empty listeners') self.assertEqual(resp['listeners'], {}, 'listeners')
self.assertEqual(resp['applications'], self.assertEqual(resp['applications'],
{ {
"app": { "app": {
@@ -36,7 +53,10 @@ class TestUnitBasic(unit.TestUnitControl):
"module": "wsgi" "module": "wsgi"
} }
}, },
'python applications') 'applications')
def test_python_get_applications_prefix(self):
self.put('/applications', self.conf_app)
self.assertEqual(self.get('/applications'), self.assertEqual(self.get('/applications'),
{ {
@@ -47,7 +67,10 @@ class TestUnitBasic(unit.TestUnitControl):
"module":"wsgi" "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'), self.assertEqual(self.get('/applications/app'),
{ {
@@ -56,102 +79,67 @@ class TestUnitBasic(unit.TestUnitControl):
"path": "/app", "path": "/app",
"module": "wsgi" "module": "wsgi"
}, },
'python applications prefix 2') 'applications prefix 2')
self.assertEqual(self.get('/applications/app/type'), 'python', def test_python_get_applications_prefix_3(self):
'python applications type') self.put('/applications', self.conf_app)
self.assertEqual(self.get('/applications/app/workers'), 1,
'python applications workers')
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'], self.assertEqual(self.get()['listeners'],
{"*:7080":{"application":"app"}}, 'python listeners') {"*:7080":{"application":"app"}}, 'listeners')
def test_python_get_listeners_prefix(self):
self.put('/', self.conf_basic)
self.assertEqual(self.get('/listeners'), self.assertEqual(self.get('/listeners'),
{"*:7080":{"application":"app"}}, 'python listeners prefix') {"*:7080":{"application":"app"}}, 'listeners prefix')
def test_python_get_listeners_prefix_2(self):
self.put('/', self.conf_basic)
self.assertEqual(self.get('/listeners/*:7080'), self.assertEqual(self.get('/listeners/*:7080'),
{"application":"app"}, 'python listeners prefix 2') {"application":"app"}, 'listeners prefix 2')
self.assertEqual(self.get('/listeners/*:7080/application'), 'app',
'python listeners application')
def test_python_put(self):
self.put('/', """
{
"listeners": {
"*:7080": {
"application": "app"
}
},
"applications": {
"app": {
"type": "python",
"workers": 1,
"path": "/app",
"module": "wsgi"
}
}
}
""")
resp = self.get()
self.assertEqual(resp['listeners'], {"*:7080":{"application":"app"}},
'put listeners')
self.assertEqual(resp['applications'],
{
"app": {
"type": "python",
"workers": 1,
"path": "/app",
"module": "wsgi"
}
},
'put applications')
def test_python_change_listener(self):
self.put('/', self.conf_basic)
self.put('/listeners', '{"*:7081":{"application":"app"}}') self.put('/listeners', '{"*:7081":{"application":"app"}}')
self.assertEqual(self.get('/listeners'),
{"*:7081": {"application":"app"}}, 'put listeners prefix')
self.assertEqual(self.get('/listeners'),
{"*:7081": {"application":"app"}}, 'change listener')
def test_python_add_listener(self):
self.put('/', self.conf_basic)
self.put('/listeners/*:7082', '{"application":"app"}') self.put('/listeners/*:7082', '{"application":"app"}')
self.assertEqual(self.get('/listeners'), self.assertEqual(self.get('/listeners'),
{ {
"*:7081": { "*:7080": {
"application": "app" "application": "app"
}, },
"*:7082": { "*:7082": {
"application": "app" "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.put('/applications/app/workers', '30')
self.assertEqual(self.get('/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.put('/applications/app/path', '"/www"')
self.assertEqual(self.get('/applications/app/path'), '/www', self.assertEqual(self.get('/applications/app/path'), '/www',
'put applications path') 'change application path')
def test_python_delete(self): def test_python_delete(self):
self.put('/', """ self.put('/', self.conf_basic)
{
"listeners": {
"*:7080": {
"application": "app"
}
},
"applications": {
"app": {
"type": "python",
"workers": 1,
"path": "/app",
"module": "wsgi"
}
}
}
""")
self.assertIn('error', self.delete('/applications/app'), self.assertIn('error', self.delete('/applications/app'),
'delete app before listener') 'delete app before listener')

View File

@@ -28,10 +28,6 @@ class TestUnit(unittest.TestCase):
if '--leave' not in sys.argv: if '--leave' not in sys.argv:
shutil.rmtree(self.testdir) 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): def check_modules(self, *modules):
self._run() self._run()