Debian Codegraph @

test/unit/cli_test.py

raw
1 import sys
2 import logging
3 import typer
4 from unittest.mock import (
5 patch, Mock
6 )
7 from pytest import (
8 raises, fixture
9 )
10
11 from .test_helper import argv_kiwi_tests
12
13 from kiwi.cli import Cli
14 from kiwi.defaults import Defaults
15 from kiwi.version import __version__
16
17 from kiwi.exceptions import (
18 KiwiLoadCommandUndefined,
19 KiwiCommandNotLoaded,
20 KiwiUnknownServiceName,
21 KiwiLoadPluginError
22 )
23
24
25 class TestCli:
26 @fixture(autouse=True)
27 def inject_fixtures(self, caplog):
28 self._caplog = caplog
29
30 def setup(self):
31 self.expected_global_args = {
32 'help': False,
33 '--type': None,
34 'image': False,
35 'system': True,
36 '--logfile': None,
37 '--logsocket': None,
38 '--loglevel': None,
39 '--color-output': False,
40 '--debug': False,
41 '--debug-run-scripts-in-screen': False,
42 'result': False,
43 '--profile': [],
44 '--setenv': [],
45 '--shared-cache-dir': '/var/cache/kiwi',
46 '--temp-dir': '/var/tmp',
47 '--target-arch': None,
48 '--config': None,
49 '--kiwi-file': None
50 }
51 self.command_args = {
52 '--add-repo': [],
53 '--add-repo-credentials': [],
54 '--set-type-attr': [],
55 '--set-release-version': None,
56 '--allow-existing-root': False,
57 '--description': 'description',
58 '--ignore-repos': False,
59 '--ignore-repos-used-for-build': False,
60 '--clear-cache': False,
61 '--root': 'directory',
62 '--set-repo': None,
63 '--set-repo-credentials': None,
64 '--add-package': [],
65 '--add-bootstrap-package': [],
66 '--ca-cert': [],
67 '--ca-target-distribution': None,
68 '--delete-package': [],
69 '--set-container-derived-from': None,
70 '--set-container-tag': None,
71 '--add-container-label': [],
72 '--signing-key': [],
73 'help': False,
74 'prepare': True,
75 'command': 'prepare',
76 'system': True
77 }
78 self.command_args.update(
79 self.expected_global_args
80 )
81 self.cli = Cli()
82 self.loaded_command = self.cli.load_command()
83
84 def setup_method(self, cls):
85 self.setup()
86
87 def teardown(self):
88 sys.argv = argv_kiwi_tests
89
90 def teardown_method(self, cls):
91 self.teardown()
92
93 def test_get_servicename_system(self):
94 cli = Cli()
95 assert cli.get_servicename() == 'system'
96
97 def test_get_command_args(self):
98 with patch(
99 'sys.argv', [
100 sys.argv[0],
101 '--temp-dir', '/var/tmp',
102 '--shared-cache-dir', '/var/cache/kiwi',
103 'system', 'prepare',
104 '--description', 'description',
105 '--root', 'directory'
106 ]
107 ):
108 cli = Cli()
109 assert cli.get_command_args() == self.command_args
110 with patch(
111 'sys.argv', [
112 sys.argv[0],
113 'system', 'prepare',
114 '--description', 'description',
115 '--root', 'directory'
116 ]
117 ):
118 cli = Cli()
119 assert cli.get_command_args() == self.command_args
120
121 def test_warning_on_use_of_legacy_disk_type(self):
122 with patch(
123 'sys.argv', [
124 sys.argv[0],
125 '--type', 'vmx', 'system', 'build',
126 '--description', 'description',
127 '--target-dir', 'directory'
128 ]
129 ):
130 cli = Cli()
131 with self._caplog.at_level(logging.WARNING):
132 cli.get_global_args()
133 assert 'vmx type is now a subset of oem, --type set to oem' in \
134 self._caplog.text
135
136 def test_set_target_arch(self):
137 with patch(
138 'sys.argv', [
139 sys.argv[0],
140 '--target-arch', 'x86_64', 'system', 'build',
141 '--description', 'description',
142 '--target-dir', 'directory'
143 ]
144 ):
145 cli = Cli()
146 cli.get_global_args()
147 assert Defaults.get_platform_name() == 'x86_64'
148
149 def test_get_servicename_image(self):
150 with patch(
151 'sys.argv', [
152 sys.argv[0],
153 'image', 'resize',
154 '--target-dir', 'directory',
155 '--size', '20g'
156 ]
157 ):
158 cli = Cli()
159 assert cli.get_servicename() == 'image'
160
161 def test_get_servicename_result(self):
162 with patch(
163 'sys.argv', [
164 sys.argv[0],
165 'result', 'list',
166 '--target-dir', 'directory'
167 ]
168 ):
169 cli = Cli()
170 assert cli.get_servicename() == 'result'
171
172 def test_get_command(self):
173 assert self.cli.get_command() == 'prepare'
174
175 def test_load_command(self):
176 assert self.cli.load_command() == self.loaded_command
177
178 def test_load_command_unknown(self):
179 self.cli.loaded = False
180 self.cli.global_args['command'] = 'foo'
181 with raises(KiwiCommandNotLoaded):
182 self.cli.load_command()
183
184 def test_load_command_undefined(self):
185 self.cli.loaded = False
186 self.cli.global_args['command'] = None
187 with raises(KiwiLoadCommandUndefined):
188 self.cli.load_command()
189
190 def test_get_command_args_not_loaded(self):
191 with patch(
192 'sys.argv', [
193 sys.argv[0], 'system', 'command-not-implemented'
194 ]
195 ):
196 with patch('sys.exit'):
197 cli = Cli()
198 with raises(KiwiCommandNotLoaded):
199 cli.get_command_args()
200
201 @patch('builtins.print')
202 def test_kiwi_version(self, mock_print):
203 with patch(
204 'sys.argv', [
205 sys.argv[0], '--version'
206 ]
207 ):
208 with patch('sys.exit'):
209 Cli()
210 mock_print.assert_called_once_with(
211 f'KIWI (next generation) version {__version__}'
212 )
213
214 # @patch('kiwi.cli.Help')
215 # def test_help(self, mock_Help):
216 # manual = Mock()
217 # mock_Help.return_value = manual
218 # with patch(
219 # 'sys.argv', [
220 # sys.argv[0], 'help'
221 # ]
222 # ):
223 # with patch('sys.exit'):
224 # Cli()
225 # manual.show.assert_called_once_with('kiwi')
226
227 @patch.object(Cli, '_get_module_entries')
228 @patch('kiwi.cli.EntryPoint')
229 def test_load_plugin_cli(
230 self, mock_EntryPoint, mock_get_module_entries
231 ):
232 plugin_entry = Mock()
233 mock_EntryPoint.return_value = plugin_entry
234 entry = Mock(
235 name='system_some',
236 value='kiwi_some_plugin.tasks.system_some',
237 group='kiwi.tasks'
238 )
239 mock_get_module_entries.return_value = [entry]
240 with patch(
241 'sys.argv', [
242 sys.argv[0],
243 'system', 'build',
244 '--description', 'description',
245 '--target-dir', 'directory'
246 ]
247 ):
248 plugin_entry.load.return_value.typers = {
249 'some': typer.Typer()
250 }
251 cli = Cli()
252 plugin_entry.load.side_effect = ModuleNotFoundError
253 cli.load_plugin_cli()
254 plugin_entry.load.side_effect = Exception('error')
255 with raises(KiwiLoadPluginError):
256 cli.load_plugin_cli()
257
258 @patch.object(Cli, '_get_module_entries')
259 @patch('kiwi.cli.EntryPoint')
260 def test_load_plugin_cli_skips_obsolete_plugin(
261 self, mock_EntryPoint, mock_get_module_entries
262 ):
263 entry = Mock(
264 value='kiwi_boxed_plugin.tasks.system_boxbuild',
265 group='kiwi.tasks'
266 )
267 mock_get_module_entries.return_value = [entry]
268 assert self.cli.load_plugin_cli() == {}
269 assert not mock_EntryPoint.called
270
271 @patch.object(Cli, '_get_module_entries')
272 def test_load_command_skips_obsolete_plugin(
273 self, mock_get_module_entries
274 ):
275 builtin_entry = Mock(
276 value='kiwi.tasks.system_prepare',
277 group='kiwi.tasks'
278 )
279 builtin_entry.name = 'system_prepare'
280 obsolete_entry = Mock(
281 value='kiwi_boxed_plugin.tasks.system_prepare',
282 group='kiwi.tasks'
283 )
284 obsolete_entry.name = 'system_prepare'
285 mock_get_module_entries.return_value = [
286 builtin_entry, obsolete_entry
287 ]
288 assert self.cli.load_command() == builtin_entry.load.return_value
289 assert not obsolete_entry.load.called
290
291 def test_boxbuild_command_args(self):
292 with patch(
293 'sys.argv', [
294 sys.argv[0],
295 'system', 'boxbuild',
296 '--box', 'universal',
297 '--shared-path', '/var/tmp/shared',
298 '--no-accel',
299 '--9p-sharing',
300 '--aarch64',
301 'kiwi',
302 '--description', 'description',
303 '--target-dir', 'directory',
304 '--allow-existing-root'
305 ]
306 ):
307 cli = Cli()
308 assert cli.get_servicename() == 'system'
309 assert cli.get_command() == 'boxbuild'
310 command_args = cli.get_command_args()
311 assert command_args['--box'] == 'universal'
312 assert command_args['--shared-path'] == '/var/tmp/shared'
313 assert command_args['--no-accel'] is True
314 assert command_args['--9p-sharing'] is True
315 assert command_args['--aarch64'] is True
316 assert command_args['--x86_64'] is False
317 assert command_args['--box-smp-cpus'] == '4'
318 assert command_args['--ssh-port'] == '10022'
319 assert command_args['--ssh-key'] == 'id_rsa'
320 assert command_args['system_build'] == [
321 '--description', 'description',
322 '--target-dir', 'directory',
323 '--allow-existing-root'
324 ]
325
326 def test_boxbuild_command_args_separator(self):
327 with patch(
328 'sys.argv', [
329 sys.argv[0],
330 'system', 'boxbuild',
331 '--box', 'universal',
332 '--',
333 '--description', 'description',
334 '--target-dir', 'directory'
335 ]
336 ):
337 cli = Cli()
338 assert cli.get_command_args()['system_build'] == [
339 '--description', 'description',
340 '--target-dir', 'directory'
341 ]
342
343 @patch.object(Cli, '_get_module_entries')
344 @patch('kiwi.cli.EntryPoint')
345 def test_load_plugin_cli_skips_obsolete_stackbuild_plugin(
346 self, mock_EntryPoint, mock_get_module_entries
347 ):
348 entry = Mock(
349 value='kiwi_stackbuild_plugin.tasks.system_stackbuild',
350 group='kiwi.tasks'
351 )
352 mock_get_module_entries.return_value = [entry]
353 assert self.cli.load_plugin_cli() == {}
354 assert not mock_EntryPoint.called
355
356 def test_stackbuild_command_args(self):
357 with patch(
358 'sys.argv', [
359 sys.argv[0],
360 'system', 'stackbuild',
361 '--stash', 'base',
362 '--stash', 'layer',
363 '--target-dir', 'directory',
364 '--description', 'description',
365 '--from-registry', 'registry.uri',
366 'kiwi',
367 '--signing-key', 'some-key'
368 ]
369 ):
370 cli = Cli()
371 assert cli.get_servicename() == 'system'
372 assert cli.get_command() == 'stackbuild'
373 command_args = cli.get_command_args()
374 assert command_args['--stash'] == ['base', 'layer']
375 assert command_args['--target-dir'] == 'directory'
376 assert command_args['--description'] == 'description'
377 assert command_args['--from-registry'] == 'registry.uri'
378 assert command_args['system_build_or_create'] == [
379 '--signing-key', 'some-key'
380 ]
381
382 def test_stackbuild_command_args_no_kiwi_subcommand(self):
383 with patch(
384 'sys.argv', [
385 sys.argv[0],
386 'system', 'stackbuild',
387 '--stash', 'base',
388 '--target-dir', 'directory'
389 ]
390 ):
391 cli = Cli()
392 command_args = cli.get_command_args()
393 assert command_args['--stash'] == ['base']
394 assert command_args['--description'] is None
395 assert command_args['--from-registry'] is None
396 assert command_args['system_build_or_create'] == []
397
398 def test_stackbuild_command_args_separator(self):
399 with patch(
400 'sys.argv', [
401 sys.argv[0],
402 'system', 'stackbuild',
403 '--stash', 'base',
404 '--target-dir', 'directory',
405 '--',
406 '--signing-key', 'some-key'
407 ]
408 ):
409 cli = Cli()
410 assert cli.get_command_args()['system_build_or_create'] == [
411 '--signing-key', 'some-key'
412 ]
413
414 def test_stash_command_args(self):
415 with patch(
416 'sys.argv', [
417 sys.argv[0],
418 'system', 'stash',
419 '--root', 'directory',
420 '--tag', 'v1',
421 '--container-name', 'name'
422 ]
423 ):
424 cli = Cli()
425 assert cli.get_servicename() == 'system'
426 assert cli.get_command() == 'stash'
427 command_args = cli.get_command_args()
428 assert command_args['--root'] == 'directory'
429 assert command_args['--tag'] == 'v1'
430 assert command_args['--container-name'] == 'name'
431 assert command_args['--list'] is False
432
433 def test_get_servicename_unknown(self):
434 self.cli.global_args['image'] = False
435 self.cli.global_args['system'] = False
436 self.cli.global_args['result'] = False
437 with raises(KiwiUnknownServiceName):
438 self.cli.get_servicename()