/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 breezy/tests/test_lockdir.py

[merge] robertc's integration, updated tests to check for retcode=3

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
# Copyright (C) 2006-2012, 2016 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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
16
 
 
17
 
"""Tests for LockDir"""
18
 
 
19
 
import os
20
 
import time
21
 
 
22
 
import breezy
23
 
from .. import (
24
 
    config,
25
 
    errors,
26
 
    lock,
27
 
    lockdir,
28
 
    osutils,
29
 
    tests,
30
 
    transport,
31
 
    )
32
 
from ..errors import (
33
 
    LockBreakMismatch,
34
 
    LockBroken,
35
 
    LockContention,
36
 
    LockFailed,
37
 
    LockNotHeld,
38
 
    )
39
 
from ..lockdir import (
40
 
    LockDir,
41
 
    LockHeldInfo,
42
 
    )
43
 
from ..sixish import (
44
 
    text_type,
45
 
    )
46
 
from . import (
47
 
    features,
48
 
    TestCase,
49
 
    TestCaseInTempDir,
50
 
    TestCaseWithTransport,
51
 
    )
52
 
 
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.)
58
 
 
59
 
 
60
 
class TestLockDir(TestCaseWithTransport):
61
 
    """Test LockDir operations"""
62
 
 
63
 
    def logging_report_function(self, fmt, *args):
64
 
        self._logged_reports.append((fmt, args))
65
 
 
66
 
    def setup_log_reporter(self, lock_dir):
67
 
        self._logged_reports = []
68
 
        lock_dir._report_function = self.logging_report_function
69
 
 
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)
75
 
 
76
 
    def test_01_lock_repr(self):
77
 
        """Lock string representation"""
78
 
        lf = LockDir(self.get_transport(), 'test_lock')
79
 
        r = repr(lf)
80
 
        self.assertContainsRe(r, r'^LockDir\(.*/test_lock\)$')
81
 
 
82
 
    def test_02_unlocked_peek(self):
83
 
        lf = LockDir(self.get_transport(), 'test_lock')
84
 
        self.assertEqual(lf.peek(), None)
85
 
 
86
 
    def get_lock(self):
87
 
        return LockDir(self.get_transport(), 'test_lock')
88
 
 
89
 
    def test_unlock_after_break_raises(self):
90
 
        ld = self.get_lock()
91
 
        ld2 = self.get_lock()
92
 
        ld.create()
93
 
        ld.attempt_lock()
94
 
        ld2.force_break(ld2.peek())
95
 
        self.assertRaises(LockBroken, ld.unlock)
96
 
 
97
 
    def test_03_readonly_peek(self):
98
 
        lf = LockDir(self.get_readonly_transport(), 'test_lock')
99
 
        self.assertEqual(lf.peek(), None)
100
 
 
101
 
    def test_10_lock_uncontested(self):
102
 
        """Acquire and release a lock"""
103
 
        t = self.get_transport()
104
 
        lf = LockDir(t, 'test_lock')
105
 
        lf.create()
106
 
        lf.attempt_lock()
107
 
        try:
108
 
            self.assertTrue(lf.is_held)
109
 
        finally:
110
 
            lf.unlock()
111
 
            self.assertFalse(lf.is_held)
112
 
 
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)
118
 
 
119
 
    def test_12_lock_readonly_transport(self):
120
 
        """Fail to lock on readonly transport"""
121
 
        lf = LockDir(self.get_transport(), 'test_lock')
122
 
        lf.create()
123
 
        lf = LockDir(self.get_readonly_transport(), 'test_lock')
124
 
        self.assertRaises(LockFailed, lf.attempt_lock)
125
 
 
126
 
    def test_20_lock_contested(self):
127
 
        """Contention to get a lock"""
128
 
        t = self.get_transport()
129
 
        lf1 = LockDir(t, 'test_lock')
130
 
        lf1.create()
131
 
        lf1.attempt_lock()
132
 
        lf2 = LockDir(t, 'test_lock')
