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