Tests: added methods to manage unit configuration.

This commit is contained in:
Andrey Zelenkov
2018-01-30 16:16:52 +03:00
parent f115cb7032
commit 9f48f2b3e7
6 changed files with 240 additions and 253 deletions

View File

@@ -7,10 +7,10 @@ class TestUnitConfiguration(unit.TestUnitControl):
unit.TestUnit().check_modules('python')
def test_json_leading_zero(self):
self.assertIn('error', self.put('/', '00'), 'leading zero')
self.assertIn('error', self.conf('00'), 'leading zero')
def test_json_unicode(self):
self.assertIn('success', self.put('/applications', b"""
self.assertIn('success', self.conf(b"""
{
"ap\u0070": {
"type": "\u0070ython",
@@ -19,22 +19,20 @@ class TestUnitConfiguration(unit.TestUnitControl):
"module": "wsgi"
}
}
"""), 'unicode')
""", '/applications'), 'unicode')
def test_json_unicode_2(self):
self.assertIn('success', self.put('/applications', """
{
"приложение": {
"type": "python",
"workers": 1,
"path": "/app",
"module": "wsgi"
}
self.assertIn('success', self.conf({
"приложение": {
"type": "python",
"workers": 1,
"path": "/app",
"module": "wsgi"
}
"""), 'unicode 2')
}, '/applications'), 'unicode 2')
def test_json_unicode_number(self):
self.assertIn('error', self.put('/applications', b"""
self.assertIn('error', self.conf(b"""
{
"app": {
"type": "python",
@@ -43,39 +41,35 @@ class TestUnitConfiguration(unit.TestUnitControl):
"module": "wsgi"
}
}
"""), 'unicode number')
""", '/applications'), 'unicode number')
def test_applications_open_brace(self):
self.assertIn('error', self.put('/applications', '{'), 'open brace')
self.assertIn('error', self.conf('{', '/applications'), 'open brace')
def test_applications_string(self):
self.assertIn('error', self.put('/applications', '"{}"'), 'string')
self.assertIn('error', self.conf('"{}"', '/applications'), 'string')
@unittest.expectedFailure
def test_negative_workers(self):
self.assertIn('error', self.put('/applications', """
{
"app": {
"type": "python",
"workers": -1,
"path": "/app",
"module": "wsgi"
}
self.assertIn('error', self.conf({
"app": {
"type": "python",
"workers": -1,
"path": "/app",
"module": "wsgi"
}
"""), 'negative workers')
}, '/applications'), 'negative workers')
@unittest.expectedFailure
def test_applications_type_only(self):
self.assertIn('error', self.put('/applications', """
{
"app": {
"type": "python"
}
self.assertIn('error', self.conf({
"app": {
"type": "python"
}
"""), 'type only')
}, '/applications'), 'type only')
def test_applications_miss_quote(self):
self.assertIn('error', self.put('/applications', """
self.assertIn('error', self.conf("""
{
app": {
"type": "python",
@@ -84,10 +78,10 @@ class TestUnitConfiguration(unit.TestUnitControl):
"module": "wsgi"
}
}
"""), 'miss quote')
""", '/applications'), 'miss quote')
def test_applications_miss_colon(self):
self.assertIn('error', self.put('/applications', """
self.assertIn('error', self.conf("""
{
"app" {
"type": "python",
@@ -96,10 +90,10 @@ class TestUnitConfiguration(unit.TestUnitControl):
"module": "wsgi"
}
}
"""), 'miss colon')
""", '/applications'), 'miss colon')
def test_applications_miss_comma(self):
self.assertIn('error', self.put('/applications', """
self.assertIn('error', self.conf("""
{
"app": {
"type": "python"
@@ -108,108 +102,98 @@ class TestUnitConfiguration(unit.TestUnitControl):
"module": "wsgi"
}
}
"""), 'miss comma')
""", '/applications'), 'miss comma')
def test_applications_skip_spaces(self):
self.assertIn('success', self.put('/applications', b'{ \n\r\t}'),
self.assertIn('success', self.conf(b'{ \n\r\t}', '/applications'),
'skip spaces')
def test_applications_relative_path(self):
self.assertIn('success', self.put('/applications', """
{
"app": {
"type": "python",
"workers": 1,
"path": "../app",
"module": "wsgi"
}
self.assertIn('success', self.conf({
"app": {
"type": "python",
"workers": 1,
"path": "../app",
"module": "wsgi"
}
"""), 'relative path')
}, '/applications'), 'relative path')
@unittest.expectedFailure
def test_listeners_empty(self):
self.assertIn('error', self.put('/listeners', '{"*:7080":{}}'),
self.assertIn('error', self.conf({"*:7080":{}}, '/listeners'),
'listener empty')
def test_listeners_no_app(self):
self.assertIn('error', self.put('/listeners',
'{"*:7080":{"application":"app"}}'), 'listeners no app')
self.assertIn('error', self.conf({"*:7080":{"application":"app"}},
'/listeners'), '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"
}
self.assertIn('success', self.conf({
"listeners": {
"*:7080": {
"application":"app"
}
},
"applications": {
"app": {
"type": "python",
"workers": 1,
"path": "/app",
"module": "wsgi"
}
}
"""), 'listeners wildcard')
}), 'listeners wildcard')
def test_listeners_explicit(self):
self.assertIn('success', self.put('/', """
{
"listeners": {
"127.0.0.1:7080": {
"application":"app"
}
},
"applications": {
"app": {
"type": "python",
"workers": 1,
"path": "/app",
"module": "wsgi"
}
self.assertIn('success', self.conf({
"listeners": {
"127.0.0.1:7080": {
"application":"app"
}
},
"applications": {
"app": {
"type": "python",
"workers": 1,
"path": "/app",
"module": "wsgi"
}
}
"""), 'explicit')
}), 'explicit')
def test_listeners_explicit_ipv6(self):
self.assertIn('success', self.put('/', """
{
"listeners": {
"[::1]:7080": {
"application":"app"
}
},
"applications": {
"app": {
"type": "python",
"workers": 1,
"path": "/app",
"module": "wsgi"
}
self.assertIn('success', self.conf({
"listeners": {
"[::1]:7080": {
"application":"app"
}
},
"applications": {
"app": {
"type": "python",
"workers": 1,
"path": "/app",
"module": "wsgi"
}
}
"""), 'explicit ipv6')
}), 'explicit ipv6')
def test_listeners_no_port(self):
self.assertIn('error', self.put('/', """
{
"listeners": {
"127.0.0.1": {
"application":"app"
}
},
"applications": {
"app": {
"type": "python",
"workers": 1,
"path": "/app",
"module": "wsgi"
}
self.assertIn('error', self.conf({
"listeners": {
"127.0.0.1": {
"application":"app"
}
},
"applications": {
"app": {
"type": "python",
"workers": 1,
"path": "/app",
"module": "wsgi"
}
}
"""), 'no port')
}), 'no port')
if __name__ == '__main__':
unittest.main()

View File

@@ -6,35 +6,31 @@ class TestUnitBasic(unit.TestUnitControl):
def setUpClass():
unit.TestUnit().check_modules('php')
conf_app = """
{
"app": {
"type": "php",
"workers": 1,
"root": "/app",
"index": "index.php"
}
conf_app = {
"app": {
"type": "php",
"workers": 1,
"root": "/app",
"index": "index.php"
}
"""
}
conf_basic = """
{
"listeners": {
"*:7080": {
"application": "app"
}
},
"applications": %s
}
""" % (conf_app)
conf_basic = {
"listeners": {
"*:7080": {
"application": "app"
}
},
"applications": conf_app
}
def test_php_get_applications(self):
self.put('/applications', self.conf_app)
self.conf(self.conf_app, '/applications')
resp = self.get()
conf = self.conf_get()
self.assertEqual(resp['listeners'], {}, 'listeners')
self.assertEqual(resp['applications'],
self.assertEqual(conf['listeners'], {}, 'listeners')
self.assertEqual(conf['applications'],
{
"app": {
"type": "php",
@@ -46,9 +42,9 @@ class TestUnitBasic(unit.TestUnitControl):
'applications')
def test_php_get_applications_prefix(self):
self.put('/applications', self.conf_app)
self.conf(self.conf_app, '/applications')
self.assertEqual(self.get('/applications'),
self.assertEqual(self.conf_get('/applications'),
{
"app": {
"type": "php",
@@ -60,9 +56,9 @@ class TestUnitBasic(unit.TestUnitControl):
'applications prefix')
def test_php_get_applications_prefix_2(self):
self.put('/applications', self.conf_app)
self.conf(self.conf_app, '/applications')
self.assertEqual(self.get('/applications/app'),
self.assertEqual(self.conf_get('/applications/app'),
{
"type": "php",
"workers": 1,
@@ -72,41 +68,43 @@ class TestUnitBasic(unit.TestUnitControl):
'applications prefix 2')
def test_php_get_applications_prefix_3(self):
self.put('/applications', self.conf_app)
self.conf(self.conf_app, '/applications')
self.assertEqual(self.get('/applications/app/type'), 'php', 'type')
self.assertEqual(self.get('/applications/app/workers'), 1, 'workers')
self.assertEqual(self.conf_get('/applications/app/type'), 'php',
'type')
self.assertEqual(self.conf_get('/applications/app/workers'), 1,
'workers')
def test_php_get_listeners(self):
self.put('/', self.conf_basic)
self.conf(self.conf_basic)
self.assertEqual(self.get()['listeners'],
self.assertEqual(self.conf_get()['listeners'],
{"*:7080":{"application":"app"}}, 'listeners')
def test_php_get_listeners_prefix(self):
self.put('/', self.conf_basic)
self.conf(self.conf_basic)
self.assertEqual(self.get('/listeners'),
self.assertEqual(self.conf_get('/listeners'),
{"*:7080":{"application":"app"}}, 'listeners prefix')
def test_php_get_listeners_prefix_2(self):
self.put('/', self.conf_basic)
self.conf(self.conf_basic)
self.assertEqual(self.get('/listeners/*:7080'),
self.assertEqual(self.conf_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.conf(self.conf_basic)
self.conf({"*:7081":{"application":"app"}}, '/listeners')
self.assertEqual(self.get('/listeners'),
self.assertEqual(self.conf_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.conf(self.conf_basic)
self.conf({"application":"app"}, '/listeners/*:7082')
self.assertEqual(self.get('/listeners'),
self.assertEqual(self.conf_get('/listeners'),
{
"*:7080": {
"application": "app"
@@ -118,26 +116,26 @@ class TestUnitBasic(unit.TestUnitControl):
'add listener')
def test_php_change_application(self):
self.put('/', self.conf_basic)
self.conf(self.conf_basic)
self.put('/applications/app/workers', '30')
self.assertEqual(self.get('/applications/app/workers'), 30,
self.conf('30', '/applications/app/workers')
self.assertEqual(self.conf_get('/applications/app/workers'), 30,
'change application workers')
self.put('/applications/app/root', '"/www"')
self.assertEqual(self.get('/applications/app/root'), '/www',
self.conf('"/www"', '/applications/app/root')
self.assertEqual(self.conf_get('/applications/app/root'), '/www',
'change application root')
def test_php_delete(self):
self.put('/', self.conf_basic)
self.conf(self.conf_basic)
self.assertIn('error', self.delete('/applications/app'),
self.assertIn('error', self.conf_delete('/applications/app'),
'delete app before listener')
self.assertIn('success', self.delete('/listeners/*:7080'),
self.assertIn('success', self.conf_delete('/listeners/*:7080'),
'delete listener')
self.assertIn('success', self.delete('/applications/app'),
self.assertIn('success', self.conf_delete('/applications/app'),
'delete app after listener')
self.assertIn('error', self.delete('/applications/app'),
self.assertIn('error', self.conf_delete('/applications/app'),
'delete app again')
if __name__ == '__main__':

View File

@@ -9,8 +9,8 @@ class TestUnitApplication(unit.TestUnitControl):
u.check_modules('python')
u.check_version('0.4')
conf = """
{
def conf_with_name(self, name):
self.conf({
"listeners": {
"*:7080": {
"application": "app"
@@ -20,12 +20,11 @@ class TestUnitApplication(unit.TestUnitControl):
"app": {
"type": "python",
"workers": 1,
"path": "%s",
"path": self.testdir + '/' + name,
"module": "wsgi"
}
}
}
"""
})
def test_python_application_simple(self):
code, name = """
@@ -49,7 +48,7 @@ def application(environ, start_response):
""", 'py_app'
self.python_application(name, code)
self.put('/', self.conf % (self.testdir + '/' + name))
self.conf_with_name(name)
body = 'Test body string.'
@@ -88,7 +87,7 @@ def application(environ, start_response):
""", 'py_app'
self.python_application(name, code)
self.put('/', self.conf % (self.testdir + '/' + name))
self.conf_with_name(name)
r = unit.TestUnitHTTP.get(uri='/?var1=val1&var2=val2', headers={
'Host': 'localhost'
@@ -117,7 +116,7 @@ def application(environ, start_response):
""", 'py_app'
self.python_application(name, code)
self.put('/', self.conf % (self.testdir + '/' + name))
self.conf_with_name(name)
r = unit.TestUnitHTTP.get(headers={'Host': 'localhost'})
@@ -136,7 +135,7 @@ def application(environ, start_response):
""", 'py_app'
self.python_application(name, code)
self.put('/', self.conf % (self.testdir + '/' + name))
self.conf_with_name(name)
r = unit.TestUnitHTTP.get(headers={'Host': 'localhost'})
self.assertNotIn('Transfer-Encoding', r.headers,

View File

@@ -28,32 +28,28 @@ def application(env, start_response):
self.python_application(name, code)
self.put('/', """
{
"listeners": {
"*:7080": {
"application": "app"
}
},
"applications": {
"app": {
"type": "python",
"workers": 1,
"path": "%s",
"module": "wsgi"
}
self.conf({
"listeners": {
"*:7080": {
"application": "app"
}
},
"applications": {
"app": {
"type": "python",
"workers": 1,
"path": self.testdir + '/' + name,
"module": "wsgi"
}
}
""" % (self.testdir + '/' + name))
})
unit.TestUnitHTTP.get()
self.put('/', """
{
"listeners": {},
"applications": {}
}
""")
self.conf({
"listeners": {},
"applications": {}
})
time.sleep(0.2) # wait for 'atexit' file

View File

@@ -6,45 +6,41 @@ class TestUnitBasic(unit.TestUnitControl):
def setUpClass():
unit.TestUnit().check_modules('python')
conf_app = """
{
"app": {
"type": "python",
"workers": 1,
"path": "/app",
"module": "wsgi"
}
conf_app = {
"app": {
"type": "python",
"workers": 1,
"path": "/app",
"module": "wsgi"
}
"""
}
conf_basic = """
{
"listeners": {
"*:7080": {
"application": "app"
}
},
"applications": %s
}
""" % (conf_app)
conf_basic = {
"listeners": {
"*:7080": {
"application": "app"
}
},
"applications": conf_app
}
def test_python_get_empty(self):
self.assertEqual(self.get(), {'listeners': {}, 'applications': {}},
'empty')
self.assertEqual(self.conf_get(),
{'listeners': {}, 'applications': {}}, 'empty')
def test_python_get_prefix_listeners(self):
self.assertEqual(self.get('/listeners'), {}, 'listeners prefix')
self.assertEqual(self.conf_get('/listeners'), {}, 'listeners prefix')
def test_python_get_prefix_applications(self):
self.assertEqual(self.get('/applications'), {}, 'applications prefix')
self.assertEqual(self.conf_get('/applications'), {}, 'applications prefix')
def test_python_get_applications(self):
self.put('/applications', self.conf_app)
self.conf(self.conf_app, '/applications')
resp = self.get()
conf = self.conf_get()
self.assertEqual(resp['listeners'], {}, 'listeners')
self.assertEqual(resp['applications'],
self.assertEqual(conf['listeners'], {}, 'listeners')
self.assertEqual(conf['applications'],
{
"app": {
"type": "python",
@@ -52,13 +48,13 @@ class TestUnitBasic(unit.TestUnitControl):
"path": "/app",
"module": "wsgi"
}
},
'applications')
},
'applications')
def test_python_get_applications_prefix(self):
self.put('/applications', self.conf_app)
self.conf(self.conf_app, '/applications')
self.assertEqual(self.get('/applications'),
self.assertEqual(self.conf_get('/applications'),
{
"app": {
"type": "python",
@@ -70,9 +66,9 @@ class TestUnitBasic(unit.TestUnitControl):
'applications prefix')
def test_python_get_applications_prefix_2(self):
self.put('/applications', self.conf_app)
self.conf(self.conf_app, '/applications')
self.assertEqual(self.get('/applications/app'),
self.assertEqual(self.conf_get('/applications/app'),
{
"type": "python",
"workers": 1,
@@ -82,41 +78,43 @@ class TestUnitBasic(unit.TestUnitControl):
'applications prefix 2')
def test_python_get_applications_prefix_3(self):
self.put('/applications', self.conf_app)
self.conf(self.conf_app, '/applications')
self.assertEqual(self.get('/applications/app/type'), 'python', 'type')
self.assertEqual(self.get('/applications/app/workers'), 1, 'workers')
self.assertEqual(self.conf_get('/applications/app/type'), 'python',
'type')
self.assertEqual(self.conf_get('/applications/app/workers'), 1,
'workers')
def test_python_get_listeners(self):
self.put('/', self.conf_basic)
self.conf(self.conf_basic)
self.assertEqual(self.get()['listeners'],
self.assertEqual(self.conf_get()['listeners'],
{"*:7080":{"application":"app"}}, 'listeners')
def test_python_get_listeners_prefix(self):
self.put('/', self.conf_basic)
self.conf(self.conf_basic)
self.assertEqual(self.get('/listeners'),
self.assertEqual(self.conf_get('/listeners'),
{"*:7080":{"application":"app"}}, 'listeners prefix')
def test_python_get_listeners_prefix_2(self):
self.put('/', self.conf_basic)
self.conf(self.conf_basic)
self.assertEqual(self.get('/listeners/*:7080'),
self.assertEqual(self.conf_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.conf(self.conf_basic)
self.conf({"*:7081":{"application":"app"}}, '/listeners')
self.assertEqual(self.get('/listeners'),
self.assertEqual(self.conf_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.conf(self.conf_basic)
self.conf({"application":"app"}, '/listeners/*:7082')
self.assertEqual(self.get('/listeners'),
self.assertEqual(self.conf_get('/listeners'),
{
"*:7080": {
"application": "app"
@@ -128,26 +126,26 @@ class TestUnitBasic(unit.TestUnitControl):
'add listener')
def test_python_change_application(self):
self.put('/', self.conf_basic)
self.conf(self.conf_basic)
self.put('/applications/app/workers', '30')
self.assertEqual(self.get('/applications/app/workers'), 30,
self.conf('30', '/applications/app/workers')
self.assertEqual(self.conf_get('/applications/app/workers'), 30,
'change application workers')
self.put('/applications/app/path', '"/www"')
self.assertEqual(self.get('/applications/app/path'), '/www',
self.conf('"/www"', '/applications/app/path')
self.assertEqual(self.conf_get('/applications/app/path'), '/www',
'change application path')
def test_python_delete(self):
self.put('/', self.conf_basic)
self.conf(self.conf_basic)
self.assertIn('error', self.delete('/applications/app'),
self.assertIn('error', self.conf_delete('/applications/app'),
'delete app before listener')
self.assertIn('success', self.delete('/listeners/*:7080'),
self.assertIn('success', self.conf_delete('/listeners/*:7080'),
'delete listener')
self.assertIn('success', self.delete('/applications/app'),
self.assertIn('success', self.conf_delete('/applications/app'),
'delete app after listener')
self.assertIn('error', self.delete('/applications/app'),
self.assertIn('error', self.conf_delete('/applications/app'),
'delete app again')
if __name__ == '__main__':

View File

@@ -149,6 +149,18 @@ class TestUnitControl(TestUnit):
# TODO socket reuse
# TODO http client
def conf(self, conf, path='/'):
if isinstance(conf, dict):
conf = json.dumps(conf)
return self._body_json(self.put(path, conf))
def conf_get(self, path='/'):
return self._body_json(self.get(path))
def conf_delete(self, path='/'):
return self._body_json(self.delete(path))
def http(self, req):
with self._control_sock() as sock:
sock.sendall(req)
@@ -167,13 +179,13 @@ class TestUnitControl(TestUnit):
resp = self.http(('GET ' + path
+ ' HTTP/1.1\r\nHost: localhost\r\n\r\n').encode())
return self._body_json(resp)
return resp
def delete(self, path='/'):
resp = self.http(('DELETE ' + path
+ ' HTTP/1.1\r\nHost: localhost\r\n\r\n').encode())
return self._body_json(resp)
return resp
def put(self, path='/', data=''):
if isinstance(data, str):
@@ -183,7 +195,7 @@ class TestUnitControl(TestUnit):
+ 'Content-Length: ' + str(len(data))
+ '\r\n\r\n').encode() + data)
return self._body_json(resp)
return resp
def _control_sock(self):
sock = socket.socket(socket.AF_UNIX, socket.SOCK_STREAM)