133
 
        try:
134
 
            # locking is between LockDir instances; aliases within
135
 
            # a single process are not detected
136
 
            lf2.attempt_lock()
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.*$')
142
 
        lf1.unlock()
143
 
 
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')
148
 
        lf1.create()
149
 
        lf1.attempt_lock()
150
 
        self.addCleanup(lf1.unlock)
151
 
        # lock is held, should get some info on it
152
 
        info1 = lf1.peek()
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
156
 
        # instance
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)
161
 
 
162
 
    def test_21_peek_readonly(self):
163
 
        """Peek over a readonly transport"""
164
 
        t = self.get_transport()
165
 
        lf1 = LockDir(t, 'test_lock')
166
 
        lf1.create()
167
 
        lf2 = LockDir(self.get_readonly_transport(), 'test_lock')
168
 
        self.assertEqual(lf2.peek(), None)
169
 
        lf1.attempt_lock()
170
 
        self.addCleanup(lf1.unlock)
171
 
        info2 = lf2.peek()
172
 
        self.assertTrue(info2)
173
 
        self.assertEqual(info2.get('nonce'), lf1.nonce)
174
 
 
175
 
    def test_30_lock_wait_fail(self):
176
 
        """Wait on a lock, then fail
177
 
 
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.)
181
 
        """
182
 
        t = self.get_transport()
183
 
        lf1 = LockDir(t, 'test_lock')
184
 
        lf1.create()
185
 
        lf2 = LockDir(t, 'test_lock')
186
 
        self.setup_log_reporter(lf2)
187
 
        lf1.attempt_lock()
188
 
        try:
189
 
            before = time.time()
190
 
            self.assertRaises(LockContention, lf2.wait_lock,
191
 
                              timeout=0.4, poll=0.1)
192
 
            after = time.time()
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))
197
 
        finally:
198
 
            lf1.unlock()
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.')
206
 
 
207
 
    def test_31_lock_wait_easy(self):
208
 
        """Succeed when waiting on a lock with no contention.
209
 
        """
210
 
        t = self.get_transport()
211
 
        lf1 = LockDir(t, 'test_lock')
212
 
        lf1.create()
213
 
        self.setup_log_reporter(lf1)
214
 
        try:
215
 
            before = time.time()
216
 
            lf1.wait_lock(timeout=0.4, poll=0.1)
217
 
            after = time.time()
218
 
            self.assertTrue(after - before <= 1.0)
219
 
        finally:
220
 
            lf1.unlock()
221
 
        self.assertEqual([], self._logged_reports)
222
 
 
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')
227
 
        lf1.create()
228
 
        lf1.attempt_lock()
229
 
        self.addCleanup(lf1.unlock)
230
 
        lf1.confirm()
231
 
 
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')
236
 
        lf1.create()
237
 
        self.assertRaises(LockNotHeld, lf1.confirm)
238
 
 
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')
243
 
        lf1.create()
244
 
        lf1.attempt_lock()
245
 
        t.move('test_lock', 'lock_gone_now')
246
 
        self.assertRaises(LockBroken, lf1.confirm)
247
 
        # Clean up
248
 
        t.move('lock_gone_now', 'test_lock')
249
 
        lf1.unlock()
250
 
 
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')
255
 
        lf1.create()
256
 
        lf1.attempt_lock()
257
 
        # we incorrectly discard the lock object without unlocking it
258
 
        del lf1
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
265
 
        lf2.attempt_lock()
266
 
        self.addCleanup(lf2.unlock)
267
 
        lf2.confirm()
268
 
 
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')
273
 
        lf1.create()
274
 
        lf1.attempt_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
279
 
        lf1.unlock()
280
 
        # break should succeed
281
 
        lf2.force_break(holder_info)
282
 
        # now we should be able to take it
283
 
        lf2.attempt_lock()
284
 
        self.addCleanup(lf2.unlock)
285
 
        lf2.confirm()
286
 
 
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')
291
 
        lf1.create()
