Debian Codegraph @

test/unit/package_manager/apt_test.py

raw
1 import logging
2 import io
3 from unittest.mock import (
4 patch, call, Mock, MagicMock
5 )
6 from pytest import (
7 raises, fixture
8 )
9
10 from kiwi.package_manager.apt import PackageManagerApt
11
12 import kiwi.defaults as defaults
13
14 from kiwi.exceptions import (
15 KiwiDebianBootstrapError,
16 KiwiRequestError,
17 KiwiFileNotFound
18 )
19
20
21 class TestPackageManagerApt:
22 @fixture(autouse=True)
23 def inject_fixtures(self, caplog):
24 self._caplog = caplog
25
26 def setup(self):
27 repository = Mock()
28 repository.root_dir = 'root-dir'
29 repository.signing_keys = ['key-file.asc']
30 repository.keyring = 'trusted.gpg'
31 repository.unauthenticated = 'false'
32 repository.components = ['main', 'restricted']
33 self.env = {'key': 'val'}
34
35 repository.runtime_config = Mock(
36 return_value={
37 'apt_get_args': ['-c', 'apt.conf', '-y'],
38 'command_env': self.env,
39 'distribution': 'xenial',
40 'distribution_path': 'xenial_path'
41 }
42 )
43 self.manager = PackageManagerApt(repository)
44
45 def setup_method(self, cls):
46 self.setup()
47
48 def test_request_package(self):
49 self.manager.request_package('name')
50 assert self.manager.package_requests == ['name']
51
52 def test_request_collection(self):
53 self.manager.request_collection('name')
54 with self._caplog.at_level(logging.WARNING):
55 assert self.manager.collection_requests == []
56
57 def test_request_product(self):
58 self.manager.request_product('name')
59 with self._caplog.at_level(logging.WARNING):
60 assert self.manager.product_requests == []
61
62 def test_request_package_exclusion(self):
63 self.manager.request_package_exclusion('name')
64 with self._caplog.at_level(logging.WARNING):
65 assert self.manager.exclude_requests == []
66
67 def test_setup_repository_modules(self):
68 self.manager.setup_repository_modules({})
69
70 @patch('pathlib.Path')
71 @patch('kiwi.command.Command.run')
72 @patch.object(PackageManagerApt, 'process_install_requests')
73 @patch('os.path.isfile')
74 def test_process_install_requests_bootstrap_prebuild_root(
75 self, mock_os_path_isfile, mock_process_install_requests,
76 mock_Command_run, mock_pathlib_Path
77 ):
78 mock_os_path_isfile.return_value = True
79 self.manager.process_install_requests_bootstrap(
80 bootstrap_package='bootstrap-package'
81 )
82 assert mock_Command_run.call_args_list == [
83 call(['apt-get', '-c', 'apt.conf', '-y', 'update'], self.env),
84 call(
85 [
86 'apt-get', '-c', 'apt.conf', '-y',
87 'install', 'bootstrap-package'
88 ], self.env
89 ),
90 call(
91 [
92 'tar', '-C', 'root-dir', '-xf',
93 '/var/lib/bootstrap/bootstrap-package.{0}.tar.xz'.format(
94 defaults.PLATFORM_MACHINE
95 )
96 ]
97 )
98 ]
99 mock_process_install_requests.assert_called_once_with()
100 mock_os_path_isfile.return_value = False
101 with raises(KiwiFileNotFound):
102 self.manager.process_install_requests_bootstrap(
103 bootstrap_package='bootstrap-package'
104 )
105
106 @patch('pathlib.Path')
107 @patch('kiwi.command.Command.call')
108 @patch('kiwi.command.Command.run')
109 def test_process_install_requests_bootstrap_failed(
110 self, mock_Command_run, mock_Command_call, mock_pathlib_Path
111 ):
112 self.manager.request_package('apt')
113 mock_Command_call.side_effect = Exception
114 with patch('builtins.open', create=True):
115 with raises(KiwiDebianBootstrapError):
116 self.manager.process_install_requests_bootstrap()
117
118 @patch('pathlib.Path')
119 @patch('kiwi.command.Command.run')
120 @patch('kiwi.command.Command.call')
121 @patch('kiwi.package_manager.apt.Temporary.new_file')
122 @patch('kiwi.package_manager.apt.Temporary.new_dir')
123 @patch('os.path.exists')
124 def test_process_install_requests_bootstrap(
125 self, mock_os_path_exists, mock_Temporary_new_dir,
126 mock_Temporary_new_file, mock_Command_call, mock_Command_run,
127 mock_pathlib_Path
128 ):
129 def command_run(command, env=None):
130 # mock a postinst error for base-passwd to test the retry logic
131 if command == [
132 'chroot', 'root-dir',
133 'tempdir/base-passwd/postinst', 'configure'
134 ]:
135 raise Exception
136 return MagicMock(stdout='stdout', stderr='stderr')
137
138 mock_Command_run.side_effect = command_run
139 mock_os_path_exists.return_value = True
140 mock_Temporary_new_dir.return_value.name = 'tempdir'
141 mock_Temporary_new_file.return_value.name = 'temporary'
142 self.manager.request_package('vim')
143 call_result = Mock()
144 call_result.process.communicate.return_value = ('stdout', 'stderr')
145 mock_Command_call.return_value = call_result
146 with patch('builtins.open', create=True) as mock_open:
147 mock_open.return_value = MagicMock(spec=io.IOBase)
148 file_handle = mock_open.return_value.__enter__.return_value
149 file_handle.__iter__.return_value = ['base-passwd\n', 'usrmerge', 'vim\n']
150 self.manager.process_install_requests_bootstrap()
151 new_env = self.env | {
152 'PATH': '$PATH:/usr/bin:/bin:/usr/sbin:/sbin',
153 'DPKG_MAINTSCRIPT_NAME': 'true',
154 'DPKG_MAINTSCRIPT_PACKAGE': 'libc6'
155 }
156 assert mock_Command_run.call_args_list == [
157 call(
158 ['apt-get', '-c', 'apt.conf', '-y', 'update'], self.env
159 ),
160 call(
161 [
162 'apt-get', '-c', 'apt.conf', '-y', 'install',
163 '-oDebug::pkgDPkgPm=1',
164 '-oDPkg::Pre-Install-Pkgs::=cat >temporary',
165 '?essential',
166 'vim',
167 'apt'
168 ], self.env
169 ),
170 call(
171 [
172 'bash', '-c',
173 'dpkg-deb --fsys-tarfile base-passwd | tar -C root-dir -x'
174 ], self.env
175 ),
176 call(
177 [
178 'bash', '-c',
179 'dpkg-deb --fsys-tarfile usrmerge | tar -C root-dir -x'
180 ], self.env
181 ),
182 call(
183 [
184 'bash', '-c',
185 'dpkg-deb --fsys-tarfile vim | tar -C root-dir -x'
186 ], self.env
187 ),
188 call(
189 [
190 'dpkg', '-e', 'base-passwd', 'tempdir/base-passwd'
191 ]
192 ),
193 call(
194 [
195 'chmod', '755', 'tempdir/base-passwd/preinst'
196 ]
197 ),
198 call(
199 [
200 'chroot', 'root-dir',
201 'tempdir/base-passwd/preinst', 'install'
202 ], new_env
203 ),
204 call(
205 [
206 'chmod', '755', 'tempdir/base-passwd/postinst'
207 ]
208 ),
209 call(
210 [
211 'chroot', 'root-dir',
212 'tempdir/base-passwd/postinst', 'configure'
213 ], new_env
214 ),
215 call(
216 [
217 'chroot', 'root-dir',
218 'tempdir/base-passwd/postinst', 'configure', '0'
219 ], new_env
220 ),
221 call(
222 ['dpkg', '-e', 'base-passwd', 'tempdir/base-passwd']
223 ),
224 call(
225 [
226 'chmod', '755', 'tempdir/base-passwd/preinst'
227 ]
228 ),
229 call(
230 [
231 'chroot', 'root-dir',
232 'tempdir/base-passwd/preinst', 'install'
233 ], new_env
234 ),
235 call(
236 [
237 'chmod', '755', 'tempdir/base-passwd/postinst'
238 ]
239 ),
240 call(
241 [
242 'chroot', 'root-dir',
243 'tempdir/base-passwd/postinst', 'configure'
244 ], new_env
245 ),
246 call(
247 [
248 'chroot', 'root-dir',
249 'tempdir/base-passwd/postinst', 'configure', '0'
250 ], new_env
251 ),
252 call(
253 ['dpkg', '-e', 'vim', 'tempdir/vim']
254 ),
255 call(
256 [
257 'chmod', '755', 'tempdir/vim/preinst'
258 ]
259 ),
260 call(
261 [
262 'chroot', 'root-dir',
263 'tempdir/vim/preinst', 'install'
264 ], new_env
265 ),
266 call(
267 [
268 'chmod', '755', 'tempdir/vim/postinst'
269 ]
270 ),
271 call(
272 [
273 'chroot', 'root-dir',
274 'tempdir/vim/postinst', 'configure'
275 ], new_env
276 )
277 ]
278
279 @patch('kiwi.command.Command.call')
280 @patch('kiwi.command.Command.run')
281 def test_process_install_requests(self, mock_run, mock_call):
282 self.manager.request_package('vim')
283 self.manager.bootstrap_packages = ['?essential', 'some']
284 self.manager.process_install_requests()
285 mock_call.assert_called_once_with(
286 [
287 'chroot', 'root-dir', 'apt-get',
288 '-c', 'apt.conf', '-y', 'install',
289 '?essential', 'some', 'vim'
290 ], self.env
291 )
292
293 @patch('kiwi.command.Command.call')
294 @patch('kiwi.command.Command.run')
295 def test_process_delete_requests(self, mock_run, mock_call):
296 self.manager.request_package('vim')
297 self.manager.process_delete_requests()
298 mock_call.assert_called_once_with(
299 [
300 'chroot', 'root-dir', 'apt-get', '-c', 'apt.conf', '-y',
301 '--auto-remove', 'remove', 'vim'
302 ], self.env
303 )
304
305 @patch('kiwi.command.Command.call')
306 @patch('kiwi.command.Command.run')
307 @patch('kiwi.package_manager.apt.Path.wipe')
308 @patch('glob.iglob')
309 def test_process_delete_requests_force(
310 self, mock_iglob, mock_Path_wipe, mock_run, mock_call
311 ):
312 mock_iglob.return_value = ['glob-result']
313 self.manager.request_package('vim')
314 self.manager.process_delete_requests(True)
315 assert mock_run.call_args_list == [
316 call(
317 [
318 'chroot', 'root-dir', 'dpkg', '-l', 'vim'
319 ]
320 ),
321 call(
322 [
323 'cp', 'root-dir/usr/sbin/ldconfig',
324 'root-dir/usr/sbin/ldconfig.orig'
325 ]
326 ),
327 call(
328 [
329 'cp', 'root-dir/usr/bin/true',
330 'root-dir/usr/sbin/ldconfig'
331 ]
332 )
333 ]
334 mock_call.assert_called_once_with(
335 [
336 'chroot', 'root-dir', 'dpkg',
337 '--remove', '--force-remove-reinstreq',
338 '--force-remove-essential', '--force-depends', 'vim'
339 ],
340 self.env
341 )
342 mock_iglob.call_args_list == [
343 call('root-dir/var/lib/dpkg/info/vim*.pre*'),
344 call('root-dir/var/lib/dpkg/info/vim*.post*')
345 ]
346 mock_Path_wipe.call_args_list == [
347 call('glob-result'), call('glob-result')
348 ]
349
350 @patch('kiwi.command.Command.run')
351 def test_post_process_delete_requests(self, mock_run):
352 self.manager.post_process_delete_requests()
353 assert mock_run.call_args_list == [
354 call(
355 [
356 'mv', 'root-dir/usr/sbin/ldconfig.orig',
357 'root-dir/usr/sbin/ldconfig'
358 ]
359 )
360 ]
361
362 @patch('kiwi.command.Command.run')
363 def test_process_delete_requests_package_missing(self, mock_run):
364 mock_run.side_effect = Exception
365 self.manager.request_package('vim')
366 with raises(KiwiRequestError):
367 self.manager.process_delete_requests()
368
369 @patch('kiwi.command.Command.call')
370 def test_update(self, mock_call):
371 self.manager.update()
372 mock_call.assert_called_once_with([
373 'chroot', 'root-dir', 'apt-get',
374 '-c', 'apt.conf', '-y', 'upgrade'],
375 self.env
376 )
377
378 def test_process_only_required(self):
379 self.manager.process_only_required()
380 assert self.manager.custom_args == ['--no-install-recommends']
381
382 def test_process_plus_recommended(self):
383 self.manager.process_only_required()
384 assert self.manager.custom_args == ['--no-install-recommends']
385 self.manager.process_plus_recommended()
386 assert '--no-install-recommends' not in self.manager.custom_args
387
388 def test_match_package_installed(self):
389 assert self.manager.match_package_installed('foo', 'Unpacking foo')
390
391 def test_match_package_deleted(self):
392 assert self.manager.match_package_deleted('foo', 'Removing foo')