| 1 |
import os |
| 2 |
from unittest.mock import ( |
| 3 |
patch, MagicMock, Mock, call |
| 4 |
) |
| 5 |
from pytest import fixture |
| 6 |
|
| 7 |
from kiwi.system.mount import ImageSystem |
| 8 |
from kiwi.storage.mapped_device import MappedDevice |
| 9 |
from kiwi.storage.disk import ptable_entry_type |
| 10 |
|
| 11 |
|
| 12 |
class TestImageSystem: |
| 13 |
@fixture(autouse=True) |
| 14 |
def inject_fixtures(self, caplog): |
| 15 |
self._caplog = caplog |
| 16 |
|
| 17 |
@patch('os.path.exists') |
| 18 |
def setup(self, mock_os_path_exists): |
| 19 |
mock_os_path_exists.return_value = True |
| 20 |
self.device_map = { |
| 21 |
'root': MappedDevice('/dev/root-device', Mock()), |
| 22 |
'readonly': MappedDevice('/dev/readonly-root-device', Mock()), |
| 23 |
'boot': MappedDevice('/dev/boot-device', Mock()), |
| 24 |
'efi': MappedDevice('/dev/efi-device', Mock()), |
| 25 |
'var': MappedDevice('/dev/var-device', Mock()) |
| 26 |
} |
| 27 |
volume_manager = Mock() |
| 28 |
volume_manager.get_mountpoint.return_value = 'volume_manager_root' |
| 29 |
self.volume_manager = volume_manager |
| 30 |
self.partitions = { |
| 31 |
'var': ptable_entry_type( |
| 32 |
mbsize=100, |
| 33 |
clone=1, |
| 34 |
partition_name='p.lxvar', |
| 35 |
partition_type='t.linux', |
| 36 |
partition_id=None, |
| 37 |
mountpoint='/var', |
| 38 |
filesystem='ext3', |
| 39 |
label='var' |
| 40 |
) |
| 41 |
} |
| 42 |
self.image_system = ImageSystem( |
| 43 |
self.device_map, 'root_dir', self.volume_manager, self.partitions |
| 44 |
) |
| 45 |
|
| 46 |
@patch('os.path.exists') |
| 47 |
def setup_method(self, cls, mock_os_path_exists): |
| 48 |
self.setup() |
| 49 |
|
| 50 |
def test_mountpoint(self): |
| 51 |
assert self.image_system.mountpoint() == '' |
| 52 |
|
| 53 |
@patch('kiwi.system.mount.MountManager') |
| 54 |
def test_mount_with_volumes(self, mock_MountManager): |
| 55 |
self.image_system.mount() |
| 56 |
root_mount_mountpoint = self.image_system.mountpoint() |
| 57 |
self.volume_manager.mount_volumes.assert_called_once_with() |
| 58 |
assert mock_MountManager.call_args_list == [ |
| 59 |
call( |
| 60 |
device='/dev/boot-device', |
| 61 |
mountpoint=os.path.join(root_mount_mountpoint, 'boot') |
| 62 |
), |
| 63 |
call( |
| 64 |
device='/dev/efi-device', |
| 65 |
mountpoint=os.path.join(root_mount_mountpoint, 'boot', 'efi') |
| 66 |
), |
| 67 |
call( |
| 68 |
device='/dev/var-device', |
| 69 |
mountpoint=os.path.join(root_mount_mountpoint, 'var') |
| 70 |
), |
| 71 |
call( |
| 72 |
device='root_dir/image', |
| 73 |
mountpoint=os.path.join(root_mount_mountpoint, 'image') |
| 74 |
), |
| 75 |
call( |
| 76 |
device='tmpfs', |
| 77 |
mountpoint=os.path.join(root_mount_mountpoint, 'tmp') |
| 78 |
), |
| 79 |
call( |
| 80 |
device='tmpfs', |
| 81 |
mountpoint=os.path.join(root_mount_mountpoint, 'var', 'tmp') |
| 82 |
), |
| 83 |
call( |
| 84 |
device='/proc', |
| 85 |
mountpoint=os.path.join(root_mount_mountpoint, 'proc') |
| 86 |
), |
| 87 |
call( |
| 88 |
device='/sys', |
| 89 |
mountpoint=os.path.join(root_mount_mountpoint, 'sys') |
| 90 |
), |
| 91 |
call( |
| 92 |
device='/dev', |
| 93 |
mountpoint=os.path.join(root_mount_mountpoint, 'dev') |
| 94 |
) |
| 95 |
] |
| 96 |
|
| 97 |
@patch('kiwi.system.mount.MountManager') |
| 98 |
def test_mount(self, mock_MountManager): |
| 99 |
self.image_system.volume_manager = None |
| 100 |
self.image_system.mount() |
| 101 |
root_mount_mountpoint = self.image_system.mountpoint() |
| 102 |
assert mock_MountManager.call_args_list == [ |
| 103 |
call(device='/dev/readonly-root-device'), |
| 104 |
call( |
| 105 |
device='/dev/boot-device', |
| 106 |
mountpoint=os.path.join(root_mount_mountpoint, 'boot') |
| 107 |
), |
| 108 |
call( |
| 109 |
device='/dev/efi-device', |
| 110 |
mountpoint=os.path.join(root_mount_mountpoint, 'boot', 'efi') |
| 111 |
), |
| 112 |
call( |
| 113 |
device='/dev/var-device', |
| 114 |
mountpoint=os.path.join(root_mount_mountpoint, 'var') |
| 115 |
), |
| 116 |
call( |
| 117 |
device='root_dir/image', |
| 118 |
mountpoint=os.path.join(root_mount_mountpoint, 'image') |
| 119 |
), |
| 120 |
call( |
| 121 |
device='tmpfs', |
| 122 |
mountpoint=os.path.join(root_mount_mountpoint, 'tmp') |
| 123 |
), |
| 124 |
call( |
| 125 |
device='tmpfs', |
| 126 |
mountpoint=os.path.join(root_mount_mountpoint, 'var', 'tmp') |
| 127 |
), |
| 128 |
call( |
| 129 |
device='/proc', |
| 130 |
mountpoint=os.path.join(root_mount_mountpoint, 'proc') |
| 131 |
), |
| 132 |
call( |
| 133 |
device='/sys', |
| 134 |
mountpoint=os.path.join(root_mount_mountpoint, 'sys') |
| 135 |
), |
| 136 |
call( |
| 137 |
device='/dev', |
| 138 |
mountpoint=os.path.join(root_mount_mountpoint, 'dev') |
| 139 |
) |
| 140 |
] |
| 141 |
|
| 142 |
@patch('kiwi.system.mount.MountManager') |
| 143 |
def test_mount_s390(self, mock_MountManager): |
| 144 |
self.image_system.arch = 's390x' |
| 145 |
self.image_system.mount() |
| 146 |
root_mount_mountpoint = self.image_system.mountpoint() |
| 147 |
assert mock_MountManager.call_args_list[0] == call( |
| 148 |
device='/dev/boot-device', |
| 149 |
mountpoint=os.path.join(root_mount_mountpoint, 'boot', 'zipl') |
| 150 |
) |
| 151 |
|
| 152 |
def test_umount(self): |
| 153 |
some_mount = MagicMock() |
| 154 |
some_mount.is_mounted.return_value = True |
| 155 |
self.image_system.mount_list.append(some_mount) |
| 156 |
self.image_system.umount() |
| 157 |
some_mount.umount.assert_called_once_with() |
| 158 |
|
| 159 |
@patch('os.path.exists') |
| 160 |
@patch('kiwi.system.mount.MountManager') |
| 161 |
def test_context_manager_exit(self, mock_MountManager, mock_os_path_exists): |
| 162 |
mount = MagicMock() |
| 163 |
mount.is_mounted.return_value = True |
| 164 |
mock_os_path_exists.return_value = True |
| 165 |
mock_MountManager.return_value = mount |
| 166 |
device_map = { |
| 167 |
'root': MappedDevice('/dev/root-device', Mock()) |
| 168 |
} |
| 169 |
with ImageSystem(device_map, 'root_dir') as system: |
| 170 |
system.mount() |
| 171 |
mount.umount.assert_called() |