/brz/remove-bazaar

To get this branch, use:
bzr branch http://gegoxaren.bato24.eu/bzr/brz/remove-bazaar

« back to all changes in this revision

Viewing changes to bzrlib/tests/test_lockable_files.py

Deprecate LockableFiles.put_utf8

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
# Copyright (C) 2005, 2006, 2008 Canonical Ltd
 
2
#
 
3
# This program is free software; you can redistribute it and/or modify
 
4
# it under the terms of the GNU General Public License as published by
 
5
# the Free Software Foundation; either version 2 of the License, or
 
6
# (at your option) any later version.
 
7
#
 
8
# This program is distributed in the hope that it will be useful,
 
9
# but WITHOUT ANY WARRANTY; without even the implied warranty of
 
10
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
11
# GNU General Public License for more details.
 
12
#
 
13
# You should have received a copy of the GNU General Public License
 
14
# along with this program; if not, write to the Free Software
 
15
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 
16
 
 
17
from StringIO import StringIO
 
18
 
 
19
import bzrlib
 
20
from bzrlib import (
 
21
    errors,
 
22
    lockdir,
 
23
    osutils,
 
24
    )
 
25
from bzrlib.errors import BzrBadParameterNotString, NoSuchFile, ReadOnlyError
 
26
from bzrlib.lockable_files import LockableFiles, TransportLock
 
27
from bzrlib.symbol_versioning import (
 
28
    deprecated_in,
 
29
    )
 
30
from bzrlib.tests import TestCaseInTempDir
 
31
from bzrlib.tests.test_smart import TestCaseWithSmartMedium
 
32
from bzrlib.tests.test_transactions import DummyWeave
 
33
from bzrlib.transactions import (PassThroughTransaction,
 
34
                                 ReadOnlyTransaction,
 
35
                                 WriteTransaction,
 
36
                                 )
 
37
from bzrlib.transport import get_transport
 
38
 
 
39
 
 
40
# these tests are applied in each parameterized suite for LockableFiles
 
41
#
 
42
# they use an old style of parameterization, but we want to remove this class
 
43
# so won't modernize them now. - mbp 20080430
 
44
class _TestLockableFiles_mixin(object):
 
45
 
 
46
    def test_read_write(self):
 
47
        self.assertRaises(NoSuchFile,
 
48
            self.applyDeprecated,
 
49
            deprecated_in((1, 5, 0)),
 
50
            self.lockable.get, 'foo')
 
51
        self.assertRaises(NoSuchFile,
 
52
            self.applyDeprecated,
 
53
            deprecated_in((1, 5, 0)),
 
54
            self.lockable.get_utf8, 'foo')
 
55
        self.lockable.lock_write()
 
56
        try:
 
57
            unicode_string = u'bar\u1234'
 
58
            self.assertEqual(4, len(unicode_string))
 
59
            byte_string = unicode_string.encode('utf-8')
 
60
            self.assertEqual(6, len(byte_string))
 
61
            self.assertRaises(UnicodeEncodeError, self.lockable.put, 'foo',
 
62
                              StringIO(unicode_string))
 
63
            self.lockable.put('foo', StringIO(byte_string))
 
64
            byte_stream = self.applyDeprecated(
 
65
                deprecated_in((1, 5, 0)),
 
66
                self.lockable.get,
 
67
                'foo')
 
68
            self.assertEqual(byte_string, byte_stream.read())
 
69
            unicode_stream = self.applyDeprecated(
 
70
                deprecated_in((1, 5, 0)),
 
71
                self.lockable.get_utf8,
 
72
                'foo')
 
73
            self.assertEqual(unicode_string,
 
74
                unicode_stream.read())
 
75
            self.assertRaises(BzrBadParameterNotString,
 
76
                self.applyDeprecated,
 
77
                deprecated_in((1, 6, 0)),
 
78
                self.lockable.put_utf8,
 
79
                'bar',
 
80
                StringIO(unicode_string))
 
81
            self.applyDeprecated(
 
82
                deprecated_in((1, 6, 0)),
 
83
                self.lockable.put_utf8,
 
84
                'bar',
 
85
                unicode_string)
 
86
            unicode_stream = self.applyDeprecated(
 
87
                deprecated_in((1, 5, 0)),
 
88
                self.lockable.get_utf8,
 
89
                'bar')
 
90
            self.assertEqual(unicode_string,
 
91
                unicode_stream.read())
 
92
            byte_stream = self.applyDeprecated(
 
93
                deprecated_in((1, 5, 0)),
 
94
                self.lockable.get,
 
95
                'bar')
 
96
            self.assertEqual(byte_string, byte_stream.read())
 
97
            self.lockable.put_bytes('raw', 'raw\xffbytes')
 
98
            byte_stream = self.applyDeprecated(
 
99
                deprecated_in((1, 5, 0)),
 
100
                self.lockable.get,
 
101
                'raw')
 
