~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/tests/test_lockable_files.py

  • Committer: Robert Collins
  • Date: 2007-04-19 02:27:44 UTC
  • mto: This revision was merged to the branch mainline in revision 2426.
  • Revision ID: robertc@robertcollins.net-20070419022744-pfdqz42kp1wizh43
``make docs`` now creates a man page at ``man1/bzr.1`` fixing bug 107388.
(Robert Collins)

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
# Copyright (C) 2005, 2006, 2008 Canonical Ltd
 
1
# Copyright (C) 2005, 2006 Canonical Ltd
2
2
#
3
3
# This program is free software; you can redistribute it and/or modify
4
4
# it under the terms of the GNU General Public License as published by
12
12
#
13
13
# You should have received a copy of the GNU General Public License
14
14
# along with this program; if not, write to the Free Software
15
 
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
 
15
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
16
16
 
17
17
from StringIO import StringIO
18
18
 
19
19
import bzrlib
20
 
from bzrlib import (
21
 
    errors,
22
 
    lockdir,
23
 
    osutils,
24
 
    )
 
20
import bzrlib.errors as errors
25
21
from bzrlib.errors import BzrBadParameterNotString, NoSuchFile, ReadOnlyError
26
22
from bzrlib.lockable_files import LockableFiles, TransportLock
27
 
from bzrlib.symbol_versioning import (
28
 
    deprecated_in,
29
 
    )
30
 
from bzrlib.tests import (
31
 
    TestCaseInTempDir,
32
 
    TestNotApplicable,
33
 
    )
34
 
from bzrlib.tests.test_smart import TestCaseWithSmartMedium
 
23
from bzrlib.lockdir import LockDir
 