292
 
        lf1.attempt_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
297
 
        lf1.unlock()
298
 
        lf3 = LockDir(t, 'test_lock')
299
 
        lf3.attempt_lock()
300
 
        # break should now *fail*
301
 
        self.assertRaises(LockBreakMismatch, lf2.force_break,
302
 
                          holder_info)
303
 
        lf3.unlock()
304
 
 
305
 
    def test_46_fake_read_lock(self):
306
 
        t = self.get_transport()
307
 
        lf1 = LockDir(t, 'test_lock')
308
 
        lf1.create()
309
 
        lf1.lock_read()
310
 
        lf1.unlock()
311
 
 
312
 
    def test_50_lockdir_representation(self):
313
 
        """Check the on-disk representation of LockDirs is as expected.
314
 
 
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.
318
 
        """
319
 
        t = self.get_transport()
320
 
        lf1 = LockDir(t, 'test_lock')
321
 
        lf1.create()
322
 
        self.assertTrue(t.has('test_lock'))
323
 
        lf1.lock_write()
324
 
        self.assertTrue(t.has('test_lock/held/info'))
325
 
        lf1.unlock()
326
 
        self.assertFalse(t.has('test_lock/held/info'))
327
 
 
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()
332
 
        ld1.create()
333
 
        ld1.lock_write()
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])
338
 
        try:
339
 
            ld2.break_lock()
340
 
            self.assertRaises(LockBroken, ld1.unlock)
341
 
        finally:
342
 
            breezy.ui.ui_factory = orig_factory
343
 
 
344
 
    def test_break_lock_corrupt_info(self):
345
 
        """break_lock works even if the info file is corrupt (and tells the UI
346
 
        that it is corrupt).
347
 
        """
348
 
        ld = self.get_lock()
349
 
        ld2 = self.get_lock()
350
 
        ld.create()
351
 
        ld.lock_write()
352
 
        ld.transport.put_bytes_non_atomic('test_lock/held/info', b'\0')
353
 
 
354
 
        class LoggingUIFactory(breezy.ui.SilentUIFactory):
355
 
            def __init__(self):
356
 
                self.prompts = []
357
 
 
358
 
            def get_boolean(self, prompt):
359
 
                self.prompts.append(('boolean', prompt))
360
 
                return True
361
 
 
362
 
        ui = LoggingUIFactory()
363
 
        self.overrideAttr(breezy.ui, 'ui_factory', ui)
364
 
        ld2.break_lock()
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)
369
 
 
370
 
    def test_break_lock_missing_info(self):
371
 
        """break_lock works even if the info file is missing (and tells the UI
372
 
        that it is corrupt).
373
 
        """
374
 
        ld = self.get_lock()
375
 
        ld2 = self.get_lock()
376
 
        ld.create()
377
 
        ld.lock_write()
378
 
        ld.transport.delete('test_lock/held/info')
379
 
 
380
 
        class LoggingUIFactory(breezy.ui.SilentUIFactory):
381
 
            def __init__(self):
382
 
                self.prompts = []
383
 
 
384
 
            def get_boolean(self, prompt):
385
 
                self.prompts.append(('boolean', prompt))
386
 
                return True
387
 
 
388
 
        ui = LoggingUIFactory()
389
 
        orig_factory = breezy.ui.ui_factory
390
 
        breezy.ui.ui_factory = ui
391
 
        try:
392
 
            ld2.break_lock()
393
 
            self.assertRaises(LockBroken, ld.unlock)
394
 
            self.assertLength(0, ui.prompts)
395
 
        finally:
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[:]
401
 
 
402
 
    def test_create_missing_base_directory(self):
403
 
        """If LockDir.path doesn't exist, it can be created
404
 
 
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.
408
 
        """
409
 
        t = self.get_transport()
410
 
        lf1 = LockDir(t, 'test_lock')
411
 
 
412
 
        lf1.create()
413
 
        self.assertTrue(t.has('test_lock'))
414
 
 
415
 
        t.rmdir('test_lock')
