File: t6_upgrade.py

package info (click to toggle)
s3ql 5.2.3%2Bdfsg-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 4,416 kB
  • sloc: python: 19,027; cpp: 408; makefile: 147; sh: 133
file content (315 lines) | stat: -rwxr-xr-x 9,754 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
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
#!/usr/bin/env python3
'''
t6_upgrade.py - this file is part of S3QL.

Copyright © 2008 Nikolaus Rath <Nikolaus@rath.org>

This work can be distributed under the terms of the GNU GPLv3.
'''

if __name__ == '__main__':
    import sys

    import pytest

    sys.exit(pytest.main([__file__] + sys.argv[1:]))

import os
import shutil
import subprocess
import tempfile
from subprocess import CalledProcessError, check_output

import pytest
import t4_fuse
from common import populate_dir, retry, skip_without_rsync
from t1_backends import NoTestSection, get_remote_test_info

from s3ql import backends


def Popen_old(*a, **kw):
    '''Run subprocess.Popen after unsetting PYTHONWARNINGS'''

    # We do not want to see warnings from the old S3QL version when testing
    # the new one.
    env = os.environ.copy()
    env.pop('PYTHONWARNINGS', None)

    return subprocess.Popen(*a, **kw, env=env)


@pytest.mark.usefixtures('pass_reg_output')
class TestUpgrade(t4_fuse.TestFuse):
    def setup_method(self, method):
        skip_without_rsync()

        self.basedir_old = os.path.abspath(
            os.path.join(os.path.dirname(__file__), '..', 's3ql.old')
        )
        if not os.path.exists(os.path.join(self.basedir_old, 'bin', 'mkfs.s3ql')):
            pytest.skip('no previous S3QL version found')

        super().setup_method(method)
        self.tempdir = tempfile.mkdtemp(prefix='s3ql-upgrade-test')
        self.ref_dir = tempfile.mkdtemp(prefix='s3ql-upgrade-ref')

    def teardown_method(self, method):
        super().teardown_method(method)
        shutil.rmtree(self.tempdir)
        shutil.rmtree(self.ref_dir)

    def mkfs_old(self, force=False, max_obj_size=500):
        argv = [
            os.path.join(self.basedir_old, 'bin', 'mkfs.s3ql'),
            '-L',
            'test fs',
            '--max-obj-size',
            str(max_obj_size),
            '--cachedir',
            self.cache_dir,
            '--quiet',
            '--authfile',
            '/dev/null',
            self.storage_url,
        ]
        if force:
            argv.append('--force')
        if self.passphrase is None:
            argv.append('--plain')
        proc = Popen_old(argv, stdin=subprocess.PIPE, universal_newlines=True)

        if self.backend_login is not None:
            print(self.backend_login, file=proc.stdin)
            print(self.backend_passphrase, file=proc.stdin)
        if self.passphrase is not None:
            print(self.passphrase, file=proc.stdin)
            print(self.passphrase, file=proc.stdin)
        proc.stdin.close()
        assert proc.wait() == 0
        self.reg_output(
            r'^WARNING: Maximum object sizes less than 1 MiB will degrade performance\.$',
            count=1,
        )

    def mount_old(self):
        self.mount_process = Popen_old(
            [
                os.path.join(self.basedir_old, 'bin', 'mount.s3ql'),
                "--fg",
                '--cachedir',
                self.cache_dir,
                '--log',
                'none',
                '--quiet',
                '--authfile',
                '/dev/null',
                '--compress',
                'zlib',
                self.storage_url,
                self.mnt_dir,
            ],
            stdin=subprocess.PIPE,
            universal_newlines=True,
        )
        if self.backend_login is not None:
            print(self.backend_login, file=self.mount_process.stdin)
            print(self.backend_passphrase, file=self.mount_process.stdin)
        if self.passphrase is not None:
            print(self.passphrase, file=self.mount_process.stdin)
        self.mount_process.stdin.close()

        def poll():
            if os.path.ismount(self.mnt_dir):
                return True
            assert self.mount_process.poll() is None

        retry(30, poll)

    def umount_old(self):
        with open('/dev/null', 'wb') as devnull:
            retry(
                5,
                lambda: subprocess.call(
                    ['fuser', '-m', self.mnt_dir], stdout=devnull, stderr=devnull
                )
                == 1,
            )

        proc = Popen_old(
            [os.path.join(self.basedir_old, 'bin', 'umount.s3ql'), '--quiet', self.mnt_dir]
        )
        retry(90, lambda: proc.poll() is not None)
        assert proc.wait() == 0

        assert self.mount_process.poll() == 0
        assert not os.path.ismount(self.mnt_dir)

    def upgrade(self):
        proc = subprocess.Popen(
            self.s3ql_cmd_argv('s3qladm')
            + [
                '--cachedir',
                self.cache_dir,
                '--authfile',
                '/dev/null',
                '--quiet',
                'upgrade',
                self.storage_url,
            ],
            stdin=subprocess.PIPE,
            universal_newlines=True,
        )

        if self.backend_login is not None:
            print(self.backend_login, file=proc.stdin)
            print(self.backend_passphrase, file=proc.stdin)
        if self.passphrase is not None:
            print(self.passphrase, file=proc.stdin)
        print('yes', file=proc.stdin)
        proc.stdin.close()

        assert proc.wait() == 0

    def compare(self):
        try:
            out = check_output(
                [
                    'rsync',
                    '-anciHAX',
                    '--delete',
                    '--exclude',
                    '/lost+found',
                    self.ref_dir + '/',
                    self.mnt_dir + '/',
                ],
                universal_newlines=True,
                stderr=subprocess.STDOUT,
            )
        except CalledProcessError as exc:
            pytest.fail('rsync failed with ' + exc.output)
        if out:
            pytest.fail('Copy not equal to original, rsync says:\n' + out)

    def populate(self):
        populate_dir(self.ref_dir)

    def test(self):
        self.populate()

        # Create and mount using previous S3QL version
        self.mkfs_old()
        self.mount_old()
        subprocess.check_call(['rsync', '-aHAX', self.ref_dir + '/', self.mnt_dir + '/'])
        self.umount_old()

        # Preserve a copy of the old cache
        prev_cache = os.path.join(self.tempdir, 'cache_prev')
        shutil.copytree(self.cache_dir, prev_cache)

        # Try to access with new version (should fail)
        self.reg_output(r'^ERROR: File system revision too old', count=1)
        self.mount(expect_fail=32)

        # Try to access with new version without cache (should also fail)
        self.reg_output(r'^ERROR: File system revision too old', count=1)
        orig_cache = self.cache_dir
        self.cache_dir = os.path.join(self.tempdir, 'empty-cache')
        try:
            self.mount(expect_fail=32)
        finally:
            self.cache_dir = orig_cache

        # Upgrade to revision 26 is only supported with populated local cache
        self.upgrade()

        # Test with local cache
        self.fsck()
        self.mount()
        self.compare()
        self.umount()

        # Test with old cache
        orig_cache = self.cache_dir
        self.cache_dir = prev_cache
        try:
            self.fsck()
            self.mount()
            self.compare()
            self.umount()
        finally:
            self.cache_dir = orig_cache

        # Test without cache
        orig_cache = self.cache_dir
        self.cache_dir = os.path.join(self.tempdir, 'empty-cache2')
        try:
            self.fsck()
            self.mount()
            self.compare()
            self.umount()
        finally:
            self.cache_dir = orig_cache


