1
# Copyright (C) 2005, 2006 by Canonical Ltd
1
# Copyright (C) 2005, 2006, 2008 Canonical Ltd
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
5
5
# the Free Software Foundation; either version 2 of the License, or
6
6
# (at your option) any later version.
8
8
# This program is distributed in the hope that it will be useful,
9
9
# but WITHOUT ANY WARRANTY; without even the implied warranty of
10
10
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11
11
# GNU General Public License for more details.
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
15
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
34
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
35
44
class _TestLockableFiles_mixin(object):
37
46
def test_read_write(self):
38
self.assertRaises(NoSuchFile, self.lockable.get, 'foo')
39
self.assertRaises(NoSuchFile, self.lockable.get_utf8, 'foo')
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')
40
55
self.lockable.lock_write()
42
57
unicode_string = u'bar\u1234'
43
58
self.assertEqual(4, len(unicode_string))
44
59
byte_string = unicode_string.encode('utf-8')
45
60
self.assertEqual(6, len(byte_string))
46
self.assertRaises(UnicodeEncodeError, self.lockable.put, 'foo',
47
StringIO(unicode_string))
48
self.lockable.put('foo', StringIO(byte_string))
49
self.assertEqual(byte_string,
50
self.lockable.get('foo').read())
61
self.assertRaises(UnicodeEncodeError,
63
deprecated_in((1, 6, 0)),
64
self.lockable.put, 'foo',
65
StringIO(unicode_string))
67
deprecated_in((1, 6, 0)),
69
'foo', StringIO(byte_string))
70
byte_stream = self.applyDeprecated(
71
deprecated_in((1, 5, 0)),
74
self.assertEqual(byte_string, byte_stream.read())
75
unicode_stream = self.applyDeprecated(
76
deprecated_in((1, 5, 0)),
77
self.lockable.get_utf8,
51
79
self.assertEqual(unicode_string,
52
self.lockable.get_utf8('foo').read())
80
unicode_stream.read())
53
81
self.assertRaises(BzrBadParameterNotString,
54
self.lockable.put_utf8,
56
StringIO(unicode_string)
58
self.lockable.put_utf8('bar', unicode_string)
59
self.assertEqual(unicode_string,
60
self.lockable.get_utf8('bar').read())
61
self.assertEqual(byte_string,
62
self.lockable.get('bar').read())
83
deprecated_in((1, 6, 0)),
84
self.lockable.put_utf8,
86
StringIO(unicode_string))
88
deprecated_in((1, 6, 0)),
89
self.lockable.put_utf8,
92
unicode_stream = self.applyDeprecated(
93
deprecated_in((1, 5, 0)),
94
self.lockable.get_utf8,
96
self.assertEqual(unicode_string,
97
unicode_stream.read())
98
byte_stream = self.applyDeprecated(
99
deprecated_in((1, 5, 0)),
102
self.assertEqual(byte_string, byte_stream.read())
103
self.applyDeprecated(
104
deprecated_in((1, 6, 0)),
105
self.lockable.put_bytes,
106
'raw', 'raw\xffbytes')
107
byte_stream = self.applyDeprecated(
108
deprecated_in((1, 5, 0)),
111
self.assertEqual('raw\xffbytes', byte_stream.read())
64
113
self.lockable.unlock()
66
115
def test_locks(self):
67
116
self.lockable.lock_read()
69
self.assertRaises(ReadOnlyError, self.lockable.put, 'foo',
118
self.assertRaises(ReadOnlyError, self.lockable.put, 'foo',
70
119
StringIO('bar\u1234'))
72
121
self.lockable.unlock()
122
171
self.assertRaises(errors.LockBroken, self.lockable.unlock)
123
172
self.assertFalse(self.lockable.is_locked())
174
def test_lock_write_returns_None_refuses_token(self):
175
token = self.lockable.lock_write()
177
if token is not None:
178
# This test does not apply, because this lockable supports
181
self.assertRaises(errors.TokenLockingNotSupported,
182
self.lockable.lock_write, token='token')
184
self.lockable.unlock()
186
def test_lock_write_returns_token_when_given_token(self):
187
token = self.lockable.lock_write()
190
# This test does not apply, because this lockable refuses
193
new_lockable = self.get_lockable()
194
token_from_new_lockable = new_lockable.lock_write(token=token)
196
self.assertEqual(token, token_from_new_lockable)
198
new_lockable.unlock()
200
self.lockable.unlock()
202
def test_lock_write_raises_on_token_mismatch(self):
203
token = self.lockable.lock_write()
206
# This test does not apply, because this lockable refuses
209
different_token = token + 'xxx'
210
# Re-using the same lockable instance with a different token will
211
# raise TokenMismatch.
212
self.assertRaises(errors.TokenMismatch,
213
self.lockable.lock_write, token=different_token)
214
# A seperate instance for the same lockable will also raise
216
# This detects the case where a caller claims to have a lock (via
217
# the token) for an external resource, but doesn't (the token is
218
# different). Clients need a seperate lock object to make sure the
219
# external resource is probed, whereas the existing lock object
221
new_lockable = self.get_lockable()
222
self.assertRaises(errors.TokenMismatch,
223
new_lockable.lock_write, token=different_token)
225
self.lockable.unlock()
227
def test_lock_write_with_matching_token(self):
228
# If the token matches, so no exception is raised by lock_write.
229
token = self.lockable.lock_write()
232
# This test does not apply, because this lockable refuses
235
# The same instance will accept a second lock_write if the specified
237
self.lockable.lock_write(token=token)
238
self.lockable.unlock()
239
# Calling lock_write on a new instance for the same lockable will
241
new_lockable = self.get_lockable()
242
new_lockable.lock_write(token=token)
243
new_lockable.unlock()
245
self.lockable.unlock()
247
def test_unlock_after_lock_write_with_token(self):
248
# If lock_write did not physically acquire the lock (because it was
249
# passed a token), then unlock should not physically release it.
250
token = self.lockable.lock_write()
253
# This test does not apply, because this lockable refuses
256
new_lockable = self.get_lockable()
257
new_lockable.lock_write(token=token)
258
new_lockable.unlock()
259
self.assertTrue(self.lockable.get_physical_lock_status())
261
self.lockable.unlock()
263
def test_lock_write_with_token_fails_when_unlocked(self):
264
# Lock and unlock to get a superficially valid token. This mimics a
265
# likely programming error, where a caller accidentally tries to lock
266
# with a token that is no longer valid (because the original lock was
268
token = self.lockable.lock_write()
269
self.lockable.unlock()
271
# This test does not apply, because this lockable refuses
275
self.assertRaises(errors.TokenMismatch,
276
self.lockable.lock_write, token=token)
278
def test_lock_write_reenter_with_token(self):
279
token = self.lockable.lock_write()
282
# This test does not apply, because this lockable refuses
285
# Relock with a token.
286
token_from_reentry = self.lockable.lock_write(token=token)
288
self.assertEqual(token, token_from_reentry)
290
self.lockable.unlock()
292
self.lockable.unlock()
293
# The lock should be unlocked on disk. Verify that with a new lock
295
new_lockable = self.get_lockable()
296
# Calling lock_write now should work, rather than raise LockContention.
297
new_lockable.lock_write()
298
new_lockable.unlock()
300
def test_second_lock_write_returns_same_token(self):
301
first_token = self.lockable.lock_write()
303
if first_token is None:
304
# This test does not apply, because this lockable refuses
307
# Relock the already locked lockable. It should return the same
309
second_token = self.lockable.lock_write()
311
self.assertEqual(first_token, second_token)
313
self.lockable.unlock()
315
self.lockable.unlock()
317
def test_leave_in_place(self):
318
token = self.lockable.lock_write()
321
# This test does not apply, because this lockable refuses
324
self.lockable.leave_in_place()
326
self.lockable.unlock()
327
# At this point, the lock is still in place on disk
328
self.assertRaises(errors.LockContention, self.lockable.lock_write)
329
# But should be relockable with a token.
330
self.lockable.lock_write(token=token)
331
self.lockable.unlock()
333
def test_dont_leave_in_place(self):
334
token = self.lockable.lock_write()
337
# This test does not apply, because this lockable refuses
340
self.lockable.leave_in_place()
342
self.lockable.unlock()
343
# At this point, the lock is still in place on disk.
344
# Acquire the existing lock with the token, and ask that it is removed
345
# when this object unlocks, and unlock to trigger that removal.
346
new_lockable = self.get_lockable()
347
new_lockable.lock_write(token=token)
348
new_lockable.dont_leave_in_place()
349
new_lockable.unlock()
350
# At this point, the lock is no longer on disk, so we can lock it.
351
third_lockable = self.get_lockable()
352
third_lockable.lock_write()
353
third_lockable.unlock()
126
356
# This method of adapting tests to parameters is different to
127
357
# the TestProviderAdapters used elsewhere, but seems simpler for this
172
402
self.assertFalse(self.transport.has('my-lock/held/info'))
173
403
self.assertTrue(self.transport.has('my-lock'))
176
# TODO: Test the lockdir inherits the right file and directory permissions
177
# from the LockableFiles.
405
def test__file_modes(self):
406
self.transport.mkdir('readonly')
407
osutils.make_readonly('readonly')
408
lockable = LockableFiles(self.transport.clone('readonly'), 'test-lock',
410
# The directory mode should be read-write-execute for the current user
411
self.assertEqual(00700, lockable._dir_mode & 00700)
412
# Files should be read-write for the current user
413
self.assertEqual(00600, lockable._file_mode & 00700)
416
class TestLockableFiles_RemoteLockDir(TestCaseWithSmartMedium,
417
_TestLockableFiles_mixin):
418
"""LockableFile tests run with RemoteLockDir on a branch."""
421
TestCaseWithSmartMedium.setUp(self)
422
# can only get a RemoteLockDir with some RemoteObject...
423
# use a branch as thats what we want. These mixin tests test the end
424
# to end behaviour, so stubbing out the backend and simulating would
425
# defeat the purpose. We test the protocol implementation separately
426
# in test_remote and test_smart as usual.
427
b = self.make_branch('foo')
428
self.addCleanup(b.bzrdir.transport.disconnect)
429
self.transport = get_transport('.')
430
self.lockable = self.get_lockable()
432
def get_lockable(self):
433
# getting a new lockable involves opening a new instance of the branch
434
branch = bzrlib.branch.Branch.open(self.get_url('foo'))
435
self.addCleanup(branch.bzrdir.transport.disconnect)
436
return branch.control_files