Debian Codegraph @

test/unit/system/mount_test.py

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