1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212
|
import sys
from unittest.mock import (
patch, Mock
)
from pytest import raises
from ..test_helper import argv_kiwi_tests
import kiwi
from kiwi.defaults import Defaults
from kiwi.exceptions import KiwiFileSystemSetupError
from kiwi.builder.filesystem import FileSystemBuilder
class TestFileSystemBuilder:
@patch('kiwi.builder.filesystem.FileSystemSetup')
def setup(self, mock_fs_setup):
Defaults.set_platform_name('x86_64')
self.filesystem = Mock()
self.filesystem.create_on_device = Mock()
self.filesystem.create_on_file = Mock()
self.filesystem.sync_data = Mock()
self.xml_state = Mock()
self.xml_state.profiles = None
self.xml_state.get_build_type_unpartitioned_bytes = Mock(
return_value=0
)
self.xml_state.get_fs_mount_option_list = Mock(
return_value=['async']
)
self.xml_state.get_fs_create_option_list = Mock(
return_value=['-O', 'option']
)
self.xml_state.get_build_type_name = Mock(
return_value='ext3'
)
self.xml_state.get_image_version = Mock(
return_value='1.2.3'
)
self.xml_state.xml_data.get_name = Mock(
return_value='myimage'
)
self.xml_state.build_type.get_target_blocksize = Mock(
return_value=4096
)
self.xml_state.build_type.get_squashfscompression = Mock(
return_value='gzip'
)
self.xml_state.build_type.get_erofscompression = Mock(
return_value='zstd,level=12'
)
self.fs_setup = Mock()
self.fs_setup.get_size_mbytes = Mock(
return_value=42
)
self.setup = Mock()
kiwi.builder.filesystem.SystemSetup = Mock(
return_value=self.setup
)
@patch('kiwi.builder.filesystem.FileSystemSetup')
def setup_method(self, cls, mock_fs_setup):
self.setup()
def test_create_unknown_filesystem(self):
self.xml_state.get_build_type_name = Mock(
return_value='super-fs'
)
fs = FileSystemBuilder(
self.xml_state, 'target_dir', 'root_dir'
)
with raises(KiwiFileSystemSetupError):
fs.create()
def test_no_filesystem_configured(self):
self.xml_state.get_build_type_name = Mock(
return_value='pxe'
)
self.xml_state.build_type.get_filesystem = Mock(
return_value=None
)
with raises(KiwiFileSystemSetupError):
FileSystemBuilder(
self.xml_state, 'target_dir', 'root_dir'
)
@patch('kiwi.builder.filesystem.LoopDevice')
@patch('kiwi.builder.filesystem.FileSystem.new')
@patch('kiwi.builder.filesystem.FileSystemSetup')
def test_create_on_loop(
self, mock_fs_setup, mock_fs, mock_LoopDevice
):
Defaults.set_platform_name('x86_64')
mock_fs_setup.return_value = self.fs_setup
mock_fs.return_value.__enter__.return_value = self.filesystem
loop_provider = Mock()
loop_provider.get_device = Mock(
return_value='/dev/loop1'
)
mock_LoopDevice.return_value.__enter__.return_value = loop_provider
fs = FileSystemBuilder(
self.xml_state, 'target_dir', 'root_dir'
)
fs.create()
mock_LoopDevice.assert_called_once_with(
'target_dir/myimage.x86_64-1.2.3.ext3', 42, 4096
)
loop_provider.create.assert_called_once_with()
mock_fs.assert_called_once_with(
'ext3', loop_provider, 'root_dir/', {
'mount_options': ['async'],
'create_options': ['-O', 'option']
}
)
self.filesystem.create_on_device.assert_called_once_with(None)
self.filesystem.sync_data.assert_called_once_with([
'image', '.kconfig', 'run/*', 'tmp/*',
'.buildenv', 'var/cache/kiwi'
])
self.setup.export_package_verification.assert_called_once_with(
'target_dir'
)
self.setup.export_package_list.assert_called_once_with(
'target_dir'
)
@patch('kiwi.builder.filesystem.FileSystem.new')
@patch('kiwi.builder.filesystem.DeviceProvider')
def test_create_on_file_erofs(
self, mock_provider, mock_fs
):
Defaults.set_platform_name('x86_64')
provider = Mock()
mock_provider.return_value = provider
mock_fs.return_value.__enter__.return_value = self.filesystem
self.xml_state.get_build_type_name = Mock(
return_value='erofs'
)
fs = FileSystemBuilder(
self.xml_state, 'target_dir', 'root_dir'
)
fs.create()
mock_fs.assert_called_once_with(
'erofs', provider, 'root_dir', {
'mount_options': ['async'],
'create_options': ['-O', 'option'],
'compression': 'zstd,level=12'
}
)
self.filesystem.create_on_file.assert_called_once_with(
'target_dir/myimage.x86_64-1.2.3.erofs', None,
[
'image', '.kconfig', 'run/*', 'tmp/*',
'.buildenv', 'var/cache/kiwi'
]
)
self.setup.export_package_verification.assert_called_once_with(
'target_dir'
)
self.setup.export_package_list.assert_called_once_with(
'target_dir'
)
@patch('kiwi.builder.filesystem.FileSystem.new')
@patch('kiwi.builder.filesystem.DeviceProvider')
def test_create_on_file_squashfs(
self, mock_provider, mock_fs
):
Defaults.set_platform_name('x86_64')
provider = Mock()
mock_provider.return_value = provider
mock_fs.return_value.__enter__.return_value = self.filesystem
self.xml_state.get_build_type_name = Mock(
return_value='squashfs'
)
fs = FileSystemBuilder(
self.xml_state, 'target_dir', 'root_dir'
)
fs.create()
mock_fs.assert_called_once_with(
'squashfs', provider, 'root_dir', {
'mount_options': ['async'],
'create_options': ['-O', 'option'],
'compression': 'gzip'
}
)
self.filesystem.create_on_file.assert_called_once_with(
'target_dir/myimage.x86_64-1.2.3.squashfs', None,
[
'image', '.kconfig', 'run/*', 'tmp/*',
'.buildenv', 'var/cache/kiwi'
]
)
self.setup.export_package_verification.assert_called_once_with(
'target_dir'
)
self.setup.export_package_list.assert_called_once_with(
'target_dir'
)
def teardown(self):
sys.argv = argv_kiwi_tests
def teardown_method(self, cls):
self.teardown()
|