1import pytest 2from unit.applications.lang.python import TestApplicationPython 3from unit.option import option 4from unit.utils import findmnt 5from unit.utils import waitformount 6from unit.utils import waitforunmount 7 8 9class TestPythonIsolation(TestApplicationPython): 10 prerequisites = {'modules': {'python': 'any'}, 'features': ['isolation']} 11 12 def test_python_isolation_rootfs(self, is_su, temp_dir): 13 isolation_features = option.available['features']['isolation'].keys() 14 15 if not is_su: 16 if not 'unprivileged_userns_clone' in isolation_features: 17 pytest.skip('requires unprivileged userns or root') 18 19 if 'user' not in isolation_features: 20 pytest.skip('user namespace is not supported') 21 22 if 'mnt' not in isolation_features: 23 pytest.skip('mnt namespace is not supported') 24 25 if 'pid' not in isolation_features: 26 pytest.skip('pid namespace is not supported') 27 28 isolation = {'rootfs': temp_dir} 29 30 if not is_su: 31 isolation['namespaces'] = { 32 'mount': True, 33 'credential': True, 34 'pid': True, 35 } 36 37 self.load('ns_inspect', isolation=isolation) 38 39 assert ( 40 self.getjson(url='/?path=' + temp_dir)['body']['FileExists'] 41 == False 42 ), 'temp_dir does not exists in rootfs' 43 44 assert ( 45 self.getjson(url='/?path=/proc/self')['body']['FileExists'] == True 46 ), 'no /proc/self' 47 48 assert ( 49 self.getjson(url='/?path=/dev/pts')['body']['FileExists'] == False 50 ), 'no /dev/pts' 51 52 assert ( 53 self.getjson(url='/?path=/sys/kernel')['body']['FileExists'] 54 == False 55 ), 'no /sys/kernel' 56 57 ret = self.getjson(url='/?path=/app/python/ns_inspect') 58 59 assert ( 60 ret['body']['FileExists'] == True 61 ), 'application exists in rootfs' 62 63 def test_python_isolation_rootfs_no_language_deps(self, is_su, temp_dir): 64 if not is_su: 65 pytest.skip('requires root') 66 67 isolation = {'rootfs': temp_dir, 'automount': {'language_deps': False}} 68 69 self.load('empty', isolation=isolation) 70 71 assert findmnt().find(temp_dir) == -1 72 assert self.get()['status'] != 200, 'disabled language_deps' 73 assert findmnt().find(temp_dir) == -1 74 75 isolation['automount']['language_deps'] = True 76 77 self.load('empty', isolation=isolation) 78 79 assert findmnt().find(temp_dir) == -1 80 assert self.get()['status'] == 200, 'enabled language_deps' 81 assert waitformount(temp_dir), 'language_deps mount' 82 83 self.conf({"listeners": {}, "applications": {}}) 84 85 assert waitforunmount(temp_dir), 'language_deps unmount' 86 87 def test_python_isolation_procfs(self, is_su, temp_dir): 88 if not is_su: 89 pytest.skip('requires root') 90 91 isolation = {'rootfs': temp_dir, 'automount': {'procfs': False}} 92 93 self.load('ns_inspect', isolation=isolation) 94 95 assert ( 96 self.getjson(url='/?path=/proc/self')['body']['FileExists'] 97 == False 98 ), 'no /proc/self' 99 100 isolation['automount']['procfs'] = True 101 102 self.load('ns_inspect', isolation=isolation) 103 104 assert ( 105 self.getjson(url='/?path=/proc/self')['body']['FileExists'] == True 106 ), '/proc/self' 107