Debian Codegraph @

test/unit/volume_manager/btrfs_test.py

raw
1 import datetime
2 from unittest.mock import (
3 patch, call, Mock, mock_open
4 )
5 from pytest import (
6 raises, fixture
7 )
8
9 import pytest
10
11 from kiwi.volume_manager.btrfs import VolumeManagerBtrfs
12 from kiwi.xml_state import volume_type
13
14 from kiwi.exceptions import (
15 KiwiVolumeRootIDError,
16 KiwiVolumeManagerSetupError
17 )
18
19
20 class TestVolumeManagerBtrfs:
21 @fixture(autouse=True)
22 def inject_fixtures(self, caplog):
23 self._caplog = caplog
24
25 @patch('os.path.exists')
26 def setup(self, mock_path):
27 self.volumes = [
28 volume_type(
29 name='@', parent='', size='freespace:100', realpath='/',
30 mountpoint=None, fullsize=False, label=None,
31 attributes=[], is_root_volume=True
32 ),
33 volume_type(
34 name='etc', parent='', size='freespace:200', realpath='/etc',
35 mountpoint='/etc', fullsize=False, label=None,
36 attributes=['quota=2G'], is_root_volume=False
37 ),
38 volume_type(
39 name='myvol', parent='', size='size:500', realpath='/data',
40 mountpoint='LVdata', fullsize=False, label=None,
41 attributes=[], is_root_volume=False
42 ),
43 volume_type(
44 name='home', parent='', size=None, realpath='/home',
45 mountpoint='/home', fullsize=True, label=None,
46 attributes=[], is_root_volume=False
47 )
48 ]
49 mock_path.return_value = True
50 self.device_map = {
51 'root': Mock()
52 }
53 self.device_map['root'].is_loop = Mock(
54 return_value=True
55 )
56 self.device_map['root'].get_device = Mock(
57 return_value='/dev/storage'
58 )
59 self.volume_manager = VolumeManagerBtrfs(
60 self.device_map, 'root_dir', self.volumes
61 )
62 self.volume_manager.mountpoint = '/var/tmp/kiwi_volumes.XXX'
63
64 @patch('os.path.exists')
65 def setup_method(self, cls, mock_path):
66 self.setup()
67
68 def test_get_root_volume_name(self):
69 assert self.volume_manager.get_root_volume_name() == '@'
70
71 def test_post_init(self):
72 self.volume_manager.post_init({'some-arg': 'some-val'})
73 assert self.volume_manager.custom_args['some-arg'] == 'some-val'
74
75 def test_post_init_root_is_snapper_snapshot_without_root_volume(self):
76 self.volume_manager.volumes = [
77 volume_type(
78 name='/', parent='', size='freespace:100', realpath='/',
79 mountpoint=None, fullsize=False, label=None,
80 attributes=[], is_root_volume=True
81 )
82 ]
83 self.volume_manager.post_init({'root_is_snapper_snapshot': True})
84 assert self.volume_manager.custom_args['root_is_snapper_snapshot'] is False
85
86 def test_fails_to_get_mountpoint_without_mountpoint(self):
87 self.volume_manager.mountpoint = ''
88 with pytest.raises(KiwiVolumeManagerSetupError):
89 self.volume_manager.get_mountpoint()
90
91 @patch('os.path.exists')
92 @patch('kiwi.volume_manager.btrfs.Command.run')
93 @patch('kiwi.volume_manager.btrfs.FileSystem.new')
94 @patch('kiwi.volume_manager.btrfs.MappedDevice')
95 @patch('kiwi.volume_manager.btrfs.MountManager')
96 @patch('kiwi.volume_manager.base.Temporary')
97 def test_setup_no_snapshot(
98 self, mock_Temporary, mock_mount, mock_mapped_device, mock_fs,
99 mock_command, mock_os_exists
100 ):
101 mock_Temporary.return_value.new_dir.return_value.name = 'tmpdir'
102 toplevel_mount = Mock()
103 mock_mount.return_value = toplevel_mount
104 command_call = Mock()
105 command_call.output = 'ID 256 gen 23 top level 5 path @'
106 mock_mapped_device.return_value = 'mapped_device'
107 mock_os_exists.return_value = False
108 mock_command.return_value = command_call
109
110 self.volume_manager.setup()
111
112 assert mock_mount.call_args_list == [
113 call(device='/dev/storage', mountpoint='tmpdir'),
114 call(device='tmpdir/@', mountpoint='tmpdir/@')
115 ]
116 toplevel_mount.mount.assert_called_once_with([])
117 assert mock_command.call_args_list == [
118 call(['btrfs', 'subvolume', 'create', 'tmpdir/@']),
119 call(['btrfs', 'subvolume', 'list', 'tmpdir']),
120 call(['btrfs', 'subvolume', 'set-default', '256', 'tmpdir'])
121 ]
122
123 @patch('os.path.exists')
124 @patch('kiwi.volume_manager.btrfs.Command.run')
125 @patch('kiwi.volume_manager.btrfs.SnapshotManager.new')
126 @patch('kiwi.volume_manager.btrfs.FileSystem.new')
127 @patch('kiwi.volume_manager.btrfs.MappedDevice')
128 @patch('kiwi.volume_manager.btrfs.MountManager')
129 @patch('kiwi.volume_manager.base.Temporary')
130 def test_setup_with_snapshot(
131 self, mock_Temporary, mock_mount, mock_mapped_device, mock_fs,
132 mock_snapshot_mngr, mock_command, mock_os_exists
133 ):
134 snapper = Mock()
135 snapper.create_first_snapshot = Mock(return_value=[mock_mount])
136 snapper.get_default_snapshot_name = Mock()
137 mock_snapshot_mngr.return_value = snapper
138
139 mock_Temporary.return_value.new_dir.return_value.name = 'tmpdir'
140 toplevel_mount = Mock()
141 mock_mount.return_value = toplevel_mount
142 mock_mapped_device.return_value = 'mapped_device'
143 mock_os_exists.return_value = False
144 self.volume_manager.custom_args['root_is_snapper_snapshot'] = True
145 self.volume_manager.custom_args['quota_groups'] = True
146
147 self.volume_manager.setup()
148
149 assert mock_mount.call_args_list == [
150 call(device='/dev/storage', mountpoint='tmpdir'),
151 call(
152 device='tmpdir/@/.snapshots/1/snapshot',
153 mountpoint='tmpdir/@/.snapshots/1/snapshot'
154 )
155 ]
156 toplevel_mount.mount.assert_called_once_with([])
157 snapper.create_first_snapshot.assert_called_once_with()
158 snapper.get_default_snapshot_name.assert_called_once_with()
159 assert mock_command.call_args_list == [
160 call(['btrfs', 'quota', 'enable', 'tmpdir']),
161 call(['btrfs', 'subvolume', 'create', 'tmpdir/@'])
162 ]
163
164 @patch('os.path.exists')
165 @patch('kiwi.volume_manager.btrfs.Command.run')
166 @patch('kiwi.volume_manager.btrfs.FileSystem.new')
167 @patch('kiwi.volume_manager.btrfs.MappedDevice')
168 @patch('kiwi.volume_manager.btrfs.MountManager')
169 @patch('kiwi.volume_manager.base.Temporary')
170 def test_setup_volume_id_not_detected(
171 self, mock_Temporary, mock_mount, mock_mapped_device, mock_fs,
172 mock_command, mock_os_exists
173 ):
174 command_call = Mock()
175 command_call.output = 'id-string-invalid'
176 mock_mapped_device.return_value = 'mapped_device'
177 mock_os_exists.return_value = False
178 mock_command.return_value = command_call
179 with raises(KiwiVolumeRootIDError):
180 self.volume_manager.setup()
181
182 @patch('os.path.exists')
183 @patch('kiwi.volume_manager.btrfs.Command.run')
184 @patch('kiwi.volume_manager.btrfs.MountManager')
185 @patch('kiwi.volume_manager.btrfs.Path.create')
186 @patch('kiwi.volume_manager.base.VolumeManagerBase.apply_attributes_on_volume')
187 def test_create_volumes_no_root_volume(
188 self, mock_attrs, mock_path, mock_mount, mock_command, mock_os_exists
189 ):
190 volume_mount = Mock()
191 mock_mount.return_value = volume_mount
192 self.volume_manager.mountpoint = 'tmpdir'
193 self.volume_manager.custom_args['root_is_snapper_snapshot'] = False
194 mock_os_exists.return_value = False
195
196 self.volume_manager.root_volume_name = '/'
197 self.volume_manager.volumes = [
198 volume_type(
199 name='/', parent='', size='freespace:100', realpath='/',
200 mountpoint=None, fullsize=False, label=None,
201 attributes=[], is_root_volume=True
202 ),
203 volume_type(
204 name='home', parent='/', size=None, realpath='/home',
205 mountpoint='/home', fullsize=True, label=None,
206 attributes=[], is_root_volume=False
207 )
208 ]
209
210 self.volume_manager.create_volumes('btrfs')
211
212 assert mock_path.call_args_list == [
213 call('root_dir/home'),
214 call('tmpdir')
215 ]
216 mock_command.assert_called_once_with(
217 ['btrfs', 'subvolume', 'create', 'tmpdir/home']
218 )
219 mock_mount.assert_called_once_with(
220 device='/dev/storage',
221 attributes={
222 'parent': '/',
223 'subvol_path': 'home',
224 'subvol_name': 'home'
225 },
226 mountpoint='tmpdir/home'
227 )
228
229 @patch('os.path.exists')
230 @patch('kiwi.volume_manager.btrfs.Command.run')
231 @patch('kiwi.volume_manager.btrfs.MountManager')
232 @patch('kiwi.volume_manager.btrfs.Path.create')
233 @patch('kiwi.volume_manager.base.VolumeManagerBase.apply_attributes_on_volume')
234 def test_create_volumes(
235 self, mock_attrs, mock_path, mock_mount, mock_command, mock_os_exists
236 ):
237 volume_mount = Mock()
238 mock_mount.return_value = volume_mount
239 self.volume_manager.mountpoint = 'tmpdir'
240 self.volume_manager.custom_args['root_is_snapper_snapshot'] = True
241 mock_os_exists.return_value = False
242
243 self.volume_manager.create_volumes('btrfs')
244
245 assert mock_attrs.call_args_list == [
246 call(
247 'tmpdir/@', volume_type(
248 name='myvol', parent='', size='size:500', realpath='/data',
249 mountpoint='LVdata', fullsize=False, label=None,
250 attributes=[],
251 is_root_volume=False
252 )
253 ),
254 call(
255 'tmpdir/@', volume_type(
256 name='etc', parent='', size='freespace:200', realpath='/etc',
257 mountpoint='/etc', fullsize=False, label=None,
258 attributes=['quota=2G'],
259 is_root_volume=False
260 )
261 ),
262 call(
263 'tmpdir/@', volume_type(
264 name='home', parent='', size=None, realpath='/home',
265 mountpoint='/home', fullsize=True, label=None,
266 attributes=[],
267 is_root_volume=False
268 )
269 )
270 ]
271 assert mock_path.call_args_list == [
272 call('root_dir/etc'),
273 call('root_dir/data'),
274 call('root_dir/home'),
275 call('tmpdir/@'),
276 call('tmpdir/@'),
277 call('tmpdir/@')
278 ]
279 assert mock_command.call_args_list == [
280 call(['btrfs', 'subvolume', 'create', 'tmpdir/@/data']),
281 call(['btrfs', 'subvolume', 'create', 'tmpdir/@/etc']),
282 call(['btrfs', 'quota', 'enable', '--simple', 'tmpdir/@/etc']),
283 call(['btrfs', 'qgroup', 'limit', '2G', 'tmpdir/@/etc']),
284 call(['btrfs', 'subvolume', 'create', 'tmpdir/@/home'])
285 ]
286 assert mock_mount.call_args_list == [
287 call(
288 device='/dev/storage',
289 mountpoint='tmpdir/@/.snapshots/1/snapshot/data',
290 attributes={
291 'subvol_path': '@/data',
292 'subvol_name': '@/data'
293 }
294 ),
295 call(
296 device='/dev/storage',
297 mountpoint='tmpdir/@/.snapshots/1/snapshot/etc',
298 attributes={
299 'subvol_path': '@/etc',
300 'subvol_name': '@/etc'
301 }
302 ),
303 call(
304 device='/dev/storage',
305 mountpoint='tmpdir/@/.snapshots/1/snapshot/home',
306 attributes={
307 'subvol_path': '@/home',
308 'subvol_name': '@/home'
309 }
310 )
311 ]
312
313 def test_get_volumes(self):
314 volume_mount = Mock()
315 volume_mount.mountpoint = \
316 '/var/tmp/kiwi_volumes.xx/@/.snapshots/1/snapshot/boot/grub2'
317 volume_mount.device = 'device'
318 volume_mount.get_attributes.return_value = {
319 'subvol_path': '@/boot/grub2'
320 }
321 self.volume_manager.subvol_mount_list = [volume_mount]
322 self.volume_manager.custom_args['root_is_snapper_snapshot'] = True
323 assert self.volume_manager.get_volumes() == {
324 '/boot/grub2': {
325 'volume_options': 'subvol=@/boot/grub2',
326 'volume_device': 'device'
327 }
328 }
329
330 @patch('kiwi.volume_manager.btrfs.Command.run')
331 def test_get_fstab(self, mock_command):
332 blkid_result = Mock()
333 blkid_result.output = 'id'
334 mock_command.return_value = blkid_result
335 volume_mount = Mock()
336 volume_mount.mountpoint = \
337 '/var/tmp/kiwi_volumes.XXX/@/.snapshots/1/snapshot/var/tmp'
338 volume_mount.device = 'device'
339 volume_mount.get_attributes.return_value = {
340 'subvol_path': '@/var/tmp',
341 'parent': 'subvol_takes_precedence'
342 }
343 self.volume_manager.subvol_mount_list = [volume_mount]
344 self.volume_manager.custom_args['root_is_snapper_snapshot'] = True
345 assert self.volume_manager.get_fstab() == [
346 'LABEL=id /var/tmp btrfs defaults,subvol=@/var/tmp 0 0'
347 ]
348 self.volumes.append(
349 volume_type(
350 name='device',
351 parent='',
352 size='freespace:100',
353 realpath='/var/tmp',
354 mountpoint=volume_mount.mountpoint,
355 fullsize=False,
356 label=None,
357 attributes=['enable-for-filesystem-check'],
358 is_root_volume=False
359 )
360 )
361 assert self.volume_manager.get_fstab() == [
362 'LABEL=id /var/tmp btrfs defaults,subvol=@/var/tmp 0 2'
363 ]
364
365 @patch('os.path.exists')
366 @patch('kiwi.volume_manager.btrfs.Path.create')
367 def test_mount_volumes(self, mock_path, mock_os_exists):
368 self.volume_manager.toplevel_mount = Mock()
369 self.volume_manager.toplevel_mount.is_mounted = Mock(
370 return_value=False
371 )
372 mock_os_exists.return_value = False
373 self.volume_manager.snapshots_root_mount = Mock()
374 volume_mount = Mock()
375 volume_mount.mountpoint = \
376 '/var/tmp/kiwi_volumes.xx/@/.snapshots/1/snapshot/var/tmp'
377 volume_mount.get_attributes.return_value = {
378 'subvol_path': '@/var/tmp'
379 }
380 self.volume_manager.custom_args['root_is_snapper_snapshot'] = True
381 self.volume_manager.subvol_mount_list = [volume_mount]
382
383 self.volume_manager.mount_volumes()
384
385 self.volume_manager.snapshots_root_mount.bind_mount.assert_called_once_with()
386 self.volume_manager.toplevel_mount.mount.assert_called_once_with([])
387 mock_path.assert_called_once_with(volume_mount.mountpoint)
388 volume_mount.mount.assert_called_once_with(
389 options=['subvol=@/var/tmp']
390 )
391
392 def test_umount_volumes(self):
393 self.volume_manager.toplevel_mount = Mock()
394 self.volume_manager.root_volume_mount = Mock()
395 volume_mount = Mock()
396 volume_mount.is_mounted = Mock(
397 return_value=True
398 )
399 self.volume_manager.toplevel_mount.is_mounted = Mock(
400 return_value=True
401 )
402 self.volume_manager.snapshots_root_mount = Mock()
403 self.volume_manager.snapshots_root_mount.is_mounted = Mock(
404 return_value=True
405 )
406 self.volume_manager.subvol_mount_list = [volume_mount]
407
408 self.volume_manager.umount_volumes()
409
410 volume_mount.is_mounted.assert_called_once_with()
411 volume_mount.umount.assert_called_once_with()
412 self.volume_manager.snapshots_root_mount.is_mounted.assert_called_once_with()
413 self.volume_manager.snapshots_root_mount.umount.assert_called_once_with()
414 self.volume_manager.toplevel_mount.is_mounted.assert_called_once_with()
415 self.volume_manager.root_volume_mount.umount.assert_called_once_with()
416 self.volume_manager.toplevel_mount.umount.assert_called_once_with()
417
418 @patch('kiwi.volume_manager.btrfs.DataSync')
419 def test_sync_data(self, mock_sync):
420 self.volume_manager.custom_args['quota_groups'] = True
421
422 snapper = Mock()
423 snapper.setup_first_snapshot = Mock()
424
425 self.volume_manager.snapper = snapper
426 self.volume_manager.toplevel_mount = Mock()
427 self.volume_manager.mountpoint = 'tmpdir'
428 self.volume_manager.custom_args['root_is_snapper_snapshot'] = True
429 sync = Mock()
430 mock_sync.return_value = sync
431
432 self.volume_manager.sync_data(['exclude_me'])
433
434 root_path = 'tmpdir/@/.snapshots/1/snapshot'
435 mock_sync.assert_called_once_with('root_dir', root_path)
436 sync.sync_data.assert_called_once_with(
437 exclude=['exclude_me'],
438 options=[
439 '--archive', '--hard-links', '--xattrs',
440 '--acls', '--one-file-system', '--inplace'
441 ]
442 )
443
444 @patch('kiwi.volume_manager.btrfs.DataSync')
445 @patch('kiwi.volume_manager.btrfs.Command.run')
446 @patch('os.path.exists')
447 @patch.object(datetime, 'datetime', Mock(wraps=datetime.datetime))
448 def test_sync_data_no_root_snapshot(
449 self, mock_exists, mock_command, mock_sync
450 ):
451 self.volume_manager.custom_args['quota_groups'] = True
452 mock_exists.return_value = True
453 datetime.datetime.now.return_value = datetime.datetime(2016, 1, 1)
454 self.volume_manager.toplevel_mount = Mock()
455 self.volume_manager.mountpoint = 'tmpdir'
456 self.volume_manager.custom_args['root_is_snapper_snapshot'] = False
457 sync = Mock()
458 mock_sync.return_value = sync
459
460 m_open = mock_open(read_data='')
461 with patch('builtins.open', m_open, create=True):
462 self.volume_manager.sync_data(['exclude_me'])
463
464 mock_sync.assert_called_once_with(
465 'root_dir', 'tmpdir/@'
466 )
467 sync.sync_data.assert_called_once_with(
468 exclude=['exclude_me'],
469 options=[
470 '--archive', '--hard-links', '--xattrs', '--acls',
471 '--one-file-system', '--inplace'
472 ]
473 )
474
475 @patch('kiwi.volume_manager.btrfs.Command.run')
476 def test_set_property_readonly_root(self, mock_command):
477 self.volume_manager.mountpoint = 'tmpdir'
478 self.volume_manager.custom_args['root_is_snapper_snapshot'] = True
479 self.volume_manager.custom_args['root_is_readonly_snapshot'] = True
480 self.volume_manager.set_property_readonly_root()
481 mock_command.assert_called_once_with(
482 [
483 'btrfs', 'property',
484 'set', 'tmpdir/@/.snapshots/1/snapshot', 'ro', 'true'
485 ]
486 )
487
488 @patch('kiwi.volume_manager.btrfs.VolumeManagerBtrfs.umount_volumes')
489 @patch('os.path.exists')
490 def test_context_manager_exit(
491 self, mock_os_path_exists, mock_VolumeManagerBtrfs_umount_volumes
492 ):
493 mock_os_path_exists.return_value = True
494 with VolumeManagerBtrfs(
495 self.device_map, 'root_dir', self.volumes
496 ) as volume_manager:
497 volume_manager.toplevel_mount = Mock()
498 volume_manager.root_volume_mount = Mock()
499
500 mock_VolumeManagerBtrfs_umount_volumes.assert_called_once_with()