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
|
import logging
from unittest.mock import (
patch, Mock
)
from pytest import (
raises, fixture
)
from kiwi.utils.compress import Compress
from kiwi.exceptions import (
KiwiFileNotFound,
KiwiCompressionFormatUnknown
)
class TestCompress:
@fixture(autouse=True)
def inject_fixtures(self, caplog):
self._caplog = caplog
@patch('os.path.exists')
def setup(self, mock_exists):
mock_exists.return_value = True
self.compress = Compress('some-file', True)
@patch('os.path.exists')
def setup_method(self, cls, mock_exists):
self.setup()
def test_source_file_not_found(self):
with raises(KiwiFileNotFound):
Compress('some-file')
@patch('kiwi.command.Command.run')
def test_xz(self, mock_command):
assert self.compress.xz() == 'some-file.xz'
mock_command.assert_called_once_with(
[
'xz', '-f', '--threads=0', '--keep',
'some-file'
]
)
assert self.compress.compressed_filename == 'some-file.xz'
@patch('kiwi.command.Command.run')
def test_xz_with_custom_options(self, mock_command):
assert self.compress.xz(options=['foo', 'bar']) == 'some-file.xz'
mock_command.assert_called_once_with(
[
'xz', '-f', 'foo', 'bar', '--keep',
'some-file'
]
)
assert self.compress.compressed_filename == 'some-file.xz'
@patch('kiwi.command.Command.run')
def test_gzip(self, mock_command):
assert self.compress.gzip() == 'some-file.gz'
mock_command.assert_called_once_with(
['gzip', '-f', '-9', '--keep', 'some-file']
)
assert self.compress.compressed_filename == 'some-file.gz'
@patch('kiwi.command.Command.run')
@patch('kiwi.utils.compress.Temporary.new_file')
@patch('kiwi.utils.compress.Compress.get_format')
def test_uncompress(self, mock_format, mock_temp, mock_command):
mock_format.return_value = 'xz'
self.compress.uncompress()
mock_command.assert_called_once_with(
['xz', '-d', 'some-file']
)
assert self.compress.uncompressed_filename == 'some-file'
@patch('kiwi.command.Command.run')
@patch('kiwi.utils.compress.Temporary.new_file')
@patch('kiwi.utils.compress.Compress.get_format')
def test_uncompress_temporary(self, mock_format, mock_temp, mock_command):
tempfile = Mock()
tempfile.name = 'tempfile'
mock_temp.return_value = tempfile
mock_format.return_value = 'xz'
self.compress.uncompress(temporary=True)
mock_command.assert_called_once_with(
['bash', '-c', 'xz -c -d some-file > tempfile']
)
assert self.compress.uncompressed_filename == 'tempfile'
@patch('kiwi.utils.compress.Compress.get_format')
def test_uncompress_unknown_format(self, mock_format):
mock_format.return_value = None
with raises(KiwiCompressionFormatUnknown):
self.compress.uncompress()
@patch('kiwi.path.Path.which')
def test_get_format(self, mock_which):
mock_which.side_effect = ['xz']
xz = Compress('../data/xz_data.xz')
assert xz.get_format() == 'xz'
mock_which.side_effect = ['xz', 'gzip']
gzip = Compress('../data/gz_data.gz')
assert gzip.get_format() == 'gzip'
@patch('kiwi.command.Command.run')
def test_get_format_invalid_format(self, mock_run):
result = Mock()
result.returncode = 1
mock_run.return_value = result
invalid = Compress("../data/gz_data.gz")
invalid.supported_zipper = ["mock_zip"]
with self._caplog.at_level(logging.DEBUG):
assert invalid.get_format() is None
mock_run.assert_called_once_with(
['mock_zip', '-l', '../data/gz_data.gz'], raise_on_error=False
)
|