| 1 |
import sys |
| 2 |
import logging |
| 3 |
from unittest.mock import patch |
| 4 |
from pytest import ( |
| 5 |
raises, fixture |
| 6 |
) |
| 7 |
|
| 8 |
from .test_helper import argv_kiwi_tests |
| 9 |
|
| 10 |
from kiwi.cli import Cli |
| 11 |
from kiwi.defaults import Defaults |
| 12 |
|
| 13 |
from kiwi.exceptions import ( |
| 14 |
KiwiLoadCommandUndefined, |
| 15 |
KiwiCommandNotLoaded, |
| 16 |
KiwiUnknownServiceName |
| 17 |
) |
| 18 |
|
| 19 |
|
| 20 |
class TestCli: |
| 21 |
@fixture(autouse=True) |
| 22 |
def inject_fixtures(self, caplog): |
| 23 |
self._caplog = caplog |
| 24 |
|
| 25 |
def setup(self): |
| 26 |
self.expected_global_args = { |
| 27 |
'help': False, |
| 28 |
'--type': None, |
| 29 |
'image': False, |
| 30 |
'system': True, |
| 31 |
'-h': False, |
| 32 |
'--logfile': None, |
| 33 |
'--logsocket': None, |
| 34 |
'--loglevel': None, |
| 35 |
'--color-output': False, |
| 36 |
'--version': False, |
| 37 |
'--debug': False, |
| 38 |
'--debug-run-scripts-in-screen': False, |
| 39 |
'result': False, |
| 40 |
'--profile': [], |
| 41 |
'--setenv': [], |
| 42 |
'--shared-cache-dir': '/var/cache/kiwi', |
| 43 |
'--temp-dir': '/var/tmp', |
| 44 |
'--target-arch': None, |
| 45 |
'--help': False, |
| 46 |
'--config': 'config-file', |
| 47 |
'--kiwi-file': None |
| 48 |
} |
| 49 |
self.command_args = { |
| 50 |
'--add-repo': [], |
| 51 |
'--add-repo-credentials': [], |
| 52 |
'--set-type-attr': [], |
| 53 |
'--set-release-version': None, |
| 54 |
'--allow-existing-root': False, |
| 55 |
'--description': 'description', |
| 56 |
'--help': False, |
| 57 |
'--ignore-repos': False, |
| 58 |
'--ignore-repos-used-for-build': False, |
| 59 |
'--clear-cache': False, |
| 60 |
'--root': 'directory', |
| 61 |
'--set-repo': None, |
| 62 |
'--set-repo-credentials': None, |
| 63 |
'--add-package': [], |
| 64 |
'--add-bootstrap-package': [], |
| 65 |
'--ca-cert': [], |
| 66 |
'--ca-target-distribution': None, |
| 67 |
'--delete-package': [], |
| 68 |
'--set-container-derived-from': None, |
| 69 |
'--set-container-tag': None, |
| 70 |
'--add-container-label': [], |
| 71 |
'--signing-key': [], |
| 72 |
'-h': False, |
| 73 |
'help': False, |
| 74 |
'prepare': True, |
| 75 |
'system': True |
| 76 |
} |
| 77 |
self.cli = Cli() |
| 78 |
self.loaded_command = self.cli.load_command() |
| 79 |
|
| 80 |
def setup_method(self, cls): |
| 81 |
self.setup() |
| 82 |
|
| 83 |
def teardown(self): |
| 84 |
sys.argv = argv_kiwi_tests |
| 85 |
|
| 86 |
def teardown_method(self, cls): |
| 87 |
self.teardown() |
| 88 |
|
| 89 |
@patch('kiwi.cli.Help.show') |
| 90 |
def test_show_and_exit_on_help_request(self, help_show): |
| 91 |
self.cli.all_args['help'] = True |
| 92 |
with raises(SystemExit): |
| 93 |
self.cli.show_and_exit_on_help_request() |
| 94 |
help_show.assert_called_once_with('kiwi') |
| 95 |
|
| 96 |
def test_get_servicename_system(self): |
| 97 |
cli = Cli() |
| 98 |
assert cli.get_servicename() == 'system' |
| 99 |
|
| 100 |
def test_warning_on_use_of_legacy_disk_type(self): |
| 101 |
sys.argv = [ |
| 102 |
sys.argv[0], |
| 103 |
'--type', 'vmx', 'system', 'build', |
| 104 |
'--description', 'description', |
| 105 |
'--target-dir', 'directory' |
| 106 |
] |
| 107 |
cli = Cli() |
| 108 |
with self._caplog.at_level(logging.WARNING): |
| 109 |
cli.get_global_args() |
| 110 |
assert 'vmx type is now a subset of oem, --type set to oem' in \ |
| 111 |
self._caplog.text |
| 112 |
|
| 113 |
def test_set_target_arch(self): |
| 114 |
sys.argv = [ |
| 115 |
sys.argv[0], |
| 116 |
'--target-arch', 'x86_64', 'system', 'build', |
| 117 |
'--description', 'description', |
| 118 |
'--target-dir', 'directory' |
| 119 |
] |
| 120 |
cli = Cli() |
| 121 |
cli.get_global_args() |
| 122 |
assert Defaults.get_platform_name() == 'x86_64' |
| 123 |
|
| 124 |
def test_get_servicename_image(self): |
| 125 |
sys.argv = [ |
| 126 |
sys.argv[0], |
| 127 |
'image', 'resize', |
| 128 |
'--target-dir', 'directory', |
| 129 |
'--size', '20g' |
| 130 |
] |
| 131 |
cli = Cli() |
| 132 |
assert cli.get_servicename() == 'image' |
| 133 |
|
| 134 |
def test_get_servicename_result(self): |
| 135 |
sys.argv = [ |
| 136 |
sys.argv[0], |
| 137 |
'result', 'list', |
| 138 |
'--target-dir', 'directory' |
| 139 |
] |
| 140 |
cli = Cli() |
| 141 |
assert cli.get_servicename() == 'result' |
| 142 |
|
| 143 |
def test_get_command(self): |
| 144 |
assert self.cli.get_command() == 'prepare' |
| 145 |
|
| 146 |
def test_get_command_args(self): |
| 147 |
assert self.cli.get_command_args() == self.command_args |
| 148 |
|
| 149 |
def test_load_command(self): |
| 150 |
assert self.cli.load_command() == self.loaded_command |
| 151 |
|
| 152 |
def test_load_command_unknown(self): |
| 153 |
self.cli.loaded = False |
| 154 |
self.cli.all_args['<command>'] = 'foo' |
| 155 |
with raises(KiwiCommandNotLoaded): |
| 156 |
self.cli.load_command() |
| 157 |
|
| 158 |
def test_load_command_undefined(self): |
| 159 |
self.cli.loaded = False |
| 160 |
self.cli.all_args['<command>'] = None |
| 161 |
with raises(KiwiLoadCommandUndefined): |
| 162 |
self.cli.load_command() |
| 163 |
|
| 164 |
def test_get_command_args_not_loaded(self): |
| 165 |
sys.argv = [ |
| 166 |
sys.argv[0], 'system', 'command-not-implemented' |
| 167 |
] |
| 168 |
cli = Cli() |
| 169 |
with raises(KiwiCommandNotLoaded): |
| 170 |
cli.get_command_args() |
| 171 |
|
| 172 |
def test_get_servicename_unknown(self): |
| 173 |
self.cli.all_args['system'] = False |
| 174 |
self.cli.all_args['foo'] = False |
| 175 |
with raises(KiwiUnknownServiceName): |
| 176 |
self.cli.get_servicename() |