| 1 |
import sys |
| 2 |
from unittest.mock import ( |
| 3 |
patch, call, Mock, MagicMock |
| 4 |
) |
| 5 |
from pytest import raises |
| 6 |
|
| 7 |
import kiwi |
| 8 |
|
| 9 |
from ...test_helper import argv_kiwi_tests |
| 10 |
|
| 11 |
from kiwi.defaults import Defaults |
| 12 |
from kiwi.boot.image.builtin_kiwi import BootImageKiwi |
| 13 |
from kiwi.xml_description import XMLDescription |
| 14 |
from kiwi.xml_state import XMLState |
| 15 |
|
| 16 |
from kiwi.exceptions import KiwiConfigFileNotFound |
| 17 |
|
| 18 |
|
| 19 |
class TestBootImageKiwi: |
| 20 |
@patch('kiwi.boot.image.builtin_kiwi.Temporary') |
| 21 |
@patch('kiwi.boot.image.builtin_kiwi.os.path.exists') |
| 22 |
@patch('kiwi.defaults.Defaults.get_boot_image_description_path') |
| 23 |
def setup(self, mock_boot_path, mock_exists, mock_Temporary): |
| 24 |
mock_Temporary.return_value.new_dir.return_value.name = \ |
| 25 |
'boot-root-directory' |
| 26 |
mock_boot_path.return_value = '../data' |
| 27 |
Defaults.set_platform_name('x86_64') |
| 28 |
mock_exists.return_value = True |
| 29 |
description = XMLDescription('../data/example_config.xml') |
| 30 |
self.xml_state = XMLState( |
| 31 |
description.load() |
| 32 |
) |
| 33 |
self.setup = Mock() |
| 34 |
self.profile = Mock() |
| 35 |
self.profile.dot_profile = dict() |
| 36 |
kiwi.boot.image.builtin_kiwi.SystemSetup = Mock( |
| 37 |
return_value=self.setup |
| 38 |
) |
| 39 |
kiwi.boot.image.builtin_kiwi.Profile = Mock( |
| 40 |
return_value=self.profile |
| 41 |
) |
| 42 |
self.boot_image = BootImageKiwi( |
| 43 |
self.xml_state, 'some-target-dir' |
| 44 |
) |
| 45 |
|
| 46 |
@patch('kiwi.boot.image.builtin_kiwi.Temporary') |
| 47 |
@patch('kiwi.boot.image.builtin_kiwi.os.path.exists') |
| 48 |
@patch('kiwi.defaults.Defaults.get_boot_image_description_path') |
| 49 |
def setup_method(self, cls, mock_boot_path, mock_exists, mock_Temporary): |
| 50 |
self.setup() |
| 51 |
|
| 52 |
def test_include_file(self): |
| 53 |
# is a nop for builtin kiwi initrd and does nothing |
| 54 |
self.boot_image.include_file('/root/a') |
| 55 |
|
| 56 |
@patch('kiwi.defaults.Defaults.get_boot_image_description_path') |
| 57 |
@patch('kiwi.boot.image.builtin_kiwi.Temporary') |
| 58 |
@patch('kiwi.boot.image.builtin_kiwi.SystemPrepare') |
| 59 |
def test_prepare(self, mock_SystemPrepare, mock_Temporary, mock_boot_path): |
| 60 |
system_prepare = Mock() |
| 61 |
manager = MagicMock() |
| 62 |
system_prepare.setup_repositories = Mock( |
| 63 |
return_value=manager |
| 64 |
) |
| 65 |
mock_SystemPrepare.return_value.__enter__.return_value = system_prepare |
| 66 |
mock_Temporary.return_value.new_dir.return_value.name = \ |
| 67 |
'boot-root-directory' |
| 68 |
mock_boot_path.return_value = '../data' |
| 69 |
self.boot_image.prepare() |
| 70 |
system_prepare.setup_repositories.assert_called_once_with( |
| 71 |
signing_keys=None, target_arch=None |
| 72 |
) |
| 73 |
system_prepare.install_bootstrap.assert_called_once_with( |
| 74 |
manager.__enter__.return_value |
| 75 |
) |
| 76 |
system_prepare.install_system.assert_called_once_with( |
| 77 |
manager.__enter__.return_value |
| 78 |
) |
| 79 |
assert self.profile.add.call_args_list[0] == call( |
| 80 |
'kiwi_initrdname', 'initrd-oemboot-suse-13.2' |
| 81 |
) |
| 82 |
self.profile.create.assert_called_once_with( |
| 83 |
'boot-root-directory/.profile' |
| 84 |
) |
| 85 |
self.setup.import_description.assert_called_once_with() |
| 86 |
self.setup.import_overlay_files.assert_called_once_with( |
| 87 |
follow_links=True |
| 88 |
) |
| 89 |
self.setup.call_config_script.assert_called_once_with() |
| 90 |
system_prepare.pinch_system.assert_called_once_with( |
| 91 |
manager=manager.__enter__.return_value, force=True |
| 92 |
) |
| 93 |
self.setup.call_image_script.assert_called_once_with() |
| 94 |
|
| 95 |
@patch('os.path.exists') |
| 96 |
@patch('kiwi.boot.image.builtin_kiwi.Temporary') |
| 97 |
def test_prepare_no_boot_description_found( |
| 98 |
self, mock_Temporary, mock_os_path |
| 99 |
): |
| 100 |
mock_Temporary.return_value.new_dir.return_value.name = \ |
| 101 |
'boot-root-directory' |
| 102 |
mock_os_path.return_value = False |
| 103 |
with raises(KiwiConfigFileNotFound): |
| 104 |
self.boot_image.post_init() |
| 105 |
|
| 106 |
@patch('kiwi.boot.image.builtin_kiwi.ArchiveCpio') |
| 107 |
@patch('kiwi.boot.image.builtin_kiwi.Compress') |
| 108 |
@patch('kiwi.boot.image.builtin_kiwi.Path.create') |
| 109 |
@patch('kiwi.boot.image.builtin_kiwi.Path.wipe') |
| 110 |
@patch('kiwi.boot.image.builtin_kiwi.DataSync') |
| 111 |
@patch('kiwi.boot.image.base.BootImageBase.is_prepared') |
| 112 |
@patch('kiwi.boot.image.builtin_kiwi.os.chmod') |
| 113 |
@patch('kiwi.boot.image.builtin_kiwi.Temporary') |
| 114 |
def test_create_initrd( |
| 115 |
self, mock_Temporary, mock_os_chmod, |
| 116 |
mock_prepared, mock_sync, mock_wipe, mock_create, |
| 117 |
mock_compress, mock_cpio |
| 118 |
): |
| 119 |
data = Mock() |
| 120 |
mock_sync.return_value = data |
| 121 |
mock_Temporary.return_value.new_dir.return_value.name = \ |
| 122 |
'temp-boot-directory' |
| 123 |
mock_prepared.return_value = True |
| 124 |
self.boot_image.boot_root_directory = 'boot-root-directory' |
| 125 |
mbrid = Mock() |
| 126 |
mbrid.write = Mock() |
| 127 |
cpio = Mock() |
| 128 |
compress = Mock() |
| 129 |
mock_cpio.return_value = cpio |
| 130 |
mock_compress.return_value = compress |
| 131 |
self.boot_image.create_initrd(mbrid) |
| 132 |
mock_sync.assert_called_once_with( |
| 133 |
'boot-root-directory/', 'temp-boot-directory' |
| 134 |
) |
| 135 |
mock_os_chmod.assert_called_once_with( |
| 136 |
'temp-boot-directory', 0o755 |
| 137 |
) |
| 138 |
data.sync_data.assert_called_once_with(options=['--archive', '--hard-links', '--xattrs', '--acls', '--one-file-system', '--inplace']) |
| 139 |
mock_cpio.assert_called_once_with( |
| 140 |
''.join( |
| 141 |
[ |
| 142 |
self.boot_image.target_dir, |
| 143 |
'/LimeJeOS.x86_64-1.13.2.initrd' |
| 144 |
] |
| 145 |
) |
| 146 |
) |
| 147 |
mock_compress.assert_called_once_with( |
| 148 |
''.join( |
| 149 |
[ |
| 150 |
self.boot_image.target_dir, |
| 151 |
'/LimeJeOS.x86_64-1.13.2.initrd' |
| 152 |
] |
| 153 |
) |
| 154 |
) |
| 155 |
mock_wipe.assert_called_once_with( |
| 156 |
'temp-boot-directory/boot' |
| 157 |
) |
| 158 |
mock_create.assert_called_once_with( |
| 159 |
'temp-boot-directory/boot' |
| 160 |
) |
| 161 |
mbrid.write.assert_called_once_with( |
| 162 |
'temp-boot-directory/boot/mbrid' |
| 163 |
) |
| 164 |
cpio.create.assert_called_once_with( |
| 165 |
source_dir='temp-boot-directory', |
| 166 |
exclude=[ |
| 167 |
'/var/cache/kiwi', '/image', '/usr/lib/grub*', |
| 168 |
'/usr/share/doc', '/usr/share/man', '/home', '/media', '/srv' |
| 169 |
] |
| 170 |
) |
| 171 |
compress.xz.assert_called_once_with( |
| 172 |
['--check=crc32', '--lzma2=dict=1MiB', '--threads=0'] |
| 173 |
) |
| 174 |
mock_cpio.reset_mock() |
| 175 |
mock_compress.reset_mock() |
| 176 |
self.boot_image.create_initrd(mbrid, 'foo') |
| 177 |
mock_cpio.assert_called_once_with( |
| 178 |
self.boot_image.target_dir + '/foo' |
| 179 |
) |
| 180 |
mock_compress.assert_called_once_with( |
| 181 |
self.boot_image.target_dir + '/foo' |
| 182 |
) |
| 183 |
|
| 184 |
@patch('kiwi.boot.image.builtin_kiwi.Path.wipe') |
| 185 |
@patch('os.path.exists') |
| 186 |
def test_cleanup(self, mock_path, mock_wipe): |
| 187 |
mock_path.return_value = True |
| 188 |
self.boot_image.temp_directories.append('boot-root-directory') |
| 189 |
self.boot_image.cleanup() |
| 190 |
mock_wipe.assert_called_once_with('boot-root-directory') |
| 191 |
|
| 192 |
def test_has_initrd_support(self): |
| 193 |
assert self.boot_image.has_initrd_support() is True |
| 194 |
|
| 195 |
def teardown(self): |
| 196 |
sys.argv = argv_kiwi_tests |
| 197 |
|
| 198 |
def teardown_method(self, cls): |
| 199 |
self.teardown() |