Debian Codegraph @

test/unit/volume_manager/lvm_test.py

raw
1 import logging
2 from unittest.mock import (
3 patch, call, Mock
4 )
5 from pytest import (
6 raises, fixture
7 )
8
9 from kiwi.volume_manager.lvm import VolumeManagerLVM
10 from kiwi.defaults import Defaults
11
12 from kiwi.exceptions import (
13 KiwiVolumeGroupConflict,
14 KiwiCommandError
15 )
16 from kiwi.xml_state import volume_type
17
18
19 class TestVolumeManagerLVM:
20 @fixture(autouse=True)
21 def inject_fixtures(self, caplog):
22 self._caplog = caplog
23
24 @patch('os.path.exists')
25 def setup(self, mock_path):
26 self.volumes = [
27 volume_type(
28 name='LVRoot', parent='', size='freespace:100', realpath='/',
29 mountpoint=None, fullsize=False, label=None, attributes=[],
30 is_root_volume=True
31 ),
32 volume_type(
33 name='LVSwap', parent='', size='size:100', realpath='swap',
34 mountpoint=None, fullsize=False, label='SWAP', attributes=[],
35 is_root_volume=False
36 ),
37 volume_type(
38 name='LVetc', parent='', size='freespace:200', realpath='/etc',
39 mountpoint='/etc', fullsize=False, label='etc', attributes=[],
40 is_root_volume=False
41 ),
42 volume_type(
43 name='myvol', parent='', size='size:500', realpath='/data',
44 mountpoint='LVdata', fullsize=False, label=None, attributes=[],
45 is_root_volume=False
46 ),
47 volume_type(
48 name='LVhome', parent='', size=None, realpath='/home',
49 mountpoint='/home', fullsize=True, label=None, attributes=[],
50 is_root_volume=False
51 ),
52 ]
53 mock_path.return_value = True
54 self.device_map = {
55 'root': Mock()
56 }
57 self.device_map['root'].is_loop = Mock(
58 return_value=True
59 )
60 self.device_map['root'].get_device = Mock(
61 return_value='/dev/storage'
62 )
63 self.volume_manager = VolumeManagerLVM(
64 self.device_map, 'root_dir', self.volumes,
65 {'some-arg': 'some-val', 'fs_mount_options': ['a,b,c']}
66 )
67 assert self.volume_manager.mount_options == 'a,b,c'
68
69 @patch('os.path.exists')
70 def setup_method(self, cls, mock_path):
71 self.setup()
72
73 def test_post_init_custom_args(self):
74 self.volume_manager.post_init({'some-arg': 'some-val'})
75 assert self.volume_manager.custom_args['some-arg'] == 'some-val'
76
77 def test_post_init_no_additional_custom_args(self):
78 self.volume_manager.post_init(None)
79 assert self.volume_manager.custom_args == {
80 'root_label': 'ROOT', 'resize_on_boot': False
81 }
82
83 def test_post_init_no_mount_options(self):
84 self.volume_manager.custom_filesystem_args['mount_options'] = None
85 self.volume_manager.post_init(None)
86 assert self.volume_manager.mount_options == 'defaults'
87
88 @patch('os.path.exists')
89 def test_get_device(self, mock_path):
90 mock_path.return_value = True
91 self.volume_manager.volume_map = {
92 'LVRoot': '/dev/lvroot', 'LVx': '/dev/lvx',
93 'LVSwap': '/dev/lvswap', 'LVs': '/dev/lvs'
94 }
95 assert self.volume_manager.get_device()['LVx'].get_device() == \
96 '/dev/lvx'
97 assert self.volume_manager.get_device()['root'].get_device() == \
98 '/dev/lvroot'
99 assert self.volume_manager.get_device()['swap'].get_device() == \
100 '/dev/lvswap'
101
102 @patch('kiwi.volume_manager.lvm.Command.run')
103 @patch('kiwi.volume_manager.base.Temporary')
104 def test_setup(self, mock_Temporary, mock_command):
105 mock_Temporary.return_value.new_dir.return_value.name = 'tmpdir'
106 command = Mock()
107 # no output for commands to mock empty information for
108 # vgs command, indicating the volume group is not in use
109 command.output = ''
110 mock_command.return_value = command
111 self.volume_manager.setup('volume_group')
112 call = mock_command.call_args_list[0]
113 assert mock_command.call_args_list[0] == call(
114 ['vgs', '--noheadings', '--select', 'vg_name=volume_group']
115 )
116 call = mock_command.call_args_list[1]
117 assert mock_command.call_args_list[1] == call(
118 command=['vgremove'] + self.volume_manager.lvm_tool_options + [
119 '--force', 'volume_group'
120 ], raise_on_error=False
121 )
122 call = mock_command.call_args_list[2]
123 assert mock_command.call_args_list[2] == call(
124 ['pvcreate'] + self.volume_manager.lvm_tool_options + [
125 '/dev/storage'
126 ]
127 )
128 call = mock_command.call_args_list[3]
129 assert mock_command.call_args_list[3] == call(
130 ['vgcreate'] + self.volume_manager.lvm_tool_options + [
131 'volume_group', '/dev/storage'
132 ]
133 )
134 assert self.volume_manager.volume_group == 'volume_group'
135 self.volume_manager.volume_group = None
136
137 @patch('kiwi.volume_manager.lvm.Command.run')
138 @patch('kiwi.volume_manager.base.Temporary')
139 def test_setup_volume_group_host_conflict(
140 self, mock_Temporary, mock_command
141 ):
142 command = Mock()
143 command.output = 'some_data_about_volume_group'
144 mock_command.return_value = command
145 with raises(KiwiVolumeGroupConflict):
146 self.volume_manager.setup('volume_group')
147
148 @patch('os.path.exists')
149 @patch('kiwi.path.Path.create')
150 @patch('kiwi.volume_manager.base.SystemSize')
151 @patch('kiwi.volume_manager.lvm.Command.run')
152 @patch('kiwi.volume_manager.lvm.FileSystem.new')
153 @patch('kiwi.volume_manager.lvm.MappedDevice')
154 @patch('kiwi.volume_manager.lvm.MountManager')
155 @patch('kiwi.volume_manager.base.VolumeManagerBase.apply_attributes_on_volume')
156 def test_create_volumes(
157 self, mock_attrs, mock_mount, mock_mapped_device, mock_fs,
158 mock_command, mock_size, mock_create, mock_os_exists
159 ):
160 mock_os_exists_return_list = [True, True, False, False, False]
161
162 def mock_os_exists_return(path):
163 return mock_os_exists_return_list.pop()
164
165 mock_os_exists.side_effect = mock_os_exists_return
166 filesystem = Mock()
167 mock_fs.return_value.__enter__.return_value = filesystem
168 self.volume_manager.mountpoint = 'tmpdir'
169 mock_mapped_device.return_value = 'mapped_device'
170 size = Mock()
171 size.customize = Mock(
172 return_value=42
173 )
174 mock_size.return_value = size
175 self.volume_manager.volume_group = 'volume_group'
176 self.volume_manager.create_volumes('ext3')
177 myvol_size = 500
178 etc_size = 200 + 42 + Defaults.get_min_volume_mbytes('ext3')
179 root_size = 100 + 42 + Defaults.get_min_volume_mbytes('ext3')
180
181 assert mock_attrs.call_args_list == [
182 call(
183 'root_dir', volume_type(
184 name='LVSwap', parent='', size='size:100', realpath='swap',
185 mountpoint=None, fullsize=False, label='SWAP',
186 attributes=[], is_root_volume=False
187 )
188 ),
189 call(
190 'root_dir', volume_type(
191 name='LVRoot', parent='', size='freespace:100', realpath='/',
192 mountpoint=None, fullsize=False, label=None,
193 attributes=[], is_root_volume=True
194 )
195 ),
196 call(
197 'root_dir', volume_type(
198 name='myvol', parent='', size='size:500', realpath='/data',
199 mountpoint='LVdata', fullsize=False, label=None,
200 attributes=[], is_root_volume=False
201 )
202 ),
203 call(
204 'root_dir', volume_type(
205 name='LVetc', parent='', size='freespace:200', realpath='/etc',
206 mountpoint='/etc', fullsize=False, label='etc',
207 attributes=[], is_root_volume=False
208 )
209 )
210 ]
211 assert mock_mount.call_args_list == [
212 call(device='/dev/volume_group/LVRoot', mountpoint='tmpdir'),
213 call(device='/dev/volume_group/myvol', mountpoint='tmpdir//data'),
214 call(device='/dev/volume_group/LVetc', mountpoint='tmpdir//etc'),
215 call(device='/dev/volume_group/LVhome', mountpoint='tmpdir//home')
216 ]
217 assert mock_command.call_args_list == [
218 call(
219 ['lvcreate'] + self.volume_manager.lvm_tool_options + [
220 '-Zn', '-L', '100', '-n', 'LVSwap', 'volume_group'
221 ]
222 ),
223 call(
224 ['vgscan'] + self.volume_manager.lvm_tool_options + [
225 '--mknodes'
226 ]
227 ),
228 call(
229 ['lvcreate'] + self.volume_manager.lvm_tool_options + [
230 '-Zn', '-L', format(root_size), '-n', 'LVRoot',
231 'volume_group'
232 ]
233 ),
234 call(
235 ['vgscan'] + self.volume_manager.lvm_tool_options + [
236 '--mknodes'
237 ]
238 ),
239 call(
240 ['lvcreate'] + self.volume_manager.lvm_tool_options + [
241 '-Zn', '-L', format(myvol_size), '-n', 'myvol',
242 'volume_group'
243 ]
244 ),
245 call(
246 ['vgscan'] + self.volume_manager.lvm_tool_options + [
247 '--mknodes'
248 ]
249 ),
250 call(
251 ['lvcreate'] + self.volume_manager.lvm_tool_options + [
252 '-Zn', '-L', format(etc_size), '-n', 'LVetc',
253 'volume_group'
254 ]
255 ),
256 call(
257 ['vgscan'] + self.volume_manager.lvm_tool_options + [
258 '--mknodes'
259 ]
260 ),
261 call(
262 ['lvcreate'] + self.volume_manager.lvm_tool_options + [
263 '-Zn', '-l', '+100%FREE', '-n', 'LVhome', 'volume_group'
264 ]
265 ),
266 call(
267 ['vgscan'] + self.volume_manager.lvm_tool_options + [
268 '--mknodes'
269 ]
270 )
271 ]
272 assert mock_fs.call_args_list == [
273 call(
274 custom_args={'create_options': [], 'mount_options': ['a,b,c']},
275 device_provider='mapped_device',
276 name='ext3'
277 ),
278 call(
279 custom_args={'create_options': [], 'mount_options': ['a,b,c']},
280 device_provider='mapped_device',
281 name='ext3'
282 ),
283 call(
284 custom_args={'create_options': [], 'mount_options': ['a,b,c']},
285 device_provider='mapped_device',
286 name='ext3'
287 ),
288 call(
289 custom_args={'create_options': [], 'mount_options': ['a,b,c']},
290 device_provider='mapped_device',
291 name='ext3'
292 )
293 ]
294 assert filesystem.create_on_device.call_args_list == [
295 call(label='ROOT'),
296 call(label=None),
297 call(label='etc'),
298 call(label=None)
299 ]
300
301 assert mock_create.call_args_list == [
302 call('root_dir/etc'), call('root_dir/data'), call('root_dir/home')
303 ]
304 self.volume_manager.volume_group = None
305
306 @patch('kiwi.volume_manager.lvm.Path')
307 def test_mount_volumes(self, mock_path):
308 volume_mount = Mock()
309 volume_mount.mountpoint = 'volume_mount_point'
310 self.volume_manager.mount_list = [volume_mount]
311 self.volume_manager.mount_volumes()
312 mock_path.create.assert_called_once_with(volume_mount.mountpoint)
313 volume_mount.mount.assert_called_once_with(options=['a,b,c'])
314
315 def test_umount_volumes(self):
316 volume_mount = Mock()
317 volume_mount.mountpoint = 'volume_mount_point'
318 self.volume_manager.mount_list = [volume_mount]
319 self.volume_manager.umount_volumes()
320 volume_mount.umount.assert_called_once_with()
321
322 def test_get_volumes(self):
323 volume_mount = Mock()
324 volume_mount.mountpoint = \
325 '/var/tmp/kiwi_volumes.f2qx_d3y/boot/grub2/x86_64-efi'
326 volume_mount.device = '/dev/mapper/vg1-LVRoot'
327 self.volume_manager.mount_list = [volume_mount]
328 assert self.volume_manager.get_volumes() == {
329 'boot/grub2/x86_64-efi': {
330 'volume_options': 'a,b,c',
331 'volume_device': '/dev/mapper/vg1-LVRoot'
332 }
333 }
334
335 def test_get_fstab(self):
336 self.volume_manager.volume_group = 'vgroup'
337 self.volume_manager._add_to_volume_map('device')
338 volume_mount = Mock()
339 volume_mount.mountpoint = '/var/tmp/kiwi_volumes.XXX/var/tmp'
340 volume_mount.device = self.volume_manager.volume_map['device']
341 self.volume_manager.mount_list = [volume_mount]
342 assert self.volume_manager.get_fstab(None, 'ext3') == [
343 '/dev/vgroup/device /var/tmp ext3 a,b,c 0 0'
344 ]
345 self.volumes.append(
346 volume_type(
347 name='device',
348 parent='',
349 size='freespace:100',
350 realpath='/var/tmp',
351 mountpoint=volume_mount.mountpoint,
352 fullsize=False,
353 label=None,
354 attributes=['enable-for-filesystem-check'],
355 is_root_volume=False
356 )
357 )
358 assert self.volume_manager.get_fstab(None, 'ext3') == [
359 '/dev/vgroup/device /var/tmp ext3 a,b,c 0 2'
360 ]
361
362 @patch('kiwi.volume_manager.lvm.Command.run')
363 @patch('kiwi.volume_manager.lvm.VolumeManagerLVM.umount_volumes')
364 @patch('os.path.exists')
365 def test_context_manager_exit(
366 self, mock_os_path_exists, mock_VolumeManagerLVM_umount_volumes,
367 mock_Command_run
368 ):
369 mock_os_path_exists.return_value = True
370 mock_Command_run.side_effect = KiwiCommandError('error')
371 with self._caplog.at_level(logging.ERROR):
372 with VolumeManagerLVM(
373 self.device_map, 'root_dir', self.volumes
374 ) as volume_manager:
375 volume_manager.volume_group = 'volume_group'
376
377 mock_VolumeManagerLVM_umount_volumes.assert_called_once_with()
378 mock_Command_run.assert_called_once_with(
379 ['vgchange'] + self.volume_manager.lvm_tool_options + [
380 '-an', 'volume_group'
381 ]
382 )