| 1 |
from collections import namedtuple |
| 2 |
from unittest.mock import ( |
| 3 |
patch, Mock |
| 4 |
) |
| 5 |
from pytest import raises |
| 6 |
|
| 7 |
from kiwi.volume_manager.base import VolumeManagerBase |
| 8 |
|
| 9 |
from kiwi.exceptions import KiwiVolumeManagerSetupError |
| 10 |
|
| 11 |
|
| 12 |
class TestVolumeManagerBase: |
| 13 |
@patch('os.path.exists') |
| 14 |
def setup(self, mock_path): |
| 15 |
self.volume_type = namedtuple( |
| 16 |
'volume_type', [ |
| 17 |
'name', |
| 18 |
'size', |
| 19 |
'realpath', |
| 20 |
'mountpoint', |
| 21 |
'fullsize', |
| 22 |
'attributes', |
| 23 |
'is_root_volume' |
| 24 |
] |
| 25 |
) |
| 26 |
mock_path.return_value = True |
| 27 |
self.device_map = { |
| 28 |
'root': Mock() |
| 29 |
} |
| 30 |
self.device_map['root'].is_loop = Mock( |
| 31 |
return_value=True |
| 32 |
) |
| 33 |
self.device_map['root'].get_device = Mock( |
| 34 |
return_value='/dev/storage' |
| 35 |
) |
| 36 |
self.volume_manager = VolumeManagerBase( |
| 37 |
self.device_map, 'root_dir', Mock() |
| 38 |
) |
| 39 |
self.volume_manager.volumes = [ |
| 40 |
self.volume_type( |
| 41 |
name='LVetc', size='freespace:200', realpath='/etc', |
| 42 |
mountpoint='/etc', fullsize=False, attributes=[], |
| 43 |
is_root_volume=False |
| 44 |
), |
| 45 |
self.volume_type( |
| 46 |
name='LVRoot', size='size:500', realpath='/', |
| 47 |
mountpoint='/', fullsize=True, attributes=[], |
| 48 |
is_root_volume=True |
| 49 |
) |
| 50 |
] |
| 51 |
|
| 52 |
@patch('os.path.exists') |
| 53 |
def setup_method(self, cls, mock_path): |
| 54 |
self.setup() |
| 55 |
|
| 56 |
def test_get_root_volume_name(self): |
| 57 |
assert self.volume_manager.get_root_volume_name() == '/' |
| 58 |
|
| 59 |
@patch('os.path.exists') |
| 60 |
def test_init_custom_args(self, mock_exists): |
| 61 |
mock_exists.return_value = True |
| 62 |
volume_manager = VolumeManagerBase( |
| 63 |
self.device_map, 'root_dir', Mock(), { |
| 64 |
'fs_create_options': 'create-opts', |
| 65 |
'fs_mount_options': 'mount-opts' |
| 66 |
} |
| 67 |
) |
| 68 |
assert volume_manager.custom_filesystem_args['mount_options'] == \ |
| 69 |
'mount-opts' |
| 70 |
assert volume_manager.custom_filesystem_args['create_options'] == \ |
| 71 |
'create-opts' |
| 72 |
|
| 73 |
@patch('os.path.exists') |
| 74 |
def test_root_dir_does_not_exist(self, mock_exists): |
| 75 |
mock_exists.return_value = False |
| 76 |
with raises(KiwiVolumeManagerSetupError): |
| 77 |
VolumeManagerBase(self.device_map, 'root_dir', Mock()) |
| 78 |
|
| 79 |
def test_is_loop(self): |
| 80 |
assert self.volume_manager.is_loop() == \ |
| 81 |
self.device_map['root'].is_loop() |
| 82 |
|
| 83 |
@patch('os.path.exists') |
| 84 |
def test_get_device(self, mock_exists): |
| 85 |
mock_exists.return_value = True |
| 86 |
assert self.volume_manager.get_device()['root'].get_device() == \ |
| 87 |
'/dev/storage' |
| 88 |
|
| 89 |
def test_setup(self): |
| 90 |
with raises(NotImplementedError): |
| 91 |
self.volume_manager.setup() |
| 92 |
|
| 93 |
def test_create_volumes(self): |
| 94 |
with raises(NotImplementedError): |
| 95 |
self.volume_manager.create_volumes('ext3') |
| 96 |
|
| 97 |
def test_get_fstab(self): |
| 98 |
with raises(NotImplementedError): |
| 99 |
self.volume_manager.get_fstab('by-label', 'ext3') |
| 100 |
|
| 101 |
@patch('kiwi.volume_manager.base.Path.create') |
| 102 |
@patch('os.path.exists') |
| 103 |
def test_create_volume_paths_in_root_dir(self, mock_os_path, mock_path): |
| 104 |
mock_os_path.return_value = False |
| 105 |
self.volume_manager.create_volume_paths_in_root_dir() |
| 106 |
mock_path.assert_called_once_with('root_dir/etc') |
| 107 |
|
| 108 |
def test_get_canonical_volume_list(self): |
| 109 |
volume_list = self.volume_manager.get_canonical_volume_list() |
| 110 |
assert volume_list.volumes[0].name == 'LVetc' |
| 111 |
assert volume_list.full_size_volume.name == 'LVRoot' |
| 112 |
|
| 113 |
@patch('kiwi.volume_manager.base.SystemSize') |
| 114 |
@patch('os.path.exists') |
| 115 |
def test_get_volume_mbsize( |
| 116 |
self, mock_os_path_exists, mock_size |
| 117 |
): |
| 118 |
mock_os_path_exists.return_value = True |
| 119 |
size = Mock() |
| 120 |
size.customize = Mock( |
| 121 |
return_value=42 |
| 122 |
) |
| 123 |
mock_size.return_value = size |
| 124 |
assert self.volume_manager.get_volume_mbsize( |
| 125 |
self.volume_manager.volumes[0], self.volume_manager.volumes, |
| 126 |
'ext3' |
| 127 |
) == 272 |
| 128 |
|
| 129 |
@patch('kiwi.volume_manager.base.SystemSize') |
| 130 |
@patch('os.path.exists') |
| 131 |
def test_get_volume_mbsize_for_oem_type( |
| 132 |
self, mock_os_path_exists, mock_size |
| 133 |
): |
| 134 |
mock_os_path_exists.return_value = True |
| 135 |
size = Mock() |
| 136 |
size.customize = Mock( |
| 137 |
return_value=42 |
| 138 |
) |
| 139 |
mock_size.return_value = size |
| 140 |
assert self.volume_manager.get_volume_mbsize( |
| 141 |
self.volume_manager.volumes[0], self.volume_manager.volumes, |
| 142 |
'ext3', True |
| 143 |
) == 72 |
| 144 |
|
| 145 |
@patch('kiwi.volume_manager.base.SystemSize') |
| 146 |
@patch('os.path.exists') |
| 147 |
def test_get_volume_mbsize_nested_volumes( |
| 148 |
self, mock_os_path_exists, mock_size |
| 149 |
): |
| 150 |
mock_os_path_exists.return_value = True |
| 151 |
size = Mock() |
| 152 |
size.customize = Mock( |
| 153 |
return_value=42 |
| 154 |
) |
| 155 |
mock_size.return_value = size |
| 156 |
self.volume_manager.volumes = [ |
| 157 |
self.volume_type( |
| 158 |
name='LVusr', size='freespace:200', realpath='/usr', |
| 159 |
mountpoint='/usr', fullsize=False, attributes=[], |
| 160 |
is_root_volume=False |
| 161 |
), |
| 162 |
self.volume_type( |
| 163 |
name='LVusr_lib', size='freespace:100', realpath='/usr/lib', |
| 164 |
mountpoint='/usr/lib', fullsize=False, attributes=[], |
| 165 |
is_root_volume=False |
| 166 |
) |
| 167 |
] |
| 168 |
assert self.volume_manager.get_volume_mbsize( |
| 169 |
self.volume_manager.volumes[0], self.volume_manager.volumes, |
| 170 |
'ext3' |
| 171 |
) == 272 |
| 172 |
size.accumulate_mbyte_file_sizes.assert_called_once_with( |
| 173 |
['root_dir/usr/lib'] |
| 174 |
) |
| 175 |
|
| 176 |
@patch('kiwi.volume_manager.base.SystemSize') |
| 177 |
@patch('os.path.exists') |
| 178 |
def test_get_volume_mbsize_root_volume( |
| 179 |
self, mock_os_path_exists, mock_size |
| 180 |
): |
| 181 |
mock_os_path_exists.return_value = True |
| 182 |
size = Mock() |
| 183 |
size.customize = Mock( |
| 184 |
return_value=42 |
| 185 |
) |
| 186 |
mock_size.return_value = size |
| 187 |
self.volume_manager.volumes = [ |
| 188 |
self.volume_type( |
| 189 |
name='LVusr', size='freespace:200', realpath='/usr', |
| 190 |
mountpoint='/usr', fullsize=False, attributes=[], |
| 191 |
is_root_volume=False |
| 192 |
), |
| 193 |
self.volume_type( |
| 194 |
name='LVusr_lib', size='freespace:100', realpath='/usr/lib', |
| 195 |
mountpoint='/usr/lib', fullsize=False, attributes=[], |
| 196 |
is_root_volume=False |
| 197 |
), |
| 198 |
self.volume_type( |
| 199 |
name='LVRoot', size='size:500', realpath='/', |
| 200 |
mountpoint='/', fullsize=True, attributes=[], |
| 201 |
is_root_volume=True |
| 202 |
) |
| 203 |
] |
| 204 |
assert self.volume_manager.get_volume_mbsize( |
| 205 |
self.volume_manager.volumes[2], self.volume_manager.volumes, |
| 206 |
'ext3', True |
| 207 |
) == 72 |
| 208 |
size.accumulate_mbyte_file_sizes.assert_called_once_with( |
| 209 |
['root_dir/usr', 'root_dir/usr/lib'] |
| 210 |
) |
| 211 |
|
| 212 |
def test_mount_volumes(self): |
| 213 |
with raises(NotImplementedError): |
| 214 |
self.volume_manager.mount_volumes() |
| 215 |
|
| 216 |
def test_mount(self): |
| 217 |
with raises(NotImplementedError): |
| 218 |
self.volume_manager.mount() |
| 219 |
|
| 220 |
def test_umount_volumes(self): |
| 221 |
with raises(NotImplementedError): |
| 222 |
self.volume_manager.umount_volumes() |
| 223 |
|
| 224 |
def test_umount(self): |
| 225 |
with raises(NotImplementedError): |
| 226 |
self.volume_manager.umount() |
| 227 |
|
| 228 |
def test_get_volumes(self): |
| 229 |
with raises(NotImplementedError): |
| 230 |
self.volume_manager.get_volumes() |
| 231 |
|
| 232 |
def test_get_mountpoint(self): |
| 233 |
assert self.volume_manager.get_mountpoint() is None |
| 234 |
|
| 235 |
@patch('kiwi.volume_manager.base.DataSync') |
| 236 |
@patch('kiwi.volume_manager.base.MountManager.is_mounted') |
| 237 |
@patch('kiwi.volume_manager.base.VolumeManagerBase.mount_volumes') |
| 238 |
def test_sync_data(self, mock_mount_volumes, mock_mounted, mock_sync): |
| 239 |
data_sync = Mock() |
| 240 |
mock_sync.return_value = data_sync |
| 241 |
mock_mounted.return_value = False |
| 242 |
self.volume_manager.mountpoint = 'mountpoint' |
| 243 |
self.volume_manager.sync_data(['exclude_me']) |
| 244 |
mock_mount_volumes.assert_called_once_with() |
| 245 |
mock_sync.assert_called_once_with( |
| 246 |
'root_dir', 'mountpoint' |
| 247 |
) |
| 248 |
data_sync.sync_data.assert_called_once_with( |
| 249 |
exclude=['exclude_me'], |
| 250 |
options=[ |
| 251 |
'--archive', '--hard-links', '--xattrs', |
| 252 |
'--acls', '--one-file-system', '--inplace' |
| 253 |
] |
| 254 |
) |
| 255 |
assert self.volume_manager.get_mountpoint() == 'mountpoint' |
| 256 |
|
| 257 |
@patch('kiwi.volume_manager.base.MountManager.is_mounted') |
| 258 |
def test_sync_data_without_mountpoint(self, mock_mounted): |
| 259 |
self.volume_manager.mountpoint = '' |
| 260 |
assert self.volume_manager.sync_data() is None |
| 261 |
mock_mounted.assert_not_called() |
| 262 |
|
| 263 |
def test_create_verity_layer(self): |
| 264 |
with raises(NotImplementedError): |
| 265 |
self.volume_manager.create_verity_layer() |
| 266 |
|
| 267 |
def test_create_verification_metadata(self): |
| 268 |
with raises(NotImplementedError): |
| 269 |
self.volume_manager.create_verification_metadata('/some/device') |
| 270 |
|
| 271 |
@patch('kiwi.volume_manager.base.Temporary') |
| 272 |
def test_setup_mountpoint(self, mock_Temporary): |
| 273 |
mock_Temporary.return_value.new_dir.return_value.name = 'tmpdir' |
| 274 |
self.volume_manager.setup_mountpoint() |
| 275 |
assert self.volume_manager.mountpoint == 'tmpdir' |
| 276 |
|
| 277 |
def test_set_property_readonly_root(self): |
| 278 |
with raises(KiwiVolumeManagerSetupError): |
| 279 |
self.volume_manager.set_property_readonly_root() |
| 280 |
|
| 281 |
@patch('kiwi.volume_manager.base.Command.run') |
| 282 |
def test_apply_attributes_on_volume(self, mock_command): |
| 283 |
self.volume_manager.apply_attributes_on_volume( |
| 284 |
'toplevel', self.volume_type( |
| 285 |
name='LVetc', size='freespace:200', realpath='/etc', |
| 286 |
mountpoint='/etc', fullsize=False, |
| 287 |
attributes=['no-copy-on-write'], |
| 288 |
is_root_volume=False |
| 289 |
) |
| 290 |
) |
| 291 |
mock_command.assert_called_once_with( |
| 292 |
['chattr', '+C', 'toplevel/etc'] |
| 293 |
) |
| 294 |
|
| 295 |
@patch('os.path.exists') |
| 296 |
def test_context_manager_exit(self, mock_os_path_exists): |
| 297 |
mock_os_path_exists.return_value = True |
| 298 |
with VolumeManagerBase(self.device_map, 'root_dir', Mock()): |
| 299 |
pass |
| 300 |
# just pass |