Skip to content

Commit 4b39cb1

Browse files
Tests: added tests for "path" option in isolation/cgroup.
1 parent 24e3f17 commit 4b39cb1

File tree

1 file changed

+123
-0
lines changed

1 file changed

+123
-0
lines changed

test/test_python_isolation.py

Lines changed: 123 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,8 @@
1+
import os
2+
import re
3+
import subprocess
4+
from pathlib import Path
5+
16
import pytest
27
from unit.applications.lang.python import TestApplicationPython
38
from unit.option import option
@@ -9,6 +14,23 @@
914
class TestPythonIsolation(TestApplicationPython):
1015
prerequisites = {'modules': {'python': 'any'}, 'features': ['isolation']}
1116

17+
def get_cgroup(self, app_name):
18+
output = subprocess.check_output(
19+
['ps', 'ax', '-o', 'pid', '-o', 'cmd']
20+
).decode()
21+
22+
pid = re.search(
23+
r'(\d+)\s*unit: "' + app_name + '" application', output
24+
).group(1)
25+
26+
cgroup = '/proc/' + pid + '/cgroup'
27+
28+
if not os.path.isfile(cgroup):
29+
pytest.skip('no cgroup at ' + cgroup)
30+
31+
with open(cgroup, 'r') as f:
32+
return f.read().rstrip()
33+
1234
def test_python_isolation_rootfs(self, is_su, temp_dir):
1335
isolation_features = option.available['features']['isolation'].keys()
1436

@@ -102,3 +124,104 @@ def test_python_isolation_procfs(self, is_su, temp_dir):
102124
assert (
103125
self.getjson(url='/?path=/proc/self')['body']['FileExists'] == True
104126
), '/proc/self'
127+
128+
def test_python_isolation_cgroup(self, is_su, temp_dir):
129+
if not is_su:
130+
pytest.skip('requires root')
131+
132+
if not 'cgroup' in option.available['features']['isolation']:
133+
pytest.skip('cgroup is not supported')
134+
135+
def set_cgroup_path(path):
136+
isolation = {'cgroup': {'path': path}}
137+
self.load('empty', processes=1, isolation=isolation)
138+
139+
set_cgroup_path('scope/python')
140+
141+
cgroup_rel = Path(self.get_cgroup('empty'))
142+
assert cgroup_rel.parts[-2:] == ('scope', 'python'), 'cgroup rel'
143+
144+
set_cgroup_path('/scope2/python')
145+
146+
cgroup_abs = Path(self.get_cgroup('empty'))
147+
assert cgroup_abs.parts[-2:] == ('scope2', 'python'), 'cgroup abs'
148+
149+
assert len(cgroup_rel.parts) >= len(cgroup_abs.parts)
150+
151+
def test_python_isolation_cgroup_two(self, is_su, temp_dir):
152+
if not is_su:
153+
pytest.skip('requires root')
154+
155+
if not 'cgroup' in option.available['features']['isolation']:
156+
pytest.skip('cgroup is not supported')
157+
158+
def set_two_cgroup_path(path, path2):
159+
script_path = option.test_dir + '/python/empty'
160+
161+
assert 'success' in self.conf(
162+
{
163+
"listeners": {
164+
"*:7080": {"pass": "applications/one"},
165+
"*:7081": {"pass": "applications/two"},
166+
},
167+
"applications": {
168+
"one": {
169+
"type": "python",
170+
"processes": 1,
171+
"path": script_path,
172+
"working_directory": script_path,
173+
"module": "wsgi",
174+
"isolation": {
175+
'cgroup': {'path': path},
176+
},
177+
},
178+
"two": {
179+
"type": "python",
180+
"processes": 1,
181+
"path": script_path,
182+
"working_directory": script_path,
183+
"module": "wsgi",
184+
"isolation": {
185+
'cgroup': {'path': path2},
186+
},
187+
},
188+
},
189+
}
190+
)
191+
192+
set_two_cgroup_path('/scope/python', '/scope/python')
193+
assert self.get_cgroup('one') == self.get_cgroup('two')
194+
195+
set_two_cgroup_path('/scope/python', '/scope2/python')
196+
assert self.get_cgroup('one') != self.get_cgroup('two')
197+
198+
def test_python_isolation_cgroup_invalid(self, is_su):
199+
if not is_su:
200+
pytest.skip('requires root')
201+
202+
if not 'cgroup' in option.available['features']['isolation']:
203+
pytest.skip('cgroup is not supported')
204+
205+
def check_invalid(path):
206+
script_path = option.test_dir + '/python/empty'
207+
assert 'error' in self.conf(
208+
{
209+
"listeners": {"*:7080": {"pass": "applications/empty"}},
210+
"applications": {
211+
"empty": {
212+
"type": "python",
213+
"processes": {"spare": 0},
214+
"path": script_path,
215+
"working_directory": script_path,
216+
"module": "wsgi",
217+
"isolation": {
218+
'cgroup': {'path': path},
219+
},
220+
}
221+
},
222+
}
223+
)
224+
225+
check_invalid('')
226+
check_invalid('../scope')
227+
check_invalid('scope/../python')

0 commit comments

Comments
 (0)