|
| 1 | +import os |
| 2 | +import re |
| 3 | +import subprocess |
| 4 | +from pathlib import Path |
| 5 | + |
1 | 6 | import pytest
|
2 | 7 | from unit.applications.lang.python import TestApplicationPython
|
3 | 8 | from unit.option import option
|
|
9 | 14 | class TestPythonIsolation(TestApplicationPython):
|
10 | 15 | prerequisites = {'modules': {'python': 'any'}, 'features': ['isolation']}
|
11 | 16 |
|
| 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 | + |
12 | 34 | def test_python_isolation_rootfs(self, is_su, temp_dir):
|
13 | 35 | isolation_features = option.available['features']['isolation'].keys()
|
14 | 36 |
|
@@ -102,3 +124,104 @@ def test_python_isolation_procfs(self, is_su, temp_dir):
|
102 | 124 | assert (
|
103 | 125 | self.getjson(url='/?path=/proc/self')['body']['FileExists'] == True
|
104 | 126 | ), '/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