1
# Copyright (C) 2006-2012, 2016 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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
17
"""Tests for LockDir"""
32
from ..errors import (
39
from ..lockdir import (
43
from ..sixish import (
50
TestCaseWithTransport,
53
# These tests are run on the default transport provided by the test framework
54
# (typically a local disk transport). That can be changed by the --transport
55
# option to bzr selftest. The required properties of the transport
56
# implementation are tested separately. (The main requirement is just that
57
# they don't allow overwriting nonempty directories.)
60
class TestLockDir(TestCaseWithTransport):
61
"""Test LockDir operations"""
63
def logging_report_function(self, fmt, *args):
64
self._logged_reports.append((fmt, args))
66
def setup_log_reporter(self, lock_dir):
67
self._logged_reports = []
68
lock_dir._report_function = self.logging_report_function
70
def test_00_lock_creation(self):
71
"""Creation of lock file on a transport"""
72
t = self.get_transport()
73
lf = LockDir(t, 'test_lock')
74
self.assertFalse(lf.is_held)
76
def test_01_lock_repr(self):
77
"""Lock string representation"""
78
lf = LockDir(self.get_transport(), 'test_lock')
80
self.assertContainsRe(r, r'^LockDir\(.*/test_lock\)$')
82
def test_02_unlocked_peek(self):
83
lf = LockDir(self.get_transport(), 'test_lock')
84
self.assertEqual(lf.peek(), None)
87
return LockDir(self.get_transport(), 'test_lock')
89
def test_unlock_after_break_raises(self):
94
ld2.force_break(ld2.peek())
95
self.assertRaises(LockBroken, ld.unlock)
97
def test_03_readonly_peek(self):
98
lf = LockDir(self.get_readonly_transport(), 'test_lock')
99
self.assertEqual(lf.peek(), None)
101
def test_10_lock_uncontested(self):
102
"""Acquire and release a lock"""
103
t = self.get_transport()
104
lf = LockDir(t, 'test_lock')
108
self.assertTrue(lf.is_held)
111
self.assertFalse(lf.is_held)
113
def test_11_create_readonly_transport(self):
114
"""Fail to create lock on readonly transport"""
115
t = self.get_readonly_transport()
116
lf = LockDir(t, 'test_lock')
117
self.assertRaises(LockFailed, lf.create)
119
def test_12_lock_readonly_transport(self):
120
"""Fail to lock on readonly transport"""
121
lf = LockDir(self.get_transport(), 'test_lock')
123
lf = LockDir(self.get_readonly_transport(), 'test_lock')
124
self.assertRaises(LockFailed, lf.attempt_lock)
126
def test_20_lock_contested(self):
127
"""Contention to get a lock"""
128
t = self.get_transport()
129
lf1 = LockDir(t, 'test_lock')
132
lf2 = LockDir(t, 'test_lock')
134
# locking is between LockDir instances; aliases within
135
# a single process are not detected
137
self.fail('Failed to detect lock collision')
138
except LockContention as e:
139
self.assertEqual(e.lock, lf2)
140
self.assertContainsRe(str(e),
141
r'^Could not acquire.*test_lock.*$')
144
def test_20_lock_peek(self):
145
"""Peek at the state of a lock"""
146
t = self.get_transport()
147
lf1 = LockDir(t, 'test_lock')
150
self.addCleanup(lf1.unlock)
151
# lock is held, should get some info on it
153
self.assertEqual(set(info1.info_dict.keys()),
154
{'user', 'nonce', 'hostname', 'pid', 'start_time'})
155
# should get the same info if we look at it through a different
157
info2 = LockDir(t, 'test_lock').peek()
158
self.assertEqual(info1, info2)
159
# locks which are never used should be not-held
160
self.assertEqual(LockDir(t, 'other_lock').peek(), None)
162
def test_21_peek_readonly(self):
163
"""Peek over a readonly transport"""
164
t = self.get_transport()
165
lf1 = LockDir(t, 'test_lock')
167
lf2 = LockDir(self.get_readonly_transport(), 'test_lock')
168
self.assertEqual(lf2.peek(), None)
170
self.addCleanup(lf1.unlock)
172
self.assertTrue(info2)
173
self.assertEqual(info2.get('nonce'), lf1.nonce)
175
def test_30_lock_wait_fail(self):
176
"""Wait on a lock, then fail
178
We ask to wait up to 400ms; this should fail within at most one
179
second. (Longer times are more realistic but we don't want the test
180
suite to take too long, and this should do for now.)
182
t = self.get_transport()
183
lf1 = LockDir(t, 'test_lock')
185
lf2 = LockDir(t, 'test_lock')
186
self.setup_log_reporter(lf2)
190
self.assertRaises(LockContention, lf2.wait_lock,
191
timeout=0.4, poll=0.1)
193
# it should only take about 0.4 seconds, but we allow more time in
194
# case the machine is heavily loaded
195
self.assertTrue(after - before <= 8.0,
196
"took %f seconds to detect lock contention" % (after - before))
199
self.assertEqual(1, len(self._logged_reports))
200
self.assertContainsRe(self._logged_reports[0][0],
201
r'Unable to obtain lock .* held by jrandom@example\.com on .*'
202
r' \(process #\d+\), acquired .* ago\.\n'
203
r'Will continue to try until \d{2}:\d{2}:\d{2}, unless '
204
r'you press Ctrl-C.\n'
205
r'See "brz help break-lock" for more.')
207
def test_31_lock_wait_easy(self):
208
"""Succeed when waiting on a lock with no contention.
210
t = self.get_transport()
211
lf1 = LockDir(t, 'test_lock')
213
self.setup_log_reporter(lf1)
216
lf1.wait_lock(timeout=0.4, poll=0.1)
218
self.assertTrue(after - before <= 1.0)
221
self.assertEqual([], self._logged_reports)
223
def test_40_confirm_easy(self):
224
"""Confirm a lock that's already held"""
225
t = self.get_transport()
226
lf1 = LockDir(t, 'test_lock')
229
self.addCleanup(lf1.unlock)
232
def test_41_confirm_not_held(self):
233
"""Confirm a lock that's already held"""
234
t = self.get_transport()
235
lf1 = LockDir(t, 'test_lock')
237
self.assertRaises(LockNotHeld, lf1.confirm)
239
def test_42_confirm_broken_manually(self):
240
"""Confirm a lock broken by hand"""
241
t = self.get_transport()
242
lf1 = LockDir(t, 'test_lock')
245
t.move('test_lock', 'lock_gone_now')
246
self.assertRaises(LockBroken, lf1.confirm)
248
t.move('lock_gone_now', 'test_lock')
251
def test_43_break(self):
252
"""Break a lock whose caller has forgotten it"""
253
t = self.get_transport()
254
lf1 = LockDir(t, 'test_lock')
257
# we incorrectly discard the lock object without unlocking it
259
# someone else sees it's still locked
260
lf2 = LockDir(t, 'test_lock')
261
holder_info = lf2.peek()
262
self.assertTrue(holder_info)
263
lf2.force_break(holder_info)
264
# now we should be able to take it
266
self.addCleanup(lf2.unlock)
269
def test_44_break_already_released(self):
270
"""Lock break races with regular release"""
271
t = self.get_transport()
272
lf1 = LockDir(t, 'test_lock')
275
# someone else sees it's still locked
276
lf2 = LockDir(t, 'test_lock')
277
holder_info = lf2.peek()
278
# in the interim the lock is released
280
# break should succeed
281
lf2.force_break(holder_info)
282
# now we should be able to take it
284
self.addCleanup(lf2.unlock)
287
def test_45_break_mismatch(self):
288
"""Lock break races with someone else acquiring it"""
289
t = self.get_transport()
290
lf1 = LockDir(t, 'test_lock')
293
# someone else sees it's still locked
294
lf2 = LockDir(t, 'test_lock')
295
holder_info = lf2.peek()
296
# in the interim the lock is released
298
lf3 = LockDir(t, 'test_lock')
300
# break should now *fail*
301
self.assertRaises(LockBreakMismatch, lf2.force_break,
305
def test_46_fake_read_lock(self):
306
t = self.get_transport()
307
lf1 = LockDir(t, 'test_lock')
312
def test_50_lockdir_representation(self):
313
"""Check the on-disk representation of LockDirs is as expected.
315
There should always be a top-level directory named by the lock.
316
When the lock is held, there should be a lockname/held directory
317
containing an info file.
319
t = self.get_transport()
320
lf1 = LockDir(t, 'test_lock')
322
self.assertTrue(t.has('test_lock'))
324
self.assertTrue(t.has('test_lock/held/info'))
326
self.assertFalse(t.has('test_lock/held/info'))
328
def test_break_lock(self):
329
# the ui based break_lock routine should Just Work (tm)
330
ld1 = self.get_lock()
331
ld2 = self.get_lock()
334
# do this without IO redirection to ensure it doesn't prompt.
335
self.assertRaises(AssertionError, ld1.break_lock)
336
orig_factory = breezy.ui.ui_factory
337
breezy.ui.ui_factory = breezy.ui.CannedInputUIFactory([True])
340
self.assertRaises(LockBroken, ld1.unlock)
342
breezy.ui.ui_factory = orig_factory
344
def test_break_lock_corrupt_info(self):
345
"""break_lock works even if the info file is corrupt (and tells the UI
349
ld2 = self.get_lock()
352
ld.transport.put_bytes_non_atomic('test_lock/held/info', b'\0')
354
class LoggingUIFactory(breezy.ui.SilentUIFactory):
358
def get_boolean(self, prompt):
359
self.prompts.append(('boolean', prompt))
362
ui = LoggingUIFactory()
363
self.overrideAttr(breezy.ui, 'ui_factory', ui)
365
self.assertLength(1, ui.prompts)
366
self.assertEqual('boolean', ui.prompts[0][0])
367
self.assertStartsWith(ui.prompts[0][1], 'Break (corrupt LockDir')
368
self.assertRaises(LockBroken, ld.unlock)
370
def test_break_lock_missing_info(self):
371
"""break_lock works even if the info file is missing (and tells the UI
375
ld2 = self.get_lock()
378
ld.transport.delete('test_lock/held/info')
380
class LoggingUIFactory(breezy.ui.SilentUIFactory):
384
def get_boolean(self, prompt):
385
self.prompts.append(('boolean', prompt))
388
ui = LoggingUIFactory()
389
orig_factory = breezy.ui.ui_factory
390
breezy.ui.ui_factory = ui
393
self.assertRaises(LockBroken, ld.unlock)
394
self.assertLength(0, ui.prompts)
396
breezy.ui.ui_factory = orig_factory
397
# Suppress warnings due to ld not being unlocked
398
# XXX: if lock_broken hook was invoked in this case, this hack would
399
# not be necessary. - Andrew Bennetts, 2010-09-06.
400
del self._lock_actions[:]
402
def test_create_missing_base_directory(self):
403
"""If LockDir.path doesn't exist, it can be created
405
Some people manually remove the entire lock/ directory trying
406
to unlock a stuck repository/branch/etc. Rather than failing
407
after that, just create the lock directory when needed.
409
t = self.get_transport()
410
lf1 = LockDir(t, 'test_lock')
413
self.assertTrue(t.has('test_lock'))
416
self.assertFalse(t.has('test_lock'))
418
# This will create 'test_lock' if it needs to
420
self.assertTrue(t.has('test_lock'))
421
self.assertTrue(t.has('test_lock/held/info'))
424
self.assertFalse(t.has('test_lock/held/info'))
426
def test_display_form(self):
427
ld1 = self.get_lock()
431
info_list = ld1.peek().to_readable_dict()
434
self.assertEqual(info_list['user'], u'jrandom@example.com')
435
self.assertContainsRe(info_list['pid'], '^\\d+$')
436
self.assertContainsRe(info_list['time_ago'], '^\\d+ seconds? ago$')
438
def test_lock_without_email(self):
439
global_config = config.GlobalStack()
440
# Intentionally has no email address
441
global_config.set('email', 'User Identity')
442
ld1 = self.get_lock()
447
def test_lock_permission(self):
448
self.requireFeature(features.not_running_as_root)
449
if not osutils.supports_posix_readonly():
450
raise tests.TestSkipped('Cannot induce a permission failure')
451
ld1 = self.get_lock()
452
lock_path = ld1.transport.local_abspath('test_lock')
454
osutils.make_readonly(lock_path)
455
self.assertRaises(errors.LockFailed, ld1.attempt_lock)
457
def test_lock_by_token(self):
458
ld1 = self.get_lock()
459
token = ld1.lock_write()
460
self.addCleanup(ld1.unlock)
461
self.assertNotEqual(None, token)
462
ld2 = self.get_lock()
463
t2 = ld2.lock_write(token)
464
self.addCleanup(ld2.unlock)
465
self.assertEqual(token, t2)
467
def test_lock_with_buggy_rename(self):
468
# test that lock acquisition handles servers which pretend they
469
# renamed correctly but that actually fail
470
t = transport.get_transport_from_url(
471
'brokenrename+' + self.get_url())
472
ld1 = LockDir(t, 'test_lock')
475
ld2 = LockDir(t, 'test_lock')
476
# we should fail to lock
477
e = self.assertRaises(errors.LockContention, ld2.attempt_lock)
478
# now the original caller should succeed in unlocking
480
# and there should be nothing left over
481
self.assertEqual([], t.list_dir('test_lock'))
483
def test_failed_lock_leaves_no_trash(self):
484
# if we fail to acquire the lock, we don't leave pending directories
485
# behind -- https://bugs.launchpad.net/bzr/+bug/109169
486
ld1 = self.get_lock()
487
ld2 = self.get_lock()
488
# should be nothing before we start
490
t = self.get_transport().clone('test_lock')
493
self.assertEqual(a, t.list_dir('.'))
496
# when held, that's all we see
498
self.addCleanup(ld1.unlock)
500
# second guy should fail
501
self.assertRaises(errors.LockContention, ld2.attempt_lock)
505
def test_no_lockdir_info(self):
506
"""We can cope with empty info files."""
507
# This seems like a fairly common failure case - see
508
# <https://bugs.launchpad.net/bzr/+bug/185103> and all its dupes.
509
# Processes are often interrupted after opening the file
510
# before the actual contents are committed.
511
t = self.get_transport()
513
t.mkdir('test_lock/held')
514
t.put_bytes('test_lock/held/info', b'')
515
lf = LockDir(t, 'test_lock')
517
formatted_info = info.to_readable_dict()
519
dict(user='<unknown>', hostname='<unknown>', pid='<unknown>',
520
time_ago='(unknown)'),
523
def test_corrupt_lockdir_info(self):
524
"""We can cope with corrupt (and thus unparseable) info files."""
525
# This seems like a fairly common failure case too - see
526
# <https://bugs.launchpad.net/bzr/+bug/619872> for instance.
527
# In particular some systems tend to fill recently created files with
528
# nul bytes after recovering from a system crash.
529
t = self.get_transport()
531
t.mkdir('test_lock/held')
532
t.put_bytes('test_lock/held/info', b'\0')
533
lf = LockDir(t, 'test_lock')
534
self.assertRaises(errors.LockCorrupt, lf.peek)
535
# Currently attempt_lock gives LockContention, but LockCorrupt would be
536
# a reasonable result too.
538
(errors.LockCorrupt, errors.LockContention), lf.attempt_lock)
539
self.assertRaises(errors.LockCorrupt, lf.validate_token, 'fake token')
541
def test_missing_lockdir_info(self):
542
"""We can cope with absent info files."""
543
t = self.get_transport()
545
t.mkdir('test_lock/held')
546
lf = LockDir(t, 'test_lock')
547
# In this case we expect the 'not held' result from peek, because peek
548
# cannot be expected to notice that there is a 'held' directory with no
550
self.assertEqual(None, lf.peek())
551
# And lock/unlock may work or give LockContention (but not any other
555
except LockContention:
556
# LockContention is ok, and expected on Windows
559
# no error is ok, and expected on POSIX (because POSIX allows
560
# os.rename over an empty directory).
562
# Currently raises TokenMismatch, but LockCorrupt would be reasonable
565
(errors.TokenMismatch, errors.LockCorrupt),
566
lf.validate_token, 'fake token')
569
class TestLockDirHooks(TestCaseWithTransport):
572
super(TestLockDirHooks, self).setUp()
576
return LockDir(self.get_transport(), 'test_lock')
578
def record_hook(self, result):
579
self._calls.append(result)
581
def test_LockDir_acquired_success(self):
582
# the LockDir.lock_acquired hook fires when a lock is acquired.
583
LockDir.hooks.install_named_hook('lock_acquired',
584
self.record_hook, 'record_hook')
587
self.assertEqual([], self._calls)
588
result = ld.attempt_lock()
589
lock_path = ld.transport.abspath(ld.path)
590
self.assertEqual([lock.LockResult(lock_path, result)], self._calls)
592
self.assertEqual([lock.LockResult(lock_path, result)], self._calls)
594
def test_LockDir_acquired_fail(self):
595
# the LockDir.lock_acquired hook does not fire on failure.
598
ld2 = self.get_lock()
600
# install a lock hook now, when the disk lock is locked
601
LockDir.hooks.install_named_hook('lock_acquired',
602
self.record_hook, 'record_hook')
603
self.assertRaises(errors.LockContention, ld.attempt_lock)
604
self.assertEqual([], self._calls)
606
self.assertEqual([], self._calls)
608
def test_LockDir_released_success(self):
609
# the LockDir.lock_released hook fires when a lock is acquired.
610
LockDir.hooks.install_named_hook('lock_released',
611
self.record_hook, 'record_hook')
614
self.assertEqual([], self._calls)
615
result = ld.attempt_lock()
616
self.assertEqual([], self._calls)
618
lock_path = ld.transport.abspath(ld.path)
619
self.assertEqual([lock.LockResult(lock_path, result)], self._calls)
621
def test_LockDir_released_fail(self):
622
# the LockDir.lock_released hook does not fire on failure.
625
ld2 = self.get_lock()
627
ld2.force_break(ld2.peek())
628
LockDir.hooks.install_named_hook('lock_released',
629
self.record_hook, 'record_hook')
630
self.assertRaises(LockBroken, ld.unlock)
631
self.assertEqual([], self._calls)
633
def test_LockDir_broken_success(self):
634
# the LockDir.lock_broken hook fires when a lock is broken.
637
ld2 = self.get_lock()
638
result = ld.attempt_lock()
639
LockDir.hooks.install_named_hook('lock_broken',
640
self.record_hook, 'record_hook')
641
ld2.force_break(ld2.peek())
642
lock_path = ld.transport.abspath(ld.path)
643
self.assertEqual([lock.LockResult(lock_path, result)], self._calls)
645
def test_LockDir_broken_failure(self):
646
# the LockDir.lock_broken hook does not fires when a lock is already
650
ld2 = self.get_lock()
651
result = ld.attempt_lock()
652
holder_info = ld2.peek()
654
LockDir.hooks.install_named_hook('lock_broken',
655
self.record_hook, 'record_hook')
656
ld2.force_break(holder_info)
657
lock_path = ld.transport.abspath(ld.path)
658
self.assertEqual([], self._calls)
661
class TestLockHeldInfo(TestCaseInTempDir):
662
"""Can get information about the lock holder, and detect whether they're
666
info = LockHeldInfo.for_this_process(None)
667
self.assertContainsRe(repr(info), r"LockHeldInfo\(.*\)")
669
def test_unicode(self):
670
info = LockHeldInfo.for_this_process(None)
671
self.assertContainsRe(text_type(info),
672
r'held by .* on .* \(process #\d+\), acquired .* ago')
674
def test_is_locked_by_this_process(self):
675
info = LockHeldInfo.for_this_process(None)
676
self.assertTrue(info.is_locked_by_this_process())
678
def test_is_not_locked_by_this_process(self):
679
info = LockHeldInfo.for_this_process(None)
680
info.info_dict['pid'] = '123123123123123'
681
self.assertFalse(info.is_locked_by_this_process())
683
def test_lock_holder_live_process(self):
684
"""Detect that the holder (this process) is still running."""
685
info = LockHeldInfo.for_this_process(None)
686
self.assertFalse(info.is_lock_holder_known_dead())
688
def test_lock_holder_dead_process(self):
689
"""Detect that the holder (this process) is still running."""
690
self.overrideAttr(lockdir, 'get_host_name',
691
lambda: 'aproperhostname')
692
info = LockHeldInfo.for_this_process(None)
693
info.info_dict['pid'] = '123123123'
694
self.assertTrue(info.is_lock_holder_known_dead())
696
def test_lock_holder_other_machine(self):
697
"""The lock holder isn't here so we don't know if they're alive."""
698
info = LockHeldInfo.for_this_process(None)
699
info.info_dict['hostname'] = 'egg.example.com'
700
info.info_dict['pid'] = '123123123'
701
self.assertFalse(info.is_lock_holder_known_dead())
703
def test_lock_holder_other_user(self):
704
"""Only auto-break locks held by this user."""
705
info = LockHeldInfo.for_this_process(None)
706
info.info_dict['user'] = 'notme@example.com'
707
info.info_dict['pid'] = '123123123'
708
self.assertFalse(info.is_lock_holder_known_dead())
710
def test_no_good_hostname(self):
711
"""Correctly handle ambiguous hostnames.
713
If the lock's recorded with just 'localhost' we can't really trust
714
it's the same 'localhost'. (There are quite a few of them. :-)
715
So even if the process is known not to be alive, we can't say that's
718
self.overrideAttr(lockdir, 'get_host_name',
720
info = LockHeldInfo.for_this_process(None)
721
info.info_dict['pid'] = '123123123'
722
self.assertFalse(info.is_lock_holder_known_dead())
725
class TestStaleLockDir(TestCaseWithTransport):
726
"""Can automatically break stale locks.
728
:see: https://bugs.launchpad.net/bzr/+bug/220464
731
def test_auto_break_stale_lock(self):
732
"""Locks safely known to be stale are just cleaned up.
734
This generates a warning but no other user interaction.
736
self.overrideAttr(lockdir, 'get_host_name',
737
lambda: 'aproperhostname')
738
# Stealing dead locks is enabled by default.
739
# Create a lock pretending to come from a different nonexistent
740
# process on the same machine.
741
l1 = LockDir(self.get_transport(), 'a',
742
extra_holder_info={'pid': '12312313'})
743
token_1 = l1.attempt_lock()
744
l2 = LockDir(self.get_transport(), 'a')
745
token_2 = l2.attempt_lock()
746
# l1 will notice its lock was stolen.
747
self.assertRaises(errors.LockBroken,
751
def test_auto_break_stale_lock_configured_off(self):
752
"""Automatic breaking can be turned off"""
753
l1 = LockDir(self.get_transport(), 'a',
754
extra_holder_info={'pid': '12312313'})
755
# Stealing dead locks is enabled by default, so disable it.
756
config.GlobalStack().set('locks.steal_dead', False)
757
token_1 = l1.attempt_lock()
758
self.addCleanup(l1.unlock)
759
l2 = LockDir(self.get_transport(), 'a')
760
# This fails now, because dead lock breaking is disabled.
761
self.assertRaises(LockContention,
763
# and it's in fact not broken