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():
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')
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')
def test_json_unicode(self):
self.assertIn('success', self.put('/applications', b"""
{
"ap\u0070": {
@@ -94,6 +21,7 @@ class TestUnitConfiguration(unit.TestUnitControl):
}
"""), 'unicode')
def test_json_unicode_2(self):
self.assertIn('success', self.put('/applications', """
{
"приложение": {
@@ -105,6 +33,7 @@ class TestUnitConfiguration(unit.TestUnitControl):
}
"""), 'unicode 2')
def test_json_unicode_number(self):
self.assertIn('error', self.put('/applications', b"""
{
"app": {
@@ -116,14 +45,105 @@ class TestUnitConfiguration(unit.TestUnitControl):
}
"""), 'unicode number')
def test_json_listeners(self):
self.assertTry('assertIn', 'listener empty', 'error',
self.put('/listeners', '{"*:7080":{}}'))
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",
"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",
"workers": 1,
"path": "/app",
"module": "wsgi"
}
}
"""), 'miss quote')
def test_applications_miss_colon(self):
self.assertIn('error', self.put('/applications', """
{
"app" {
"type": "python",
"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',
'{"*:7080":{"application":"app"}}'), 'listeners no app')
self.put('/applications', """
def test_listeners_wildcard(self):
self.assertIn('success', self.put('/', """
{
"listeners": {
"*:7080": {
"application":"app"
}
},
"applications": {
"app": {
"type": "python",
"workers": 1,
@@ -131,16 +151,65 @@ class TestUnitConfiguration(unit.TestUnitControl):
"module": "wsgi"
}
}
""")
}
"""), 'listeners wildcard')
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')
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__':
unittest.main()

View File

@@ -6,14 +6,7 @@ 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')
self.put('/applications', """
conf_app = """
{
"app": {
"type": "php",
@@ -22,11 +15,25 @@ class TestUnitBasic(unit.TestUnitControl):
"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')
self.assertEqual(self.get('/applications/app/type'), 'php',
'php applications type')
self.assertEqual(self.get('/applications/app/workers'), 1,
'php applications workers')
def test_php_get_applications_prefix_3(self):
self.put('/applications', self.conf_app)
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'],
{"*: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'),
{"*: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'),
{"application":"app"}, 'php 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')
{"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')
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.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')

View File

@@ -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()

View File

@@ -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')

View File

@@ -6,14 +6,7 @@ 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')
self.put('/applications', """
conf_app = """
{
"app": {
"type": "python",
@@ -22,11 +15,35 @@ class TestUnitBasic(unit.TestUnitControl):
"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')
{"*:7080":{"application":"app"}}, 'listeners')
def test_python_get_listeners_prefix(self):
self.put('/', self.conf_basic)
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'),
{"application":"app"}, 'python 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')
{"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')
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.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')

View File

@@ -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()