xref: /unit/test/test_python_isolation.py (revision 2073:bc6ad31ce286)
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 ret['body']['FileExists'] == True, 'application exists in rootfs'
60
61    def test_python_isolation_rootfs_no_language_deps(self, is_su, temp_dir):
62        if not is_su:
63            pytest.skip('requires root')
64
65        isolation = {'rootfs': temp_dir, 'automount': {'language_deps': False}}
66
67        self.load('empty', isolation=isolation)
68
69        assert findmnt().find(temp_dir) == -1
70        assert self.get()['status'] != 200, 'disabled language_deps'
71        assert findmnt().find(temp_dir) == -1
72
73        isolation['automount']['language_deps'] = True
74
75        self.load('empty', isolation=isolation)
76
77        assert findmnt().find(temp_dir) == -1
78        assert self.get()['status'] == 200, 'enabled language_deps'
79        assert waitformount(temp_dir), 'language_deps mount'
80
81        self.conf({"listeners": {}, "applications": {}})
82
83        assert waitforunmount(temp_dir), 'language_deps unmount'
84
85    def test_python_isolation_procfs(self, is_su, temp_dir):
86        if not is_su:
87            pytest.skip('requires root')
88
89        isolation = {'rootfs': temp_dir, 'automount': {'procfs': False}}
90
91        self.load('ns_inspect', isolation=isolation)
92
93        assert (
94            self.getjson(url='/?path=/proc/self')['body']['FileExists'] == False
95        ), 'no /proc/self'
96
97        isolation['automount']['procfs'] = True
98
99        self.load('ns_inspect', isolation=isolation)
100
101        assert (
102            self.getjson(url='/?path=/proc/self')['body']['FileExists'] == True
103        ), '/proc/self'
104