| 1 |
import io |
| 2 |
from pytest import raises |
| 3 |
from unittest.mock import ( |
| 4 |
patch, Mock, MagicMock, call |
| 5 |
) |
| 6 |
|
| 7 |
from kiwi.container.appx import ContainerImageAppx |
| 8 |
from kiwi.exceptions import KiwiContainerSetupError |
| 9 |
|
| 10 |
|
| 11 |
class TestContainerImageAppx: |
| 12 |
@patch('kiwi.container.appx.RuntimeConfig') |
| 13 |
@patch('os.path.exists') |
| 14 |
def setup(self, mock_os_path_exists, mock_RuntimeConfig): |
| 15 |
mock_os_path_exists.return_value = True |
| 16 |
self.appx = ContainerImageAppx( |
| 17 |
'root_dir', { |
| 18 |
'metadata_path': 'meta/data' |
| 19 |
} |
| 20 |
) |
| 21 |
|
| 22 |
@patch('kiwi.container.appx.RuntimeConfig') |
| 23 |
@patch('os.path.exists') |
| 24 |
def setup_method(self, cls, mock_os_path_exists, mock_RuntimeConfig): |
| 25 |
self.setup() |
| 26 |
|
| 27 |
@patch('kiwi.container.appx.RuntimeConfig') |
| 28 |
@patch('os.path.exists') |
| 29 |
def test_init_raises(self, mock_os_path_exists, mock_RuntimeConfig): |
| 30 |
mock_os_path_exists.return_value = True |
| 31 |
with raises(KiwiContainerSetupError): |
| 32 |
ContainerImageAppx('root_dir', custom_args=None) |
| 33 |
mock_os_path_exists.return_value = False |
| 34 |
with raises(KiwiContainerSetupError): |
| 35 |
ContainerImageAppx( |
| 36 |
'root_dir', custom_args={ |
| 37 |
'metadata_path': 'meta/data' |
| 38 |
} |
| 39 |
) |
| 40 |
|
| 41 |
@patch('kiwi.container.appx.ArchiveTar') |
| 42 |
@patch('kiwi.container.appx.Compress') |
| 43 |
@patch('kiwi.container.appx.Defaults.get_exclude_list_for_root_data_sync') |
| 44 |
@patch('kiwi.container.appx.Defaults.get_exclude_list_from_custom_exclude_files') |
| 45 |
@patch('kiwi.container.appx.Temporary.new_file') |
| 46 |
@patch('kiwi.container.appx.Command.run') |
| 47 |
@patch('os.walk') |
| 48 |
def test_create( |
| 49 |
self, mock_os_walk, mock_Command_run, mock_Temporary_new_file, |
| 50 |
mock_get_exclude_list_from_custom_exclude_files, |
| 51 |
mock_get_exclude_list_for_root_data_sync, |
| 52 |
mock_Compress, mock_ArchiveTar |
| 53 |
): |
| 54 |
mock_os_walk.return_value = [ |
| 55 |
('source', ['bar', 'baz'], []), |
| 56 |
('source/bar', [], []), |
| 57 |
('source/baz', [], ['baz_file']) |
| 58 |
] |
| 59 |
tempfile = Mock() |
| 60 |
tempfile.name = 'tempfile' |
| 61 |
mock_Temporary_new_file.return_value = tempfile |
| 62 |
archive = Mock() |
| 63 |
mock_ArchiveTar.return_value = archive |
| 64 |
compress = Mock() |
| 65 |
mock_Compress.return_value = compress |
| 66 |
with patch('builtins.open', create=True) as mock_open: |
| 67 |
mock_open.return_value = MagicMock(spec=io.IOBase) |
| 68 |
file_handle = mock_open.return_value.__enter__.return_value |
| 69 |
self.appx.create('target_dir/image.appx', '', False, True) |
| 70 |
assert file_handle.write.call_args_list == [ |
| 71 |
call('[Files]\n'), |
| 72 |
call('"source/baz/baz_file" "../../source/baz/baz_file"\n') |
| 73 |
] |
| 74 |
mock_ArchiveTar.assert_called_once_with('meta/data/install.tar') |
| 75 |
archive.create.assert_called_once_with( |
| 76 |
'root_dir', |
| 77 |
exclude=mock_get_exclude_list_for_root_data_sync. |
| 78 |
return_value + mock_get_exclude_list_from_custom_exclude_files. |
| 79 |
return_value |
| 80 |
) |
| 81 |
assert mock_Compress.call_args_list == [ |
| 82 |
call(archive.create.return_value), |
| 83 |
call('target_dir/image.appx') |
| 84 |
] |
| 85 |
compress.gzip.assert_called_once_with() |
| 86 |
mock_Command_run.assert_called_once_with( |
| 87 |
['appx', '-o', 'target_dir/image.appx', '-f', 'tempfile'] |
| 88 |
) |
| 89 |
compress.xz.assert_called_once_with( |
| 90 |
self.appx.runtime_config.get_xz_options.return_value |
| 91 |
) |