| 1 |
import sys |
| 2 |
from unittest.mock import ( |
| 3 |
patch, call |
| 4 |
) |
| 5 |
from pytest import ( |
| 6 |
raises, fixture |
| 7 |
) |
| 8 |
import logging |
| 9 |
|
| 10 |
from ..test_helper import argv_kiwi_tests |
| 11 |
|
| 12 |
import kiwi.xml_parse |
| 13 |
from kiwi.tasks.base import CliTask |
| 14 |
|
| 15 |
from kiwi.defaults import Defaults |
| 16 |
from kiwi.exceptions import KiwiConfigFileNotFound |
| 17 |
|
| 18 |
|
| 19 |
class TestCliTask: |
| 20 |
@fixture(autouse=True) |
| 21 |
def inject_fixtures(self, caplog): |
| 22 |
self._caplog = caplog |
| 23 |
|
| 24 |
@patch('kiwi.logger.Logger.setLogLevel') |
| 25 |
@patch('kiwi.logger.Logger.setLogFlag') |
| 26 |
@patch('kiwi.logger.Logger.set_logfile') |
| 27 |
@patch('kiwi.logger.Logger.set_log_socket') |
| 28 |
@patch('kiwi.logger.Logger.set_color_format') |
| 29 |
@patch('kiwi.cli.Cli.load_command') |
| 30 |
@patch('kiwi.cli.Cli.get_command_args') |
| 31 |
@patch('kiwi.cli.Cli.get_global_args') |
| 32 |
@patch('kiwi.runtime_config.RuntimeConfig') |
| 33 |
def setup( |
| 34 |
self, mock_runtime_config, mock_global_args, mock_command_args, |
| 35 |
mock_load_command, mock_color, |
| 36 |
mock_set_log_socket, mock_setlog, mock_setLogFlag, mock_setLogLevel |
| 37 |
): |
| 38 |
Defaults.set_platform_name('x86_64') |
| 39 |
mock_global_args.return_value = { |
| 40 |
'--debug': True, |
| 41 |
'--debug-run-scripts-in-screen': True, |
| 42 |
'--logfile': 'stdout', |
| 43 |
'--logsocket': 'log_socket', |
| 44 |
'--loglevel': None, |
| 45 |
'--color-output': True, |
| 46 |
'--profile': ['vmxFlavour'], |
| 47 |
'--setenv': ['SOURCE_DATE_EPOCH=42'], |
| 48 |
'--type': None |
| 49 |
} |
| 50 |
mock_command_args.return_value = { |
| 51 |
'system': True, |
| 52 |
'prepare': True, |
| 53 |
'--description': 'description', |
| 54 |
'--root': 'directory' |
| 55 |
} |
| 56 |
|
| 57 |
self.task = CliTask() |
| 58 |
|
| 59 |
mock_load_command.assert_called_once_with() |
| 60 |
assert mock_command_args.call_args_list == [ |
| 61 |
call(raise_if_no_command=False), call() |
| 62 |
] |
| 63 |
mock_global_args.assert_called_once_with() |
| 64 |
assert mock_setLogLevel.call_args_list == [ |
| 65 |
call(logging.DEBUG), |
| 66 |
call(logging.CRITICAL, except_for=['file', 'socket']) |
| 67 |
] |
| 68 |
mock_setLogFlag.assert_called_once_with('run-scripts-in-screen') |
| 69 |
mock_setlog.assert_called_once_with('stdout') |
| 70 |
mock_color.assert_called_once_with() |
| 71 |
mock_runtime_config.assert_called_once_with() |
| 72 |
|
| 73 |
mock_setLogLevel.reset_mock() |
| 74 |
mock_global_args.return_value = { |
| 75 |
'--debug': True, |
| 76 |
'--debug-run-scripts-in-screen': True, |
| 77 |
'--logfile': 'some', |
| 78 |
'--logsocket': 'log_socket', |
| 79 |
'--loglevel': None, |
| 80 |
'--color-output': True, |
| 81 |
'--profile': ['vmxFlavour'], |
| 82 |
'--setenv': [], |
| 83 |
'--type': None |
| 84 |
} |
| 85 |
self.task = CliTask() |
| 86 |
assert mock_setLogLevel.call_args_list == [ |
| 87 |
call(logging.DEBUG), |
| 88 |
call(logging.DEBUG, only_for=['file', 'socket']) |
| 89 |
] |
| 90 |
|
| 91 |
@patch('kiwi.logger.Logger.setLogLevel') |
| 92 |
@patch('kiwi.logger.Logger.setLogFlag') |
| 93 |
@patch('kiwi.logger.Logger.set_logfile') |
| 94 |
@patch('kiwi.logger.Logger.set_log_socket') |
| 95 |
@patch('kiwi.logger.Logger.set_color_format') |
| 96 |
@patch('kiwi.cli.Cli.load_command') |
| 97 |
@patch('kiwi.cli.Cli.get_command_args') |
| 98 |
@patch('kiwi.cli.Cli.get_global_args') |
| 99 |
@patch('kiwi.runtime_config.RuntimeConfig') |
| 100 |
def setup_method( |
| 101 |
self, cls, mock_runtime_config, mock_global_args, mock_command_args, |
| 102 |
mock_load_command, mock_color, |
| 103 |
mock_set_log_socket, mock_setlog, mock_setLogFlag, mock_setLogLevel |
| 104 |
): |
| 105 |
self.setup() |
| 106 |
|
| 107 |
@patch('kiwi.logger.Logger.setLogLevel') |
| 108 |
@patch('kiwi.cli.Cli.load_command') |
| 109 |
@patch('kiwi.cli.Cli.get_command_args') |
| 110 |
@patch('kiwi.cli.Cli.get_global_args') |
| 111 |
@patch('kiwi.runtime_config.RuntimeConfig') |
| 112 |
def test_setup_custom_log_level( |
| 113 |
self, mock_runtime_config, mock_global_args, mock_command_args, |
| 114 |
mock_load_command, mock_setLogLevel |
| 115 |
): |
| 116 |
Defaults.set_platform_name('x86_64') |
| 117 |
mock_global_args.return_value = { |
| 118 |
'--debug': None, |
| 119 |
'--logfile': None, |
| 120 |
'--logsocket': None, |
| 121 |
'--debug-run-scripts-in-screen': None, |
| 122 |
'--color-output': None, |
| 123 |
'--loglevel': '10', |
| 124 |
'--setenv': [] |
| 125 |
} |
| 126 |
self.task = CliTask() |
| 127 |
mock_setLogLevel.assert_called_once_with(10) |
| 128 |
|
| 129 |
@patch('kiwi.logger.Logger.setLogLevel') |
| 130 |
@patch('kiwi.cli.Cli.load_command') |
| 131 |
@patch('kiwi.cli.Cli.get_command_args') |
| 132 |
@patch('kiwi.cli.Cli.get_global_args') |
| 133 |
@patch('kiwi.runtime_config.RuntimeConfig') |
| 134 |
def test_setup_custom_log_level_invalid( |
| 135 |
self, mock_runtime_config, mock_global_args, mock_command_args, |
| 136 |
mock_load_command, mock_setLogLevel |
| 137 |
): |
| 138 |
Defaults.set_platform_name('x86_64') |
| 139 |
mock_global_args.return_value = { |
| 140 |
'--debug': None, |
| 141 |
'--logfile': None, |
| 142 |
'--logsocket': None, |
| 143 |
'--debug-run-scripts-in-screen': None, |
| 144 |
'--color-output': None, |
| 145 |
'--loglevel': 'bogus', |
| 146 |
'--setenv': [] |
| 147 |
} |
| 148 |
self.task = CliTask() |
| 149 |
mock_setLogLevel.assert_called_once_with(20) |
| 150 |
|
| 151 |
def test_eleventuple_token(self): |
| 152 |
assert self.task.eleventuple_token( |
| 153 |
'a,b,,d,e,f,{1;2;3},x y z,jammy,false,metalink' |
| 154 |
) == [ |
| 155 |
'a', 'b', '', 'd', 'e', 'f', ['1', '2', '3'], 'x y z', |
| 156 |
'jammy', False, 'metalink' |
| 157 |
] |
| 158 |
assert self.task.eleventuple_token('a,b,,d,e,f,{1;2;3}') == [ |
| 159 |
'a', 'b', '', 'd', 'e', 'f', ['1', '2', '3'], |
| 160 |
None, None, None, None |
| 161 |
] |
| 162 |
|
| 163 |
def test_attr_token(self): |
| 164 |
assert self.task.attr_token('a=b') == ['a', 'b'] |
| 165 |
|
| 166 |
@patch('kiwi.tasks.base.RuntimeChecker') |
| 167 |
def test_load_xml_description(self, mock_runtime_checker): |
| 168 |
self.task.load_xml_description('../data/description') |
| 169 |
mock_runtime_checker.assert_called_once_with(self.task.xml_state) |
| 170 |
assert self.task.config_file == '../data/description/config.xml' |
| 171 |
assert isinstance(self.task.xml_data, kiwi.xml_parse.image) |
| 172 |
assert self.task.xml_state.profiles == ['vmxFlavour'] |
| 173 |
|
| 174 |
def test_load_xml_description_buildservice(self): |
| 175 |
self.task.load_xml_description('../data/description.buildservice') |
| 176 |
assert self.task.config_file == \ |
| 177 |
'../data/description.buildservice/appliance.kiwi' |
| 178 |
|
| 179 |
def test_load_xml_description_raises(self): |
| 180 |
with raises(KiwiConfigFileNotFound): |
| 181 |
self.task.load_xml_description('foo') |
| 182 |
with raises(KiwiConfigFileNotFound): |
| 183 |
self.task.load_xml_description('path', 'custom_kiwi_file') |
| 184 |
|
| 185 |
def teardown(self): |
| 186 |
sys.argv = argv_kiwi_tests |
| 187 |
|
| 188 |
def teardown_method(self, cls): |
| 189 |
self.teardown() |