-
Notifications
You must be signed in to change notification settings - Fork 136
/
test_fakechroot.py
executable file
·332 lines (301 loc) · 14.2 KB
/
test_fakechroot.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
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
#!/usr/bin/env python
"""
udocker unit tests: FakechrootEngine
"""
import os
from unittest import TestCase, main
from unittest.mock import patch, Mock
from udocker.config import Config
from udocker.engine.fakechroot import FakechrootEngine
import collections
collections.Callable = collections.abc.Callable
class FakechrootEngineTestCase(TestCase):
"""Docker container execution engine using Fakechroot
Provides a chroot like environment to run containers.
Uses Fakechroot as chroot alternative.
Inherits from ContainerEngine class
"""
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()
def test_01__init(self):
"""Test01 FakechrootEngine Constructor."""
ufake = FakechrootEngine(self.local, self.xmode)
self.assertEqual(ufake._fakechroot_so, "")
self.assertIsNone(ufake._elfpatcher)
@patch('udocker.engine.fakechroot.Msg')
@patch('udocker.engine.fakechroot.sys.exit')
@patch('udocker.engine.fakechroot.OSInfo')
@patch('udocker.engine.fakechroot.os.path.realpath')
@patch('udocker.engine.fakechroot.os.path.exists')
@patch('udocker.engine.fakechroot.FileUtil')
def test_02_select_fakechroot_so(self, mock_futil, mock_exists,
mock_rpath, mock_osinfo, mock_sysex, mock_msg):
"""Test02 FakechrootEngine.select_fakechroot_so."""
Config().conf['fakechroot_so'] = "/s/fake1"
mock_msg.level = 0
mock_exists.return_value = True
mock_rpath.return_value = "/s/fake1"
ufake = FakechrootEngine(self.local, self.xmode)
out = ufake.select_fakechroot_so()
self.assertEqual(out, "/s/fake1")
Config().conf['fakechroot_so'] = ""
mock_exists.return_value = True
ufake = FakechrootEngine(self.local, self.xmode)
out = ufake.select_fakechroot_so()
self.assertEqual(out, "/libfakechroot.so")
Config().conf['fakechroot_so'] = ""
mock_exists.return_value = False
mock_osinfo.return_value.arch.return_value = "amd64"
mock_osinfo.return_value.osdistribution.return_value = \
("linux", "4.8.1")
mock_futil.return_value.find_file_in_dir.return_value = ""
mock_sysex.return_value = 1
ufake = FakechrootEngine(self.local, self.xmode)
out = ufake.select_fakechroot_so()
self.assertTrue(mock_sysex.called)
self.assertTrue(mock_futil.called)
Config().conf['fakechroot_so'] = ""
mock_exists.return_value = False
mock_osinfo.return_value.arch.return_value = "amd64"
mock_osinfo.return_value.osdistribution.return_value = \
("linux", "4.8.1")
mock_futil.return_value.find_file_in_dir.return_value = \
"/libfakechroot.so"
ufake = FakechrootEngine(self.local, self.xmode)
out = ufake.select_fakechroot_so()
self.assertTrue(mock_futil.called)
self.assertEqual(out, "/libfakechroot.so")
Config().conf['fakechroot_so'] = ""
mock_exists.return_value = False
mock_osinfo.return_value.arch.return_value = "i386"
mock_osinfo.return_value.osdistribution.return_value = \
("linux", "4.8.1")
mock_futil.return_value.find_file_in_dir.return_value = \
"/libfakechroot.so"
ufake = FakechrootEngine(self.local, self.xmode)
out = ufake.select_fakechroot_so()
self.assertTrue(mock_futil.called)
self.assertEqual(out, "/libfakechroot.so")
Config().conf['fakechroot_so'] = ""
mock_exists.return_value = False
mock_osinfo.return_value.arch.return_value = "arm64"
mock_osinfo.return_value.osdistribution.return_value = \
("linux", "4.8.1")
mock_futil.return_value.find_file_in_dir.return_value = \
"/libfakechroot.so"
ufake = FakechrootEngine(self.local, self.xmode)
out = ufake.select_fakechroot_so()
self.assertTrue(mock_futil.called)
self.assertEqual(out, "/libfakechroot.so")
@patch('udocker.engine.fakechroot.ExecutionEngineCommon._setup_container_user_noroot')
def test_03__setup_container_user(self, mock_cmm):
"""Test03 FakechrootEngine._setup_container_user()."""
mock_cmm.return_value = True
ufake = FakechrootEngine(self.local, self.xmode)
status = ufake._setup_container_user("someuser")
self.assertTrue(mock_cmm.called)
self.assertTrue(status)
@patch('udocker.engine.fakechroot.Msg')
def test_04__uid_check(self, mock_msg):
"""Test04 FakechrootEngine._uid_check()."""
mock_msg.level = 3
ufake = FakechrootEngine(self.local, self.xmode)
ufake.opt["user"] = "0"
ufake._uid_check()
self.assertTrue(mock_msg.return_value.out.called)
def test_05__get_volume_bindings(self):
"""Test05 FakechrootEngine._get_volume_bindings()."""
ufake = FakechrootEngine(self.local, self.xmode)
ufake.opt["vol"] = ()
status = ufake._get_volume_bindings()
self.assertEqual(status, ('', ''))
ufake = FakechrootEngine(self.local, self.xmode)
dtmp = os.path.realpath("/tmp")
dbin = os.path.realpath("/bin")
ufake.opt["vol"] = (dtmp, dbin)
status = ufake._get_volume_bindings()
if status[1].startswith(dtmp):
self.assertEqual(status, ('', dtmp+'!'+dtmp+':'+dbin+'!'+dbin))
else:
self.assertEqual(status, ('', dbin+'!'+dbin+':'+dtmp+'!'+dtmp))
@patch('udocker.engine.fakechroot.os.path.exists')
@patch('udocker.engine.fakechroot.FileUtil.cont2host')
def test_06__get_access_filesok(self, mock_cont2host, mock_exists):
"""Test06 FakechrootEngine._get_access_filesok()."""
Config.conf['access_files'] = ("/sys/infin",)
mock_cont2host.return_value = "/c/sys/infin"
mock_exists.return_value = False
ufake = FakechrootEngine(self.local, self.xmode)
out = ufake._get_access_filesok()
self.assertEqual(out, "")
Config.conf['access_files'] = ("/sys/infin",)
mock_cont2host.return_value = "/c/sys/infin"
mock_exists.return_value = True
ufake = FakechrootEngine(self.local, self.xmode)
ufake.opt["vol"] = ("/tmp",)
out = ufake._get_access_filesok()
self.assertTrue(mock_cont2host.called)
self.assertTrue(mock_exists.called)
self.assertEqual(out, "/sys/infin")
@patch.object(FakechrootEngine, '_get_volume_bindings')
@patch.object(FakechrootEngine, 'select_fakechroot_so')
@patch.object(FakechrootEngine, '_get_access_filesok')
@patch.object(FakechrootEngine, '_is_volume')
@patch('udocker.engine.fakechroot.os.path.realpath')
@patch('udocker.engine.fakechroot.ElfPatcher')
@patch('udocker.engine.fakechroot.FileUtil')
def test_07__fakechroot_env_set(self, mock_futil, mock_elf,
mock_rpath, mock_isvol, mock_fok, mock_sel,
mock_getvol):
"""Test07 FakechrootEngine._fakechroot_env_set()."""
Config.conf['fakechroot_expand_symlinks'] = None
mock_getvol.return_value = ('v1:v2', 'm1:m2')
mock_sel.return_value = '/ROOT/lib/libfakechroot.so'
mock_fok.return_value = "/sys/infin"
mock_rpath.return_value = '/bin/fake.so'
mock_elf.return_value.get_ld_library_path.return_value = '/a'
mock_elf.return_value.get_container_loader.return_value = '/ROOT/elf'
mock_futil.return_value.find_file_in_dir.return_value = True
mock_isvol.return_value = False
self.xmode.get_mode.return_value = 'F1'
ufake = FakechrootEngine(self.local, self.xmode)
ufake._elfpatcher = mock_elf
ufake._fakechroot_env_set()
self.assertTrue(self.xmode.get_mode.called)
self.assertTrue(mock_getvol.called)
self.assertTrue(mock_sel.called)
self.assertTrue(mock_fok.called)
self.assertTrue(mock_rpath.called)
self.assertTrue(mock_isvol.called)
Config.conf['fakechroot_expand_symlinks'] = None
mock_getvol.return_value = ('v1:v2', 'm1:m2')
mock_sel.return_value = '/ROOT/lib/libfakechroot.so'
mock_fok.return_value = "/sys/infin"
mock_rpath.return_value = '/bin/fake.so'
mock_elf.return_value.get_ld_library_path.return_value = '/a'
mock_elf.return_value.select_patchelf.return_value = '/ROOT/elf'
mock_elf.return_value.get_patch_last_time.return_value = '/ROOT/elf'
mock_elf.return_value.get_container_loader.return_value = '/ROOT/elf'
mock_futil.return_value.find_file_in_dir.return_value = True
mock_isvol.return_value = False
self.xmode.get_mode.return_value = 'F4'
ufake = FakechrootEngine(self.local, self.xmode)
ufake._elfpatcher = mock_elf
ufake._fakechroot_env_set()
self.assertTrue(mock_getvol.called)
self.assertTrue(mock_sel.called)
self.assertTrue(mock_fok.called)
self.assertTrue(mock_rpath.called)
self.assertTrue(mock_isvol.called)
@patch('udocker.engine.fakechroot.Msg')
def test_08__run_invalid_options(self, mock_msg):
"""Test08 FakechrootEngine._run_invalid_options()"""
mock_msg.level = 5
ufake = FakechrootEngine(self.local, self.xmode)
ufake.opt['netcoop'] = False
ufake.opt['portsmap'] = True
ufake._run_invalid_options()
self.assertTrue(mock_msg.called)
@patch('udocker.engine.fakechroot.FileUtil.get1stline')
@patch('udocker.engine.fakechroot.FileUtil.cont2host')
@patch('udocker.engine.fakechroot.FileUtil.find_exec')
@patch('udocker.engine.fakechroot.OSInfo.is_binary_executable')
@patch('udocker.engine.fakechroot.Msg')
def test_09__run_add_script_support(self, mock_msg, mock_isbinary,
mock_findexe, mock_cont2host,
mock_1stline):
"""Test09 FakechrootEngine._run_add_script_support()"""
mock_msg.level = 3
mock_isbinary.return_value = True
ufake = FakechrootEngine(self.local, self.xmode)
ufake.container_root = "/ROOT"
ufake.opt["cmd"] = [""]
status = ufake._run_add_script_support("/ROOT/bin/ls")
self.assertEqual(status, [])
mock_msg.level = 3
mock_isbinary.return_value = False
mock_cont2host.return_value = "/ROOT/bin/ls"
mock_1stline.return_value = b"#!/bin/python"
mock_findexe.side_effect = ["/bin/ls", ""]
ufake = FakechrootEngine(self.local, self.xmode)
ufake.container_root = "/ROOT"
status = ufake._run_add_script_support("/ROOT/bin/ls")
self.assertEqual(status, ["/ROOT//bin/python"])
mock_msg.level = 3
mock_isbinary.return_value = False
mock_cont2host.return_value = "/ROOT/bin/ls"
mock_1stline.return_value = b""
mock_findexe.side_effect = ["/bin/ls", ""]
ufake = FakechrootEngine(self.local, self.xmode)
ufake.container_root = "/ROOT"
status = ufake._run_add_script_support("/ROOT/bin/ls")
self.assertEqual(status, ["/ROOT//bin/ls"])
@patch('udocker.engine.fakechroot.FileUtil.cont2host')
@patch.object(FakechrootEngine, '_run_banner')
@patch.object(FakechrootEngine, '_run_add_script_support')
@patch.object(FakechrootEngine, '_set_cpu_affinity')
@patch.object(FakechrootEngine, '_run_env_cleanup_list')
@patch.object(FakechrootEngine, '_fakechroot_env_set')
@patch.object(FakechrootEngine, '_run_env_set')
@patch.object(FakechrootEngine, '_run_invalid_options')
@patch.object(FakechrootEngine, '_run_init')
@patch.object(FakechrootEngine, '_uid_check')
@patch('udocker.engine.fakechroot.subprocess.call')
@patch('udocker.engine.fakechroot.ElfPatcher')
@patch('udocker.engine.fakechroot.Msg')
def test_10_run(self, mock_msg, mock_elfp, mock_call, mock_uidc,
mock_rinit, mock_rinval, mock_renv, mock_fakeenv,
mock_renvclean, mock_setaff, mock_raddsup,
mock_runban, mock_cont2host):
"""Test10 FakechrootEngine.run()."""
mock_uidc.return_value = None
mock_rinit.return_value = ""
ufake = FakechrootEngine(self.local, self.xmode)
status = ufake.run("12345")
self.assertEqual(status, 2)
mock_uidc.return_value = None
mock_rinit.return_value = '/bin/exec'
mock_rinval.return_value = None
self.xmode.get_mode.return_value = 'F1'
mock_elfp.return_value.check_container_path.return_value = False
mock_renv.return_value = None
mock_fakeenv.return_value = None
mock_renvclean.return_value = None
mock_setaff.return_value = ['1', '2']
mock_elfp.return_value.get_container_loader.return_value = '/ROOT/xx'
mock_raddsup.return_value = ['/ROOT/xx.sh']
mock_runban.return_value = None
mock_cont2host.return_value = "/cont/ROOT"
mock_call.return_value = 0
ufake = FakechrootEngine(self.local, self.xmode)
ufake.opt["cmd"] = [""]
status = ufake.run("12345")
self.assertEqual(status, 0)
self.assertTrue(mock_uidc.called)
self.assertTrue(mock_rinit.called)
self.assertTrue(mock_msg.called)
self.assertTrue(mock_call.called)
if __name__ == '__main__':
main()