Debian Codegraph @

test/unit/boxbuild/box_download_test.py

raw
1 import io
2 from pytest import raises
3 from unittest.mock import (
4 patch, Mock, MagicMock, call
5 )
6
7 from kiwi.exceptions import (
8 KiwiBoxChecksumError
9 )
10 from kiwi.boxbuild.box_download import (
11 BoxDownload, vm_setup_type
12 )
13
14
15 class TestBoxDownload:
16 @patch('kiwi.boxbuild.defaults.BoxBuildDefaults.get_box_config_file')
17 @patch('kiwi.boxbuild.box_download.Path')
18 @patch('kiwi.boxbuild.box_download.DirFiles')
19 def setup(self, mock_DirFiles, mock_Path, mock_get_box_config_file):
20 self.box_stage = Mock()
21 self.box_stage.register.return_value = 'register_file'
22 mock_DirFiles.return_value = self.box_stage
23 mock_get_box_config_file.return_value = \
24 '../data/boxbuild/kiwi_boxed_plugin.yml'
25 with patch.dict('os.environ', {'HOME': 'HOME'}):
26 self.box = BoxDownload('suse', 'x86_64')
27 mock_Path.create.assert_called_once_with(
28 'HOME/.kiwi_boxes/suse'
29 )
30 self.result = vm_setup_type(
31 system='HOME/.kiwi_boxes/suse/'
32 'SUSE-Box.x86_64-1.42.1-System-BuildBox.qcow2',
33 kernel='HOME/.kiwi_boxes/suse/kernel.x86_64',
34 initrd='HOME/.kiwi_boxes/suse/initrd.x86_64',
35 append='console=hvc0 root=/dev/vda1 rd.plymouth=0',
36 console='hvc0',
37 ram='8096M',
38 smp=4
39 )
40
41 @patch('kiwi.boxbuild.defaults.BoxBuildDefaults.get_box_config_file')
42 @patch('kiwi.boxbuild.box_download.Path')
43 @patch('kiwi.boxbuild.box_download.DirFiles')
44 def setup_method(
45 self, cls, mock_DirFiles, mock_Path, mock_get_box_config_file
46 ):
47 self.setup()
48
49 @patch('kiwi.boxbuild.defaults.BoxBuildDefaults.get_box_config_file')
50 @patch('kiwi.boxbuild.box_download.Path')
51 @patch('kiwi.boxbuild.box_download.DirFiles')
52 @patch('os.path.isdir')
53 def test_custom_box_cache_dir(
54 self, mock_os_path_isdir, mock_DirFiles, mock_Path,
55 mock_get_box_config_file
56 ):
57 mock_os_path_isdir.return_value = True
58 mock_DirFiles.return_value = \
59 Mock().register.return_value = 'register_file'
60 mock_get_box_config_file.return_value = \
61 '../data/boxbuild/kiwi_boxed_plugin.yml'
62 with patch.dict(
63 'os.environ', {'KIWI_BOXED_CACHE_DIR': '/some/custom/dir'}
64 ):
65 BoxDownload('suse', 'x86_64')
66 mock_Path.create.assert_called_once_with(
67 '/some/custom/dir/suse'
68 )
69
70 @patch('kiwi.boxbuild.box_download.Command.run')
71 @patch('kiwi.boxbuild.box_download.Uri')
72 @patch('kiwi.boxbuild.box_download.SolverRepository.new')
73 @patch('kiwi.boxbuild.box_download.Checksum')
74 @patch('os.path.exists')
75 @patch('os.chdir')
76 @patch('kiwi.boxbuild.box_download.FetchFiles')
77 def test_fetch_image_checksum_failed(
78 self, mock_FetchFiles, mock_os_chdir,
79 mock_os_path_exist, mock_Checksum, mock_SolverRepository,
80 mock_Uri, mock_Command_run
81 ):
82
83 def matches(shasum, filename):
84 if filename.endswith('.qcow2.sha256'):
85 return False
86 return True
87
88 checksum = Mock()
89 checksum.matches.side_effect = matches
90 checksum.sha256.return_value = 'sum'
91 mock_Checksum.return_value = checksum
92 with patch('builtins.open', create=True):
93 with raises(KiwiBoxChecksumError):
94 self.box.fetch(update_check=True)
95
96 @patch('kiwi.boxbuild.box_download.Command.run')
97 @patch('kiwi.boxbuild.box_download.Uri')
98 @patch('kiwi.boxbuild.box_download.SolverRepository.new')
99 @patch('kiwi.boxbuild.box_download.Checksum')
100 @patch('os.path.exists')
101 @patch('os.chdir')
102 @patch('kiwi.boxbuild.box_download.FetchFiles')
103 def test_fetch_kernel_checksum_failed(
104 self, mock_FetchFiles, mock_os_chdir,
105 mock_os_path_exist, mock_Checksum, mock_SolverRepository,
106 mock_Uri, mock_Command_run
107 ):
108
109 def matches(shasum, filename):
110 if filename.endswith('.tar.xz.sha256'):
111 return False
112 return True
113
114 checksum = Mock()
115 checksum.matches.side_effect = matches
116 checksum.sha256.return_value = 'sum'
117 mock_Checksum.return_value = checksum
118 with patch('builtins.open', create=True):
119 with raises(KiwiBoxChecksumError):
120 self.box.fetch(update_check=True)
121
122 @patch('kiwi.boxbuild.box_download.Command.run')
123 @patch('kiwi.boxbuild.box_download.Uri')
124 @patch('kiwi.boxbuild.box_download.SolverRepository.new')
125 @patch('kiwi.boxbuild.box_download.Checksum')
126 @patch('os.path.exists')
127 @patch('os.chdir')
128 @patch('kiwi.boxbuild.box_download.FetchFiles')
129 @patch.object(BoxDownload, '_checksum_ok')
130 def test_fetch_sbom_checksum_did_not_match(
131 self, mock__checksum_ok, mock_FetchFiles, mock_os_chdir,
132 mock_os_path_exist, mock_Checksum, mock_SolverRepository,
133 mock_Uri, mock_Command_run
134 ):
135 mock__checksum_ok.return_value = True
136 fetcher = Mock()
137 mock_FetchFiles.return_value = fetcher
138 checksum = Mock()
139 checksum.matches.return_value = False
140 checksum.sha256.return_value = 'sum'
141 mock_Checksum.return_value = checksum
142 repo = Mock()
143 repo._get_mime_typed_uri.return_value = 'uri://'
144 mock_SolverRepository.return_value = repo
145 mock_os_path_exist.return_value = False
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 with patch.dict('os.environ', {'HOME': 'HOME'}):
150 assert self.box.fetch(update_check=True) == self.result
151 assert self.box_stage.register.call_args_list == [
152 call(
153 'HOME/.kiwi_boxes/suse/'
154 'SUSE-Box.x86_64-1.42.1-System-BuildBox.report'
155 ),
156 call(
157 'HOME/.kiwi_boxes/suse/'
158 'SUSE-Box.x86_64-1.42.1-System-BuildBox.report.sha256'
159 ),
160 call(
161 'HOME/.kiwi_boxes/suse/'
162 'SUSE-Box.x86_64-1.42.1-Kernel-BuildBox.tar.xz'
163 ),
164 call(
165 'HOME/.kiwi_boxes/suse/'
166 'SUSE-Box.x86_64-1.42.1-Kernel-BuildBox.tar.xz.sha256'
167 ),
168 call(
169 'HOME/.kiwi_boxes/suse/'
170 'SUSE-Box.x86_64-1.42.1-System-BuildBox.qcow2'
171 ),
172 call(
173 'HOME/.kiwi_boxes/suse/'
174 'SUSE-Box.x86_64-1.42.1-System-BuildBox.qcow2.sha256'
175 )
176 ]
177 mock_open.assert_called_once_with(
178 self.box_stage.register.return_value, 'w'
179 )
180 file_handle.write.assert_called_once_with('sum')
181 repo.download_from_repository.assert_called_once_with(
182 'SUSE-Box.x86_64-1.42.1-System-BuildBox.report',
183 self.box_stage.register.return_value
184 )
185 assert fetcher.wget.call_args_list == [
186 call(
187 url='uri:///SUSE-Box.x86_64-1.42.1-Kernel-BuildBox.tar.xz',
188 filename='register_file'
189 ),
190 call(
191 url='uri:///SUSE-Box.x86_64-1.42.1-Kernel-BuildBox.tar.xz.sha256',
192 filename='register_file'
193 ),
194 call(
195 url='uri:///SUSE-Box.x86_64-1.42.1-System-BuildBox.qcow2',
196 filename='register_file'
197 ),
198 call(
199 url='uri:///SUSE-Box.x86_64-1.42.1-System-BuildBox.qcow2.sha256',
200 filename='register_file'
201 )
202 ]
203 self.box_stage.commit.assert_called_once_with()
204 assert mock_Command_run.call_args_list == [
205 call(
206 [
207 'tar', '-C', 'HOME/.kiwi_boxes/suse',
208 '--transform', 's/.*/kernel.x86_64/',
209 '--wildcards', '-xf',
210 'HOME/.kiwi_boxes/suse/'
211 'SUSE-Box.x86_64-1.42.1-Kernel-BuildBox.tar.xz',
212 '*.kernel'
213 ]
214 ),
215 call(
216 [
217 'tar', '-C', 'HOME/.kiwi_boxes/suse',
218 '--transform', 's/.*/initrd.x86_64/',
219 '--wildcards', '-xf',
220 'HOME/.kiwi_boxes/suse/'
221 'SUSE-Box.x86_64-1.42.1-Kernel-BuildBox.tar.xz',
222 '*.initrd'
223 ]
224 )
225 ]
226
227 @patch('kiwi.boxbuild.box_download.Command.run')
228 @patch('kiwi.boxbuild.box_download.Uri')
229 @patch('kiwi.boxbuild.box_download.SolverRepository.new')
230 @patch('kiwi.boxbuild.box_download.Checksum')
231 @patch('os.path.exists')
232 def test_fetch_checksum_matches(
233 self, mock_os_path_exist, mock_Checksum, mock_SolverRepository,
234 mock_Uri, mock_Command_run
235 ):
236 checksum = Mock()
237 checksum.matches.return_value = True
238 checksum.sha256.return_value = 'sum'
239 mock_Checksum.return_value = checksum
240 repo = Mock()
241 mock_SolverRepository.return_value = repo
242 mock_os_path_exist.return_value = True
243 assert self.box.fetch(update_check=True) == self.result
244 repo.download_from_repository.assert_called_once_with(
245 'SUSE-Box.x86_64-1.42.1-System-BuildBox.report',
246 self.box_stage.register.return_value
247 )
248
249 @patch('kiwi.boxbuild.box_download.Command.run')
250 @patch('kiwi.boxbuild.box_download.Uri')
251 @patch('kiwi.boxbuild.box_download.SolverRepository.new')
252 @patch('kiwi.boxbuild.box_download.Checksum')
253 @patch('os.path.exists')
254 def test_fetch_update_check_disabled(
255 self, mock_os_path_exist, mock_Checksum, mock_SolverRepository,
256 mock_Uri, mock_Command_run
257 ):
258 mock_os_path_exist.return_value = True
259 assert self.box.fetch(update_check=False) == self.result
260
261 @patch('os.system')
262 def test_fetch_container(self, mock_os_system):
263 assert self.box.fetch_container() == 'some'
264 mock_os_system.assert_called_once_with('sudo podman pull some')