102
            self.assertEqual('raw\xffbytes', byte_stream.read())
 
103
        finally:
 
104
            self.lockable.unlock()
 
105
 
 
106
    def test_locks(self):
 
107
        self.lockable.lock_read()
 
108
        try:
 
109
            self.assertRaises(ReadOnlyError, self.lockable.put, 'foo', 
 
110
                              StringIO('bar\u1234'))
 
111
        finally:
 
112
            self.lockable.unlock()
 
113
 
 
114
    def test_transactions(self):
 
115
        self.assertIs(self.lockable.get_transaction().__class__,
 
116
                      PassThroughTransaction)
 
117
        self.lockable.lock_read()
 
118
        try:
 
119
            self.assertIs(self.lockable.get_transaction().__class__,
 
120
                          ReadOnlyTransaction)
 
121
        finally:
 
122
            self.lockable.unlock()
 
123
        self.assertIs(self.lockable.get_transaction().__class__,
 
124
                      PassThroughTransaction)
 
125
        self.lockable.lock_write()
 
126
        self.assertIs(self.lockable.get_transaction().__class__,
 
127
                      WriteTransaction)
 
128
        # check that finish is called:
 
129
        vf = DummyWeave('a')
 
130
        self.lockable.get_transaction().register_dirty(vf)
 
131
        self.lockable.unlock()
 
132
        self.assertTrue(vf.finished)
 
133
 
 
134
    def test__escape(self):
 
135
        self.assertEqual('%25', self.lockable._escape('%'))
 
136
        
 
137
    def test__escape_empty(self):
 
138
        self.assertEqual('', self.lockable._escape(''))
 
139
 
 
140
    def test_break_lock(self):
 
141
        # some locks are not breakable
 
142
        self.lockable.lock_write()
 
143
        try:
 
144
            self.assertRaises(AssertionError, self.lockable.break_lock)
 
145
        except NotImplementedError:
 
146
            # this lock cannot be broken
 
147
            self.lockable.unlock()
 
148
            return
 
149
        l2 = self.get_lockable()
 
150
        orig_factory = bzrlib.ui.ui_factory
 
151
        # silent ui - no need for stdout
 
152
        bzrlib.ui.ui_factory = bzrlib.ui.SilentUIFactory()
 
153
        bzrlib.ui.ui_factory.stdin = StringIO("y\n")
 
154
        try:
 
155
            l2.break_lock()
 
156
        finally:
 
157
            bzrlib.ui.ui_factory = orig_factory
 
158
        try:
 
159
            l2.lock_write()
 
160
            l2.unlock()
 
161
        finally:
 
162
            self.assertRaises(errors.LockBroken, self.lockable.unlock)
 
163
            self.assertFalse(self.lockable.is_locked())
 
164
 
 
165
    def test_lock_write_returns_None_refuses_token(self):
 
166
        token = self.lockable.lock_write()
 
167
        try:
 
168
            if token is not None:
 
169
                # This test does not apply, because this lockable supports
 
170
                # tokens.
 
171
                return
 
172
            self.assertRaises(errors.TokenLockingNotSupported,
 
173
                              self.lockable.lock_write, token='token')
 
174
        finally:
 
175
            self.lockable.unlock()
 
176
 
 
177
    def test_lock_write_returns_token_when_given_token(self):
 
178
        token = self.lockable.lock_write()
 
179
        try:
 
180
            if token is None:
 
181
                # This test does not apply, because this lockable refuses
 
182
                # tokens.
 
183
                return
 
184
            new_lockable = self.get_lockable()
 
185
            token_from_new_lockable = new_lockable.lock_write(token=token)
 
186
            try:
 
187
                self.assertEqual(token, token_from_new_lockable)
 
188
            finally:
 
189
                new_lockable.unlock()
 
190
        finally:
 
191
            self.lockable.unlock()
 
192
 
 
193
    def test_lock_write_raises_on_token_mismatch(self):
 
194
        token = self.lockable.lock_write()
 
195
        try:
 
196
            if token is None:
 
197
                # This test does not apply, because this lockable refuses
 
198
                # tokens.
 
199
                return
 
200
            different_token = token + 'xxx'
 
201
            # Re-using the same lockable instance with a different token will
 
202
            # raise TokenMismatch.
 
203
            self.assertRaises(errors.TokenMismatch,
 
204
                              self.lockable.lock_write, token=different_token)
 
205
            # A seperate instance for the same lockable will also raise
 
206
            # TokenMismatch.
 
207
            # This detects the case where a caller claims to have a lock (via
 
208
            # the token) for an external resource, but doesn't (the token is
 
209
            # different).  Clients need a seperate lock object to make sure the
 
210
            # external resource is probed, whereas the existing lock object
 
211
            # might cache.
 
