1
# Copyright (C) 2005, 2006, 2008 Canonical Ltd
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.
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.
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
17
from StringIO import StringIO
25
from bzrlib.errors import BzrBadParameterNotString, NoSuchFile, ReadOnlyError
26
from bzrlib.lockable_files import LockableFiles, TransportLock
27
from bzrlib.symbol_versioning import (
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,
37
from bzrlib.transport import get_transport
40
# these tests are applied in each parameterized suite for LockableFiles
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):
46
def test_read_write(self):
47
self.assertRaises(NoSuchFile,
49
deprecated_in((1, 5, 0)),
50
self.lockable.get, 'foo')
51
self.assertRaises(NoSuchFile,
53
deprecated_in((1, 5, 0)),
54
self.lockable.get_utf8, 'foo')
55
self.lockable.lock_write()
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)),
68
self.assertEqual(byte_string, byte_stream.read())
69
unicode_stream = self.applyDeprecated(
70
deprecated_in((1, 5, 0)),
71
self.lockable.get_utf8,
73
self.assertEqual(unicode_string,
74
unicode_stream.read())
75
self.assertRaises(BzrBadParameterNotString,
77
deprecated_in((1, 6, 0)),
78
self.lockable.put_utf8,
80
StringIO(unicode_string))
82
deprecated_in((1, 6, 0)),
83
self.lockable.put_utf8,
86
unicode_stream = self.applyDeprecated(
87
deprecated_in((1, 5, 0)),
88
self.lockable.get_utf8,
90
self.assertEqual(unicode_string,
91
unicode_stream.read())
92
byte_stream = self.applyDeprecated(
93
deprecated_in((1, 5, 0)),
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)),
102
self.assertEqual('raw\xffbytes', byte_stream.read())
104
self.lockable.unlock()
106
def test_locks(self):
107
self.lockable.lock_read()
109
self.assertRaises(ReadOnlyError, self.lockable.put, 'foo',
110
StringIO('bar\u1234'))
112
self.lockable.unlock()
114
def test_transactions(self):
115
self.assertIs(self.lockable.get_transaction().__class__,
116
PassThroughTransaction)
117
self.lockable.lock_read()
119
self.assertIs(self.lockable.get_transaction().__class__,
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__,
128
# check that finish is called:
130
self.lockable.get_transaction().register_dirty(vf)
131
self.lockable.unlock()
132
self.assertTrue(vf.finished)
134
def test__escape(self):
135
self.assertEqual('%25', self.lockable._escape('%'))
137
def test__escape_empty(self):
138
self.assertEqual('', self.lockable._escape(''))
140
def test_break_lock(self):
141
# some locks are not breakable
142
self.lockable.lock_write()
144
self.assertRaises(AssertionError, self.lockable.break_lock)
145
except NotImplementedError:
146
# this lock cannot be broken
147
self.lockable.unlock()
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")
157
bzrlib.ui.ui_factory = orig_factory
162
self.assertRaises(errors.LockBroken, self.lockable.unlock)
163
self.assertFalse(self.lockable.is_locked())
165
def test_lock_write_returns_None_refuses_token(self):
166
token = self.lockable.lock_write()
168
if token is not None:
169
# This test does not apply, because this lockable supports
172
self.assertRaises(errors.TokenLockingNotSupported,
173
self.lockable.lock_write, token='token')
175
self.lockable.unlock()
177
def test_lock_write_returns_token_when_given_token(self):
178
token = self.lockable.lock_write()
181
# This test does not apply, because this lockable refuses
184
new_lockable = self.get_lockable()
185
token_from_new_lockable = new_lockable.lock_write(token=token)
187
self.assertEqual(token, token_from_new_lockable)
189
new_lockable.unlock()
191
self.lockable.unlock()
193
def test_lock_write_raises_on_token_mismatch(self):
194
token = self.lockable.lock_write()
197
# This test does not apply, because this lockable refuses
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
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
212
new_lockable = self.get_lockable()
213
self.assertRaises(errors.TokenMismatch,
214
new_lockable.lock_write, token=different_token)
216
self.lockable.unlock()
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()
223
# This test does not apply, because this lockable refuses
226
# The same instance will accept a second lock_write if the specified
228
self.lockable.lock_write(token=token)
229
self.lockable.unlock()
230
# Calling lock_write on a new instance for the same lockable will
232
new_lockable = self.get_lockable()
233
new_lockable.lock_write(token=token)
234
new_lockable.unlock()
236
self.lockable.unlock()
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()
244
# This test does not apply, because this lockable refuses
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())
252
self.lockable.unlock()
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
259
token = self.lockable.lock_write()
260
self.lockable.unlock()
262
# This test does not apply, because this lockable refuses
266
self.assertRaises(errors.TokenMismatch,
267
self.lockable.lock_write, token=token)
269
def test_lock_write_reenter_with_token(self):
270
token = self.lockable.lock_write()
273
# This test does not apply, because this lockable refuses
276
# Relock with a token.
277
token_from_reentry = self.lockable.lock_write(token=token)
279
self.assertEqual(token, token_from_reentry)
281
self.lockable.unlock()
283
self.lockable.unlock()
284
# The lock should be unlocked on disk. Verify that with a new lock
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()
291
def test_second_lock_write_returns_same_token(self):
292
first_token = self.lockable.lock_write()
294
if first_token is None:
295
# This test does not apply, because this lockable refuses
298
# Relock the already locked lockable. It should return the same
300
second_token = self.lockable.lock_write()
302
self.assertEqual(first_token, second_token)
304
self.lockable.unlock()
306
self.lockable.unlock()
308
def test_leave_in_place(self):
309
token = self.lockable.lock_write()
312
# This test does not apply, because this lockable refuses
315
self.lockable.leave_in_place()
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()
324
def test_dont_leave_in_place(self):
325
token = self.lockable.lock_write()
328
# This test does not apply, because this lockable refuses
331
self.lockable.leave_in_place()
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()
347
# This method of adapting tests to parameters is different to
348
# the TestProviderAdapters used elsewhere, but seems simpler for this
350
class TestLockableFiles_TransportLock(TestCaseInTempDir,
351
_TestLockableFiles_mixin):
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()
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
367
def get_lockable(self):
368
return LockableFiles(self.sub_transport, 'my-lock', TransportLock)
371
class TestLockableFiles_LockDir(TestCaseInTempDir,
372
_TestLockableFiles_mixin):
373
"""LockableFile tests run with LockDir underneath"""
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()
385
def get_lockable(self):
386
return LockableFiles(self.transport, 'my-lock', lockdir.LockDir)
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'))
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',
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)
407
class TestLockableFiles_RemoteLockDir(TestCaseWithSmartMedium,
408
_TestLockableFiles_mixin):
409
"""LockableFile tests run with RemoteLockDir on a branch."""
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()
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