summaryrefslogtreecommitdiffhomepage
path: root/test
diff options
context:
space:
mode:
authorTiago Natel <t.nateldemoura@f5.com>2019-11-11 14:36:22 +0000
committerTiago Natel <t.nateldemoura@f5.com>2019-11-11 14:36:22 +0000
commitc6a11260e8ad5fa64cb025f72aab343aea9f1d63 (patch)
tree822d7cd3ed1eac6b9337256197cde76fa6ce28ff /test
parentabbad122bb48cb1d8b650158a3e6180dbe5ee9e5 (diff)
downloadunit-c6a11260e8ad5fa64cb025f72aab343aea9f1d63.tar.gz
unit-c6a11260e8ad5fa64cb025f72aab343aea9f1d63.tar.bz2
Tests: added tests for setting user and group.
Diffstat (limited to 'test')
-rw-r--r--test/python/user_group/wsgi.py18
-rw-r--r--test/test_python_application.py77
2 files changed, 95 insertions, 0 deletions
diff --git a/test/python/user_group/wsgi.py b/test/python/user_group/wsgi.py
new file mode 100644
index 00000000..f5deb87d
--- /dev/null
+++ b/test/python/user_group/wsgi.py
@@ -0,0 +1,18 @@
+import json
+import os
+
+def application(environ, start_response):
+ uid = os.geteuid()
+ gid = os.getegid()
+
+ out = json.dumps({
+ 'UID': uid,
+ 'GID': gid,
+ }).encode('utf-8')
+
+ start_response('200 OK', [
+ ('Content-Length', str(len(out))),
+ ('Content-Type', 'application/json')
+ ])
+
+ return [out]
diff --git a/test/test_python_application.py b/test/test_python_application.py
index 5e1ba65a..818816d0 100644
--- a/test/test_python_application.py
+++ b/test/test_python_application.py
@@ -1,4 +1,7 @@
import re
+import os
+import grp
+import pwd
import time
import unittest
from unit.applications.lang.python import TestApplicationPython
@@ -676,5 +679,79 @@ Connection: close
len(self.findall(r'Traceback')), 8, 'traceback count 8'
)
+ def test_python_user_group(self):
+ if not self.is_su:
+ print("requires root")
+ raise unittest.SkipTest()
+
+ nobody_uid = pwd.getpwnam('nobody').pw_uid
+
+ group = 'nobody'
+
+ try:
+ group_id = grp.getgrnam(group).gr_gid
+ except:
+ group = 'nogroup'
+ group_id = grp.getgrnam(group).gr_gid
+
+ self.load('user_group')
+
+ obj = self.getjson()['body']
+ self.assertEqual(obj['UID'], nobody_uid, 'nobody uid')
+ self.assertEqual(obj['GID'], group_id, 'nobody gid')
+
+ self.load('user_group', user='nobody')
+
+ obj = self.getjson()['body']
+ self.assertEqual(obj['UID'], nobody_uid, 'nobody uid user=nobody')
+ self.assertEqual(obj['GID'], group_id, 'nobody gid user=nobody')
+
+ self.load('user_group', user='nobody', group=group)
+
+ obj = self.getjson()['body']
+ self.assertEqual(
+ obj['UID'], nobody_uid, 'nobody uid user=nobody group=%s' % group
+ )
+
+ self.assertEqual(
+ obj['GID'], group_id, 'nobody gid user=nobody group=%s' % group
+ )
+
+ self.load('user_group', group=group)
+
+ obj = self.getjson()['body']
+ self.assertEqual(
+ obj['UID'], nobody_uid, 'nobody uid group=%s' % group
+ )
+
+ self.assertEqual(obj['GID'], group_id, 'nobody gid group=%s' % group)
+
+ self.load('user_group', user='root')
+
+ obj = self.getjson()['body']
+ self.assertEqual(obj['UID'], 0, 'root uid user=root')
+ self.assertEqual(obj['GID'], 0, 'root gid user=root')
+
+ group = 'root'
+
+ try:
+ grp.getgrnam(group)
+ group = True
+ except:
+ group = False
+
+ if group:
+ self.load('user_group', user='root', group='root')
+
+ obj = self.getjson()['body']
+ self.assertEqual(obj['UID'], 0, 'root uid user=root group=root')
+ self.assertEqual(obj['GID'], 0, 'root gid user=root group=root')
+
+ self.load('user_group', group='root')
+
+ obj = self.getjson()['body']
+ self.assertEqual(obj['UID'], nobody_uid, 'root uid group=root')
+ self.assertEqual(obj['GID'], 0, 'root gid group=root')
+
if __name__ == '__main__':
TestPythonApplication.main()