416
 
        self.assertFalse(t.has('test_lock'))
417
 
 
418
 
        # This will create 'test_lock' if it needs to
419
 
        lf1.lock_write()
420
 
        self.assertTrue(t.has('test_lock'))
421
 
        self.assertTrue(t.has('test_lock/held/info'))
422
 
 
423
 
        lf1.unlock()
424
 
        self.assertFalse(t.has('test_lock/held/info'))
425
 
 
426
 
    def test_display_form(self):
427
 
        ld1 = self.get_lock()
428
 
        ld1.create()
429
 
        ld1.lock_write()
430
 
        try:
431
 
            info_list = ld1.peek().to_readable_dict()
432
 
        finally:
433
 
            ld1.unlock()
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$')
437
 
 
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()
443
 
        ld1.create()
444
 
        ld1.lock_write()
445
 
        ld1.unlock()
446
 
 
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')
453
 
        os.mkdir(lock_path)
454
 
        osutils.make_readonly(lock_path)
455
 
        self.assertRaises(errors.LockFailed, ld1.attempt_lock)
456
 
 
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)
466
 
 
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')
473
 
        ld1.create()
474
 
        ld1.attempt_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
479
 
        ld1.unlock()
480
 
        # and there should be nothing left over
481
 
        self.assertEqual([], t.list_dir('test_lock'))
482
 
 
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
489
 
        ld1.create()
490
 
        t = self.get_transport().clone('test_lock')
491
 
 
492
 
        def check_dir(a):
493
 
            self.assertEqual(a, t.list_dir('.'))
494
 
 
495
 
        check_dir([])
496
 
        # when held, that's all we see
497
 
        ld1.attempt_lock()
498
 
        self.addCleanup(ld1.unlock)
499
 
        check_dir(['held'])
500
 
        # second guy should fail
501
 
        self.assertRaises(errors.LockContention, ld2.attempt_lock)
502
 
        # no kibble
503
 
        check_dir(['held'])
504
 
 
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()
512
 
        t.mkdir('test_lock')
513
 
        t.mkdir('test_lock/held')
514
 
        t.put_bytes('test_lock/held/info', b'')
515
 
        lf = LockDir(t, 'test_lock')
516
 
        info = lf.peek()
517
 
        formatted_info = info.to_readable_dict()
518
 
        self.assertEqual(
519
 
            dict(user='<unknown>', hostname='<unknown>', pid='<unknown>',
520
 
                 time_ago='(unknown)'),
521
 
            formatted_info)
522
 
 
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()
530
 
        t.mkdir('test_lock')
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.
537
 
        self.assertRaises(
538
 
            (errors.LockCorrupt, errors.LockContention), lf.attempt_lock)
539
 
        self.assertRaises(errors.LockCorrupt, lf.validate_token, 'fake token')
540
 
 
541
 
    def test_missing_lockdir_info(self):
542
 
        """We can cope with absent info files."""
543
 
        t = self.get_transport()
544
 
        t.mkdir('test_lock')
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
549
 
        # 'info' file.
550
 
        self.assertEqual(None, lf.peek())
551
 
        # And lock/unlock may work or give LockContention (but not any other
552
 
        # error).
553
 
        try:
554
 
            lf.attempt_lock()
555
 
        except LockContention:
556
 
            # LockContention is ok, and expected on Windows
557
 
            pass
558
 
        else:
559
 
            # no error is ok, and expected on POSIX (because POSIX allows
560
 
            # os.rename over an empty directory).
561
 
            lf.unlock()
562
 
        # Currently raises TokenMismatch, but LockCorrupt would be reasonable
563
 
        # too.
564
 
        self.assertRaises(
565
 
            (errors.TokenMismatch, errors.LockCorrupt),
566
 
            lf.validate_token, 'fake token')
567
 
 
568
 
 
569
 
class TestLockDirHooks(TestCaseWithTransport):
570
 
 
571
 
    def setUp(self):
