| 1 |
import logging |
| 2 |
from unittest.mock import ( |
| 3 |
patch, Mock |
| 4 |
) |
| 5 |
from pytest import ( |
| 6 |
raises, fixture |
| 7 |
) |
| 8 |
|
| 9 |
from kiwi.utils.compress import Compress |
| 10 |
|
| 11 |
from kiwi.exceptions import ( |
| 12 |
KiwiFileNotFound, |
| 13 |
KiwiCompressionFormatUnknown |
| 14 |
) |
| 15 |
|
| 16 |
|
| 17 |
class TestCompress: |
| 18 |
@fixture(autouse=True) |
| 19 |
def inject_fixtures(self, caplog): |
| 20 |
self._caplog = caplog |
| 21 |
|
| 22 |
@patch('os.path.exists') |
| 23 |
def setup(self, mock_exists): |
| 24 |
mock_exists.return_value = True |
| 25 |
self.compress = Compress('some-file', True) |
| 26 |
|
| 27 |
@patch('os.path.exists') |
| 28 |
def setup_method(self, cls, mock_exists): |
| 29 |
self.setup() |
| 30 |
|
| 31 |
def test_source_file_not_found(self): |
| 32 |
with raises(KiwiFileNotFound): |
| 33 |
Compress('some-file') |
| 34 |
|
| 35 |
@patch('kiwi.command.Command.run') |
| 36 |
def test_xz(self, mock_command): |
| 37 |
assert self.compress.xz() == 'some-file.xz' |
| 38 |
mock_command.assert_called_once_with( |
| 39 |
[ |
| 40 |
'xz', '-f', '--threads=0', '--keep', |
| 41 |
'some-file' |
| 42 |
] |
| 43 |
) |
| 44 |
assert self.compress.compressed_filename == 'some-file.xz' |
| 45 |
|
| 46 |
@patch('kiwi.command.Command.run') |
| 47 |
def test_xz_with_custom_options(self, mock_command): |
| 48 |
assert self.compress.xz(options=['foo', 'bar']) == 'some-file.xz' |
| 49 |
mock_command.assert_called_once_with( |
| 50 |
[ |
| 51 |
'xz', '-f', 'foo', 'bar', '--keep', |
| 52 |
'some-file' |
| 53 |
] |
| 54 |
) |
| 55 |
assert self.compress.compressed_filename == 'some-file.xz' |
| 56 |
|
| 57 |
@patch('kiwi.command.Command.run') |
| 58 |
def test_gzip(self, mock_command): |
| 59 |
assert self.compress.gzip() == 'some-file.gz' |
| 60 |
mock_command.assert_called_once_with( |
| 61 |
['gzip', '-f', '-9', '--keep', 'some-file'] |
| 62 |
) |
| 63 |
assert self.compress.compressed_filename == 'some-file.gz' |
| 64 |
|
| 65 |
@patch('kiwi.command.Command.run') |
| 66 |
@patch('kiwi.utils.compress.Temporary.new_file') |
| 67 |
@patch('kiwi.utils.compress.Compress.get_format') |
| 68 |
def test_uncompress(self, mock_format, mock_temp, mock_command): |
| 69 |
mock_format.return_value = 'xz' |
| 70 |
self.compress.uncompress() |
| 71 |
mock_command.assert_called_once_with( |
| 72 |
['xz', '-d', 'some-file'] |
| 73 |
) |
| 74 |
assert self.compress.uncompressed_filename == 'some-file' |
| 75 |
|
| 76 |
@patch('kiwi.command.Command.run') |
| 77 |
@patch('kiwi.utils.compress.Temporary.new_file') |
| 78 |
@patch('kiwi.utils.compress.Compress.get_format') |
| 79 |
def test_uncompress_temporary(self, mock_format, mock_temp, mock_command): |
| 80 |
tempfile = Mock() |
| 81 |
tempfile.name = 'tempfile' |
| 82 |
mock_temp.return_value = tempfile |
| 83 |
mock_format.return_value = 'xz' |
| 84 |
self.compress.uncompress(temporary=True) |
| 85 |
mock_command.assert_called_once_with( |
| 86 |
['bash', '-c', 'xz -c -d some-file > tempfile'] |
| 87 |
) |
| 88 |
assert self.compress.uncompressed_filename == 'tempfile' |
| 89 |
|
| 90 |
@patch('kiwi.utils.compress.Compress.get_format') |
| 91 |
def test_uncompress_unknown_format(self, mock_format): |
| 92 |
mock_format.return_value = None |
| 93 |
with raises(KiwiCompressionFormatUnknown): |
| 94 |
self.compress.uncompress() |
| 95 |
|
| 96 |
@patch('kiwi.path.Path.which') |
| 97 |
def test_get_format(self, mock_which): |
| 98 |
mock_which.side_effect = ['xz'] |
| 99 |
xz = Compress('../data/xz_data.xz') |
| 100 |
assert xz.get_format() == 'xz' |
| 101 |
|
| 102 |
mock_which.side_effect = ['xz', 'gzip'] |
| 103 |
gzip = Compress('../data/gz_data.gz') |
| 104 |
assert gzip.get_format() == 'gzip' |
| 105 |
|
| 106 |
@patch('kiwi.command.Command.run') |
| 107 |
def test_get_format_invalid_format(self, mock_run): |
| 108 |
result = Mock() |
| 109 |
result.returncode = 1 |
| 110 |
mock_run.return_value = result |
| 111 |
invalid = Compress("../data/gz_data.gz") |
| 112 |
invalid.supported_zipper = ["mock_zip"] |
| 113 |
|
| 114 |
with self._caplog.at_level(logging.DEBUG): |
| 115 |
assert invalid.get_format() is None |
| 116 |
mock_run.assert_called_once_with( |
| 117 |
['mock_zip', '-l', '../data/gz_data.gz'], raise_on_error=False |
| 118 |
) |