Debian Codegraph @

test/unit/boxbuild/box_build_test.py

raw
1 from unittest.mock import (
2 patch, Mock, call
3 )
4 from pytest import raises
5
6 from kiwi.boxbuild.box_build import BoxBuild
7 import kiwi.boxbuild.defaults as defaults
8
9 from kiwi.exceptions import (
10 KiwiBoxDownloadError,
11 KiwiBoxVirtioFsError,
12 KiwiBoxQEMUBinaryNotFound,
13 KiwiBoxSSHPortInvalid,
14 KiwiBoxSSHKeyNotFound,
15 KiwiBoxBuildError
16 )
17
18
19 class TestBoxBuild:
20 @patch('kiwi.boxbuild.box_build.BoxDownload')
21 def setup(self, mock_BoxDownload):
22 self.box = Mock()
23 self.vm_setup = Mock()
24 self.vm_setup.kernel = 'kernel'
25 self.vm_setup.append = 'console=hvc0'
26 self.vm_setup.system = 'system'
27 self.vm_setup.initrd = 'initrd'
28 self.vm_setup.console = 'hvc0'
29 self.vm_setup.ram = 4096
30 self.vm_setup.smp = 4
31 self.box.fetch.return_value = self.vm_setup
32 mock_BoxDownload.return_value = self.box
33 self.build = BoxBuild(
34 boxname='suse', arch='x86_64'
35 )
36 self.build_arm = BoxBuild(
37 boxname='universal', arch='aarch64',
38 machine='virt', cpu='cortex-a57',
39 console='ttyAMA0'
40 )
41
42 @patch('kiwi.boxbuild.box_build.BoxDownload')
43 def setup_method(self, cls, mock_BoxDownload):
44 self.setup()
45
46 @patch('os.environ')
47 @patch('os.system')
48 @patch('kiwi.boxbuild.box_build.BoxBuildDefaults.create_build_target_dir')
49 @patch('kiwi.boxbuild.box_build.Path.which')
50 def test_raises_on_kiwi_error(
51 self, mock_path_which, mock_create_build_target_dir,
52 mock_os_system, mock_os_environ
53 ):
54 mock_path_which.return_value = 'qemu-system-x86_64'
55 with raises(KiwiBoxBuildError):
56 self.build.run(
57 [
58 '--type', 'oem', 'system', 'build',
59 '--description', 'desc', '--target-dir', '../data/boxbuild/target'
60 ]
61 )
62
63 @patch('os.environ')
64 @patch('os.system')
65 @patch('kiwi.boxbuild.box_build.BoxBuildDefaults.create_build_target_dir')
66 @patch('kiwi.boxbuild.box_build.Path.which')
67 def test_raises_on_invalid_ssh_port(
68 self, mock_path_which, mock_create_build_target_dir,
69 mock_os_system, mock_os_environ
70 ):
71 mock_path_which.return_value = 'qemu-system-x86_64'
72 self.build.ssh_port = 'bogus'
73 with raises(KiwiBoxSSHPortInvalid):
74 self.build.run(
75 [
76 '--debug', '--type', 'oem', 'system', 'build',
77 '--description', 'desc', '--target-dir', 'target'
78 ]
79 )
80
81 @patch('os.environ')
82 @patch('os.system')
83 @patch('kiwi.boxbuild.box_build.BoxBuildDefaults.create_build_target_dir')
84 @patch('kiwi.boxbuild.box_build.Path.which')
85 def test_raises_on_download_error(
86 self, mock_path_which, mock_create_build_target_dir,
87 mock_os_system, mock_os_environ
88 ):
89 mock_path_which.return_value = 'qemu-system-x86_64'
90 self.box.fetch.side_effect = Exception('error')
91 with raises(KiwiBoxDownloadError):
92 self.build.run(
93 [
94 '--debug', '--type', 'oem', 'system', 'build',
95 '--description', 'desc', '--target-dir', 'target'
96 ]
97 )
98
99 @patch('os.environ')
100 @patch('os.system')
101 @patch('kiwi.boxbuild.box_build.BoxBuildDefaults.create_build_target_dir')
102 @patch('kiwi.boxbuild.box_build.Path.which')
103 def test_run_with_9p_sharing(
104 self, mock_path_which, mock_create_build_target_dir,
105 mock_os_system, mock_os_environ
106 ):
107 mock_path_which.return_value = 'qemu-system-x86_64'
108 self.build.run(
109 [
110 '--debug', '--type', 'oem', 'system', 'build',
111 '--description', 'desc', '--target-dir', 'target'
112 ],
113 keep_open=True,
114 kiwi_version='9.22.1',
115 custom_shared_path='/var/tmp/repos'
116 )
117 mock_create_build_target_dir.assert_called_once_with('target')
118 mock_os_system.assert_called_once_with(
119 'qemu-system-x86_64 '
120 '-m 4096 '
121 '-accel accel=kvm '
122 '-cpu host '
123 '-nographic '
124 '-nodefaults '
125 '-snapshot '
126 '-kernel kernel '
127 '-append "console=hvc0 kiwi=\\"--debug --type oem system build\\"'
128 ' kiwi-no-halt kiwi_version=_9.22.1_'
129 ' custom_mount=_/var/tmp/repos_'
130 ' sharing_backend=_9p_" '
131 '-drive file=system,if=virtio,driver=qcow2,cache=off,snapshot=on '
132 '-nic user,model=virtio,hostfwd=tcp::10022-:22 '
133 '-device virtio-serial '
134 '-chardev stdio,id=virtiocon0 '
135 '-device virtconsole,chardev=virtiocon0 '
136 '-fsdev local,security_model=mapped,id=fsdev0,path=desc '
137 '-device virtio-9p-pci,id=fs0,fsdev=fsdev0,mount_tag='
138 'kiwidescription '
139 '-fsdev local,security_model=mapped,id=fsdev1,path=target '
140 '-device virtio-9p-pci,id=fs1,fsdev=fsdev1,mount_tag='
141 'kiwibundle '
142 '-fsdev local,security_model=mapped,id=fsdev2,path=/var/tmp/repos '
143 '-device virtio-9p-pci,id=fs2,fsdev=fsdev2,mount_tag='
144 'custompath '
145 '-initrd initrd '
146 '-smp 4'
147 )
148
149 @patch('os.environ')
150 @patch('os.system')
151 @patch('kiwi.boxbuild.box_build.BoxBuildDefaults.create_build_target_dir')
152 @patch('kiwi.boxbuild.box_build.Path.which')
153 def test_run_cross_arch_aarch64_on_x86_64(
154 self, mock_path_which, mock_create_build_target_dir,
155 mock_os_system, mock_os_environ
156 ):
157 mock_path_which.return_value = 'qemu-system-aarch64'
158 self.build_arm.run(
159 [
160 '--type', 'oem', 'system', 'build',
161 '--description', 'desc', '--target-dir', 'target'
162 ],
163 keep_open=True,
164 kiwi_version='9.22.1',
165 custom_shared_path='/var/tmp/repos'
166 )
167 mock_create_build_target_dir.assert_called_once_with('target')
168 mock_os_system.assert_called_once_with(
169 'qemu-system-aarch64 '
170 '-m 4096 '
171 '-machine virt '
172 '-accel accel=kvm '
173 '-cpu cortex-a57 '
174 '-nographic '
175 '-nodefaults '
176 '-snapshot '
177 '-kernel kernel '
178 '-append "console=ttyAMA0 kiwi=\\"--type oem system build\\"'
179 ' kiwi-no-halt kiwi_version=_9.22.1_'
180 ' custom_mount=_/var/tmp/repos_'
181 ' sharing_backend=_9p_" '
182 '-drive file=system,if=virtio,driver=qcow2,cache=off,snapshot=on '
183 '-nic user,model=virtio,hostfwd=tcp::10022-:22 '
184 '-device virtio-serial '
185 '-chardev stdio,id=virtiocon0 '
186 '-device virtconsole,chardev=virtiocon0 '
187 '-fsdev local,security_model=mapped,id=fsdev0,path=desc '
188 '-device virtio-9p-pci,id=fs0,fsdev=fsdev0,mount_tag='
189 'kiwidescription '
190 '-fsdev local,security_model=mapped,id=fsdev1,path=target '
191 '-device virtio-9p-pci,id=fs1,fsdev=fsdev1,mount_tag='
192 'kiwibundle '
193 '-fsdev local,security_model=mapped,id=fsdev2,path=/var/tmp/repos '
194 '-device virtio-9p-pci,id=fs2,fsdev=fsdev2,mount_tag='
195 'custompath '
196 '-initrd initrd '
197 '-smp 4'
198 )
199
200 @patch('os.environ')
201 @patch('os.system')
202 @patch('kiwi.boxbuild.box_build.BoxBuildDefaults.create_build_target_dir')
203 @patch('kiwi.boxbuild.box_build.Path.which')
204 def test_run_raises_no_qemu_binary_found(
205 self, mock_path_which, mock_create_build_target_dir,
206 mock_os_system, mock_os_environ
207 ):
208 mock_path_which.return_value = None
209 with raises(KiwiBoxQEMUBinaryNotFound):
210 self.build.run(
211 [
212 '--type', 'oem', 'system', 'build',
213 '--description', 'desc', '--target-dir', 'target'
214 ],
215 keep_open=True,
216 kiwi_version='9.22.1',
217 custom_shared_path='/var/tmp/repos'
218 )
219 with raises(KiwiBoxQEMUBinaryNotFound):
220 self.build_arm.run(
221 [
222 '--type', 'oem', 'system', 'build',
223 '--description', 'desc', '--target-dir', 'target'
224 ]
225 )
226
227 @patch('os.environ')
228 @patch('os.system')
229 @patch('subprocess.Popen')
230 @patch('kiwi.boxbuild.box_build.BoxBuildDefaults.create_build_target_dir')
231 @patch('kiwi.boxbuild.defaults.Path.which')
232 def test_run_with_virtiofs_sharing_raises(
233 self, mock_path_which, mock_create_build_target_dir,
234 mock_subprocess_Popen, mock_os_system, mock_os_environ
235 ):
236 path_which_results = [None, 'qemu-system-x86_64']
237
238 def path_which(name, lookup=None):
239 return path_which_results.pop()
240
241 mock_path_which.side_effect = path_which
242
243 self.build.sharing_backend = 'virtiofs'
244 with raises(KiwiBoxVirtioFsError):
245 self.build.run(
246 [
247 '--type', 'oem', 'system', 'build',
248 '--description', 'desc', '--target-dir', 'target'
249 ]
250 )
251
252 path_which_results = ['virtiofsd', 'qemu-system-x86_64']
253 mock_subprocess_Popen.side_effect = Exception
254 with raises(KiwiBoxVirtioFsError):
255 self.build.run(
256 [
257 '--type', 'oem', 'system', 'build',
258 '--description', 'desc', '--target-dir', 'target'
259 ]
260 )
261
262 @patch('os.environ')
263 @patch('os.system')
264 @patch('os.path.abspath')
265 @patch('subprocess.Popen')
266 @patch('kiwi.boxbuild.box_build.BoxBuildDefaults.create_build_target_dir')
267 @patch('kiwi.boxbuild.defaults.Path.which')
268 def test_run_with_virtiofs_sharing(
269 self, mock_path_which, mock_create_build_target_dir,
270 mock_subprocess_Popen, mock_os_path_abspath, mock_os_system,
271 mock_os_environ
272 ):
273 def abs_path(arg):
274 return 'abspath/{0}'.format(arg)
275
276 def new_process(self, **args):
277 return Mock()
278
279 def path_which(name, lookup=None):
280 if name == 'qemu-system-x86_64':
281 return 'qemu-system-x86_64'
282 elif name == 'virtiofsd':
283 return '/usr/libexec/virtiofsd'
284
285 mock_path_which.side_effect = path_which
286 mock_os_path_abspath.side_effect = abs_path
287 mock_subprocess_Popen.side_effect = new_process
288 self.build.sharing_backend = 'virtiofs'
289 self.build.run(
290 [
291 '--type', 'oem', 'system', 'build',
292 '--description', 'desc', '--target-dir', 'target'
293 ],
294 keep_open=True,
295 kiwi_version='9.22.1',
296 custom_shared_path='var/tmp/repos'
297 )
298 mock_create_build_target_dir.assert_called_once_with('target')
299 mock_os_system.assert_called_once_with(
300 'qemu-system-x86_64 '
301 '-m 4096 '
302 '-accel accel=kvm '
303 '-cpu host '
304 '-nographic '
305 '-nodefaults '
306 '-snapshot '
307 '-kernel kernel '
308 '-append "console=hvc0 kiwi=\\"--type oem system build\\"'
309 ' kiwi-no-halt kiwi_version=_9.22.1_'
310 ' custom_mount=_var/tmp/repos_'
311 ' sharing_backend=_virtiofs_" '
312 '-drive file=system,if=virtio,driver=qcow2,cache=off,snapshot=on '
313 '-nic user,model=virtio,hostfwd=tcp::10022-:22 '
314 '-device virtio-serial '
315 '-chardev stdio,id=virtiocon0 '
316 '-device virtconsole,chardev=virtiocon0 '
317 '-chardev socket,id=char0,path=/tmp/vhostqemu_0 '
318 '-device vhost-user-fs-pci,queue-size=1024,chardev=char0,tag='
319 'kiwidescription '
320 '-chardev socket,id=char1,path=/tmp/vhostqemu_1 '
321 '-device vhost-user-fs-pci,queue-size=1024,chardev=char1,tag='
322 'kiwibundle '
323 '-chardev socket,id=char2,path=/tmp/vhostqemu_2 '
324 '-device vhost-user-fs-pci,queue-size=1024,chardev=char2,tag='
325 'custompath '
326 '-object '
327 'memory-backend-file,id=mem,size=4096,mem-path=/dev/shm,share=on '
328 '-numa node,memdev=mem '
329 '-initrd initrd '
330 '-smp 4'
331 )
332 assert mock_subprocess_Popen.call_args_list == [
333 call(
334 [
335 '/usr/libexec/virtiofsd',
336 '--socket-path=/tmp/vhostqemu_0',
337 '--shared-dir', 'abspath/desc',
338 '--sandbox', 'namespace',
339 '--cache', 'always',
340 '--allow-direct-io',
341 '--posix-acl',
342 '--xattr'
343 ], close_fds=True
344 ),
345 call(
346 [
347 '/usr/libexec/virtiofsd',
348 '--socket-path=/tmp/vhostqemu_1',
349 '--shared-dir', 'abspath/target',
350 '--sandbox', 'namespace',
351 '--cache', 'always',
352 '--allow-direct-io',
353 '--posix-acl',
354 '--xattr'
355 ], close_fds=True
356 ),
357 call(
358 [
359 '/usr/libexec/virtiofsd',
360 '--socket-path=/tmp/vhostqemu_2',
361 '--shared-dir', 'abspath/var/tmp/repos',
362 '--sandbox', 'namespace',
363 '--cache', 'always',
364 '--allow-direct-io',
365 '--posix-acl',
366 '--xattr'
367 ], close_fds=True
368 )
369 ]
370 for virtiofsd_process in defaults.VIRTIOFSD_PROCESS_LIST:
371 virtiofsd_process.terminate.assert_called_once_with()
372
373 @patch('pwd.getpwuid')
374 @patch('time.sleep')
375 @patch('os.path.isfile')
376 @patch('os.system')
377 @patch('kiwi.boxbuild.box_build.Command.run')
378 @patch('kiwi.boxbuild.box_build.BoxBuildDefaults.create_build_target_dir')
379 @patch('kiwi.boxbuild.defaults.Path.which')
380 def test_run_with_sshfs_sharing(
381 self, mock_path_which, mock_create_build_target_dir, mock_Command_run,
382 mock_os_system, mock_os_path_isfile, mock_time_sleep,
383 mock_pwd_getpwuid
384 ):
385 def command_run(args, raise_on_error=True):
386 if args[0] == 'ssh':
387 raise Exception
388
389 def path_which(name, lookup=None):
390 if name == 'qemu-system-x86_64':
391 return 'qemu-system-x86_64'
392
393 mock_path_which.side_effect = path_which
394 mock_Command_run.side_effect = command_run
395
396 mock_pwd_getpwuid.return_value.pw_name = 'user'
397 mock_os_path_isfile.return_value = False
398 self.build.sharing_backend = 'sshfs'
399 self.build.ssh_port = '10022'
400
401 with raises(KiwiBoxSSHKeyNotFound):
402 self.build.run(
403 [
404 '--type', 'oem', 'system', 'build',
405 '--description', 'desc', '--target-dir', 'target'
406 ],
407 keep_open=True,
408 kiwi_version='9.22.1',
409 custom_shared_path='var/tmp/repos'
410 )
411
412 mock_create_build_target_dir.reset_mock()
413 mock_os_path_isfile.return_value = True
414 with patch('builtins.open', create=True) as mock_open:
415 with patch.dict('os.environ', {'HOME': '~'}):
416 file_handle = mock_open.return_value.__enter__.return_value
417 file_handle.read.return_value = 'key_type key_value'
418 self.build.run(
419 [
420 '--type', 'oem', 'system', 'build',
421 '--description', 'desc', '--target-dir', 'target'
422 ],
423 keep_open=True,
424 kiwi_version='9.22.1',
425 custom_shared_path='var/tmp/repos'
426 )
427
428 mock_create_build_target_dir.assert_called_once_with('target')
429 mock_os_system.assert_called_once_with(
430 'qemu-system-x86_64 '
431 '-m 4096 '
432 '-accel accel=kvm '
433 '-cpu host '
434 '-nographic '
435 '-nodefaults '
436 '-snapshot '
437 '-kernel kernel '
438 '-append "console=hvc0 kiwi=\\"--type oem system build\\"'
439 ' kiwi-no-halt kiwi_version=_9.22.1_'
440 ' custom_mount=_var/tmp/repos_'
441 ' ssh_key=_key_value_'
442 ' ssh_key_type=_key_type_'
443 ' host_kiwidescription=_user@localhost:desc_'
444 ' host_kiwibundle=_user@localhost:target_'
445 ' host_custompath=_user@localhost:var/tmp/repos_'
446 ' sharing_backend=_sshfs_" '
447 '-drive file=system,if=virtio,driver=qcow2,cache=off,snapshot=on '
448 '-nic user,model=virtio,hostfwd=tcp::10022-:22 '
449 '-device virtio-serial '
450 '-chardev stdio,id=virtiocon0 '
451 '-device virtconsole,chardev=virtiocon0 '
452 '-initrd initrd '
453 '-smp 4'
454 )
455 assert mock_Command_run.call_args_list[0] == call(
456 [
457 'ssh-keygen', '-R', '[localhost]:10022'
458 ], raise_on_error=False
459 )
460 assert mock_Command_run.call_args_list[1] == call(
461 [
462 'ssh', '-NT', '-o', 'StrictHostKeyChecking=no',
463 'root@localhost', '-p', '10022', '-R',
464 '10000:localhost:22'
465 ]
466 )