File: location_map_filenames_test.py

package info (click to toggle)
rdiff-backup 2.2.6-3
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 4,640 kB
  • sloc: python: 24,129; javascript: 9,512; sh: 1,230; ansic: 580; makefile: 36
file content (166 lines) | stat: -rw-r--r-- 7,032 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
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
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
"""
Test the quoting of characters in filenames with api version >= 201
"""
import os
import unittest

import commontest as comtst
import fileset
from rdiff_backup import Globals


class LocationMapFilenamesTest(unittest.TestCase):
    """
    Test that rdiff-backup really restores what has been backed-up with quotes
    """

    def setUp(self):
        self.base_dir = os.path.join(comtst.abs_test_dir,
                                     b"location_map_filenames")
        # Windows can't handle too long filenames
        long_multi = 5 if os.name == "nt" else 25
        self.from1_struct = {
            "from1": {"contents": {
                "fileABC": {"content": "initial"},
                "fileXYZ": {},
                "dirAbCXyZ": {"type": "dir"},
                "itemX": {"type": "dir"},
                "itemY": {"type": "file"},
                "longDiRnAm" * long_multi: {"type": "dir"},
                "longFiLnAm" * long_multi: {"content": "not so long content"},
                # it should be aux.123 but it can't be created under Windows
                # and isn't special under Linux
                "aux123": {"content": "looks like DOS file"},
                "ends_in_blank ": {"content": "looks like DOS file"},
            }}
        }
        self.from1_path = os.path.join(self.base_dir, b"from1")
        self.from2_struct = {
            "from2": {"contents": {
                "fileABC": {"content": "modified"},
                "fileXYZ": {},
                "diraBcXyZ": {"type": "dir"},
                "itemX": {"type": "file"},
                "itemY": {"type": "dir"},
                "longDiRnAm" * long_multi: {"type": "dir"},
                "longFiLnAm" * long_multi: {"content": "differently long"},
                # it should be aux.123 but it can't be created under Windows
                # and isn't special under Linux
                "aux123": {"content": "still looks like DOS file"},
                "ends_in_blank ": {"content": "still looks like DOS file"},
            }}
        }
        self.from2_path = os.path.join(self.base_dir, b"from2")
        fileset.create_fileset(self.base_dir, self.from1_struct)
        fileset.create_fileset(self.base_dir, self.from2_struct)
        fileset.remove_fileset(self.base_dir, {"bak": {"type": "dir"}})
        fileset.remove_fileset(self.base_dir, {"to1": {"type": "dir"}})
        fileset.remove_fileset(self.base_dir, {"to2": {"type": "dir"}})
        self.bak_path = os.path.join(self.base_dir, b"bak")
        self.to1_path = os.path.join(self.base_dir, b"to1")
        self.to2_path = os.path.join(self.base_dir, b"to2")
        self.success = False

    def test_location_map_filenames(self):
        """
        test the "backup" and "restore" actions with quoted filenames
        """
        # we backup twice to the same backup repository at different times
        self.assertEqual(comtst.rdiff_backup_action(
            False, False, self.from1_path, self.bak_path,
            ("--api-version", "201", "--current-time", "10000",
             "--chars-to-quote", "A-Z:"),
            b"backup", ()), 0)
        self.assertEqual(comtst.rdiff_backup_action(
            False, True, self.from2_path, self.bak_path,
            ("--api-version", "201", "--current-time", "20000",
             "--chars-to-quote", "A-Z:"),
            b"backup", ()), 0)

        # then we restore the increment and the last mirror to two directories
        self.assertEqual(comtst.rdiff_backup_action(
            True, False, self.bak_path, self.to1_path,
            ("--api-version", "201"),
            b"restore", ("--at", "1B")), 0)
        self.assertEqual(comtst.rdiff_backup_action(
            True, True, self.bak_path, self.to2_path,
            ("--api-version", "201"),
            b"restore", ()), 0)

        self.assertFalse(fileset.compare_paths(self.from1_path, self.to1_path))
        self.assertFalse(fileset.compare_paths(self.from2_path, self.to2_path))

        # all tests were successful
        self.success = True

    def test_location_map_filenames_change_quotes(self):
        """
        test the "backup" and "restore" actions with quoted filenames
        while changing the quoted characters, which isn't supported
        """
        # we backup twice to the same backup repository at different times
        self.assertEqual(comtst.rdiff_backup_action(
            False, False, self.from1_path, self.bak_path,
            ("--api-version", "201", "--current-time", "10000",
             "--chars-to-quote", "A-P:"),
            b"backup", ()), 0)
        # we try the 2nd time to change the chars-to-quote, which fails
        self.assertNotEqual(comtst.rdiff_backup_action(
            False, True, self.from2_path, self.bak_path,
            ("--api-version", "201", "--current-time", "15000",
             "--chars-to-quote", "H-Z:"),
            b"backup", ()), 0)
        self.assertNotEqual(comtst.rdiff_backup_action(
            False, True, self.from2_path, self.bak_path,
            ("--api-version", "201", "--current-time", "20000",
             "--chars-to-quote", "H-Z:", "--force"),
            b"backup", ()), 0)

        # then we restore the last mirror to a directory without issue
        self.assertEqual(comtst.rdiff_backup_action(
            True, True, self.bak_path, self.to1_path,
            ("--api-version", "201"),
            b"restore", ()), 0)

        self.assertFalse(fileset.compare_paths(self.from1_path, self.to1_path))

        # all tests were successful
        self.success = True

    def tearDown(self):
        # we clean-up only if the test was successful
        if self.success:
            fileset.remove_fileset(self.base_dir, self.from1_struct)
            fileset.remove_fileset(self.base_dir, self.from2_struct)
            fileset.remove_fileset(self.base_dir, {"bak": {"type": "dir"}})
            fileset.remove_fileset(self.base_dir, {"to1": {"type": "dir"}})
            fileset.remove_fileset(self.base_dir, {"to2": {"type": "dir"}})


class LocationMapFilenamesUnitTest(unittest.TestCase):
    """
    Test specific aspects of locations.map.filenames to increase coverage
    """

    def test_location_map_filenames_dos_quotes(self):
        """
        Check that DOS filenames are properly quoted
        """
        Globals.escape_dos_devices = True
        Globals.escape_trailing_spaces = True
        from rdiffbackup.locations.map import filenames as map_filenames

        chars_to_quote = b"A-Z"
        regexp, unregexp = map_filenames.get_quoting_regexps(
            chars_to_quote, Globals.quoting_char)
        Globals.set_all("chars_to_quote", chars_to_quote)
        Globals.set_all('chars_to_quote_regexp', regexp)
        Globals.set_all('chars_to_quote_unregexp', unregexp)

        self.assertEqual(map_filenames.quote(b'aux.123'), b";097ux.123")
        self.assertEqual(map_filenames.quote(b'ends in space '),
                         b"ends in space;032")


if __name__ == "__main__":
    unittest.main()