212
            new_lockable = self.get_lockable()
 
213
            self.assertRaises(errors.TokenMismatch,
 
214
                              new_lockable.lock_write, token=different_token)
 
215
        finally:
 
216
            self.lockable.unlock()
 
217
 
 
218
    def test_lock_write_with_matching_token(self):
 
219
        # If the token matches, so no exception is raised by lock_write.
 
220
        token = self.lockable.lock_write()
 
221
        try:
 
222
            if token is None:
 
223
                # This test does not apply, because this lockable refuses
 
224
                # tokens.
 
225
                return
 
226
            # The same instance will accept a second lock_write if the specified
 
227
            # token matches.
 
228
            self.lockable.lock_write(token=token)
 
229
            self.lockable.unlock()
 
230
            # Calling lock_write on a new instance for the same lockable will
 
231
            # also succeed.
 
232
            new_lockable = self.get_lockable()
 
233
            new_lockable.lock_write(token=token)
 
234
            new_lockable.unlock()
 
235
        finally:
 
236
            self.lockable.unlock()
 
237
 
 
238
    def test_unlock_after_lock_write_with_token(self):
 
239
        # If lock_write did not physically acquire the lock (because it was
 
240
        # passed a token), then unlock should not physically release it.
 
241
        token = self.lockable.lock_write()
 
242
        try:
 
243
            if token is None:
 
244
                # This test does not apply, because this lockable refuses
 
245
                # tokens.
 
246
                return
 
247
            new_lockable = self.get_lockable()
 
248
            new_lockable.lock_write(token=token)
 
249
            new_lockable.unlock()
 
250
            self.assertTrue(self.lockable.get_physical_lock_status())
 
251
        finally:
 
252
            self.lockable.unlock()
 
253
 
 
254
    def test_lock_write_with_token_fails_when_unlocked(self):
 
255
        # Lock and unlock to get a superficially valid token.  This mimics a
 
256
        # likely programming error, where a caller accidentally tries to lock
 
257
        # with a token that is no longer valid (because the original lock was
 
258
        # released).
 
259
        token = self.lockable.lock_write()
 
260
        self.lockable.unlock()
 
261
        if token is None:
 
262
            # This test does not apply, because this lockable refuses
 
263
            # tokens.
 
264
            return
 
265
 
 
266
        self.assertRaises(errors.TokenMismatch,
 
267
                          self.lockable.lock_write, token=token)
 
268
 
 
269
    def test_lock_write_reenter_with_token(self):
 
270
        token = self.lockable.lock_write()
 
271
        try:
 
272
            if token is None:
 
273
                # This test does not apply, because this lockable refuses
 
274
                # tokens.
 
275
                return
 
276
            # Relock with a token.
 
277
            token_from_reentry = self.lockable.lock_write(token=token)
 
278
            try:
 
279
                self.assertEqual(token, token_from_reentry)
 
280
            finally:
 
281
                self.lockable.unlock()
 
282
        finally:
 
283
            self.lockable.unlock()
 
284
        # The lock should be unlocked on disk.  Verify that with a new lock
 
285
        # instance.
 
286
        new_lockable = self.get_lockable()
 
287
        # Calling lock_write now should work, rather than raise LockContention.
 
288
        new_lockable.lock_write()
 
289
        new_lockable.unlock()
 
290
 
 
291
    def test_second_lock_write_returns_same_token(self):
 
292
        first_token = self.lockable.lock_write()
 
293
        try:
 
294
            if first_token is None:
 
295
                # This test does not apply, because this lockable refuses
 
296
                # tokens.
 
297
                return
 
298
            # Relock the already locked lockable.  It should return the same
 
299
            # token.
 
300
            second_token = self.lockable.lock_write()
 
301
            try:
 
302
                self.assertEqual(first_token, second_token)
 
303
            finally:
 
304
                self.lockable.unlock()
 
305
        finally:
 
306
            self.lockable.unlock()
 
307
 
 
308
    def test_leave_in_place(self):
 
309
        token = self.lockable.lock_write()
 
310
        try:
 
311
            if token is None:
 
312
                # This test does not apply, because this lockable refuses
 
313
                # tokens.
 
314
                return
 
315
            self.lockable.leave_in_place()
 
316
        finally:
 
317
            self.lockable.unlock()
 
318
        # At this point, the lock is still in place on disk
 
319
        self.assertRaises(errors.LockContention, self.lockable.lock_write)
 
320
        # But should be relockable with a token.
 
321
        self.lockable.lock_write(token=token)
 
322
        self.lockable.unlock()
 
323
 
 
324
    def test_dont_leave_in_place(self):
 
325
        token = self.lockable.lock_write()
 
326
        try:
 
327
            if token is None:
 
328
                # This test does not apply, because this lockable refuses
 
329
                # tokens.
 
330
                return
 
331
            self.lockable.leave_in_place()
 
