-
Notifications
You must be signed in to change notification settings - Fork 136
/
test_proot.py
executable file
·224 lines (197 loc) · 8.7 KB
/
test_proot.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
#!/usr/bin/env python
"""
udocker unit tests: PRootEngine
"""
from unittest import TestCase, main
from unittest.mock import Mock, patch
from udocker.config import Config
from udocker.engine.proot import PRootEngine
import collections
collections.Callable = collections.abc.Callable
class PRootEngineTestCase(TestCase):
"""Test PRootEngine() class for containers execution."""
def setUp(self):
Config().getconf()
Config().conf['hostauth_list'] = ("/etc/passwd", "/etc/group")
Config().conf['cmd'] = "/bin/bash"
Config().conf['cpu_affinity_exec_tools'] = \
(["numactl", "-C", "%s", "--", ], ["taskset", "-c", "%s", ])
Config().conf['valid_host_env'] = "HOME"
Config().conf['username'] = "user"
Config().conf['userhome'] = "/"
Config().conf['oskernel'] = "4.8.13"
Config().conf['location'] = ""
str_local = 'udocker.container.localrepo.LocalRepository'
self.lrepo = patch(str_local)
self.local = self.lrepo.start()
self.mock_lrepo = Mock()
self.local.return_value = self.mock_lrepo
str_exmode = 'udocker.engine.execmode.ExecutionMode'
self.execmode = patch(str_exmode)
self.xmode = self.execmode.start()
self.mock_execmode = Mock()
self.xmode.return_value = self.mock_execmode
def tearDown(self):
self.lrepo.stop()
self.execmode.stop()
@patch('udocker.engine.proot.HostInfo.oskernel')
def test_01_init(self, mock_kernel):
"""Test01 PRootEngine() constructor."""
mock_kernel.return_value = "4.8.13"
prex = PRootEngine(self.local, self.xmode)
self.assertFalse(prex.proot_noseccomp)
self.assertEqual(prex._kernel, "4.8.13")
@patch.object(PRootEngine, '_is_seccomp_patched')
@patch('udocker.engine.proot.HostInfo.oskernel_isgreater')
@patch('udocker.engine.proot.FileUtil.find_file_in_dir')
@patch('udocker.helper.elfpatcher.os.path.exists')
def test_02_select_proot(self, mock_exists, mock_fimage,
mock_isgreater, mock_issecomp):
"""Test02 PRootEngine().select_proot()."""
Config().conf['arch'] = "amd64"
Config().conf['proot_noseccomp'] = None
mock_isgreater.return_value = False
mock_fimage.return_value = "proot-4_8_0"
mock_issecomp.return_value = False
mock_exists.return_value = True
prex = PRootEngine(self.local, self.xmode)
prex.select_proot()
self.assertFalse(prex.proot_noseccomp)
Config().conf['proot_noseccomp'] = True
mock_isgreater.return_value = True
mock_fimage.return_value = "proot"
mock_issecomp.return_value = False
mock_exists.return_value = True
prex = PRootEngine(self.local, self.xmode)
prex.select_proot()
self.assertTrue(prex.proot_noseccomp)
Config().conf['proot_noseccomp'] = False
mock_isgreater.return_value = True
mock_fimage.return_value = "proot"
mock_issecomp.return_value = False
mock_exists.return_value = True
prex = PRootEngine(self.local, self.xmode)
prex.select_proot()
self.assertFalse(prex.proot_noseccomp)
Config().conf['proot_noseccomp'] = None
mock_isgreater.return_value = True
mock_fimage.return_value = "proot-x86_64-4_8_0"
mock_issecomp.return_value = False
mock_exists.return_value = True
prex = PRootEngine(self.local, self.xmode)
prex.select_proot()
self.assertFalse(prex.proot_noseccomp)
Config().conf['proot_noseccomp'] = False
mock_isgreater.return_value = True
mock_fimage.return_value = "proot-x86_64-4_8_0"
mock_issecomp.return_value = False
mock_exists.return_value = True
prex = PRootEngine(self.local, self.xmode)
prex.select_proot()
self.assertFalse(prex.proot_noseccomp)
Config().conf['proot_noseccomp'] = True
mock_isgreater.return_value = True
mock_fimage.return_value = "proot-x86_64-4_8_0"
mock_issecomp.return_value = False
mock_exists.return_value = True
prex = PRootEngine(self.local, self.xmode)
prex.select_proot()
self.assertTrue(prex.proot_noseccomp)
def test_03__set_uid_map(self):
"""Test03 PRootEngine()._set_uid_map()."""
prex = PRootEngine(self.local, self.xmode)
prex.opt["uid"] = "0"
status = prex._set_uid_map()
self.assertEqual(status, ['-0'])
prex = PRootEngine(self.local, self.xmode)
prex.opt["uid"] = "1000"
prex.opt["gid"] = "1001"
status = prex._set_uid_map()
self.assertEqual(status, ['-i', '1000:1001'])
def test_04__create_mountpoint(self):
"""Test04 PRootEngine()._create_mountpoint()."""
prex = PRootEngine(self.local, self.xmode)
status = prex._create_mountpoint("", "")
self.assertTrue(status)
def test_05__get_volume_bindings(self):
"""Test05 PRootEngine()._get_volume_bindings()."""
prex = PRootEngine(self.local, self.xmode)
prex.opt["vol"] = ()
status = prex._get_volume_bindings()
self.assertEqual(status, [])
prex = PRootEngine(self.local, self.xmode)
prex.opt["vol"] = ("/tmp", "/bbb",)
status = prex._get_volume_bindings()
self.assertEqual(status, ['-b', '/tmp:/tmp', '-b', '/bbb:/bbb'])
@patch.object(PRootEngine, '_has_option')
@patch('udocker.engine.proot.ExecutionEngineCommon._get_portsmap')
def test_06__get_network_map(self, mock_pmap, mock_hasopt):
"""Test06 PRootEngine()._get_network_map()."""
mock_pmap.return_value = {}
prex = PRootEngine(self.local, self.xmode)
prex.opt['netcoop'] = False
status = prex._get_network_map()
self.assertEqual(status, [])
mock_pmap.return_value = {}
mock_hasopt.side_effect = [True, True]
prex = PRootEngine(self.local, self.xmode)
prex.opt['netcoop'] = True
status = prex._get_network_map()
self.assertEqual(status, ['-n'])
mock_pmap.return_value = {80: 8080, 443: 8443}
mock_hasopt.side_effect = [True, True]
prex = PRootEngine(self.local, self.xmode)
prex.opt['netcoop'] = True
status = prex._get_network_map()
self.assertEqual(status, ['-p', '80:8080', '-p', '443:8443', '-n'])
@patch.object(PRootEngine, '_get_qemu_string')
@patch('udocker.engine.proot.os.environ.update')
@patch.object(PRootEngine, '_get_network_map')
@patch('udocker.engine.nvidia.Msg')
@patch('udocker.engine.proot.HostInfo.oskernel_isgreater')
@patch.object(PRootEngine, '_run_banner')
@patch.object(PRootEngine, '_run_env_cleanup_dict')
@patch.object(PRootEngine, '_set_uid_map')
@patch.object(PRootEngine, '_get_volume_bindings')
@patch.object(PRootEngine, '_set_cpu_affinity')
@patch.object(PRootEngine, '_run_env_set')
@patch.object(PRootEngine, 'select_proot')
@patch('udocker.engine.proot.os.getenv')
@patch('udocker.engine.proot.subprocess.call')
@patch('udocker.engine.proot.ExecutionEngineCommon._run_init')
def test_07_run(self, mock_run_init, mock_call, mock_getenv, mock_sel_proot,
mock_run_env_set,
mock_set_cpu_aff, mock_get_vol_bind, mock_set_uid_map,
mock_env_cleanup_dict, mock_run_banner, mock_isgreater,
mock_msg, mock_netmap, mock_envupd, mock_getqemu):
"""Test07 PRootEngine().run()."""
mock_run_init.return_value = False
prex = PRootEngine(self.local, self.xmode)
status = prex.run("CONTAINERID")
self.assertEqual(status, 2)
Config().conf['proot_killonexit'] = False
mock_run_init.return_value = True
mock_sel_proot.return_value = None
mock_isgreater.return_value = "2.9.0"
mock_run_env_set.return_value = None
mock_msg.level = 5
mock_set_cpu_aff.return_value = []
mock_get_vol_bind.return_value = ['-b', '/tmp:/tmp']
mock_set_uid_map.return_value = ['-i', '1000:1001']
mock_netmap.return_value = ['-p', '80:8080', '-n']
mock_env_cleanup_dict.return_value = None
mock_run_banner.return_value = None
mock_call.return_value = 5
mock_getenv.return_value = False
mock_envupd.return_value = None
mock_getqemu.return_value = []
prex = PRootEngine(self.local, self.xmode)
prex.opt["kernel"] = "5.4.0"
prex.opt["cmd"] = [""]
status = prex.run("CONTAINERID")
self.assertEqual(status, 5)
self.assertTrue(mock_run_init.called)
self.assertTrue(mock_call.called)
self.assertTrue(mock_envupd.called)
if __name__ == '__main__':
main()