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