| 1 |
from unittest.mock import ( |
| 2 |
patch, call |
| 3 |
) |
| 4 |
from pytest import raises |
| 5 |
import unittest.mock as mock |
| 6 |
|
| 7 |
from kiwi.archive.tar import ArchiveTar |
| 8 |
|
| 9 |
from kiwi.exceptions import KiwiCommandCapabilitiesError |
| 10 |
|
| 11 |
|
| 12 |
class TestArchiveTar: |
| 13 |
@patch('kiwi.archive.tar.Command.run') |
| 14 |
def setup(self, mock_command): |
| 15 |
command = mock.Mock() |
| 16 |
command.output = 'version 1.27.0' |
| 17 |
mock_command.return_value = command |
| 18 |
self.archive = ArchiveTar('foo.tar') |
| 19 |
|
| 20 |
@patch('kiwi.archive.tar.Command.run') |
| 21 |
def setup_method(self, cls, mock_command): |
| 22 |
self.setup() |
| 23 |
|
| 24 |
@patch('kiwi.archive.tar.Command.run') |
| 25 |
def test_invalid_tar_command_version(self, mock_command): |
| 26 |
command = mock.Mock() |
| 27 |
command.output = 'version cannot be parsed' |
| 28 |
mock_command.return_value = command |
| 29 |
with raises(KiwiCommandCapabilitiesError): |
| 30 |
self.archive = ArchiveTar('foo.tar') |
| 31 |
|
| 32 |
@patch('kiwi.archive.tar.Command.run') |
| 33 |
def test_extract(self, mock_command): |
| 34 |
self.archive.extract('destination') |
| 35 |
mock_command.assert_called_once_with( |
| 36 |
['tar', '-C', 'destination', '-x', '-v', '-f', 'foo.tar'] |
| 37 |
) |
| 38 |
|
| 39 |
@patch('kiwi.archive.tar.Command.run') |
| 40 |
@patch('os.listdir') |
| 41 |
def test_create(self, mock_os_dir, mock_command): |
| 42 |
mock_os_dir.return_value = ['foo', 'bar'] |
| 43 |
assert self.archive.create('source-dir') == 'foo.tar' |
| 44 |
mock_command.assert_called_once_with( |
| 45 |
[ |
| 46 |
'tar', '-C', 'source-dir', |
| 47 |
'--xattrs', '--xattrs-include=*', |
| 48 |
'-c', '-f', 'foo.tar', 'bar', 'foo' |
| 49 |
] |
| 50 |
) |
| 51 |
|
| 52 |
@patch('kiwi.archive.tar.Command.run') |
| 53 |
def test_append_files(self, mock_command): |
| 54 |
assert self.archive.append_files('source-dir', ['foo', 'bar']) \ |
| 55 |
== 'foo.tar' |
| 56 |
mock_command.assert_called_once_with( |
| 57 |
[ |
| 58 |
'tar', '-C', 'source-dir', '-r', |
| 59 |
'--file=' + self.archive.filename, |
| 60 |
'--xattrs', '--xattrs-include=*', |
| 61 |
'foo', 'bar' |
| 62 |
] |
| 63 |
) |
| 64 |
|
| 65 |
@patch('kiwi.archive.tar.Command.run') |
| 66 |
@patch('os.listdir') |
| 67 |
def test_create_with_options(self, mock_os_dir, mock_command): |
| 68 |
mock_os_dir.return_value = ['foo', 'bar'] |
| 69 |
assert self.archive.create('source-dir', options=[ |
| 70 |
'--fake-option', 'fake_arg' |
| 71 |
]) == 'foo.tar' |
| 72 |
mock_command.assert_called_once_with( |
| 73 |
[ |
| 74 |
'tar', '-C', 'source-dir', |
| 75 |
'--fake-option', 'fake_arg', |
| 76 |
'--xattrs', '--xattrs-include=*', |
| 77 |
'-c', '-f', 'foo.tar', 'bar', 'foo' |
| 78 |
] |
| 79 |
) |
| 80 |
|
| 81 |
@patch('kiwi.archive.tar.Command.run') |
| 82 |
@patch('os.listdir') |
| 83 |
def test_create_with_old_tar_version(self, mock_os_dir, mock_command): |
| 84 |
command = mock.Mock() |
| 85 |
command.output = 'version 1.26.1' |
| 86 |
mock_command.return_value = command |
| 87 |
archive = ArchiveTar('foo.tar') |
| 88 |
mock_os_dir.return_value = ['foo', 'bar'] |
| 89 |
assert archive.create('source-dir') == 'foo.tar' |
| 90 |
calls = [ |
| 91 |
call(['tar', '--version']), |
| 92 |
call( |
| 93 |
[ |
| 94 |
'tar', '-C', 'source-dir', |
| 95 |
'-c', '-f', 'foo.tar', 'bar', 'foo' |
| 96 |
] |
| 97 |
) |
| 98 |
] |
| 99 |
mock_command.assert_has_calls(calls) |
| 100 |
assert mock_command.call_count == 2 |
| 101 |
|
| 102 |
@patch('kiwi.archive.tar.Command.run') |
| 103 |
def test_create_from_dir_with_excludes(self, mock_command): |
| 104 |
command = mock.Mock() |
| 105 |
command.output = 'version 1.27.0' |
| 106 |
mock_command.return_value = command |
| 107 |
archive = ArchiveTar('foo.tar', False) |
| 108 |
assert archive.create('source-dir', ['foo', 'bar']) == 'foo.tar' |
| 109 |
calls = [ |
| 110 |
call(['tar', '--version']), |
| 111 |
call( |
| 112 |
[ |
| 113 |
'tar', '-C', 'source-dir', '--xattrs', '--xattrs-include=*', |
| 114 |
'-c', '-f', 'foo.tar', |
| 115 |
'--exclude', './foo', '--exclude', './bar', '.' |
| 116 |
] |
| 117 |
) |
| 118 |
] |
| 119 |
mock_command.assert_has_calls(calls) |
| 120 |
assert mock_command.call_count == 2 |
| 121 |
|
| 122 |
@patch('kiwi.archive.tar.Command.run') |
| 123 |
@patch('os.listdir') |
| 124 |
def test_create_xz_compressed(self, mock_os_dir, mock_command): |
| 125 |
mock_os_dir.return_value = ['foo', 'bar'] |
| 126 |
assert self.archive.create_xz_compressed('source-dir') == 'foo.tar.xz' |
| 127 |
mock_command.assert_called_once_with( |
| 128 |
[ |
| 129 |
'bash', '-c', |
| 130 |
' '.join([ |
| 131 |
'tar', '-C', 'source-dir', '--xattrs', |
| 132 |
'--xattrs-include=*', '-c', '--to-stdout', |
| 133 |
'bar', 'foo', '|', 'xz', '-f', '--threads=0', |
| 134 |
'>', 'foo.tar.xz' |
| 135 |
]) |
| 136 |
] |
| 137 |
) |
| 138 |
|
| 139 |
@patch('kiwi.archive.tar.Command.run') |
| 140 |
@patch('os.listdir') |
| 141 |
def test_create_xz_compressed_with_custom_xz_options( |
| 142 |
self, mock_os_dir, mock_command |
| 143 |
): |
| 144 |
mock_os_dir.return_value = ['foo', 'bar'] |
| 145 |
assert self.archive.create_xz_compressed( |
| 146 |
'source-dir', xz_options=['-a', '-b'] |
| 147 |
) == 'foo.tar.xz' |
| 148 |
mock_command.assert_called_once_with( |
| 149 |
[ |
| 150 |
'bash', '-c', |
| 151 |
' '.join([ |
| 152 |
'tar', '-C', 'source-dir', '--xattrs', |
| 153 |
'--xattrs-include=*', '-c', '--to-stdout', |
| 154 |
'bar', 'foo', '|', 'xz', '-f', '-a', '-b', |
| 155 |
'>', 'foo.tar.xz' |
| 156 |
]) |
| 157 |
] |
| 158 |
) |
| 159 |
|
| 160 |
@patch('kiwi.archive.tar.Command.run') |
| 161 |
@patch('os.listdir') |
| 162 |
def test_create_gnu_gzip_compressed(self, mock_os_dir, mock_command): |
| 163 |
mock_os_dir.return_value = ['foo', 'bar'] |
| 164 |
assert self.archive.create_gnu_gzip_compressed('source-dir') \ |
| 165 |
== 'foo.tar.gz' |
| 166 |
mock_command.assert_called_once_with( |
| 167 |
[ |
| 168 |
'tar', '-C', 'source-dir', |
| 169 |
'--format=gnu', '-cSz', '-f', 'foo.tar.gz', 'bar', 'foo' |
| 170 |
] |
| 171 |
) |
| 172 |
|
| 173 |
@patch('kiwi.archive.tar.Command.run') |
| 174 |
@patch('os.listdir') |
| 175 |
def test_create_exclude(self, mock_os_dir, mock_command): |
| 176 |
mock_os_dir.return_value = ['foo', 'bar'] |
| 177 |
assert self.archive.create('source-dir', ['foo']) == 'foo.tar' |
| 178 |
mock_command.assert_called_once_with( |
| 179 |
[ |
| 180 |
'tar', '-C', 'source-dir', '--xattrs', '--xattrs-include=*', |
| 181 |
'-c', '-f', 'foo.tar', 'bar' |
| 182 |
] |
| 183 |
) |