unit/test/test_python_isolation.py

118 lines
3.6 KiB
Python
Raw Normal View History

2020-10-19 21:25:29 +00:00
import shutil
2020-09-16 20:31:15 +00:00
import pytest
2020-05-28 13:59:52 +00:00
2020-10-19 21:25:29 +00:00
from conftest import unit_run
from conftest import unit_stop
2020-05-28 13:59:52 +00:00
from unit.applications.lang.python import TestApplicationPython
from unit.feature.isolation import TestFeatureIsolation
from unit.option import option
2020-05-28 13:59:52 +00:00
class TestPythonIsolation(TestApplicationPython):
prerequisites = {'modules': {'python': 'any'}, 'features': ['isolation']}
@classmethod
2020-09-16 20:31:15 +00:00
def setup_class(cls, complete_check=True):
2020-10-19 21:25:29 +00:00
check = super().setup_class(complete_check=False)
unit = unit_run()
option.temp_dir = unit['temp_dir']
TestFeatureIsolation().check(option.available, unit['temp_dir'])
2020-05-28 13:59:52 +00:00
2020-10-19 21:25:29 +00:00
assert unit_stop() is None
shutil.rmtree(unit['temp_dir'])
2020-05-28 13:59:52 +00:00
2020-10-19 21:25:29 +00:00
return check if not complete_check else check()
2020-05-28 13:59:52 +00:00
2020-10-19 21:25:29 +00:00
def test_python_isolation_rootfs(self, is_su, temp_dir):
isolation_features = option.available['features']['isolation'].keys()
2020-05-28 13:59:52 +00:00
2020-09-16 20:31:15 +00:00
if not is_su:
2020-05-28 13:59:52 +00:00
if not 'unprivileged_userns_clone' in isolation_features:
2020-09-16 20:31:15 +00:00
pytest.skip('requires unprivileged userns or root')
2020-05-28 13:59:52 +00:00
if 'user' not in isolation_features:
pytest.skip('user namespace is not supported')
2020-05-28 13:59:52 +00:00
if 'mnt' not in isolation_features:
pytest.skip('mnt namespace is not supported')
2020-05-28 13:59:52 +00:00
if 'pid' not in isolation_features:
pytest.skip('pid namespace is not supported')
isolation = {'rootfs': temp_dir}
if not is_su:
isolation['namespaces'] = {
'mount': True,
'credential': True,
'pid': True
}
2020-05-28 13:59:52 +00:00
self.load('ns_inspect', isolation=isolation)
2020-09-16 20:31:15 +00:00
assert (
2020-10-19 21:25:29 +00:00
self.getjson(url='/?path=' + temp_dir)['body']['FileExists']
2020-09-16 20:31:15 +00:00
== False
), 'temp_dir does not exists in rootfs'
2020-05-28 13:59:52 +00:00
2020-09-16 20:31:15 +00:00
assert (
self.getjson(url='/?path=/proc/self')['body']['FileExists']
== True
2020-09-16 20:31:15 +00:00
), 'no /proc/self'
assert (
self.getjson(url='/?path=/dev/pts')['body']['FileExists'] == False
), 'no /dev/pts'
2020-05-28 13:59:52 +00:00
2020-09-16 20:31:15 +00:00
assert (
self.getjson(url='/?path=/sys/kernel')['body']['FileExists']
== False
), 'no /sys/kernel'
2020-05-28 13:59:52 +00:00
2020-09-16 20:31:15 +00:00
ret = self.getjson(url='/?path=/app/python/ns_inspect')
2020-05-28 13:59:52 +00:00
2020-09-16 20:31:15 +00:00
assert (
ret['body']['FileExists'] == True
), 'application exists in rootfs'
2020-10-19 21:25:29 +00:00
def test_python_isolation_rootfs_no_language_deps(self, is_su, temp_dir):
isolation_features = option.available['features']['isolation'].keys()
if not is_su:
if not 'unprivileged_userns_clone' in isolation_features:
pytest.skip('requires unprivileged userns or root')
if 'user' not in isolation_features:
pytest.skip('user namespace is not supported')
if 'mnt' not in isolation_features:
pytest.skip('mnt namespace is not supported')
if 'pid' not in isolation_features:
pytest.skip('pid namespace is not supported')
isolation = {
2020-10-19 21:25:29 +00:00
'rootfs': temp_dir,
'automount': {'language_deps': False}
}
if not is_su:
isolation['namespaces'] = {
'mount': True,
'credential': True,
'pid': True
}
self.load('empty', isolation=isolation)
assert (self.get()['status'] != 200), 'disabled language_deps'
isolation['automount']['language_deps'] = True
self.load('empty', isolation=isolation)
assert (self.get()['status'] == 200), 'enabled language_deps'