24
from bzrlib.tests import TestCaseInTempDir
35
25
from bzrlib.tests.test_transactions import DummyWeave
36
26
from bzrlib.transactions import (PassThroughTransaction,
37
27
                                 ReadOnlyTransaction,
41
31
 
42
32
 
43
33
# these tests are applied in each parameterized suite for LockableFiles
44
 
#
45
 
# they use an old style of parameterization, but we want to remove this class
46
 
# so won't modernize them now. - mbp 20080430
47
34
class _TestLockableFiles_mixin(object):
48
35
 
49
36
    def test_read_write(self):
50
 
        self.assertRaises(NoSuchFile,
51
 
            self.applyDeprecated,
52
 
            deprecated_in((1, 5, 0)),
53
 
            self.lockable.get, 'foo')
54
 
        self.assertRaises(NoSuchFile,
55
 
            self.applyDeprecated,
56
 
            deprecated_in((1, 5, 0)),
57
 
            self.lockable.get_utf8, 'foo')
 
37
        self.assertRaises(NoSuchFile, self.lockable.get, 'foo')
 
38
        self.assertRaises(NoSuchFile, self.lockable.get_utf8, 'foo')
58
39
        self.lockable.lock_write()
59
 
        self.addCleanup(self.lockable.unlock)
60
 
        unicode_string = u'bar\u1234'
61
 
        self.assertEqual(4, len(unicode_string))
62
 
        byte_string = unicode_string.encode('utf-8')
63
 
        self.assertEqual(6, len(byte_string))
64
 
        self.assertRaises(UnicodeEncodeError,
65
 
            self.applyDeprecated,
66
 
            deprecated_in((1, 6, 0)),
67
 
            self.lockable.put, 'foo',
68
 
            StringIO(unicode_string))
69
 
        self.applyDeprecated(
70
 
            deprecated_in((1, 6, 0)),
71
 
            self.lockable.put,
72
 
            'foo', StringIO(byte_string))
73
 
        byte_stream = self.applyDeprecated(
74
 
            deprecated_in((1, 5, 0)),
75
 
            self.lockable.get,
76
 
            'foo')
77
 
        self.assertEqual(byte_string, byte_stream.read())
78
 
        unicode_stream = self.applyDeprecated(
79
 
            deprecated_in((1, 5, 0)),
80
 
            self.lockable.get_utf8,
81
 
            'foo')
82
 
        self.assertEqual(unicode_string,
83
 
            unicode_stream.read())
84
 
        self.assertRaises(BzrBadParameterNotString,
85
 
            self.applyDeprecated,
86
 
            deprecated_in((1, 6, 0)),
87
 
            self.lockable.put_utf8,
88
 
            'bar',
89
 
            StringIO(unicode_string))
90
 
        self.applyDeprecated(
91
 
            deprecated_in((1, 6, 0)),
92
 
            self.lockable.put_utf8,
93
 
            'bar',
94
 
            unicode_string)
95
 
        unicode_stream = self.applyDeprecated(
96
 
            deprecated_in((1, 5, 0)),
97
 
            self.lockable.get_utf8,
98
 
            'bar')
99
 
        self.assertEqual(unicode_string,
100
 
            unicode_stream.read())
101
 
        byte_stream = self.applyDeprecated(
102
 
            deprecated_in((1, 5, 0)),
103
 
            self.lockable.get,
104
 
            'bar')
105
 
        self.assertEqual(byte_string, byte_stream.read())
106
 
        self.applyDeprecated(
107
 
            deprecated_in((1, 6, 0)),
108
 
            self.lockable.put_bytes,
109
 
            'raw', 'raw\xffbytes')
110
 
        byte_stream = self.applyDeprecated(
111
 
            deprecated_in((1, 5, 0)),
112
 
            self.lockable.get,
113
 
            'raw')
114
 
        self.assertEqual('raw\xffbytes', byte_stream.read())
 
40
        try:
 
41
            unicode_string = u'bar\u1234'
 
42
            self.assertEqual(4, len(unicode_string))
 
43
            byte_string = unicode_string.encode('utf-8')
 
44
            self.assertEqual(6, len(byte_string))
 
45
            self.assertRaises(UnicodeEncodeError, self.lockable.put, 'foo', 
 
46
                              StringIO(unicode_string))
 
47
            self.lockable.put('foo', StringIO(byte_string))
 
48
            self.assertEqual(byte_string,
 
49
                             self.lockable.get('foo').read())
 
50
            self.assertEqual(unicode_string,
 
51
                             self.lockable.get_utf8('foo').read())
 
52
            self.assertRaises(BzrBadParameterNotString,
 
53
                              self.lockable.put_utf8,
 
54
                              'bar',
 
55
                              StringIO(unicode_string)
 
56
                              )
 
57
            self.lockable.put_utf8('bar', unicode_string)
 
58
            self.assertEqual(unicode_string,
 
59
                             self.lockable.get_utf8('bar').read())
 
60
            self.assertEqual(byte_string,
 
61
                             self.lockable.get('bar').read())
 
62
            self.lockable.put_bytes('raw', 'raw\xffbytes')
 
63
            self.assertEqual('raw\xffbytes',
 
64
                             self.lockable.get('raw').read())
 
65
        finally:
 
66
            self.lockable.unlock()
115
67
 
116
68
    def test_locks(self):
117
69
        self.lockable.lock_read()
118
 
        self.addCleanup(self.lockable.unlock)
119
 
        self.assertRaises(ReadOnlyError, self.lockable.put, 'foo',
120
 
                          StringIO('bar\u1234'))
 
70
        try:
 
71
            self.assertRaises(ReadOnlyError, self.lockable.put, 'foo', 
 
72
                              StringIO('bar\u1234'))
 
73
        finally:
 
74
            self.lockable.unlock()
121
75
 
122
76
    def test_transactions(self):
123
77
        self.assertIs(self.lockable.get_transaction().__class__,
141
95
 
142
96
    def test__escape(self):
143
97
        self.assertEqual('%25', self.lockable._escape('%'))
144
 
 
 
98
        
145
99
    def test__escape_empty(self):
146
100
        self.assertEqual('', self.lockable._escape(''))
147
101
 
153
107
        except NotImplementedError:
154
108
            # this lock cannot be broken
155
109
            self.lockable.unlock()
156
 
            raise TestNotApplicable("%r is not breakable" % (self.lockable,))
 
110
            return
157
111
        l2 = self.get_lockable()
158
112
        orig_factory = bzrlib.ui.ui_factory
159
113
        # silent ui - no need for stdout
172
126
 
173
127
    def test_lock_write_returns_None_refuses_token(self):
174
128
        token = self.lockable.lock_write()
175
 
        self.addCleanup(self.lockable.unlock)
176
 
        if token is not None:
177
 
            # This test does not apply, because this lockable supports
178
 
            # tokens.
179
 
            raise TestNotApplicable("%r uses tokens" % (self.lockable,))
180
 
        self.assertRaises(errors.TokenLockingNotSupported,
181
 
                          self.lockable.lock_write, token='token')
 
129
        try:
 
130
            if token is not None:
 
131
                # This test does not apply, because this lockable supports
 
132
                # tokens.
 
133
                return
 
134
            self.assertRaises(errors.TokenLockingNotSupported,
 
135
                              self.lockable.lock_write, token='token')
 
136
        finally:
 
137
            self.lockable.unlock()
182
138
 
183
139
    def test_lock_write_returns_token_when_given_token(self):
184
140
        token = self.lockable.lock_write()
185
 
        self.addCleanup(self.lockable.unlock)
186
 
        if token is None:
187
 
            # This test does not apply, because this lockable refuses
188
 
            # tokens.
189
 
            return
190
 
        new_lockable = self.get_lockable()
191
 
        token_from_new_lockable = new_lockable.lock_write(token=token)
192
 
        self.addCleanup(new_lockable.unlock)
193
 
        self.assertEqual(token, token_from_new_lockable)
 
141
        try:
 
142
            if token is None:
 
143
                # This test does not apply, because this lockable refuses
 
144
                # tokens.
 
145
                return
 
146
            new_lockable = self.get_lockable()
 
147
            token_from_new_lockable = new_lockable.lock_write(token=token)
 
148
            try:
 
149
                self.assertEqual(token, token_from_new_lockable)
 
150
            finally:
 
151
                new_lockable.unlock()
 
152
        finally:
 
153
            self.lockable.unlock()
194
154
 
195
155
    def test_lock_write_raises_on_token_mismatch(self):
196
156
        token = self.lockable.lock_write()
197
 
        self.addCleanup(self.lockable.unlock)
198
 
        if token is None:
199
 
            # This test does not apply, because this lockable refuses
200
 
            # tokens.
201
 
            return
202
 
        different_token = token + 'xxx'
203
 
        # Re-using the same lockable instance with a different token will
204
 
        # raise TokenMismatch.
205
 
        self.assertRaises(errors.TokenMismatch,
206
 
                          self.lockable.lock_write, token=different_token)
207
 
        # A separate instance for the same lockable will also raise
208
 
        # TokenMismatch.
209
 
        # This detects the case where a caller claims to have a lock (via
210
 
        # the token) for an external resource, but doesn't (the token is
211
 
        # different).  Clients need a separate lock object to make sure the
212
 
        # external resource is probed, whereas the existing lock object
213
 
        # might cache.
214
 
        new_lockable = self.get_lockable()
215
 
        self.assertRaises(errors.TokenMismatch,
216
 
                          new_lockable.lock_write, token=different_token)
 
157
        try:
 
158
            if token is None:
 
159
                # This test does not apply, because this lockable refuses
 
160
                # tokens.
 
161
                return
 
162
            different_token = token + 'xxx'
 
163
            # Re-using the same lockable instance with a different token will
 
164
            # raise TokenMismatch.
 
165
            self.assertRaises(errors.TokenMismatch,
 
166
                              self.lockable.lock_write, token=different_token)
 
167
            # A seperate instance for the same lockable will also raise
 
168
            # TokenMismatch.
 
169
            # This detects the case where a caller claims to have a lock (via
 
170
            # the token) for an external resource, but doesn't (the token is
 
171
            # different).  Clients need a seperate lock object to make sure the
 
172
            # external resource is probed, whereas the existing lock object
 
173
            # might cache.
 
174
            new_lockable = self.get_lockable()
 
175
            self.assertRaises(errors.TokenMismatch,
 
176
                              new_lockable.lock_write, token=different_token)
 
177
        finally:
 
178
            self.lockable.unlock()
217
179
 
218
180
    def test_lock_write_with_matching_token(self):
219
181
        # If the token matches, so no exception is raised by lock_write.
220
182
        token = self.lockable.lock_write()
221
 
        self.addCleanup(self.lockable.unlock)
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()
 
183
        try:
 
184
            if token is None:
 
185
                # This test does not apply, because this lockable refuses
 
186
                # tokens.
 
187
                return
 
188
            # The same instance will accept a second lock_write if the specified
 
189
            # token matches.
 
190
            self.lockable.lock_write(token=token)
 
191
            self.lockable.unlock()
 
192
            # Calling lock_write on a new instance for the same lockable will
 
193
            # also succeed.
 
194
            new_lockable = self.get_lockable()
 
195
            new_lockable.lock_write(token=token)
 
196
            new_lockable.unlock()
 
197
        finally:
 
198
            self.lockable.unlock()
235
199
 
236
200
    def test_unlock_after_lock_write_with_token(self):
237
201
        # If lock_write did not physically acquire the lock (because it was
238
202
        # passed a token), then unlock should not physically release it.
239
203
        token = self.lockable.lock_write()
240
 
        self.addCleanup(self.lockable.unlock)
241
 
        if token is None:
242
 
            # This test does not apply, because this lockable refuses
243
 
            # tokens.
244
 
            return
245
 
        new_lockable = self.get_lockable()
246
 
        new_lockable.lock_write(token=token)
247
 
        new_lockable.unlock()
248
 
        self.assertTrue(self.lockable.get_physical_lock_status())
 
204
        try:
 
205
            if token is None:
 
206
                # This test does not apply, because this lockable refuses
 
207
                # tokens.
 
208
                return
 
209
            new_lockable = self.get_lockable()
 
210
            new_lockable.lock_write(token=token)
 
211
            new_lockable.unlock()
 
212
            self.assertTrue(self.lockable.get_physical_lock_status())
 
213
        finally:
 
214
            self.lockable.unlock()
249
215
 
250
216
    def test_lock_write_with_token_fails_when_unlocked(self):
251
217
        # Lock and unlock to get a superficially valid token.  This mimics a
316
282
        # But should be relockable with a token.
317
283
        self.lockable.lock_write(token=token)
318
284
        self.lockable.unlock()
319
 
        # Cleanup: we should still be able to get the lock, but we restore the
320
 
        # behavior to clearing the lock when unlocking.
321
 
        self.lockable.lock_write(token=token)
322
 
        self.lockable.dont_leave_in_place()
323
 
        self.lockable.unlock()
324
285
 
325
286
    def test_dont_leave_in_place(self):
326
287
        token = self.lockable.lock_write()
345
306
        third_lockable.unlock()
346
307
 
347
308
 
348
 
# This method of adapting tests to parameters is different to
349
 
# the TestProviderAdapters used elsewhere, but seems simpler for this
350
 
# case.
 
309
# This method of adapting tests to parameters is different to 
 
310
# the TestProviderAdapters used elsewhere, but seems simpler for this 
 
311
# case.  
351
312
class TestLockableFiles_TransportLock(TestCaseInTempDir,
352
313
                                      _TestLockableFiles_mixin):
353
314
 
363
324
        super(TestLockableFiles_TransportLock, self).tearDown()
364
325
        # free the subtransport so that we do not get a 5 second
365
326
        # timeout due to the SFTP connection cache.
366
 
        try:
367
 
            del self.sub_transport
368
 
        except AttributeError:
369
 
            pass
 
327
        del self.sub_transport
370
328
 
371
329
    def get_lockable(self):
372
330
        return LockableFiles(self.sub_transport, 'my-lock', TransportLock)
373
 
 
 
331
        
374
332
 
375
333
class TestLockableFiles_LockDir(TestCaseInTempDir,
376
334
                              _TestLockableFiles_mixin):
380
338
        TestCaseInTempDir.setUp(self)
381
339
        self.transport = get_transport('.')
382
340
        self.lockable = self.get_lockable()
383
 
        # the lock creation here sets mode - test_permissions on branch
384
 
        # tests that implicitly, but it might be a good idea to factor
 
341
        # the lock creation here sets mode - test_permissions on branch 
 
342
        # tests that implicitly, but it might be a good idea to factor 
385
343
        # out the mode checking logic and have it applied to loackable files
386
344
        # directly. RBC 20060418
387
345
        self.lockable.create_lock()
388
346
 
389
347
    def get_lockable(self):
390
 
        return LockableFiles(self.transport, 'my-lock', lockdir.LockDir)
 
348
        return LockableFiles(self.transport, 'my-lock', LockDir)
391
349
 
392
350
    def test_lock_created(self):
393
351
        self.assertTrue(self.transport.has('my-lock'))
397
355
        self.assertFalse(self.transport.has('my-lock/held/info'))
398
356
        self.assertTrue(self.transport.has('my-lock'))
399
357
 
400
 
    def test__file_modes(self):
401
 
        self.transport.mkdir('readonly')
402
 
        osutils.make_readonly('readonly')
403
 
        lockable = LockableFiles(self.transport.clone('readonly'), 'test-lock',
404
 
                                 lockdir.LockDir)
405
 
        # The directory mode should be read-write-execute for the current user
406
 
        self.assertEqual(00700, lockable._dir_mode & 00700)
407
 
        # Files should be read-write for the current user
408
 
        self.assertEqual(00600, lockable._file_mode & 00700)
409
 
 
410
 
 
411
 
class TestLockableFiles_RemoteLockDir(TestCaseWithSmartMedium,
412
 
                              _TestLockableFiles_mixin):
413
 
    """LockableFile tests run with RemoteLockDir on a branch."""
414
 
 
415
 
    def setUp(self):
416
 
        TestCaseWithSmartMedium.setUp(self)
417
 
        # can only get a RemoteLockDir with some RemoteObject...
418
 
        # use a branch as thats what we want. These mixin tests test the end
419
 
        # to end behaviour, so stubbing out the backend and simulating would
420
 
        # defeat the purpose. We test the protocol implementation separately
421
 
        # in test_remote and test_smart as usual.
422
 
        b = self.make_branch('foo')
423
 
        self.addCleanup(b.bzrdir.transport.disconnect)
424
 
        self.transport = get_transport('.')
425
 
        self.lockable = self.get_lockable()
426
 
 
427
 
    def get_lockable(self):
428
 
        # getting a new lockable involves opening a new instance of the branch
429
 
        branch = bzrlib.branch.Branch.open(self.get_url('foo'))
430
 
        self.addCleanup(branch.bzrdir.transport.disconnect)
431
 
        return branch.control_files
 
358
 
 
359
    # TODO: Test the lockdir inherits the right file and directory permissions
 
360
    # from the LockableFiles.