| 1 |
import sys |
| 2 |
|
| 3 |
from unittest.mock import ( |
| 4 |
patch, call, Mock, MagicMock |
| 5 |
) |
| 6 |
|
| 7 |
import kiwi |
| 8 |
|
| 9 |
from ..test_helper import argv_kiwi_tests |
| 10 |
|
| 11 |
from kiwi.tasks.image_info import ImageInfoTask |
| 12 |
|
| 13 |
from collections import ( |
| 14 |
namedtuple, OrderedDict |
| 15 |
) |
| 16 |
|
| 17 |
|
| 18 |
class TestImageInfoTask: |
| 19 |
def setup(self): |
| 20 |
sys.argv = [ |
| 21 |
sys.argv[0], 'image', 'info', |
| 22 |
'--description', '../data/image_info', |
| 23 |
'--resolve-package-list' |
| 24 |
] |
| 25 |
result_type = namedtuple( |
| 26 |
'result_type', [ |
| 27 |
'uri', 'installsize_bytes', 'arch', 'version', 'checksum' |
| 28 |
] |
| 29 |
) |
| 30 |
self.result_info = { |
| 31 |
'image': 'LimeJeOS', |
| 32 |
'resolved-packages': { |
| 33 |
'packagename': { |
| 34 |
'version': '0.8.15', 'arch': 'x86_64', |
| 35 |
'source': 'uri', 'status': 'added_by_dependency_solver', |
| 36 |
'installsize_bytes': 42 |
| 37 |
}, |
| 38 |
'filesystem': { |
| 39 |
'version': '42', 'arch': 'x86_64', |
| 40 |
'source': 'uri', 'status': 'listed_in_kiwi_description', |
| 41 |
'installsize_bytes': 0 |
| 42 |
} |
| 43 |
} |
| 44 |
} |
| 45 |
self.runtime_checker = Mock() |
| 46 |
kiwi.tasks.base.RuntimeChecker = Mock( |
| 47 |
return_value=self.runtime_checker |
| 48 |
) |
| 49 |
self.runtime_config = Mock() |
| 50 |
kiwi.tasks.base.RuntimeConfig = Mock( |
| 51 |
return_value=self.runtime_config |
| 52 |
) |
| 53 |
self.solver = MagicMock() |
| 54 |
self.solver.solve = Mock( |
| 55 |
return_value={ |
| 56 |
'packagename': result_type( |
| 57 |
uri='uri', installsize_bytes=42, |
| 58 |
arch='x86_64', version='0.8.15', checksum='checksum' |
| 59 |
), |
| 60 |
'filesystem': result_type( |
| 61 |
uri='uri', installsize_bytes=0, |
| 62 |
arch='x86_64', version='42', checksum='checksum' |
| 63 |
) |
| 64 |
} |
| 65 |
) |
| 66 |
kiwi.tasks.image_info.Sat = Mock( |
| 67 |
return_value=self.solver |
| 68 |
) |
| 69 |
|
| 70 |
self.solver_repository = Mock() |
| 71 |
kiwi.tasks.image_info.SolverRepository = Mock( |
| 72 |
return_value=self.solver_repository |
| 73 |
) |
| 74 |
|
| 75 |
kiwi.tasks.image_info.Help = Mock( |
| 76 |
return_value=Mock() |
| 77 |
) |
| 78 |
self.task = ImageInfoTask() |
| 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 |
def _init_command_args(self): |
| 90 |
self.task.command_args = {} |
| 91 |
self.task.command_args['help'] = False |
| 92 |
self.task.command_args['info'] = False |
| 93 |
self.task.command_args['--description'] = '../data/image_info' |
| 94 |
self.task.command_args['--add-repo'] = [] |
| 95 |
self.task.command_args['--add-repo-credentials'] = [] |
| 96 |
self.task.command_args['--ignore-repos'] = False |
| 97 |
self.task.command_args['--resolve-package-list'] = False |
| 98 |
self.task.command_args['--list-profiles'] = False |
| 99 |
self.task.command_args['--print-kiwi-env'] = False |
| 100 |
self.task.command_args['--print-xml'] = False |
| 101 |
self.task.command_args['--print-yaml'] = False |
| 102 |
self.task.command_args['--print-toml'] = False |
| 103 |
|
| 104 |
@patch('kiwi.tasks.image_info.DataOutput') |
| 105 |
def test_process_image_info(self, mock_out): |
| 106 |
self._init_command_args() |
| 107 |
self.task.command_args['info'] = True |
| 108 |
self.task.process() |
| 109 |
self.runtime_checker.check_repositories_configured.assert_called_once_with() |
| 110 |
mock_out.assert_called_once_with( |
| 111 |
{'image': 'LimeJeOS'} |
| 112 |
) |
| 113 |
|
| 114 |
@patch('kiwi.tasks.image_info.DataOutput') |
| 115 |
def test_process_image_info_color_output(self, mock_out): |
| 116 |
self._init_command_args() |
| 117 |
self.task.command_args['info'] = True |
| 118 |
self.task.global_args['--color-output'] = True |
| 119 |
self.task.process() |
| 120 |
mock_out.assert_called_once_with( |
| 121 |
{'image': 'LimeJeOS'}, style='color' |
| 122 |
) |
| 123 |
|
| 124 |
@patch('kiwi.tasks.image_info.DataOutput') |
| 125 |
def test_process_image_info_list_profiles(self, mock_out): |
| 126 |
self._init_command_args() |
| 127 |
self.task.command_args['info'] = True |
| 128 |
self.task.command_args['--list-profiles'] = True |
| 129 |
self.task.process() |
| 130 |
self.runtime_checker.check_repositories_configured.assert_called_once_with() |
| 131 |
mock_out.assert_called_once_with( |
| 132 |
{'image': 'LimeJeOS', 'profile_names': ['some']} |
| 133 |
) |
| 134 |
|
| 135 |
@patch('kiwi.tasks.image_info.DataOutput') |
| 136 |
def test_process_image_info_print_kiwi_env(self, mock_out): |
| 137 |
self._init_command_args() |
| 138 |
self.task.command_args['info'] = True |
| 139 |
self.task.command_args['--print-kiwi-env'] = True |
| 140 |
self.task.process() |
| 141 |
self.runtime_checker.check_repositories_configured.assert_called_once_with() |
| 142 |
|
| 143 |
mock_out.assert_called_once_with( |
| 144 |
{ |
| 145 |
'image': 'LimeJeOS', |
| 146 |
'kiwi_env': OrderedDict( |
| 147 |
[ |
| 148 |
('kiwi_align', '1048576'), |
| 149 |
('kiwi_boot_timeout', ''), |
| 150 |
('kiwi_bootkernel', ''), |
| 151 |
('kiwi_bootloader', 'grub2'), |
| 152 |
('kiwi_bootloader_console', 'default:default'), |
| 153 |
('kiwi_bootprofile', ''), |
| 154 |
('kiwi_btrfs_root_is_snapper_snapshot', ''), |
| 155 |
('kiwi_btrfs_root_is_snapshot', ''), |
| 156 |
('kiwi_cmdline', ''), |
| 157 |
('kiwi_compressed', ''), |
| 158 |
('kiwi_delete', ''), |
| 159 |
('kiwi_devicepersistency', ''), |
| 160 |
('kiwi_displayname', 'Bob'), |
| 161 |
('kiwi_drivers', ''), |
| 162 |
('kiwi_firmware', ''), |
| 163 |
('kiwi_fsmountoptions', ''), |
| 164 |
('kiwi_gpt_hybrid_mbr', ''), |
| 165 |
('kiwi_hybridpersistent', ''), |
| 166 |
('kiwi_hybridpersistent_filesystem', ''), |
| 167 |
('kiwi_iname', 'LimeJeOS'), |
| 168 |
('kiwi_initrd_system', 'dracut'), |
| 169 |
('kiwi_installboot', ''), |
| 170 |
('kiwi_iversion', '1.13.2'), |
| 171 |
('kiwi_keytable', 'us.map.gz'), |
| 172 |
('kiwi_language', 'en_US'), |
| 173 |
('kiwi_live_volid', 'CDROM'), |
| 174 |
('kiwi_loader_theme', 'openSUSE'), |
| 175 |
('kiwi_luks_empty_passphrase', 'false'), |
| 176 |
('kiwi_profiles', ''), |
| 177 |
('kiwi_ramonly', ''), |
| 178 |
('kiwi_revision', '$Format:%H$'), |
| 179 |
('kiwi_rootpartuuid', ''), |
| 180 |
('kiwi_sectorsize', '512'), |
| 181 |
('kiwi_shasum_suffix', '.sha256'), |
| 182 |
('kiwi_showlicense', ''), |
| 183 |
('kiwi_splash_theme', 'openSUSE'), |
| 184 |
('kiwi_startsector', '2048'), |
| 185 |
('kiwi_strip_delete', ''), |
| 186 |
('kiwi_strip_libs', ''), |
| 187 |
('kiwi_strip_tools', ''), |
| 188 |
('kiwi_target_blocksize', '512'), |
| 189 |
('kiwi_target_removable', ''), |
| 190 |
('kiwi_timezone', 'Europe/Berlin'), |
| 191 |
('kiwi_type', 'iso'), |
| 192 |
('kiwi_vga', 'auto'), |
| 193 |
('kiwi_wwid_wait_timeout', '') |
| 194 |
] |
| 195 |
) |
| 196 |
} |
| 197 |
) |
| 198 |
|
| 199 |
@patch('kiwi.tasks.image_info.DataOutput') |
| 200 |
@patch('kiwi.tasks.image_info.SolverRepository.new') |
| 201 |
@patch('kiwi.tasks.image_info.Uri') |
| 202 |
@patch('kiwi.tasks.image_info.SolverRepositoryBase.get_repo_type') |
| 203 |
def test_process_image_info_resolve_package_list( |
| 204 |
self, mock_get_repo_type, mock_uri, mock_solver_repo_new, mock_out |
| 205 |
): |
| 206 |
uri = Mock() |
| 207 |
uri.uri = 'http://example.org/some/path' |
| 208 |
mock_uri.return_value = uri |
| 209 |
mock_get_repo_type.return_value = 'apt-deb' |
| 210 |
self.solver.set_dist_type.return_value = { |
| 211 |
'arch': 'amd64' |
| 212 |
} |
| 213 |
self._init_command_args() |
| 214 |
self.task.command_args['info'] = True |
| 215 |
self.task.command_args['--resolve-package-list'] = True |
| 216 |
self.task.process() |
| 217 |
|
| 218 |
assert self.solver.add_repository.called |
| 219 |
assert mock_uri.call_args_list == [ |
| 220 |
call(uri='http://us.archive.ubuntu.com/ubuntu/', source_type=''), |
| 221 |
call( |
| 222 |
'http://us.archive.ubuntu.com/ubuntu/dists/focal/' |
| 223 |
'main/binary-amd64', 'apt-deb', '' |
| 224 |
), |
| 225 |
call( |
| 226 |
'http://us.archive.ubuntu.com/ubuntu/dists/focal/' |
| 227 |
'multiverse/binary-amd64', 'apt-deb', '' |
| 228 |
), |
| 229 |
call( |
| 230 |
'http://us.archive.ubuntu.com/ubuntu/dists/focal/' |
| 231 |
'restricted/binary-amd64', 'apt-deb', '' |
| 232 |
), |
| 233 |
call( |
| 234 |
'http://us.archive.ubuntu.com/ubuntu/dists/focal/' |
| 235 |
'universe/binary-amd64', 'apt-deb', '' |
| 236 |
), |
| 237 |
call('obs://Devel:PubCloud:AmazonEC2/SLE_12_GA', 'rpm-md', '') |
| 238 |
] |
| 239 |
mock_out.assert_called_once_with(self.result_info) |
| 240 |
|
| 241 |
@patch('kiwi.xml_state.XMLState.add_repository') |
| 242 |
@patch('kiwi.tasks.image_info.DataOutput') |
| 243 |
@patch('os.path.isfile') |
| 244 |
def test_process_image_info_add_repo( |
| 245 |
self, mock_os_path_is_file, mock_out, mock_add_repo |
| 246 |
): |
| 247 |
self._init_command_args() |
| 248 |
mock_os_path_is_file.return_value = False |
| 249 |
self.task.command_args['--add-repo'] = [ |
| 250 |
'http://example1.com,rpm-md,alias,99,true', |
| 251 |
'http://example2.com,rpm-md,alias,99,false,true', |
| 252 |
'http://example3.com,rpm-md,alias,99,false,true', |
| 253 |
'http://example4.com,,alias,99,false,true', |
| 254 |
'http://example5.com,,,99,false,true' |
| 255 |
] |
| 256 |
self.task.process() |
| 257 |
assert mock_add_repo.call_args_list == [ |
| 258 |
call( |
| 259 |
'http://example1.com', 'rpm-md', 'alias', '99', |
| 260 |
True, None, [], None, None, None, None |
| 261 |
), |
| 262 |
call( |
| 263 |
'http://example2.com', 'rpm-md', 'alias', '99', |
| 264 |
False, True, [], None, None, None, None |
| 265 |
), |
| 266 |
call( |
| 267 |
'http://example3.com', 'rpm-md', 'alias', '99', |
| 268 |
False, True, [], None, None, None, None |
| 269 |
), |
| 270 |
call( |
| 271 |
'http://example4.com', None, 'alias', '99', |
| 272 |
False, True, [], None, None, None, None |
| 273 |
), |
| 274 |
call( |
| 275 |
'http://example5.com', None, None, '99', |
| 276 |
False, True, [], None, None, None, None |
| 277 |
) |
| 278 |
] |
| 279 |
self.task.command_args['--add-repo-credentials'] = [ |
| 280 |
'user1:pass1', |
| 281 |
'user2:pass2' |
| 282 |
] |
| 283 |
mock_add_repo.reset_mock() |
| 284 |
self.task.process() |
| 285 |
assert mock_add_repo.call_args_list == [ |
| 286 |
call( |
| 287 |
'http://user1:[email protected]', 'rpm-md', 'alias', '99', |
| 288 |
True, None, [], None, None, None, None |
| 289 |
), |
| 290 |
call( |
| 291 |
'http://user2:[email protected]', 'rpm-md', 'alias', '99', |
| 292 |
False, True, [], None, None, None, None |
| 293 |
), |
| 294 |
call( |
| 295 |
'http://example3.com', 'rpm-md', 'alias', '99', |
| 296 |
False, True, [], None, None, None, None |
| 297 |
), |
| 298 |
call( |
| 299 |
'http://example4.com', None, 'alias', '99', |
| 300 |
False, True, [], None, None, None, None |
| 301 |
), |
| 302 |
call( |
| 303 |
'http://example5.com', None, None, '99', |
| 304 |
False, True, [], None, None, None, None |
| 305 |
) |
| 306 |
] |
| 307 |
self.task.command_args['--add-repo'] = [ |
| 308 |
'http://example1.com,rpm-md,alias,99,true' |
| 309 |
] |
| 310 |
self.task.command_args['--add-repo-credentials'] = [ |
| 311 |
'../data/credentials' |
| 312 |
] |
| 313 |
mock_os_path_is_file.return_value = True |
| 314 |
mock_add_repo.reset_mock() |
| 315 |
self.task.process() |
| 316 |
mock_add_repo.assert_called_once_with( |
| 317 |
'http://user:[email protected]', 'rpm-md', 'alias', '99', |
| 318 |
True, None, [], None, None, None, None |
| 319 |
) |
| 320 |
|
| 321 |
@patch('kiwi.xml_state.XMLState.delete_repository_sections') |
| 322 |
@patch('kiwi.tasks.image_info.DataOutput') |
| 323 |
def test_process_image_info_delete_repos(self, mock_out, mock_delete_repos): |
| 324 |
self._init_command_args() |
| 325 |
self.task.command_args['--ignore-repos'] = True |
| 326 |
self.task.process() |
| 327 |
mock_delete_repos.assert_called_once_with() |
| 328 |
|
| 329 |
def test_process_image_info_help(self): |
| 330 |
self._init_command_args() |
| 331 |
self.task.command_args['help'] = True |
| 332 |
self.task.command_args['info'] = True |
| 333 |
self.task.process() |
| 334 |
self.task.manual.show.assert_called_once_with( |
| 335 |
'kiwi::image::info' |
| 336 |
) |
| 337 |
|
| 338 |
@patch('kiwi.tasks.image_info.DataOutput') |
| 339 |
def test_process_image_info_print_xml(self, mock_out): |
| 340 |
self._init_command_args() |
| 341 |
self.task.command_args['--print-xml'] = True |
| 342 |
self.task.process() |
| 343 |
tmpfile, message = mock_out.display_file.call_args.args |
| 344 |
assert tmpfile.startswith('/var/tmp/kiwi_xslt-') |
| 345 |
assert message == 'Description(XML):' |
| 346 |
|
| 347 |
@patch('kiwi.tasks.image_info.DataOutput') |
| 348 |
def test_process_image_info_print_yaml(self, mock_out): |
| 349 |
self._init_command_args() |
| 350 |
self.task.command_args['--print-yaml'] = True |
| 351 |
self.task.process() |
| 352 |
tmpfile, message = mock_out.display_file.call_args.args |
| 353 |
assert tmpfile.startswith('/var/tmp/kiwi_xslt-') |
| 354 |
assert message == 'Description(YAML):' |
| 355 |
|
| 356 |
@patch('kiwi.tasks.image_info.DataOutput') |
| 357 |
def test_process_image_info_print_toml(self, mock_out): |
| 358 |
self._init_command_args() |
| 359 |
self.task.command_args['--print-toml'] = True |
| 360 |
self.task.process() |
| 361 |
tmpfile, message = mock_out.display_file.call_args.args |
| 362 |
assert tmpfile.startswith('/var/tmp/kiwi_xslt-') |
| 363 |
assert message == 'Description(TOML):' |