| 1 |
import logging |
| 2 |
from unittest.mock import ( |
| 3 |
patch, call |
| 4 |
) |
| 5 |
import sys |
| 6 |
from unittest.mock import MagicMock |
| 7 |
from pytest import ( |
| 8 |
fixture, raises |
| 9 |
) |
| 10 |
|
| 11 |
from .test_helper import argv_kiwi_tests |
| 12 |
|
| 13 |
from kiwi.defaults import Defaults |
| 14 |
from kiwi.defaults import grub_loader_type |
| 15 |
from kiwi.defaults import shim_loader_type |
| 16 |
|
| 17 |
from kiwi.exceptions import KiwiBootLoaderGrubDataError |
| 18 |
|
| 19 |
|
| 20 |
class TestDefaults: |
| 21 |
@fixture(autouse=True) |
| 22 |
def inject_fixtures(self, caplog): |
| 23 |
self._caplog = caplog |
| 24 |
|
| 25 |
def setup(self): |
| 26 |
self.defaults = Defaults() |
| 27 |
|
| 28 |
def setup_method(self, cls): |
| 29 |
self.setup() |
| 30 |
|
| 31 |
def teardown(self): |
| 32 |
sys.argv = argv_kiwi_tests |
| 33 |
|
| 34 |
def teardown_method(self, cls): |
| 35 |
self.teardown() |
| 36 |
|
| 37 |
def test_get(self): |
| 38 |
assert self.defaults.get('kiwi_align') == 1048576 |
| 39 |
assert self.defaults.get('kiwi_startsector') == 2048 |
| 40 |
assert self.defaults.get('kiwi_sectorsize') == 512 |
| 41 |
assert self.defaults.get('kiwi_inode_size') == 256 |
| 42 |
assert self.defaults.get('kiwi_inode_ratio') == 16384 |
| 43 |
assert self.defaults.get('kiwi_min_inodes') == 20000 |
| 44 |
assert self.defaults.get('kiwi_revision') |
| 45 |
|
| 46 |
def test_to_profile(self): |
| 47 |
profile = MagicMock() |
| 48 |
self.defaults.to_profile(profile) |
| 49 |
profile.add.assert_any_call('kiwi_align', 1048576) |
| 50 |
profile.add.assert_any_call('kiwi_startsector', 2048) |
| 51 |
profile.add.assert_any_call('kiwi_sectorsize', 512) |
| 52 |
profile.add.assert_any_call( |
| 53 |
'kiwi_revision', self.defaults.get('kiwi_revision') |
| 54 |
) |
| 55 |
|
| 56 |
def test_get_preparer(self): |
| 57 |
assert Defaults.get_preparer() == \ |
| 58 |
'KIWI - https://github.com/OSInside/kiwi' |
| 59 |
|
| 60 |
def test_get_publisher(self): |
| 61 |
assert Defaults.get_publisher() == 'SUSE LINUX GmbH' |
| 62 |
|
| 63 |
def test_get_default_shared_cache_location(self): |
| 64 |
assert Defaults.get_shared_cache_location() == 'var/cache/kiwi' |
| 65 |
|
| 66 |
@patch('kiwi.defaults.Path.which') |
| 67 |
def test_get_grub_boot_directory_name(self, mock_which): |
| 68 |
mock_which.return_value = 'grub2-install-was-found' |
| 69 |
assert Defaults.get_grub_boot_directory_name( |
| 70 |
lookup_path='lookup_path' |
| 71 |
) == 'grub2' |
| 72 |
mock_which.return_value = None |
| 73 |
assert Defaults.get_grub_boot_directory_name( |
| 74 |
lookup_path='lookup_path' |
| 75 |
) == 'grub' |
| 76 |
|
| 77 |
def test_get_live_dracut_modules_from_flag(self): |
| 78 |
assert Defaults.get_live_dracut_modules_from_flag('foo') == \ |
| 79 |
['kiwi-live'] |
| 80 |
assert Defaults.get_live_dracut_modules_from_flag('overlay') == \ |
| 81 |
['kiwi-live'] |
| 82 |
assert Defaults.get_live_dracut_modules_from_flag('dmsquash') == \ |
| 83 |
['dmsquash-live', 'livenet'] |
| 84 |
|
| 85 |
def test_get_iso_boot_path(self): |
| 86 |
Defaults.set_platform_name('i686') |
| 87 |
assert Defaults.get_iso_boot_path() == 'boot/ix86' |
| 88 |
Defaults.set_platform_name('x86_64') |
| 89 |
assert Defaults.get_iso_boot_path() == 'boot/x86_64' |
| 90 |
|
| 91 |
@patch('kiwi.defaults.glob.iglob') |
| 92 |
def test_get_unsigned_grub_loader(self, mock_glob): |
| 93 |
mock_glob.return_value = ['/usr/share/grub2/x86_64-efi/grub.efi'] |
| 94 |
assert mock_glob.return_value[0] in \ |
| 95 |
Defaults.get_unsigned_grub_loader('root')[0].filename |
| 96 |
assert mock_glob.call_args_list == [ |
| 97 |
call('root/usr/share/grub*/x86_64-efi/grub.efi'), |
| 98 |
call('root/usr/lib/grub*/x86_64-efi/grub.efi'), |
| 99 |
call('root/boot/efi/EFI/*/grubx64.efi'), |
| 100 |
call('root/boot/efi/EFI/*/grubia32.efi'), |
| 101 |
call('root/boot/efi/EFI/*/grubaa64.efi'), |
| 102 |
call('root/boot/efi/EFI/*/grubriscv64.efi') |
| 103 |
] |
| 104 |
mock_glob.reset_mock() |
| 105 |
mock_glob.return_value = [] |
| 106 |
assert Defaults.get_unsigned_grub_loader('root') == [] |
| 107 |
|
| 108 |
def test_is_x86_arch(self): |
| 109 |
assert Defaults.is_x86_arch('x86_64') is True |
| 110 |
assert Defaults.is_x86_arch('aarch64') is False |
| 111 |
|
| 112 |
@patch('os.path.exists') |
| 113 |
def test_get_vendor_grubenv(self, mock_path_exists): |
| 114 |
mock_path_exists.return_value = True |
| 115 |
assert Defaults.get_vendor_grubenv('boot/efi') == \ |
| 116 |
'boot/efi/EFI/fedora/grubenv' |
| 117 |
|
| 118 |
def test_parse_exclude_file_is_valid(self): |
| 119 |
assert Defaults._parse_exclude_file( |
| 120 |
'../data/root-dir', 'exclude_files.yaml' |
| 121 |
) == [ |
| 122 |
'usr/bin/qemu-binfmt', |
| 123 |
'usr/bin/qemu-x86_64-binfmt', |
| 124 |
'usr/bin/qemu-x86_64' |
| 125 |
] |
| 126 |
|
| 127 |
@patch('yaml.safe_load') |
| 128 |
def test_parse_exclude_file_is_invalid(self, mock_yaml_safe_load): |
| 129 |
mock_yaml_safe_load.return_value = {'invalid': 'artificial'} |
| 130 |
with self._caplog.at_level(logging.WARNING): |
| 131 |
assert Defaults._parse_exclude_file( |
| 132 |
'../data/root-dir', 'exclude_files.yaml' |
| 133 |
) == [] |
| 134 |
|
| 135 |
def test_get_exclude_list_from_custom_exclude_files(self): |
| 136 |
assert Defaults.get_exclude_list_from_custom_exclude_files( |
| 137 |
'../data/root-dir' |
| 138 |
) == [ |
| 139 |
'usr/bin/qemu-binfmt', |
| 140 |
'usr/bin/qemu-x86_64-binfmt', |
| 141 |
'usr/bin/qemu-x86_64' |
| 142 |
] |
| 143 |
|
| 144 |
@patch('yaml.safe_load') |
| 145 |
def test_get_exclude_list_from_custom_exclude_files_is_invalid( |
| 146 |
self, mock_yaml_safe_load |
| 147 |
): |
| 148 |
mock_yaml_safe_load.return_value = {'invalid': 'artificial'} |
| 149 |
with self._caplog.at_level(logging.WARNING): |
| 150 |
assert Defaults.get_exclude_list_from_custom_exclude_files( |
| 151 |
'../data/root-dir' |
| 152 |
) == [] |
| 153 |
|
| 154 |
def test_get_exclude_list_from_custom_exclude_files_for_efifatimage(self): |
| 155 |
assert Defaults.get_exclude_list_from_custom_exclude_files_for_efifatimage( |
| 156 |
'../data/root-dir' |
| 157 |
) == [ |
| 158 |
'BOOT/fbia32.efi', |
| 159 |
'BOOT/fbx64.efi', |
| 160 |
] |
| 161 |
|
| 162 |
@patch('yaml.safe_load') |
| 163 |
def test_get_exclude_list_from_custom_exclude_files_for_efifatimage_is_invalid( |
| 164 |
self, mock_yaml_safe_load |
| 165 |
): |
| 166 |
mock_yaml_safe_load.return_value = {'invalid': 'artificial'} |
| 167 |
with self._caplog.at_level(logging.WARNING): |
| 168 |
assert Defaults.get_exclude_list_from_custom_exclude_files_for_efifatimage( |
| 169 |
'../data/root-dir' |
| 170 |
) == [] |
| 171 |
|
| 172 |
def test_get_exclude_list_for_root_data_sync(self): |
| 173 |
assert Defaults.get_exclude_list_for_root_data_sync() == [ |
| 174 |
'image', '.kconfig', |
| 175 |
'run/*', 'tmp/*', |
| 176 |
'.buildenv', 'var/cache/kiwi' |
| 177 |
] |
| 178 |
assert Defaults.get_exclude_list_for_root_data_sync(no_tmpdirs=False) == [ |
| 179 |
'image', '.kconfig', |
| 180 |
'.buildenv', 'var/cache/kiwi' |
| 181 |
] |
| 182 |
|
| 183 |
@patch('glob.iglob') |
| 184 |
def test_get_signed_grub_loader(self, mock_iglob): |
| 185 |
def iglob_no_matches(pattern): |
| 186 |
return [] |
| 187 |
|
| 188 |
def iglob_simple_match(pattern): |
| 189 |
if '/boot/efi/EFI/*/grubx64.efi' in pattern: |
| 190 |
return ['root_path/boot/efi/EFI/BOOT/grubx64.efi'] |
| 191 |
else: |
| 192 |
return [] |
| 193 |
|
| 194 |
def iglob_custom_binary_match(pattern): |
| 195 |
if '/usr/lib/grub/x86_64-efi-signed/grubx64.efi.signed' in pattern: |
| 196 |
return [ |
| 197 |
'root_path' |
| 198 |
'/usr/lib/grub/x86_64-efi-signed/grubx64.efi.signed' |
| 199 |
] |
| 200 |
else: |
| 201 |
return [] |
| 202 |
|
| 203 |
mock_iglob.side_effect = iglob_no_matches |
| 204 |
assert Defaults.get_signed_grub_loader('root_path') == [] |
| 205 |
|
| 206 |
mock_iglob.side_effect = iglob_simple_match |
| 207 |
assert Defaults.get_signed_grub_loader('root_path') == [ |
| 208 |
grub_loader_type( |
| 209 |
filename='root_path/boot/efi/EFI/BOOT/grubx64.efi', |
| 210 |
binaryname='grubx64.efi', |
| 211 |
targetname='bootx64.efi' |
| 212 |
) |
| 213 |
] |
| 214 |
|
| 215 |
mock_iglob.side_effect = iglob_custom_binary_match |
| 216 |
assert Defaults.get_signed_grub_loader('root_path') == [ |
| 217 |
grub_loader_type( |
| 218 |
filename='root_path' |
| 219 |
'/usr/lib/grub/x86_64-efi-signed/grubx64.efi.signed', |
| 220 |
binaryname='grubx64.efi', |
| 221 |
targetname='bootx64.efi' |
| 222 |
) |
| 223 |
] |
| 224 |
|
| 225 |
@patch('glob.iglob') |
| 226 |
def test_get_mok_manager(self, mock_iglob): |
| 227 |
mock_iglob.return_value = [] |
| 228 |
assert Defaults.get_mok_manager('root_path') == [] |
| 229 |
|
| 230 |
def iglob_match(pattern): |
| 231 |
if pattern == 'root_path/boot/efi/EFI/*/mm*.efi': |
| 232 |
return ['some'] |
| 233 |
return [] |
| 234 |
|
| 235 |
mock_iglob.side_effect = iglob_match |
| 236 |
|
| 237 |
assert Defaults.get_mok_manager('root_path') == ['some'] |
| 238 |
|
| 239 |
@patch('glob.iglob') |
| 240 |
def test_get_shim_loader(self, mock_iglob): |
| 241 |
def iglob_simple_match(pattern): |
| 242 |
if '/usr/lib64/efi/shim.efi' in pattern: |
| 243 |
return ['root_path/usr/lib64/efi/shim.efi'] |
| 244 |
else: |
| 245 |
return [] |
| 246 |
|
| 247 |
def iglob_custom_binary_match(pattern): |
| 248 |
if '/usr/lib/shim/shim*.efi.signed.latest' in pattern: |
| 249 |
return [ |
| 250 |
'root_path' |
| 251 |
'/usr/lib/shim/shimx64.efi.signed.latest' |
| 252 |
] |
| 253 |
else: |
| 254 |
return [] |
| 255 |
|
| 256 |
assert Defaults.get_shim_loader('root_path') == [] |
| 257 |
|
| 258 |
mock_iglob.side_effect = iglob_simple_match |
| 259 |
assert Defaults.get_shim_loader('root_path') == [ |
| 260 |
shim_loader_type( |
| 261 |
filename='root_path' |
| 262 |
'/usr/lib64/efi/shim.efi', |
| 263 |
binaryname='bootx64.efi' |
| 264 |
) |
| 265 |
] |
| 266 |
|
| 267 |
mock_iglob.side_effect = iglob_custom_binary_match |
| 268 |
assert Defaults.get_shim_loader('root_path') == [ |
| 269 |
shim_loader_type( |
| 270 |
filename='root_path' |
| 271 |
'/usr/lib/shim/shimx64.efi.signed.latest', |
| 272 |
binaryname='bootx64.efi' |
| 273 |
) |
| 274 |
] |
| 275 |
|
| 276 |
@patch('os.path.exists') |
| 277 |
def test_get_snapper_config_template_file(self, mock_os_path_exists): |
| 278 |
mock_os_path_exists.return_value = False |
| 279 |
assert Defaults.get_snapper_config_template_file('root') == '' |
| 280 |
mock_os_path_exists.return_value = True |
| 281 |
assert Defaults.get_snapper_config_template_file('root') == \ |
| 282 |
'root/etc/snapper/config-templates/default' |
| 283 |
|
| 284 |
def test_get_min_volume_mbytes(self): |
| 285 |
assert Defaults.get_min_volume_mbytes('btrfs') == 120 |
| 286 |
assert Defaults.get_min_volume_mbytes('xfs') == 300 |
| 287 |
assert Defaults.get_min_volume_mbytes('some') == 30 |
| 288 |
|
| 289 |
@patch('glob.iglob') |
| 290 |
def test_get_grub_chrp_loader(self, mock_glob_iglob): |
| 291 |
mock_glob_iglob.return_value = [] |
| 292 |
with raises(KiwiBootLoaderGrubDataError): |
| 293 |
Defaults.get_grub_chrp_loader('some-boot') |
| 294 |
|
| 295 |
mock_glob_iglob.reset_mock() |
| 296 |
mock_glob_iglob.return_value = [ |
| 297 |
'/some-boot/boot/grub2/powerpc-ieee1275/grub.elf' |
| 298 |
] |
| 299 |
assert Defaults.get_grub_chrp_loader('some-boot') == 'grub.elf' |
| 300 |
mock_glob_iglob.assert_called_once_with( |
| 301 |
'some-boot/boot/grub*/powerpc-ieee1275/grub.elf' |
| 302 |
) |
| 303 |
|
| 304 |
@patch('os.path.exists') |
| 305 |
def test_get_grub_path(self, mock_os_path_exists): |
| 306 |
mock_os_path_exists.return_value = False |
| 307 |
assert Defaults.get_grub_path( |
| 308 |
'root', 'some_file_to_search', False |
| 309 |
) == '' |