| 1 |
import os |
| 2 |
import errno |
| 3 |
import logging |
| 4 |
from pytest import fixture |
| 5 |
from stat import ST_MODE |
| 6 |
from unittest.mock import patch |
| 7 |
|
| 8 |
from kiwi.utils.sync import DataSync |
| 9 |
|
| 10 |
|
| 11 |
class TestDataSync: |
| 12 |
@fixture(autouse=True) |
| 13 |
def inject_fixtures(self, caplog): |
| 14 |
self._caplog = caplog |
| 15 |
|
| 16 |
def setup(self): |
| 17 |
self.sync = DataSync('source_dir', 'target_dir') |
| 18 |
|
| 19 |
def setup_method(self, cls): |
| 20 |
self.setup() |
| 21 |
|
| 22 |
@patch('kiwi.utils.sync.Command.run') |
| 23 |
@patch('kiwi.utils.sync.DataSync.target_supports_extended_attributes') |
| 24 |
@patch('os.chmod') |
| 25 |
@patch('os.stat') |
| 26 |
def test_sync_data( |
| 27 |
self, mock_stat, mock_chmod, mock_xattr_support, mock_command |
| 28 |
): |
| 29 |
mock_stat.return_value = os.stat('.') |
| 30 |
mock_xattr_support.return_value = False |
| 31 |
with self._caplog.at_level(logging.WARNING): |
| 32 |
self.sync.sync_data( |
| 33 |
options=[ |
| 34 |
'--archive', '--hard-links', '--xattrs', |
| 35 |
'--acls', '--one-file-system' |
| 36 |
], |
| 37 |
exclude=['exclude_me'] |
| 38 |
) |
| 39 |
mock_command.assert_called_once_with( |
| 40 |
[ |
| 41 |
'rsync', '--archive', '--hard-links', '--one-file-system', |
| 42 |
'--exclude', '/exclude_me', 'source_dir', 'target_dir' |
| 43 |
] |
| 44 |
) |
| 45 |
mock_chmod.assert_called_once_with( |
| 46 |
'target_dir', mock_stat.return_value[ST_MODE] |
| 47 |
) |
| 48 |
|
| 49 |
@patch('kiwi.utils.sync.Command.run') |
| 50 |
@patch('os.chmod') |
| 51 |
@patch('os.stat') |
| 52 |
def test_sync_data_force_trailing_slash( |
| 53 |
self, mock_stat, mock_chmod, mock_command |
| 54 |
): |
| 55 |
mock_stat.return_value = os.stat('.') |
| 56 |
self.sync.sync_data(force_trailing_slash=True) |
| 57 |
mock_command.assert_called_once_with( |
| 58 |
['rsync', 'source_dir/', 'target_dir'] |
| 59 |
) |
| 60 |
|
| 61 |
@patch('os.getxattr') |
| 62 |
def test_target_supports_extended_attributes(self, mock_getxattr): |
| 63 |
assert self.sync.target_supports_extended_attributes() is True |
| 64 |
mock_getxattr.assert_called_once_with( |
| 65 |
'target_dir', 'user.mime_type' |
| 66 |
) |
| 67 |
|
| 68 |
@patch('os.getxattr') |
| 69 |
def test_target_does_not_support_extended_attributes(self, mock_getxattr): |
| 70 |
effect = OSError() |
| 71 |
effect.errno = errno.ENOTSUP |
| 72 |
mock_getxattr.side_effect = effect |
| 73 |
assert self.sync.target_supports_extended_attributes() is False |
| 74 |
effect = NotImplemented |
| 75 |
mock_getxattr.side_effect = effect |
| 76 |
assert self.sync.target_supports_extended_attributes() is True |