class TestPlainUpgrade(TestUpgrade):
    def setup_method(self, method):
        super().setup_method(method)
        self.passphrase = None


class RemoteUpgradeTest:
    def setup_method(self, method, name):
        super().setup_method(method)
        try:
            (backend_login, backend_pw, self.storage_url) = get_remote_test_info(name)
        except NoTestSection as exc:
            super().teardown_method(method)
            pytest.skip(exc.reason)
        self.backend_login = backend_login
        self.backend_passphrase = backend_pw

    def populate(self):
        populate_dir(self.ref_dir, entries=50, size=5 * 1024 * 1024)

    def teardown_method(self, method):
        super().teardown_method(method)

        proc = subprocess.Popen(
            self.s3ql_cmd_argv('s3qladm')
            + ['--quiet', '--authfile', '/dev/null', 'clear', self.storage_url],
            stdin=subprocess.PIPE,
            universal_newlines=True,
        )
        if self.backend_login is not None:
            print(self.backend_login, file=proc.stdin)
            print(self.backend_passphrase, file=proc.stdin)
        print('yes', file=proc.stdin)
        proc.stdin.close()

        assert proc.wait() == 0


# Dynamically generate tests for other backends
for backend_name in backends.prefix_map:
    if backend_name == 'local':
        continue

    # Plain
    def setup_method(self, method, backend_name=backend_name):
        RemoteUpgradeTest.setup_method(self, method, backend_name + '-test')
        self.passphrase = None

    test_class_name = 'TestPlain' + backend_name + 'Upgrade'
    globals()[test_class_name] = type(
        test_class_name, (RemoteUpgradeTest, TestUpgrade), {'setup_method': setup_method}
    )

    # Encrypted
    def setup_method(self, method, backend_name=backend_name):
        RemoteUpgradeTest.setup_method(self, method, backend_name + '-test')

    test_class_name = 'Test' + backend_name + 'Upgrade'
    globals()[test_class_name] = type(
        test_class_name, (RemoteUpgradeTest, TestUpgrade), {'setup_method': setup_method}
    )