| 1 |
import logging |
| 2 |
from unittest.mock import ( |
| 3 |
patch, call |
| 4 |
) |
| 5 |
from pytest import ( |
| 6 |
raises, fixture |
| 7 |
) |
| 8 |
|
| 9 |
from kiwi.storage.loop_device import LoopDevice |
| 10 |
|
| 11 |
from kiwi.exceptions import ( |
| 12 |
KiwiLoopSetupError, |
| 13 |
KiwiCommandError |
| 14 |
) |
| 15 |
|
| 16 |
|
| 17 |
class TestLoopDevice: |
| 18 |
@fixture(autouse=True) |
| 19 |
def inject_fixtures(self, caplog): |
| 20 |
self._caplog = caplog |
| 21 |
|
| 22 |
@patch('os.path.exists') |
| 23 |
def setup(self, mock_exists): |
| 24 |
mock_exists.return_value = False |
| 25 |
self.loop = LoopDevice('loop-file', 20, 4096) |
| 26 |
|
| 27 |
@patch('os.path.exists') |
| 28 |
def setup_method(self, cls, mock_exists): |
| 29 |
self.setup() |
| 30 |
|
| 31 |
def test_loop_setup_invalid(self): |
| 32 |
with raises(KiwiLoopSetupError): |
| 33 |
LoopDevice('loop-file-does-not-exist-and-no-size-given') |
| 34 |
|
| 35 |
def test_get_device(self): |
| 36 |
assert self.loop.get_device() == '' |
| 37 |
|
| 38 |
def test_is_loop(self): |
| 39 |
assert self.loop.is_loop() is True |
| 40 |
|
| 41 |
@patch('os.path.exists') |
| 42 |
@patch('kiwi.storage.loop_device.SparseFile.create') |
| 43 |
@patch('kiwi.storage.loop_device.Command.run') |
| 44 |
@patch('kiwi.storage.loop_device.CommandCapabilities.has_option_in_help') |
| 45 |
def test_create( |
| 46 |
self, mock_has_option_in_help, mock_command, mock_sparse_file_create, |
| 47 |
mock_exists |
| 48 |
): |
| 49 |
mock_has_option_in_help.return_value = True |
| 50 |
mock_exists.return_value = False |
| 51 |
self.loop.create() |
| 52 |
mock_sparse_file_create.assert_called_once_with( |
| 53 |
'loop-file', '20M' |
| 54 |
) |
| 55 |
assert mock_command.call_args_list[0] == \ |
| 56 |
call([ |
| 57 |
'losetup', '--sector-size', '4096', |
| 58 |
'-f', '--show', 'loop-file' |
| 59 |
]) |
| 60 |
mock_has_option_in_help.return_value = False |
| 61 |
mock_command.reset_mock() |
| 62 |
mock_sparse_file_create.reset_mock() |
| 63 |
self.loop.create() |
| 64 |
mock_sparse_file_create.assert_called_once_with( |
| 65 |
'loop-file', '20M' |
| 66 |
) |
| 67 |
assert mock_command.call_args_list[0] == \ |
| 68 |
call([ |
| 69 |
'losetup', '--logical-blocksize', '4096', |
| 70 |
'-f', '--show', 'loop-file' |
| 71 |
]) |
| 72 |
self.loop.node_name = None |
| 73 |
|
| 74 |
@patch('kiwi.storage.loop_device.Command.run') |
| 75 |
@patch('os.path.exists') |
| 76 |
@patch('pathlib.Path.is_block_device') |
| 77 |
@patch('time.sleep') |
| 78 |
def test_context_manager_exit_loop_released( |
| 79 |
self, mock_time_sleep, mock_is_block_device, |
| 80 |
mock_os_path_exists, mock_command_run |
| 81 |
): |
| 82 |
is_block_device = [False, True] |
| 83 |
mock_os_path_exists.return_value = True |
| 84 |
mock_command_run.side_effect = KiwiCommandError('error') |
| 85 |
|
| 86 |
def Command_run(params): |
| 87 |
# raise on first command which is 'losetup -f ...' |
| 88 |
if params[1] == '-f': |
| 89 |
raise KiwiCommandError('issue') |
| 90 |
|
| 91 |
def Path_is_block_device(): |
| 92 |
return is_block_device.pop() |
| 93 |
|
| 94 |
mock_is_block_device.side_effect = Path_is_block_device |
| 95 |
mock_command_run.side_effect = Command_run |
| 96 |
|
| 97 |
with self._caplog.at_level(logging.ERROR): |
| 98 |
with LoopDevice('loop-file', 20) as loop_provider: |
| 99 |
loop_provider.node_name = '/dev/loop0' |
| 100 |
with raises(KiwiCommandError): |
| 101 |
loop_provider.create(overwrite=False) |
| 102 |
assert len(mock_is_block_device.call_args_list) == 2 |
| 103 |
assert mock_command_run.call_args_list == [ |
| 104 |
call(['losetup', '-f', '--show', 'loop-file']), |
| 105 |
call(['losetup', '-d', '/dev/loop0']) |
| 106 |
] |
| 107 |
|
| 108 |
@patch('kiwi.storage.loop_device.Command.run') |
| 109 |
@patch('os.path.exists') |
| 110 |
@patch('pathlib.Path.is_block_device') |
| 111 |
@patch('time.sleep') |
| 112 |
def test_context_manager_exit_loop_not_released( |
| 113 |
self, mock_time_sleep, mock_is_block_device, |
| 114 |
mock_os_path_exists, mock_command_run |
| 115 |
): |
| 116 |
mock_os_path_exists.return_value = True |
| 117 |
mock_command_run.side_effect = KiwiCommandError('error') |
| 118 |
mock_is_block_device.return_value = True |
| 119 |
|
| 120 |
def Command_run(params): |
| 121 |
# raise on first command which is 'losetup -f ...' |
| 122 |
if params[1] == '-f': |
| 123 |
raise KiwiCommandError('issue') |
| 124 |
|
| 125 |
mock_command_run.side_effect = Command_run |
| 126 |
|
| 127 |
with self._caplog.at_level(logging.ERROR): |
| 128 |
with LoopDevice('loop-file', 20) as loop_provider: |
| 129 |
loop_provider.node_name = '/dev/loop0' |
| 130 |
with raises(KiwiCommandError): |
| 131 |
loop_provider.create(overwrite=False) |
| 132 |
assert mock_command_run.call_args_list == [ |
| 133 |
call(['losetup', '-f', '--show', 'loop-file']), |
| 134 |
call(['losetup', '-d', '/dev/loop0']) |
| 135 |
] |