Debian Codegraph @

test/unit/package_manager/dnf5_test.py

raw
1 from unittest.mock import (
2 patch, call
3 )
4 from pytest import raises
5 import unittest.mock as mock
6
7 from kiwi.package_manager.dnf5 import PackageManagerDnf5
8
9 from kiwi.exceptions import KiwiRequestError
10
11
12 class TestPackageManagerDnf5:
13 def setup(self):
14 repository = mock.Mock()
15 repository.root_dir = '/root-dir'
16
17 repository.runtime_config = mock.Mock(
18 return_value={
19 'dnf_args': ['--config', '/root-dir/dnf.conf', '-y'],
20 'command_env': ['env']
21 }
22 )
23 self.manager = PackageManagerDnf5(repository)
24
25 def setup_method(self, cls):
26 self.setup()
27
28 def test_request_package(self):
29 self.manager.request_package('name')
30 assert self.manager.package_requests == ['name']
31
32 def test_request_collection(self):
33 self.manager.request_collection('name')
34 assert self.manager.collection_requests == ['@name']
35
36 def test_request_product(self):
37 self.manager.request_product('name')
38 assert self.manager.product_requests == []
39
40 def test_request_package_exclusion(self):
41 self.manager.request_package_exclusion('name')
42 assert self.manager.exclude_requests == ['name']
43
44 @patch('kiwi.command.Command.run')
45 def test_setup_repository_modules(self, mock_run):
46 self.manager.setup_repository_modules(
47 {
48 'disable': ['mod_c'],
49 'enable': ['mod_a:stream', 'mod_b']
50 }
51 )
52 dnf_call_args = [
53 'dnf5', '--config', '/root-dir/dnf.conf',
54 '-y', '--installroot', '/root-dir', '--releasever=0'
55 ]
56 assert mock_run.call_args_list == [
57 call(
58 dnf_call_args + [
59 'module', 'disable', 'mod_c'
60 ], ['env']
61 ),
62 call(
63 dnf_call_args + [
64 'module', 'reset', 'mod_a'
65 ], ['env']
66 ),
67 call(
68 dnf_call_args + [
69 'module', 'enable', 'mod_a:stream'
70 ], ['env']
71 ),
72 call(
73 dnf_call_args + [
74 'module', 'reset', 'mod_b'
75 ], ['env']
76 ),
77 call(
78 dnf_call_args + [
79 'module', 'enable', 'mod_b'
80 ], ['env']
81 )
82 ]
83
84 @patch('kiwi.command.Command.call')
85 @patch('kiwi.command.Command.run')
86 def test_process_install_requests_bootstrap(self, mock_run, mock_call):
87 self.manager.request_package('vim')
88 self.manager.request_collection('collection')
89 self.manager.request_package_exclusion('skipme')
90 self.manager.process_install_requests_bootstrap()
91 mock_run.assert_called_once_with(
92 [
93 'dnf5', '--config', '/root-dir/dnf.conf', '-y',
94 '--releasever=0', 'makecache'
95 ]
96 )
97 mock_call.assert_called_once_with(
98 [
99 'dnf5', '--config', '/root-dir/dnf.conf', '-y',
100 '--installroot', '/root-dir', '--releasever=0',
101 '--exclude=skipme',
102 'install', 'vim', '@collection'
103 ], ['env']
104 )
105
106 @patch('kiwi.command.Command.call')
107 def test_process_install_requests(self, mock_call):
108 self.manager.request_package('vim')
109 self.manager.request_collection('collection')
110 self.manager.request_package_exclusion('skipme')
111 self.manager.process_install_requests()
112 mock_call.assert_called_once_with(
113 [
114 'chroot', '/root-dir', 'dnf5', '--config', '/dnf.conf', '-y',
115 '--releasever=0', '--exclude=skipme',
116 'install', 'vim', '@collection'
117 ], ['env']
118 )
119
120 @patch('kiwi.command.Command.call')
121 @patch('kiwi.command.Command.run')
122 def test_process_delete_requests_force(self, mock_run, mock_call):
123 self.manager.request_package('vim')
124 self.manager.process_delete_requests(True)
125 mock_call.assert_called_once_with(
126 [
127 'chroot', '/root-dir', 'rpm', '-e',
128 '--nodeps', '--allmatches', '--noscripts', 'vim'
129 ],
130 [
131 'env'
132 ]
133 )
134
135 @patch('kiwi.command.Command.call')
136 @patch('kiwi.command.Command.run')
137 def test_process_delete_requests_no_force(self, mock_run, mock_call):
138 self.manager.request_package('vim')
139 self.manager.process_delete_requests()
140 mock_call.assert_called_once_with(
141 [
142 'chroot', '/root-dir', 'dnf5',
143 '--config', '/dnf.conf', '-y',
144 '--releasever=0', '--setopt=clean_requirements_on_remove=true',
145 'remove', 'vim'
146 ],
147 ['env']
148 )
149
150 @patch('kiwi.command.Command.run')
151 @patch('kiwi.command.Command.call')
152 def test_process_delete_requests_package_missing(
153 self, mock_call, mock_run
154 ):
155 mock_run.side_effect = Exception
156 self.manager.request_package('vim')
157 with raises(KiwiRequestError):
158 self.manager.process_delete_requests(force=True)
159 mock_run.assert_called_once_with(
160 ['chroot', '/root-dir', 'rpm', '-q', 'vim']
161 )
162
163 @patch('kiwi.command.Command.call')
164 def test_update(self, mock_call):
165 self.manager.update()
166 mock_call.assert_called_once_with(
167 [
168 'chroot', '/root-dir', 'dnf5',
169 '--config', '/dnf.conf', '-y',
170 '--releasever=0', 'upgrade'
171 ], ['env']
172 )
173
174 def test_process_only_required(self):
175 self.manager.process_only_required()
176 assert self.manager.custom_args == [
177 '--setopt=install_weak_deps=False',
178 '--setopt=group_package_types=mandatory'
179 ]
180
181 def test_process_plus_recommended(self):
182 self.manager.process_only_required()
183 self.manager.process_plus_recommended()
184 assert self.manager.custom_args == [
185 '--setopt=install_weak_deps=True',
186 '--setopt=group_package_types=default,mandatory,conditional'
187 ]
188 self.manager.process_only_required()
189 assert self.manager.custom_args == [
190 '--setopt=install_weak_deps=False',
191 '--setopt=group_package_types=mandatory'
192 ]
193
194 def test_match_package_installed(self):
195 assert self.manager.match_package_installed('foo', 'Installing : foo')
196
197 def test_match_package_deleted(self):
198 assert self.manager.match_package_deleted('foo', 'Removing: foo')
199
200 @patch('kiwi.package_manager.dnf5.RpmDataBase')
201 def test_post_process_install_requests_bootstrap(self, mock_RpmDataBase):
202 rpmdb = mock.Mock()
203 rpmdb.has_rpm.return_value = True
204 mock_RpmDataBase.return_value = rpmdb
205 self.manager.post_process_install_requests_bootstrap()
206 rpmdb.rebuild_database.assert_called_once_with()
207 rpmdb.set_database_to_image_path.assert_called_once_with()
208
209 @patch('kiwi.package_manager.dnf5.Rpm')
210 def test_clean_leftovers(self, mock_rpm):
211 mock_rpm.return_value = mock.Mock()
212 self.manager.clean_leftovers()
213 mock_rpm.assert_called_once_with(
214 '/root-dir', 'macros.kiwi-image-config'
215 )
216 mock_rpm.return_value.wipe_config.assert_called_once_with()