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