332
        finally:
 
333
            self.lockable.unlock()
 
334
        # At this point, the lock is still in place on disk.
 
335
        # Acquire the existing lock with the token, and ask that it is removed
 
336
        # when this object unlocks, and unlock to trigger that removal.
 
337
        new_lockable = self.get_lockable()
 
338
        new_lockable.lock_write(token=token)
 
339
        new_lockable.dont_leave_in_place()
 
340
        new_lockable.unlock()
 
341
        # At this point, the lock is no longer on disk, so we can lock it.
 
342
        third_lockable = self.get_lockable()
 
343
        third_lockable.lock_write()
 
344
        third_lockable.unlock()
 
345
 
 
346
 
 
347
# This method of adapting tests to parameters is different to 
 
348
# the TestProviderAdapters used elsewhere, but seems simpler for this 
 
349
# case.  
 
350
class TestLockableFiles_TransportLock(TestCaseInTempDir,
 
351
                                      _TestLockableFiles_mixin):
 
352
 
 
353
    def setUp(self):
 
354
        TestCaseInTempDir.setUp(self)
 
355
        transport = get_transport('.')
 
356
        transport.mkdir('.bzr')
 
357
        self.sub_transport = transport.clone('.bzr')
 
358
        self.lockable = self.get_lockable()
 
359
        self.lockable.create_lock()
 
360
 
 
361
    def tearDown(self):
 
362
        super(TestLockableFiles_TransportLock, self).tearDown()
 
363
        # free the subtransport so that we do not get a 5 second
 
364
        # timeout due to the SFTP connection cache.
 
365
        del self.sub_transport
 
366
 
 
367
    def get_lockable(self):
 
368
        return LockableFiles(self.sub_transport, 'my-lock', TransportLock)
 
369
        
 
370
 
 
371
class TestLockableFiles_LockDir(TestCaseInTempDir,
 
372
                              _TestLockableFiles_mixin):
 
373
    """LockableFile tests run with LockDir underneath"""
 
374
 
 
375
    def setUp(self):
 
376
        TestCaseInTempDir.setUp(self)
 
377
        self.transport = get_transport('.')
 
378
        self.lockable = self.get_lockable()
 
379
        # the lock creation here sets mode - test_permissions on branch 
 
380
        # tests that implicitly, but it might be a good idea to factor 
 
381
        # out the mode checking logic and have it applied to loackable files
 
382
        # directly. RBC 20060418
 
383
        self.lockable.create_lock()
 
384
 
 
385
    def get_lockable(self):
 
386
        return LockableFiles(self.transport, 'my-lock', lockdir.LockDir)
 
387
 
 
388
    def test_lock_created(self):
 
389
        self.assertTrue(self.transport.has('my-lock'))
 
390
        self.lockable.lock_write()
 
391
        self.assertTrue(self.transport.has('my-lock/held/info'))
 
392
        self.lockable.unlock()
 
393
        self.assertFalse(self.transport.has('my-lock/held/info'))
 
394
        self.assertTrue(self.transport.has('my-lock'))
 
395
 
 
396
    def test__file_modes(self):
 
397
        self.transport.mkdir('readonly')
 
398
        osutils.make_readonly('readonly')
 
399
        lockable = LockableFiles(self.transport.clone('readonly'), 'test-lock',
 
400
                                 lockdir.LockDir)
 
401
        # The directory mode should be read-write-execute for the current user
 
402
        self.assertEqual(00700, lockable._dir_mode & 00700)
 
403
        # Files should be read-write for the current user
 
404
        self.assertEqual(00600, lockable._file_mode & 00700)
 
405
 
 
406
 
 
407
class TestLockableFiles_RemoteLockDir(TestCaseWithSmartMedium,
 
408
                              _TestLockableFiles_mixin):
 
409
    """LockableFile tests run with RemoteLockDir on a branch."""
 
410
 
 
411
    def setUp(self):
 
412
        TestCaseWithSmartMedium.setUp(self)
 
413
        # can only get a RemoteLockDir with some RemoteObject...
 
414
        # use a branch as thats what we want. These mixin tests test the end
 
415
        # to end behaviour, so stubbing out the backend and simulating would
 
416
        # defeat the purpose. We test the protocol implementation separately
 
417
        # in test_remote and test_smart as usual.
 
418
        b = self.make_branch('foo')
 
419
        self.addCleanup(b.bzrdir.transport.disconnect)
 
420
        self.transport = get_transport('.')
 
421
        self.lockable = self.get_lockable()
 
422
 
 
423
    def get_lockable(self):
 
424
        # getting a new lockable involves opening a new instance of the branch
 
425
        branch = bzrlib.branch.Branch.open(self.get_url('foo'))
 
426
        self.addCleanup(branch.bzrdir.transport.disconnect)
 
427
        return branch.control_files