File: apt_test.py

package info (click to toggle)
kiwi 10.2.41-1
  • links: PTS, VCS
  • area: main
  • in suites:
  • size: 7,592 kB
  • sloc: python: 69,585; sh: 4,230; xml: 3,386; ansic: 391; makefile: 360
file content (370 lines) | stat: -rw-r--r-- 12,633 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
import logging
import io
from unittest.mock import (
    patch, call, Mock, MagicMock
)
from pytest import (
    raises, fixture
)

from kiwi.package_manager.apt import PackageManagerApt

import kiwi.defaults as defaults

from kiwi.exceptions import (
    KiwiDebianBootstrapError,
    KiwiRequestError,
    KiwiFileNotFound
)


class TestPackageManagerApt:
    @fixture(autouse=True)
    def inject_fixtures(self, caplog):
        self._caplog = caplog

    def setup(self):
        repository = Mock()
        repository.root_dir = 'root-dir'
        repository.signing_keys = ['key-file.asc']
        repository.keyring = 'trusted.gpg'
        repository.unauthenticated = 'false'
        repository.components = ['main', 'restricted']
        self.env = {'key': 'val'}

        repository.runtime_config = Mock(
            return_value={
                'apt_get_args': ['-c', 'apt.conf', '-y'],
                'command_env': self.env,
                'distribution': 'xenial',
                'distribution_path': 'xenial_path'
            }
        )
        self.manager = PackageManagerApt(repository)

    def setup_method(self, cls):
        self.setup()

    def test_request_package(self):
        self.manager.request_package('name')
        assert self.manager.package_requests == ['name']

    def test_request_collection(self):
        self.manager.request_collection('name')
        with self._caplog.at_level(logging.WARNING):
            assert self.manager.collection_requests == []

    def test_request_product(self):
        self.manager.request_product('name')
        with self._caplog.at_level(logging.WARNING):
            assert self.manager.product_requests == []

    def test_request_package_exclusion(self):
        self.manager.request_package_exclusion('name')
        with self._caplog.at_level(logging.WARNING):
            assert self.manager.exclude_requests == []

    def test_setup_repository_modules(self):
        self.manager.setup_repository_modules({})

    @patch('pathlib.Path')
    @patch('kiwi.command.Command.run')
    @patch.object(PackageManagerApt, 'process_install_requests')
    @patch('os.path.isfile')
    def test_process_install_requests_bootstrap_prebuild_root(
        self, mock_os_path_isfile, mock_process_install_requests,
        mock_Command_run, mock_pathlib_Path
    ):
        mock_os_path_isfile.return_value = True
        self.manager.process_install_requests_bootstrap(
            bootstrap_package='bootstrap-package'
        )
        assert mock_Command_run.call_args_list == [
            call(['apt-get', '-c', 'apt.conf', '-y', 'update'], self.env),
            call(
                [
                    'apt-get', '-c', 'apt.conf', '-y',
                    'install', 'bootstrap-package'
                ], self.env
            ),
            call(
                [
                    'tar', '-C', 'root-dir', '-xf',
                    '/var/lib/bootstrap/bootstrap-package.{0}.tar.xz'.format(
                        defaults.PLATFORM_MACHINE
                    )
                ]
            )
        ]
        mock_process_install_requests.assert_called_once_with()
        mock_os_path_isfile.return_value = False
        with raises(KiwiFileNotFound):
            self.manager.process_install_requests_bootstrap(
                bootstrap_package='bootstrap-package'
            )

    @patch('pathlib.Path')
    @patch('kiwi.command.Command.call')
    @patch('kiwi.command.Command.run')
    def test_process_install_requests_bootstrap_failed(
        self, mock_Command_run, mock_Command_call, mock_pathlib_Path
    ):
        self.manager.request_package('apt')
        mock_Command_call.side_effect = Exception
        with patch('builtins.open', create=True):
            with raises(KiwiDebianBootstrapError):
                self.manager.process_install_requests_bootstrap()

    @patch('pathlib.Path')
    @patch('kiwi.command.Command.run')
    @patch('kiwi.command.Command.call')
    @patch('kiwi.package_manager.apt.Temporary.new_file')
    @patch('kiwi.package_manager.apt.Temporary.new_dir')
    @patch('os.path.exists')
    def test_process_install_requests_bootstrap(
        self, mock_os_path_exists, mock_Temporary_new_dir,
        mock_Temporary_new_file, mock_Command_call, mock_Command_run,
        mock_pathlib_Path
    ):
        mock_os_path_exists.return_value = True
        mock_Temporary_new_dir.return_value.name = 'tempdir'
        mock_Temporary_new_file.return_value.name = 'temporary'
        self.manager.request_package('vim')
        call_result = Mock()
        call_result.process.communicate.return_value = ('stdout', 'stderr')
        mock_Command_call.return_value = call_result
        with patch('builtins.open', create=True) as mock_open:
            mock_open.return_value = MagicMock(spec=io.IOBase)
            file_handle = mock_open.return_value.__enter__.return_value
            file_handle.__iter__.return_value = ['base-passwd\n', 'usrmerge', 'vim\n']
            self.manager.process_install_requests_bootstrap()
        new_env = self.env | {
            'PATH': '$PATH:/usr/bin:/bin:/usr/sbin:/sbin',
            'DPKG_MAINTSCRIPT_NAME': 'true',
            'DPKG_MAINTSCRIPT_PACKAGE': 'libc6'
        }
        assert mock_Command_run.call_args_list == [
            call(
                ['apt-get', '-c', 'apt.conf', '-y', 'update'], self.env
            ),
            call(
                [
                    'apt-get', '-c', 'apt.conf', '-y', 'install',
                    '-oDebug::pkgDPkgPm=1',
                    '-oDPkg::Pre-Install-Pkgs::=cat >temporary',
                    '?essential',
                    'vim',
                    'apt'
                ], self.env
            ),
            call(
                [
                    'bash', '-c',
                    'dpkg-deb --fsys-tarfile base-passwd | tar -C root-dir -x'
                ], self.env
            ),
            call(
                [
                    'bash', '-c',
                    'dpkg-deb --fsys-tarfile usrmerge | tar -C root-dir -x'
                ], self.env
            ),
            call(
                [
                    'bash', '-c',
                    'dpkg-deb --fsys-tarfile vim | tar -C root-dir -x'
                ], self.env
            ),
            call(
                [
                    'dpkg', '-e', 'base-passwd', 'tempdir/base-passwd'
                ]
            ),
            call(
                [
                    'chmod', '755', 'tempdir/base-passwd/preinst'
                ]
            ),
            call(
                [
                    'chroot', 'root-dir',
                    'tempdir/base-passwd/preinst', 'install'
                ], new_env
            ),
            call(
                [
                    'chmod', '755', 'tempdir/base-passwd/postinst'
                ]
            ),
            call(
                [
                    'chroot', 'root-dir',
                    'tempdir/base-passwd/postinst', 'configure'
                ], new_env
            ),
            call(
                ['dpkg', '-e', 'base-passwd', 'tempdir/base-passwd']
            ),
            call(
                [
                    'chmod', '755', 'tempdir/base-passwd/preinst'
                ]
            ),
            call(
                [
                    'chroot', 'root-dir',
                    'tempdir/base-passwd/preinst', 'install'
                ], new_env
            ),
            call(
                [
                    'chmod', '755', 'tempdir/base-passwd/postinst'
                ]
            ),
            call(
                [
                    'chroot', 'root-dir',
                    'tempdir/base-passwd/postinst', 'configure'
                ], new_env
            ),
            call(
                ['dpkg', '-e', 'vim', 'tempdir/vim']
            ),
            call(
                [
                    'chmod', '755', 'tempdir/vim/preinst'
                ]
            ),
            call(
                [
                    'chroot', 'root-dir',
                    'tempdir/vim/preinst', 'install'
                ], new_env
            ),
            call(
                [
                    'chmod', '755', 'tempdir/vim/postinst'
                ]
            ),
            call(
                [
                    'chroot', 'root-dir',
                    'tempdir/vim/postinst', 'configure'
                ], new_env
            )
        ]

    @patch('kiwi.command.Command.call')
    @patch('kiwi.command.Command.run')
    def test_process_install_requests(self, mock_run, mock_call):
        self.manager.request_package('vim')
        self.manager.bootstrap_packages = ['?essential', 'some']
        self.manager.process_install_requests()
        mock_call.assert_called_once_with(
            [
                'chroot', 'root-dir', 'apt-get',
                '-c', 'apt.conf', '-y', 'install',
                '?essential', 'some', 'vim'
            ], self.env
        )

    @patch('kiwi.command.Command.call')
    @patch('kiwi.command.Command.run')
    def test_process_delete_requests(self, mock_run, mock_call):
        self.manager.request_package('vim')
        self.manager.process_delete_requests()
        mock_call.assert_called_once_with(
            [
                'chroot', 'root-dir', 'apt-get', '-c', 'apt.conf', '-y',
                '--auto-remove', 'remove', 'vim'
            ], self.env
        )

    @patch('kiwi.command.Command.call')
    @patch('kiwi.command.Command.run')
    @patch('kiwi.package_manager.apt.Path.wipe')
    @patch('glob.iglob')
    def test_process_delete_requests_force(
        self, mock_iglob, mock_Path_wipe, mock_run, mock_call
    ):
        mock_iglob.return_value = ['glob-result']
        self.manager.request_package('vim')
        self.manager.process_delete_requests(True)
        assert mock_run.call_args_list == [
            call(
                [
                    'chroot', 'root-dir', 'dpkg', '-l', 'vim'
                ]
            ),
            call(
                [
                    'cp', 'root-dir/usr/sbin/ldconfig',
                    'root-dir/usr/sbin/ldconfig.orig'
                ]
            ),
            call(
                [
                    'cp', 'root-dir/usr/bin/true',
                    'root-dir/usr/sbin/ldconfig'
                ]
            )
        ]
        mock_call.assert_called_once_with(
            [
                'chroot', 'root-dir', 'dpkg',
                '--remove', '--force-remove-reinstreq',
                '--force-remove-essential', '--force-depends', 'vim'
            ],
            self.env
        )
        mock_iglob.call_args_list == [
            call('root-dir/var/lib/dpkg/info/vim*.pre*'),
            call('root-dir/var/lib/dpkg/info/vim*.post*')
        ]
        mock_Path_wipe.call_args_list == [
            call('glob-result'), call('glob-result')
        ]

    @patch('kiwi.command.Command.run')
    def test_post_process_delete_requests(self, mock_run):
        self.manager.post_process_delete_requests()
        assert mock_run.call_args_list == [
            call(
                [
                    'mv', 'root-dir/usr/sbin/ldconfig.orig',
                    'root-dir/usr/sbin/ldconfig'
                ]
            )
        ]

    @patch('kiwi.command.Command.run')
    def test_process_delete_requests_package_missing(self, mock_run):
        mock_run.side_effect = Exception
        self.manager.request_package('vim')
        with raises(KiwiRequestError):
            self.manager.process_delete_requests()

    @patch('kiwi.command.Command.call')
    def test_update(self, mock_call):
        self.manager.update()
        mock_call.assert_called_once_with([
            'chroot', 'root-dir', 'apt-get',
            '-c', 'apt.conf', '-y', 'upgrade'],
            self.env
        )

    def test_process_only_required(self):
        self.manager.process_only_required()
        assert self.manager.custom_args == ['--no-install-recommends']

    def test_process_plus_recommended(self):
        self.manager.process_only_required()
        assert self.manager.custom_args == ['--no-install-recommends']
        self.manager.process_plus_recommended()
        assert '--no-install-recommends' not in self.manager.custom_args

    def test_match_package_installed(self):
        assert self.manager.match_package_installed('foo', 'Unpacking foo')

    def test_match_package_deleted(self):
        assert self.manager.match_package_deleted('foo', 'Removing foo')