572
 
        super(TestLockDirHooks, self).setUp()
573
 
        self._calls = []
574
 
 
575
 
    def get_lock(self):
576
 
        return LockDir(self.get_transport(), 'test_lock')
577
 
 
578
 
    def record_hook(self, result):
579
 
        self._calls.append(result)
580
 
 
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')
585
 
        ld = self.get_lock()
586
 
        ld.create()
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)
591
 
        ld.unlock()
592
 
        self.assertEqual([lock.LockResult(lock_path, result)], self._calls)
593
 
 
594
 
    def test_LockDir_acquired_fail(self):
595
 
        # the LockDir.lock_acquired hook does not fire on failure.
596
 
        ld = self.get_lock()
597
 
        ld.create()
598
 
        ld2 = self.get_lock()
599
 
        ld2.attempt_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)
605
 
        ld2.unlock()
606
 
        self.assertEqual([], self._calls)
607
 
 
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')
612
 
        ld = self.get_lock()
613
 
        ld.create()
614
 
        self.assertEqual([], self._calls)
615
 
        result = ld.attempt_lock()
616
 
        self.assertEqual([], self._calls)
617
 
        ld.unlock()
618
 
        lock_path = ld.transport.abspath(ld.path)
619
 
        self.assertEqual([lock.LockResult(lock_path, result)], self._calls)
620
 
 
621
 
    def test_LockDir_released_fail(self):
622
 
        # the LockDir.lock_released hook does not fire on failure.
623
 
        ld = self.get_lock()
624
 
        ld.create()
625
 
        ld2 = self.get_lock()
626
 
        ld.attempt_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)
632
 
 
633
 
    def test_LockDir_broken_success(self):
634
 
        # the LockDir.lock_broken hook fires when a lock is broken.
635
 
        ld = self.get_lock()
636
 
        ld.create()
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)
644
 
 
645
 
    def test_LockDir_broken_failure(self):
646
 
        # the LockDir.lock_broken hook does not fires when a lock is already
647
 
        # released.
648
 
        ld = self.get_lock()
649
 
        ld.create()
650
 
        ld2 = self.get_lock()
651
 
        result = ld.attempt_lock()
652
 
        holder_info = ld2.peek()
653
 
        ld.unlock()
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)
659
 
 
660
 
 
661
 
class TestLockHeldInfo(TestCaseInTempDir):
662
 
    """Can get information about the lock holder, and detect whether they're
663
 
    still alive."""
664
 
 
665
 
    def test_repr(self):
666
 
        info = LockHeldInfo.for_this_process(None)
667
 
        self.assertContainsRe(repr(info), r"LockHeldInfo\(.*\)")
668
 
 
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')
673
 
 
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())
677
 
 
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())
682
 
 
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())
687
 
 
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())
695
 
 
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())
702
 
 
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())
709
 
 
710
 
    def test_no_good_hostname(self):
711
 
        """Correctly handle ambiguous hostnames.
712
 
 
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
716
 
        known for sure.
717
 
        """
718
 
        self.overrideAttr(lockdir, 'get_host_name',
719
 
                          lambda: 'localhost')
720
 
        info = LockHeldInfo.for_this_process(None)
721
 
        info.info_dict['pid'] = '123123123'
722
 
        self.assertFalse(info.is_lock_holder_known_dead())
723
 
 
724
 
 
725
 
class TestStaleLockDir(TestCaseWithTransport):
726
 
    """Can automatically break stale locks.
727
 
 
728
 
    :see: https://bugs.launchpad.net/bzr/+bug/220464
729
 
    """
730
 
 
731
 
    def test_auto_break_stale_lock(self):
732
 
        """Locks safely known to be stale are just cleaned up.
733
 
 
734
 
        This generates a warning but no other user interaction.
735
 
        """
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,
748
 
                          l1.unlock)
749
 
        l2.unlock()
750
 
 
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,
762
 
                          l2.attempt_lock)
763
 
        # and it's in fact not broken
764
 
        l1.confirm()