Debian Codegraph @

test/unit/mount_manager_test.py

raw
1 import logging
2 from typing import NoReturn
3 from pytest import (
4 fixture, raises
5 )
6 from unittest.mock import (
7 patch, call, Mock
8 )
9 from kiwi.exceptions import KiwiCommandError, KiwiUmountBusyError
10 from kiwi.mount_manager import MountManager
11
12
13 class TestMountManager:
14 @fixture(autouse=True)
15 def inject_fixtures(self, caplog):
16 self._caplog = caplog
17
18 @patch('kiwi.mount_manager.Path.create')
19 def setup(self, mock_path_create):
20 self.mount_manager = MountManager(
21 '/dev/some-device', '/some/mountpoint'
22 )
23 mock_path_create.assert_called_once_with('/some/mountpoint')
24
25 @patch('kiwi.mount_manager.Path.create')
26 def setup_method(self, cls, mock_path_create):
27 self.setup()
28
29 def test_get_attributes(self):
30 assert self.mount_manager.get_attributes() == {}
31
32 @patch('kiwi.mount_manager.Temporary')
33 def test_setup_empty_mountpoint(self, mock_Temporary):
34 mock_Temporary.return_value.new_dir.return_value.name = 'tmpdir'
35 mount_manager = MountManager('/dev/some-device')
36 assert mount_manager.mountpoint == 'tmpdir'
37
38 @patch('kiwi.mount_manager.Command.run')
39 @patch('kiwi.mount_manager.MountManager.is_mounted')
40 def test_bind_mount(self, mock_mounted, mock_command):
41 mock_mounted.return_value = False
42 self.mount_manager.bind_mount()
43 mock_command.assert_called_once_with(
44 ['mount', '-n', '--bind', '/dev/some-device', '/some/mountpoint']
45 )
46
47 @patch('kiwi.mount_manager.Path.create')
48 @patch('kiwi.mount_manager.Command.run')
49 @patch('kiwi.mount_manager.MountManager.is_mounted')
50 def test_overlay_mount(self, mock_mounted, mock_command, mock_path_create):
51 mock_mounted.return_value = False
52 self.mount_manager.overlay_mount('lower_path')
53 assert mock_path_create.call_args_list == [
54 call('/some/mountpoint_cow'), call('/some/mountpoint_work')
55 ]
56 mock_command.assert_called_once_with(
57 [
58 'mount', '-t', 'overlay', 'overlay', '/some/mountpoint',
59 '-o', 'lowerdir=lower_path,'
60 'upperdir=/some/mountpoint_cow,workdir=/some/mountpoint_work'
61 ]
62 )
63
64 @patch('kiwi.mount_manager.Command.run')
65 @patch('kiwi.mount_manager.MountManager.is_mounted')
66 def test_tmpfs_mount(self, mock_mounted, mock_command):
67 mock_mounted.return_value = False
68 self.mount_manager.tmpfs_mount()
69 mock_command.assert_called_once_with(
70 ['mount', '-t', 'tmpfs', 'tmpfs', '/some/mountpoint']
71 )
72
73 @patch('kiwi.mount_manager.Command.run')
74 @patch('kiwi.mount_manager.MountManager.is_mounted')
75 def test_mount(self, mock_mounted, mock_command):
76 mock_mounted.return_value = False
77 self.mount_manager.mount(['options'])
78 mock_command.assert_called_once_with(
79 ['mount', '-o', 'options', '/dev/some-device', '/some/mountpoint']
80 )
81
82 @patch('kiwi.mount_manager.Command.run')
83 @patch('kiwi.mount_manager.MountManager.is_mounted')
84 def test_context_manager(self, mock_mounted, mock_command):
85 mock_mounted.return_value = True
86 with self.mount_manager:
87 pass
88 mock_command.assert_called_once_with(
89 ['umount', '/some/mountpoint']
90 )
91
92 @patch('kiwi.mount_manager.Command.run')
93 @patch('kiwi.mount_manager.MountManager.is_mounted')
94 def test_umount_lazy(self, mock_mounted, mock_command):
95 mock_mounted.return_value = True
96 self.mount_manager.umount_lazy()
97 mock_command.assert_called_once_with(
98 ['umount', '-l', '/some/mountpoint']
99 )
100
101 @patch('kiwi.mount_manager.Command.run')
102 @patch('kiwi.mount_manager.MountManager.is_mounted')
103 @patch('time.sleep')
104 def test_umount_with_errors_but_lazy(
105 self, mock_sleep, mock_mounted, mock_command
106 ):
107 command_errors = [
108 True, False, False, False, False, False
109 ]
110
111 def _cmd_err(args) -> NoReturn:
112 if not command_errors.pop():
113 raise KiwiCommandError('error')
114
115 mock_command.side_effect = _cmd_err
116 mock_mounted.return_value = True
117 with self._caplog.at_level(logging.WARNING):
118 assert self.mount_manager.umount() is True
119 assert mock_command.call_args_list == [
120 call(['umount', '/some/mountpoint']), # 1
121 call(['umount', '/some/mountpoint']), # 2
122 call(['umount', '/some/mountpoint']), # 3
123 call(['umount', '/some/mountpoint']), # 4
124 call(['umount', '/some/mountpoint']), # 5
125 call(['umount', '--lazy', '/some/mountpoint']), # lazy
126 ]
127
128 @patch('kiwi.mount_manager.Command.run')
129 @patch('kiwi.mount_manager.MountManager.is_mounted')
130 @patch('time.sleep')
131 def test_umount_with_errors(
132 self, mock_sleep, mock_mounted, mock_command
133 ):
134 def _cmd_err(args) -> NoReturn:
135 raise KiwiCommandError('error')
136
137 mock_command.side_effect = _cmd_err
138 mock_mounted.return_value = True
139 with self._caplog.at_level(logging.WARNING):
140 assert self.mount_manager.umount(raise_on_busy=False) is False
141 assert mock_command.call_args_list == [
142 call(['umount', '/some/mountpoint']), # 1
143 call(['umount', '/some/mountpoint']), # 2
144 call(['umount', '/some/mountpoint']), # 3
145 call(['umount', '/some/mountpoint']), # 4
146 call(['umount', '/some/mountpoint']), # 5
147 call(['umount', '--lazy', '/some/mountpoint']), # lazy
148 ]
149
150 @patch('kiwi.mount_manager.Command.run')
151 @patch('kiwi.mount_manager.MountManager.is_mounted')
152 @patch('time.sleep')
153 @patch('kiwi.mount_manager.Path.which')
154 def test_umount_with_errors_raises_no_lsof_present(
155 self, mock_Path_which, mock_sleep, mock_mounted, mock_command
156 ):
157 def command_call(args):
158 if 'umount' in args:
159 raise KiwiCommandError('error')
160
161 mock_Path_which.return_value = None
162 mock_command.side_effect = command_call
163 mock_mounted.return_value = True
164 with raises(KiwiUmountBusyError):
165 self.mount_manager.umount()
166
167 @patch('kiwi.mount_manager.Command.run')
168 @patch('kiwi.mount_manager.MountManager.is_mounted')
169 @patch('time.sleep')
170 @patch('kiwi.mount_manager.Path.which')
171 def test_umount_with_errors_raises_lsof_present(
172 self, mock_Path_which, mock_sleep, mock_mounted, mock_command
173 ):
174 def command_call(args, raise_on_error=None):
175 if 'umount' in args:
176 raise KiwiCommandError('error')
177 else:
178 call_return = Mock()
179 call_return.output = 'HEADLINE\ndata'
180 return call_return
181
182 mock_Path_which.return_value = 'lsof'
183 mock_command.side_effect = command_call
184 mock_mounted.return_value = True
185 with raises(KiwiUmountBusyError) as issue:
186 self.mount_manager.umount()
187 assert 'HEADLINE' in issue.value.message
188
189 @patch('kiwi.mount_manager.Command.run')
190 @patch('kiwi.mount_manager.MountManager.is_mounted')
191 def test_umount_success(self, mock_mounted, mock_command):
192 mock_mounted.return_value = True
193 assert self.mount_manager.umount() is True
194 mock_command.assert_called_once_with(
195 ['umount', '/some/mountpoint']
196 )
197
198 @patch('kiwi.mount_manager.Command.run')
199 def test_is_mounted_true(self, mock_command):
200 command = Mock()
201 command.returncode = 0
202 mock_command.return_value = command
203 assert self.mount_manager.is_mounted() is True
204 mock_command.assert_called_once_with(
205 command=['mountpoint', '-q', '/some/mountpoint'],
206 raise_on_error=False
207 )
208
209 @patch('kiwi.mount_manager.Command.run')
210 def test_is_mounted_false(self, mock_command):
211 command = Mock()
212 command.returncode = 1
213 mock_command.return_value = command
214 assert self.mount_manager.is_mounted() is False
215 mock_command.assert_called_once_with(
216 command=['mountpoint', '-q', '/some/mountpoint'],
217 raise_on_error=False
218 )