File: sync_test.py

package info (click to toggle)
kiwi 10.2.33-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 7,528 kB
  • sloc: python: 67,299; sh: 3,980; xml: 3,379; ansic: 391; makefile: 354
file content (73 lines) | stat: -rw-r--r-- 2,395 bytes parent folder | download
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
import os
import errno
import logging
from pytest import fixture
from stat import ST_MODE
from unittest.mock import patch

from kiwi.utils.sync import DataSync


class TestDataSync:
    @fixture(autouse=True)
    def inject_fixtures(self, caplog):
        self._caplog = caplog

    def setup(self):
        self.sync = DataSync('source_dir', 'target_dir')

    def setup_method(self, cls):
        self.setup()

    @patch('kiwi.utils.sync.Command.run')
    @patch('kiwi.utils.sync.DataSync.target_supports_extended_attributes')
    @patch('os.chmod')
    @patch('os.stat')
    def test_sync_data(
        self, mock_stat, mock_chmod, mock_xattr_support, mock_command
    ):
        mock_stat.return_value = os.stat('.')
        mock_xattr_support.return_value = False
        with self._caplog.at_level(logging.WARNING):
            self.sync.sync_data(
                options=[
                    '--archive', '--hard-links', '--xattrs',
                    '--acls', '--one-file-system'
                ],
                exclude=['exclude_me']
            )
            mock_command.assert_called_once_with(
                [
                    'rsync', '--archive', '--hard-links', '--one-file-system',
                    '--exclude', '/exclude_me', 'source_dir', 'target_dir'
                ]
            )
            mock_chmod.assert_called_once_with(
                'target_dir', mock_stat.return_value[ST_MODE]
            )

    @patch('kiwi.utils.sync.Command.run')
    @patch('os.chmod')
    @patch('os.stat')
    def test_sync_data_force_trailing_slash(
        self, mock_stat, mock_chmod, mock_command
    ):
        mock_stat.return_value = os.stat('.')
        self.sync.sync_data(force_trailing_slash=True)
        mock_command.assert_called_once_with(
            ['rsync', 'source_dir/', 'target_dir']
        )

    @patch('os.getxattr')
    def test_target_supports_extended_attributes(self, mock_getxattr):
        assert self.sync.target_supports_extended_attributes() is True
        mock_getxattr.assert_called_once_with(
            'target_dir', 'user.mime_type'
        )

    @patch('os.getxattr')
    def test_target_does_not_support_extended_attributes(self, mock_getxattr):
        effect = OSError()
        effect.errno = errno.ENOTSUP
        mock_getxattr.side_effect = effect
        assert self.sync.target_supports_extended_attributes() is False