| 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_boxbuild', |
| 236 |
value='kiwi_boxed_plugin.tasks.system_boxbuild', |
| 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 |
def test_get_servicename_unknown(self): |
| 259 |
self.cli.global_args['image'] = False |
| 260 |
self.cli.global_args['system'] = False |
| 261 |
self.cli.global_args['result'] = False |
| 262 |
with raises(KiwiUnknownServiceName): |
| 263 |
self.cli.get_servicename() |