| 1 |
from unittest.mock import ( |
| 2 |
patch, call, Mock |
| 3 |
) |
| 4 |
from pytest import raises |
| 5 |
|
| 6 |
from kiwi.chroot_manager import ( |
| 7 |
ChrootManager, ChrootMount |
| 8 |
) |
| 9 |
|
| 10 |
from kiwi.exceptions import ( |
| 11 |
KiwiCommandError, |
| 12 |
KiwiUmountBusyError |
| 13 |
) |
| 14 |
|
| 15 |
|
| 16 |
class TestChrootManager: |
| 17 |
@patch('kiwi.chroot_manager.MountManager') |
| 18 |
@patch('kiwi.chroot_manager.Command.run') |
| 19 |
def test_run_in_chroot_context(self, mock_run, mock_mount): |
| 20 |
mock_mntMngr = Mock() |
| 21 |
mock_mount.return_value = mock_mntMngr |
| 22 |
with ChrootManager( |
| 23 |
'/some/root', binds=[ |
| 24 |
ChrootMount('/dev'), ChrootMount('/proc'), ChrootMount('/sys') |
| 25 |
] |
| 26 |
) as chroot: |
| 27 |
chroot.run(['cmd', 'arg1', 'arg2']) |
| 28 |
assert mock_mount.call_args_list == [ |
| 29 |
call(device='/dev', mountpoint='/some/root/dev'), |
| 30 |
call(device='/proc', mountpoint='/some/root/proc'), |
| 31 |
call(device='/sys', mountpoint='/some/root/sys') |
| 32 |
] |
| 33 |
mock_run.assert_called_once_with( |
| 34 |
['chroot', '/some/root', 'cmd', 'arg1', 'arg2'], |
| 35 |
None, True, False, True |
| 36 |
) |
| 37 |
mock_mntMngr.bind_mount.assert_called() |
| 38 |
mock_mntMngr.umount.assert_called() |
| 39 |
|
| 40 |
@patch('kiwi.chroot_manager.MountManager') |
| 41 |
@patch('kiwi.chroot_manager.Command.run') |
| 42 |
def test_run_fails_in_chroot_context(self, mock_run, mock_mount): |
| 43 |
mock_mntMngr = Mock() |
| 44 |
mock_mount.return_value = mock_mntMngr |
| 45 |
mock_run.side_effect = Exception |
| 46 |
with ChrootManager( |
| 47 |
'/some/root', binds=[ |
| 48 |
ChrootMount('/dev'), ChrootMount('/proc'), ChrootMount('/sys') |
| 49 |
] |
| 50 |
) as chroot: |
| 51 |
with raises(Exception): |
| 52 |
chroot.run(['cmd', 'arg1', 'arg2']) |
| 53 |
assert mock_mount.call_args_list == [ |
| 54 |
call(device='/dev', mountpoint='/some/root/dev'), |
| 55 |
call(device='/proc', mountpoint='/some/root/proc'), |
| 56 |
call(device='/sys', mountpoint='/some/root/sys') |
| 57 |
] |
| 58 |
mock_run.assert_called_once_with( |
| 59 |
['chroot', '/some/root', 'cmd', 'arg1', 'arg2'], |
| 60 |
None, True, False, True |
| 61 |
) |
| 62 |
mock_mntMngr.bind_mount.assert_called() |
| 63 |
mock_mntMngr.umount.assert_called() |
| 64 |
|
| 65 |
@patch('kiwi.chroot_manager.MountManager') |
| 66 |
@patch('kiwi.chroot_manager.Command.run') |
| 67 |
def test_run_fails_to_enter_chroot_context(self, mock_run, mock_mount): |
| 68 |
mock_mntMngr = Mock() |
| 69 |
mock_mount.return_value = mock_mntMngr |
| 70 |
mock_mntMngr.bind_mount.side_effect = KiwiCommandError('mount error') |
| 71 |
with raises(KiwiCommandError): |
| 72 |
with ChrootManager( |
| 73 |
'/some/root', binds=[ |
| 74 |
ChrootMount('/dev'), ChrootMount('/proc'), |
| 75 |
ChrootMount('/sys') |
| 76 |
] |
| 77 |
) as chroot: |
| 78 |
chroot.run(['cmd', 'arg1', 'arg2']) |
| 79 |
assert mock_mount.call_args_list == [ |
| 80 |
call(device='/dev', mountpoint='/some/root/dev'), |
| 81 |
call(device='/proc', mountpoint='/some/root/proc'), |
| 82 |
call(device='/sys', mountpoint='/some/root/sys') |
| 83 |
] |
| 84 |
mock_run.assert_not_called() |
| 85 |
mock_mntMngr.bind_mount.assert_called_once() |
| 86 |
mock_mntMngr.umount.assert_called() |
| 87 |
|
| 88 |
@patch('kiwi.chroot_manager.MountManager') |
| 89 |
@patch('kiwi.chroot_manager.Command.run') |
| 90 |
def test_run_fails_to_enter_and_clean_chroot_context( |
| 91 |
self, mock_run, mock_mount |
| 92 |
): |
| 93 |
mock_mntMngr = Mock() |
| 94 |
mock_mount.return_value = mock_mntMngr |
| 95 |
mock_mntMngr.bind_mount.side_effect = KiwiCommandError('mount error') |
| 96 |
mock_mntMngr.umount.side_effect = KiwiUmountBusyError('umount error') |
| 97 |
with raises(KiwiCommandError): |
| 98 |
with ChrootManager( |
| 99 |
'/some/root', binds=[ |
| 100 |
ChrootMount('/dev'), ChrootMount('/proc'), |
| 101 |
ChrootMount('/sys') |
| 102 |
] |
| 103 |
) as chroot: |
| 104 |
chroot.run(['cmd', 'arg1', 'arg2']) |
| 105 |
assert mock_mount.call_args_list == [ |
| 106 |
call(device='/dev', mountpoint='/some/root/dev'), |
| 107 |
call(device='/proc', mountpoint='/some/root/proc'), |
| 108 |
call(device='/sys', mountpoint='/some/root/sys') |
| 109 |
] |
| 110 |
mock_run.assert_not_called() |
| 111 |
mock_mntMngr.bind_mount.assert_called_once() |
| 112 |
mock_mntMngr.umount.assert_called() |
| 113 |
|
| 114 |
@patch('kiwi.chroot_manager.MountManager') |
| 115 |
@patch('kiwi.chroot_manager.Command.run') |
| 116 |
def test_run_fails_to_clean_chroot_context( |
| 117 |
self, mock_run, mock_mount |
| 118 |
): |
| 119 |
mock_mntMngr = Mock() |
| 120 |
mock_mount.return_value = mock_mntMngr |
| 121 |
mock_mntMngr.umount.side_effect = KiwiUmountBusyError('umount error') |
| 122 |
with raises(KiwiUmountBusyError): |
| 123 |
with ChrootManager( |
| 124 |
'/some/root', binds=[ |
| 125 |
ChrootMount('/dev'), ChrootMount('/proc'), |
| 126 |
ChrootMount('/sys') |
| 127 |
] |
| 128 |
) as chroot: |
| 129 |
chroot.run(['cmd', 'arg1', 'arg2']) |
| 130 |
assert mock_mount.call_args_list == [ |
| 131 |
call(device='/dev', mountpoint='/some/root/dev'), |
| 132 |
call(device='/proc', mountpoint='/some/root/proc'), |
| 133 |
call(device='/sys', mountpoint='/some/root/sys') |
| 134 |
] |
| 135 |
mock_run.assert_called_once_with( |
| 136 |
['chroot', '/some/root', 'cmd', 'arg1', 'arg2'], |
| 137 |
None, True, False, True |
| 138 |
) |
| 139 |
mock_mntMngr.bind_mount.assert_called() |
| 140 |
mock_mntMngr.umount.assert_called() |