/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 bzrlib/tests/test_smart.py

  • Committer: John Arbash Meinel
  • Date: 2008-06-05 16:27:16 UTC
  • mfrom: (3475 +trunk)
  • mto: This revision was merged to the branch mainline in revision 3476.
  • Revision ID: john@arbash-meinel.com-20080605162716-a3hn238tnctbfd8j
merge bzr.dev, resolve NEWS

Show diffs side-by-side

added added

removed removed

Lines of Context:
25
25
"""
26
26
 
27
27
import bz2
28
 
from StringIO import StringIO
29
 
import tempfile
 
28
from cStringIO import StringIO
30
29
import tarfile
31
30
 
32
 
from bzrlib import bzrdir, errors, pack, smart, tests
 
31
from bzrlib import (
 
32
    bzrdir,
 
33
    errors,
 
34
    pack,
 
35
    smart,
 
36
    tests,
 
37
    urlutils,
 
38
    )
 
39
from bzrlib.branch import BranchReferenceFormat
 
40
import bzrlib.smart.branch
 
41
import bzrlib.smart.bzrdir
 
42
import bzrlib.smart.repository
33
43
from bzrlib.smart.request import (
34
44
    FailedSmartServerResponse,
 
45
    SmartServerRequest,
35
46
    SmartServerResponse,
36
47
    SuccessfulSmartServerResponse,
37
48
    )
38
 
import bzrlib.smart.bzrdir
39
 
import bzrlib.smart.branch
40
 
import bzrlib.smart.repository
41
49
from bzrlib.tests import (
42
50
    iter_suite_tests,
43
51
    split_suite_by_re,
44
52
    TestScenarioApplier,
45
53
    )
 
54
from bzrlib.transport import chroot, get_transport
46
55
from bzrlib.util import bencode
47
56
 
48
57
 
69
78
    return result
70
79
 
71
80
 
 
81
class TestCaseWithChrootedTransport(tests.TestCaseWithTransport):
 
82
 
 
83
    def setUp(self):
 
84
        tests.TestCaseWithTransport.setUp(self)
 
85
        self._chroot_server = None
 
86
 
 
87
    def get_transport(self, relpath=None):
 
88
        if self._chroot_server is None:
 
89
            backing_transport = tests.TestCaseWithTransport.get_transport(self)
 
90
            self._chroot_server = chroot.ChrootServer(backing_transport)
 
91
            self._chroot_server.setUp()
 
92
            self.addCleanup(self._chroot_server.tearDown)
 
93
        t = get_transport(self._chroot_server.get_url())
 
94
        if relpath is not None:
 
95
            t = t.clone(relpath)
 
96
        return t
 
97
 
 
98
 
72
99
class TestCaseWithSmartMedium(tests.TestCaseWithTransport):
73
100
 
74
101
    def setUp(self):
77
104
        # the default or a parameterized class, but rather use the
78
105
        # TestCaseWithTransport infrastructure to set up a smart server and
79
106
        # transport.
80
 
        self.transport_server = smart.server.SmartTCPServer_for_testing
 
107
        self.transport_server = self.make_transport_server
 
108
 
 
109
    def make_transport_server(self):
 
110
        return smart.server.SmartTCPServer_for_testing('-' + self.id())
81
111
 
82
112
    def get_smart_medium(self):
83
113
        """Get a smart medium to use in tests."""
98
128
        self.assertNotEqual(None,
99
129
            SmartServerResponse(('ok', )))
100
130
 
101
 
 
102
 
class TestSmartServerRequestFindRepository(tests.TestCaseWithTransport):
 
131
    def test__str__(self):
 
132
        """SmartServerResponses can be stringified."""
 
133
        self.assertEqual(
 
134
            "<SmartServerResponse status=OK args=('args',) body='body'>",
 
135
            str(SuccessfulSmartServerResponse(('args',), 'body')))
 
136
        self.assertEqual(
 
137
            "<SmartServerResponse status=ERR args=('args',) body='body'>",
 
138
            str(FailedSmartServerResponse(('args',), 'body')))
 
139
 
 
140
 
 
141
class TestSmartServerRequest(tests.TestCaseWithMemoryTransport):
 
142
 
 
143
    def test_translate_client_path(self):
 
144
        transport = self.get_transport()
 
145
        request = SmartServerRequest(transport, 'foo/')
 
146
        self.assertEqual('./', request.translate_client_path('foo/'))
 
147
        self.assertRaises(
 
148
            errors.InvalidURLJoin, request.translate_client_path, 'foo/..')
 
149
        self.assertRaises(
 
150
            errors.PathNotChild, request.translate_client_path, '/')
 
151
        self.assertRaises(
 
152
            errors.PathNotChild, request.translate_client_path, 'bar/')
 
153
        self.assertEqual('./baz', request.translate_client_path('foo/baz'))
 
154
 
 
155
    def test_transport_from_client_path(self):
 
156
        transport = self.get_transport()
 
157
        request = SmartServerRequest(transport, 'foo/')
 
158
        self.assertEqual(
 
159
            transport.base,
 
160
            request.transport_from_client_path('foo/').base)
 
161
 
 
162
 
 
163
class TestSmartServerRequestFindRepository(tests.TestCaseWithMemoryTransport):
103
164
    """Tests for BzrDir.find_repository."""
104
165
 
105
166
    def test_no_repository(self):
108
169
        request = self._request_class(backing)
109
170
        self.make_bzrdir('.')
110
171
        self.assertEqual(SmartServerResponse(('norepository', )),
111
 
            request.execute(backing.local_abspath('')))
 
172
            request.execute(''))
112
173
 
113
174
    def test_nonshared_repository(self):
114
175
        # nonshared repositorys only allow 'find' to return a handle when the 
117
178
        backing = self.get_transport()
118
179
        request = self._request_class(backing)
119
180
        result = self._make_repository_and_result()
120
 
        self.assertEqual(result, request.execute(backing.local_abspath('')))
 
181
        self.assertEqual(result, request.execute(''))
121
182
        self.make_bzrdir('subdir')
122
183
        self.assertEqual(SmartServerResponse(('norepository', )),
123
 
            request.execute(backing.local_abspath('subdir')))
 
184
            request.execute('subdir'))
124
185
 
125
186
    def _make_repository_and_result(self, shared=False, format=None):
126
187
        """Convenience function to setup a repository.
150
211
        backing = self.get_transport()
151
212
        request = self._request_class(backing)
152
213
        result = self._make_repository_and_result(shared=True)
153
 
        self.assertEqual(result, request.execute(backing.local_abspath('')))
 
214
        self.assertEqual(result, request.execute(''))
154
215
        self.make_bzrdir('subdir')
155
216
        result2 = SmartServerResponse(result.args[0:1] + ('..', ) + result.args[2:])
156
217
        self.assertEqual(result2,
157
 
            request.execute(backing.local_abspath('subdir')))
 
218
            request.execute('subdir'))
158
219
        self.make_bzrdir('subdir/deeper')
159
220
        result3 = SmartServerResponse(result.args[0:1] + ('../..', ) + result.args[2:])
160
221
        self.assertEqual(result3,
161
 
            request.execute(backing.local_abspath('subdir/deeper')))
 
222
            request.execute('subdir/deeper'))
162
223
 
163
224
    def test_rich_root_and_subtree_encoding(self):
164
225
        """Test for the format attributes for rich root and subtree support."""
168
229
        # check the test will be valid
169
230
        self.assertEqual('yes', result.args[2])
170
231
        self.assertEqual('yes', result.args[3])
171
 
        self.assertEqual(result, request.execute(backing.local_abspath('')))
 
232
        self.assertEqual(result, request.execute(''))
172
233
 
173
234
    def test_supports_external_lookups_no_v2(self):
174
235
        """Test for the supports_external_lookups attribute."""
177
238
        result = self._make_repository_and_result(format='dirstate-with-subtree')
178
239
        # check the test will be valid
179
240
        self.assertEqual('no', result.args[4])
180
 
        self.assertEqual(result, request.execute(backing.local_abspath('')))
181
 
 
182
 
 
183
 
class TestSmartServerRequestInitializeBzrDir(tests.TestCaseWithTransport):
 
241
        self.assertEqual(result, request.execute(''))
 
242
 
 
243
 
 
244
class TestSmartServerRequestInitializeBzrDir(tests.TestCaseWithMemoryTransport):
184
245
 
185
246
    def test_empty_dir(self):
186
247
        """Initializing an empty dir should succeed and do it."""
187
248
        backing = self.get_transport()
188
249
        request = smart.bzrdir.SmartServerRequestInitializeBzrDir(backing)
189
250
        self.assertEqual(SmartServerResponse(('ok', )),
190
 
            request.execute(backing.local_abspath('.')))
 
251
            request.execute(''))
191
252
        made_dir = bzrdir.BzrDir.open_from_transport(backing)
192
253
        # no branch, tree or repository is expected with the current 
193
254
        # default formart.
200
261
        backing = self.get_transport()
201
262
        request = smart.bzrdir.SmartServerRequestInitializeBzrDir(backing)
202
263
        self.assertRaises(errors.NoSuchFile,
203
 
            request.execute, backing.local_abspath('subdir'))
 
264
            request.execute, 'subdir')
204
265
 
205
266
    def test_initialized_dir(self):
206
267
        """Initializing an extant bzrdir should fail like the bzrdir api."""
208
269
        request = smart.bzrdir.SmartServerRequestInitializeBzrDir(backing)
209
270
        self.make_bzrdir('subdir')
210
271
        self.assertRaises(errors.FileExists,
211
 
            request.execute, backing.local_abspath('subdir'))
212
 
 
213
 
 
214
 
class TestSmartServerRequestOpenBranch(tests.TestCaseWithTransport):
 
272
            request.execute, 'subdir')
 
273
 
 
274
 
 
275
class TestSmartServerRequestOpenBranch(TestCaseWithChrootedTransport):
215
276
 
216
277
    def test_no_branch(self):
217
278
        """When there is no branch, ('nobranch', ) is returned."""
219
280
        request = smart.bzrdir.SmartServerRequestOpenBranch(backing)
220
281
        self.make_bzrdir('.')
221
282
        self.assertEqual(SmartServerResponse(('nobranch', )),
222
 
            request.execute(backing.local_abspath('')))
 
283
            request.execute(''))
223
284
 
224
285
    def test_branch(self):
225
286
        """When there is a branch, 'ok' is returned."""
227
288
        request = smart.bzrdir.SmartServerRequestOpenBranch(backing)
228
289
        self.make_branch('.')
229
290
        self.assertEqual(SmartServerResponse(('ok', '')),
230
 
            request.execute(backing.local_abspath('')))
 
291
            request.execute(''))
231
292
 
232
293
    def test_branch_reference(self):
233
294
        """When there is a branch reference, the reference URL is returned."""
235
296
        request = smart.bzrdir.SmartServerRequestOpenBranch(backing)
236
297
        branch = self.make_branch('branch')
237
298
        checkout = branch.create_checkout('reference',lightweight=True)
238
 
        # TODO: once we have an API to probe for references of any sort, we
239
 
        # can use it here.
240
 
        reference_url = backing.abspath('branch') + '/'
 
299
        reference_url = BranchReferenceFormat().get_reference(checkout.bzrdir)
241
300
        self.assertFileEqual(reference_url, 'reference/.bzr/branch/location')
242
301
        self.assertEqual(SmartServerResponse(('ok', reference_url)),
243
 
            request.execute(backing.local_abspath('reference')))
244
 
 
245
 
 
246
 
class TestSmartServerRequestRevisionHistory(tests.TestCaseWithTransport):
 
302
            request.execute('reference'))
 
303
 
 
304
 
 
305
class TestSmartServerRequestRevisionHistory(tests.TestCaseWithMemoryTransport):
247
306
 
248
307
    def test_empty(self):
249
308
        """For an empty branch, the body is empty."""
251
310
        request = smart.branch.SmartServerRequestRevisionHistory(backing)
252
311
        self.make_branch('.')
253
312
        self.assertEqual(SmartServerResponse(('ok', ), ''),
254
 
            request.execute(backing.local_abspath('')))
 
313
            request.execute(''))
255
314
 
256
315
    def test_not_empty(self):
257
316
        """For a non-empty branch, the body is empty."""
265
324
        tree.unlock()
266
325
        self.assertEqual(
267
326
            SmartServerResponse(('ok', ), ('\x00'.join([r1, r2]))),
268
 
            request.execute(backing.local_abspath('')))
269
 
 
270
 
 
271
 
class TestSmartServerBranchRequest(tests.TestCaseWithTransport):
 
327
            request.execute(''))
 
328
 
 
329
 
 
330
class TestSmartServerBranchRequest(tests.TestCaseWithMemoryTransport):
272
331
 
273
332
    def test_no_branch(self):
274
333
        """When there is a bzrdir and no branch, NotBranchError is raised."""
276
335
        request = smart.branch.SmartServerBranchRequest(backing)
277
336
        self.make_bzrdir('.')
278
337
        self.assertRaises(errors.NotBranchError,
279
 
            request.execute, backing.local_abspath(''))
 
338
            request.execute, '')
280
339
 
281
340
    def test_branch_reference(self):
282
341
        """When there is a branch reference, NotBranchError is raised."""
285
344
        branch = self.make_branch('branch')
286
345
        checkout = branch.create_checkout('reference',lightweight=True)
287
346
        self.assertRaises(errors.NotBranchError,
288
 
            request.execute, backing.local_abspath('checkout'))
289
 
 
290
 
 
291
 
class TestSmartServerBranchRequestLastRevisionInfo(tests.TestCaseWithTransport):
 
347
            request.execute, 'checkout')
 
348
 
 
349
 
 
350
class TestSmartServerBranchRequestLastRevisionInfo(tests.TestCaseWithMemoryTransport):
292
351
 
293
352
    def test_empty(self):
294
353
        """For an empty branch, the result is ('ok', '0', 'null:')."""
296
355
        request = smart.branch.SmartServerBranchRequestLastRevisionInfo(backing)
297
356
        self.make_branch('.')
298
357
        self.assertEqual(SmartServerResponse(('ok', '0', 'null:')),
299
 
            request.execute(backing.local_abspath('')))
 
358
            request.execute(''))
300
359
 
301
360
    def test_not_empty(self):
302
361
        """For a non-empty branch, the result is ('ok', 'revno', 'revid')."""
311
370
        tree.unlock()
312
371
        self.assertEqual(
313
372
            SmartServerResponse(('ok', '2', rev_id_utf8)),
314
 
            request.execute(backing.local_abspath('')))
315
 
 
316
 
 
317
 
class TestSmartServerBranchRequestGetConfigFile(tests.TestCaseWithTransport):
 
373
            request.execute(''))
 
374
 
 
375
 
 
376
class TestSmartServerBranchRequestGetConfigFile(tests.TestCaseWithMemoryTransport):
318
377
 
319
378
    def test_default(self):
320
379
        """With no file, we get empty content."""
324
383
        # there should be no file by default
325
384
        content = ''
326
385
        self.assertEqual(SmartServerResponse(('ok', ), content),
327
 
            request.execute(backing.local_abspath('')))
 
386
            request.execute(''))
328
387
 
329
388
    def test_with_content(self):
330
389
        # SmartServerBranchGetConfigFile should return the content from
333
392
        backing = self.get_transport()
334
393
        request = smart.branch.SmartServerBranchGetConfigFile(backing)
335
394
        branch = self.make_branch('.')
336
 
        branch.control_files.put_utf8('branch.conf', 'foo bar baz')
 
395
        branch._transport.put_bytes('branch.conf', 'foo bar baz')
337
396
        self.assertEqual(SmartServerResponse(('ok', ), 'foo bar baz'),
338
 
            request.execute(backing.local_abspath('')))
339
 
 
340
 
 
341
 
class TestSmartServerBranchRequestSetLastRevision(tests.TestCaseWithTransport):
 
397
            request.execute(''))
 
398
 
 
399
 
 
400
class TestSmartServerBranchRequestSetLastRevision(tests.TestCaseWithMemoryTransport):
342
401
 
343
402
    def test_empty(self):
344
403
        backing = self.get_transport()
350
409
        try:
351
410
            self.assertEqual(SmartServerResponse(('ok',)),
352
411
                request.execute(
353
 
                    backing.local_abspath(''), branch_token, repo_token,
 
412
                    '', branch_token, repo_token,
354
413
                    'null:'))
355
414
        finally:
356
415
            b.unlock()
367
426
            self.assertEqual(
368
427
                SmartServerResponse(('NoSuchRevision', revision_id)),
369
428
                request.execute(
370
 
                    backing.local_abspath(''), branch_token, repo_token,
 
429
                    '', branch_token, repo_token,
371
430
                    revision_id))
372
431
        finally:
373
432
            b.unlock()
389
448
            self.assertEqual(
390
449
                SmartServerResponse(('ok',)),
391
450
                request.execute(
392
 
                    backing.local_abspath(''), branch_token, repo_token,
 
451
                    '', branch_token, repo_token,
393
452
                    rev_id_utf8))
394
453
            self.assertEqual([rev_id_utf8], tree.branch.revision_history())
395
454
        finally:
413
472
            self.assertEqual(
414
473
                SmartServerResponse(('ok',)),
415
474
                request.execute(
416
 
                    backing.local_abspath(''), branch_token, repo_token,
 
475
                    '', branch_token, repo_token,
417
476
                    rev_id_utf8))
418
477
            self.assertEqual([rev_id_utf8], tree.branch.revision_history())
419
478
        finally:
420
479
            tree.branch.unlock()
421
480
 
422
481
 
423
 
class TestSmartServerBranchRequestLockWrite(tests.TestCaseWithTransport):
 
482
class TestSmartServerBranchRequestSetLastRevisionInfo(tests.TestCaseWithTransport):
 
483
 
 
484
    def lock_branch(self, branch):
 
485
        branch_token = branch.lock_write()
 
486
        repo_token = branch.repository.lock_write()
 
487
        branch.repository.unlock()
 
488
        self.addCleanup(branch.unlock)
 
489
        return branch_token, repo_token
 
490
 
 
491
    def make_locked_branch(self, format=None):
 
492
        branch = self.make_branch('.', format=format)
 
493
        branch_token, repo_token = self.lock_branch(branch)
 
494
        return branch, branch_token, repo_token
 
495
 
 
496
    def test_empty(self):
 
497
        """An empty branch can have its last revision set to 'null:'."""
 
498
        b, branch_token, repo_token = self.make_locked_branch()
 
499
        backing = self.get_transport()
 
500
        request = smart.branch.SmartServerBranchRequestSetLastRevisionInfo(
 
501
            backing)
 
502
        response = request.execute('', branch_token, repo_token, '0', 'null:')
 
503
        self.assertEqual(SmartServerResponse(('ok',)), response)
 
504
 
 
505
    def assertBranchLastRevisionInfo(self, expected_info, branch_relpath):
 
506
        branch = bzrdir.BzrDir.open(branch_relpath).open_branch()
 
507
        self.assertEqual(expected_info, branch.last_revision_info())
 
508
 
 
509
    def test_branch_revision_info_is_updated(self):
 
510
        """This method really does update the branch last revision info."""
 
511
        tree = self.make_branch_and_memory_tree('.')
 
512
        tree.lock_write()
 
513
        tree.add('')
 
514
        tree.commit('First commit', rev_id='revision-1')
 
515
        tree.commit('Second commit', rev_id='revision-2')
 
516
        tree.unlock()
 
517
        branch = tree.branch
 
518
 
 
519
        branch_token, repo_token = self.lock_branch(branch)
 
520
        backing = self.get_transport()
 
521
        request = smart.branch.SmartServerBranchRequestSetLastRevisionInfo(
 
522
            backing)
 
523
        self.assertBranchLastRevisionInfo((2, 'revision-2'), '.')
 
524
        response = request.execute(
 
525
            '', branch_token, repo_token, '1', 'revision-1')
 
526
        self.assertEqual(SmartServerResponse(('ok',)), response)
 
527
        self.assertBranchLastRevisionInfo((1, 'revision-1'), '.')
 
528
 
 
529
    def test_not_present_revid(self):
 
530
        """Some branch formats will check that the revision is present in the
 
531
        repository.  When that check fails, a NoSuchRevision error is returned
 
532
        to the client.
 
533
        """
 
534
        # Make a knit format branch, because that format checks the values
 
535
        # given to set_last_revision_info.
 
536
        b, branch_token, repo_token = self.make_locked_branch(format='knit')
 
537
        backing = self.get_transport()
 
538
        request = smart.branch.SmartServerBranchRequestSetLastRevisionInfo(
 
539
            backing)
 
540
        response = request.execute(
 
541
            '', branch_token, repo_token, '1', 'not-present')
 
542
        self.assertEqual(
 
543
            SmartServerResponse(('NoSuchRevision', 'not-present')), response)
 
544
 
 
545
 
 
546
class TestSmartServerBranchRequestLockWrite(tests.TestCaseWithMemoryTransport):
424
547
 
425
548
    def setUp(self):
426
 
        tests.TestCaseWithTransport.setUp(self)
427
 
        self.reduceLockdirTimeout()
 
549
        tests.TestCaseWithMemoryTransport.setUp(self)
428
550
 
429
551
    def test_lock_write_on_unlocked_branch(self):
430
552
        backing = self.get_transport()
431
553
        request = smart.branch.SmartServerBranchRequestLockWrite(backing)
432
554
        branch = self.make_branch('.', format='knit')
433
555
        repository = branch.repository
434
 
        response = request.execute(backing.local_abspath(''))
 
556
        response = request.execute('')
435
557
        branch_nonce = branch.control_files._lock.peek().get('nonce')
436
558
        repository_nonce = repository.control_files._lock.peek().get('nonce')
437
559
        self.assertEqual(
449
571
        branch.lock_write()
450
572
        branch.leave_lock_in_place()
451
573
        branch.unlock()
452
 
        response = request.execute(backing.local_abspath(''))
 
574
        response = request.execute('')
453
575
        self.assertEqual(
454
576
            SmartServerResponse(('LockContention',)), response)
455
577
 
463
585
        branch.leave_lock_in_place()
464
586
        branch.repository.leave_lock_in_place()
465
587
        branch.unlock()
466
 
        response = request.execute(backing.local_abspath(''),
 
588
        response = request.execute('',
467
589
                                   branch_token, repo_token)
468
590
        self.assertEqual(
469
591
            SmartServerResponse(('ok', branch_token, repo_token)), response)
478
600
        branch.leave_lock_in_place()
479
601
        branch.repository.leave_lock_in_place()
480
602
        branch.unlock()
481
 
        response = request.execute(backing.local_abspath(''),
 
603
        response = request.execute('',
482
604
                                   branch_token+'xxx', repo_token)
483
605
        self.assertEqual(
484
606
            SmartServerResponse(('TokenMismatch',)), response)
490
612
        branch.repository.lock_write()
491
613
        branch.repository.leave_lock_in_place()
492
614
        branch.repository.unlock()
493
 
        response = request.execute(backing.local_abspath(''))
 
615
        response = request.execute('')
494
616
        self.assertEqual(
495
617
            SmartServerResponse(('LockContention',)), response)
496
618
 
498
620
        backing = self.get_readonly_transport()
499
621
        request = smart.branch.SmartServerBranchRequestLockWrite(backing)
500
622
        branch = self.make_branch('.')
501
 
        response = request.execute('')
 
623
        root = self.get_transport().clone('/')
 
624
        path = urlutils.relative_url(root.base, self.get_transport().base)
 
625
        response = request.execute(path)
502
626
        error_name, lock_str, why_str = response.args
503
627
        self.assertFalse(response.is_successful())
504
628
        self.assertEqual('LockFailed', error_name)
505
629
 
506
630
 
507
 
class TestSmartServerBranchRequestUnlock(tests.TestCaseWithTransport):
 
631
class TestSmartServerBranchRequestUnlock(tests.TestCaseWithMemoryTransport):
508
632
 
509
633
    def setUp(self):
510
 
        tests.TestCaseWithTransport.setUp(self)
511
 
        self.reduceLockdirTimeout()
 
634
        tests.TestCaseWithMemoryTransport.setUp(self)
512
635
 
513
636
    def test_unlock_on_locked_branch_and_repo(self):
514
637
        backing = self.get_transport()
523
646
        branch.leave_lock_in_place()
524
647
        branch.repository.leave_lock_in_place()
525
648
        branch.unlock()
526
 
        response = request.execute(backing.local_abspath(''),
 
649
        response = request.execute('',
527
650
                                   branch_token, repo_token)
528
651
        self.assertEqual(
529
652
            SmartServerResponse(('ok',)), response)
538
661
        request = smart.branch.SmartServerBranchRequestUnlock(backing)
539
662
        branch = self.make_branch('.', format='knit')
540
663
        response = request.execute(
541
 
            backing.local_abspath(''), 'branch token', 'repo token')
 
664
            '', 'branch token', 'repo token')
542
665
        self.assertEqual(
543
666
            SmartServerResponse(('TokenMismatch',)), response)
544
667
 
553
676
        # Issue branch lock_write request on the unlocked branch (with locked
554
677
        # repo).
555
678
        response = request.execute(
556
 
            backing.local_abspath(''), 'branch token', repo_token)
 
679
            '', 'branch token', repo_token)
557
680
        self.assertEqual(
558
681
            SmartServerResponse(('TokenMismatch',)), response)
559
682
 
560
683
 
561
 
class TestSmartServerRepositoryRequest(tests.TestCaseWithTransport):
 
684
class TestSmartServerRepositoryRequest(tests.TestCaseWithMemoryTransport):
562
685
 
563
686
    def test_no_repository(self):
564
687
        """Raise NoRepositoryPresent when there is a bzrdir and no repo."""
571
694
        self.make_repository('.', shared=True)
572
695
        self.make_bzrdir('subdir')
573
696
        self.assertRaises(errors.NoRepositoryPresent,
574
 
            request.execute, backing.local_abspath('subdir'))
 
697
            request.execute, 'subdir')
575
698
 
576
699
 
577
700
class TestSmartServerRepositoryGetParentMap(tests.TestCaseWithTransport):
583
706
        tree = self.make_branch_and_memory_tree('.')
584
707
 
585
708
        self.assertEqual(None,
586
 
            request.execute(backing.local_abspath(''), 'missing-id'))
 
709
            request.execute('', 'missing-id'))
587
710
        # Note that it returns a body (of '' bzipped).
588
711
        self.assertEqual(
589
712
            SuccessfulSmartServerResponse(('ok', ), bz2.compress('')),
590
713
            request.do_body('\n\n0\n'))
591
714
 
592
715
 
593
 
class TestSmartServerRepositoryGetRevisionGraph(tests.TestCaseWithTransport):
 
716
class TestSmartServerRepositoryGetRevisionGraph(tests.TestCaseWithMemoryTransport):
594
717
 
595
718
    def test_none_argument(self):
596
719
        backing = self.get_transport()
605
728
        # the lines of revision_id->revision_parent_list has no guaranteed
606
729
        # order coming out of a dict, so sort both our test and response
607
730
        lines = sorted([' '.join([r2, r1]), r1])
608
 
        response = request.execute(backing.local_abspath(''), '')
 
731
        response = request.execute('', '')
609
732
        response.body = '\n'.join(sorted(response.body.split('\n')))
610
733
 
611
734
        self.assertEqual(
623
746
        tree.unlock()
624
747
 
625
748
        self.assertEqual(SmartServerResponse(('ok', ), rev_id_utf8),
626
 
            request.execute(backing.local_abspath(''), rev_id_utf8))
 
749
            request.execute('', rev_id_utf8))
627
750
    
628
751
    def test_no_such_revision(self):
629
752
        backing = self.get_transport()
637
760
        # Note that it still returns body (of zero bytes).
638
761
        self.assertEqual(
639
762
            SmartServerResponse(('nosuchrevision', 'missingrevision', ), ''),
640
 
            request.execute(backing.local_abspath(''), 'missingrevision'))
641
 
 
642
 
 
643
 
class TestSmartServerRequestHasRevision(tests.TestCaseWithTransport):
 
763
            request.execute('', 'missingrevision'))
 
764
 
 
765
 
 
766
class TestSmartServerRequestHasRevision(tests.TestCaseWithMemoryTransport):
644
767
 
645
768
    def test_missing_revision(self):
646
769
        """For a missing revision, ('no', ) is returned."""
648
771
        request = smart.repository.SmartServerRequestHasRevision(backing)
649
772
        self.make_repository('.')
650
773
        self.assertEqual(SmartServerResponse(('no', )),
651
 
            request.execute(backing.local_abspath(''), 'revid'))
 
774
            request.execute('', 'revid'))
652
775
 
653
776
    def test_present_revision(self):
654
777
        """For a present revision, ('yes', ) is returned."""
662
785
        tree.unlock()
663
786
        self.assertTrue(tree.branch.repository.has_revision(rev_id_utf8))
664
787
        self.assertEqual(SmartServerResponse(('yes', )),
665
 
            request.execute(backing.local_abspath(''), rev_id_utf8))
666
 
 
667
 
 
668
 
class TestSmartServerRepositoryGatherStats(tests.TestCaseWithTransport):
 
788
            request.execute('', rev_id_utf8))
 
789
 
 
790
 
 
791
class TestSmartServerRepositoryGatherStats(tests.TestCaseWithMemoryTransport):
669
792
 
670
793
    def test_empty_revid(self):
671
794
        """With an empty revid, we get only size an number and revisions"""
676
799
        size = stats['size']
677
800
        expected_body = 'revisions: 0\nsize: %d\n' % size
678
801
        self.assertEqual(SmartServerResponse(('ok', ), expected_body),
679
 
                         request.execute(backing.local_abspath(''), '', 'no'))
 
802
                         request.execute('', '', 'no'))
680
803
 
681
804
    def test_revid_with_committers(self):
682
805
        """For a revid we get more infos."""
699
822
                         'revisions: 2\n'
700
823
                         'size: %d\n' % size)
701
824
        self.assertEqual(SmartServerResponse(('ok', ), expected_body),
702
 
                         request.execute(backing.local_abspath(''),
 
825
                         request.execute('',
703
826
                                         rev_id_utf8, 'no'))
704
827
 
705
828
    def test_not_empty_repository_with_committers(self):
725
848
                         'revisions: 2\n'
726
849
                         'size: %d\n' % size)
727
850
        self.assertEqual(SmartServerResponse(('ok', ), expected_body),
728
 
                         request.execute(backing.local_abspath(''),
 
851
                         request.execute('',
729
852
                                         rev_id_utf8, 'yes'))
730
853
 
731
854
 
732
 
class TestSmartServerRepositoryIsShared(tests.TestCaseWithTransport):
 
855
class TestSmartServerRepositoryIsShared(tests.TestCaseWithMemoryTransport):
733
856
 
734
857
    def test_is_shared(self):
735
858
        """For a shared repository, ('yes', ) is returned."""
737
860
        request = smart.repository.SmartServerRepositoryIsShared(backing)
738
861
        self.make_repository('.', shared=True)
739
862
        self.assertEqual(SmartServerResponse(('yes', )),
740
 
            request.execute(backing.local_abspath(''), ))
 
863
            request.execute('', ))
741
864
 
742
865
    def test_is_not_shared(self):
743
866
        """For a shared repository, ('no', ) is returned."""
745
868
        request = smart.repository.SmartServerRepositoryIsShared(backing)
746
869
        self.make_repository('.', shared=False)
747
870
        self.assertEqual(SmartServerResponse(('no', )),
748
 
            request.execute(backing.local_abspath(''), ))
749
 
 
750
 
 
751
 
class TestSmartServerRepositoryLockWrite(tests.TestCaseWithTransport):
 
871
            request.execute('', ))
 
872
 
 
873
 
 
874
class TestSmartServerRepositoryLockWrite(tests.TestCaseWithMemoryTransport):
752
875
 
753
876
    def setUp(self):
754
 
        tests.TestCaseWithTransport.setUp(self)
755
 
        self.reduceLockdirTimeout()
 
877
        tests.TestCaseWithMemoryTransport.setUp(self)
756
878
 
757
879
    def test_lock_write_on_unlocked_repo(self):
758
880
        backing = self.get_transport()
759
881
        request = smart.repository.SmartServerRepositoryLockWrite(backing)
760
882
        repository = self.make_repository('.', format='knit')
761
 
        response = request.execute(backing.local_abspath(''))
 
883
        response = request.execute('')
762
884
        nonce = repository.control_files._lock.peek().get('nonce')
763
885
        self.assertEqual(SmartServerResponse(('ok', nonce)), response)
764
886
        # The repository is now locked.  Verify that with a new repository
773
895
        repository.lock_write()
774
896
        repository.leave_lock_in_place()
775
897
        repository.unlock()
776
 
        response = request.execute(backing.local_abspath(''))
 
898
        response = request.execute('')
777
899
        self.assertEqual(
778
900
            SmartServerResponse(('LockContention',)), response)
779
901
 
786
908
        self.assertEqual('LockFailed', response.args[0])
787
909
 
788
910
 
789
 
class TestSmartServerRepositoryUnlock(tests.TestCaseWithTransport):
 
911
class TestSmartServerRepositoryUnlock(tests.TestCaseWithMemoryTransport):
790
912
 
791
913
    def setUp(self):
792
 
        tests.TestCaseWithTransport.setUp(self)
793
 
        self.reduceLockdirTimeout()
 
914
        tests.TestCaseWithMemoryTransport.setUp(self)
794
915
 
795
916
    def test_unlock_on_locked_repo(self):
796
917
        backing = self.get_transport()
799
920
        token = repository.lock_write()
800
921
        repository.leave_lock_in_place()
801
922
        repository.unlock()
802
 
        response = request.execute(backing.local_abspath(''), token)
 
923
        response = request.execute('', token)
803
924
        self.assertEqual(
804
925
            SmartServerResponse(('ok',)), response)
805
926
        # The repository is now unlocked.  Verify that with a new repository
812
933
        backing = self.get_transport()
813
934
        request = smart.repository.SmartServerRepositoryUnlock(backing)
814
935
        repository = self.make_repository('.', format='knit')
815
 
        response = request.execute(backing.local_abspath(''), 'some token')
 
936
        response = request.execute('', 'some token')
816
937
        self.assertEqual(
817
938
            SmartServerResponse(('TokenMismatch',)), response)
818
939
 
826
947
        # make some extraneous junk in the repository directory which should
827
948
        # not be copied
828
949
        self.build_tree(['.bzr/repository/extra-junk'])
829
 
        response = request.execute(backing.local_abspath(''), 'bz2')
 
950
        response = request.execute('', 'bz2')
830
951
        self.assertEqual(('ok',), response.args)
831
952
        # body should be a tbz2
832
953
        body_file = StringIO(response.body)
841
962
            "extraneous file present in tar file")
842
963
 
843
964
 
844
 
class TestSmartServerRepositoryStreamKnitData(tests.TestCaseWithTransport):
 
965
class TestSmartServerRepositoryStreamKnitData(tests.TestCaseWithMemoryTransport):
845
966
 
846
967
    def test_fetch_revisions(self):
847
968
        backing = self.get_transport()
855
976
        r1 = tree.commit('2nd commit', rev_id=rev_id2_utf8)
856
977
        tree.unlock()
857
978
 
858
 
        response = request.execute(backing.local_abspath(''), rev_id2_utf8)
 
979
        response = request.execute('', rev_id2_utf8)
859
980
        self.assertEqual(('ok',), response.args)
860
 
        from cStringIO import StringIO
861
981
        unpacker = pack.ContainerReader(StringIO(response.body))
862
982
        names = []
863
983
        for [name], read_bytes in unpacker.iter_records():
873
993
        request = smart.repository.SmartServerRepositoryStreamKnitDataForRevisions(backing)
874
994
        repo = self.make_repository('.')
875
995
        rev_id1_utf8 = u'\xc8'.encode('utf-8')
876
 
        response = request.execute(backing.local_abspath(''), rev_id1_utf8)
 
996
        response = request.execute('', rev_id1_utf8)
877
997
        self.assertEqual(
878
998
            SmartServerResponse(('NoSuchRevision', rev_id1_utf8)),
879
999
            response)
880
1000
 
881
1001
 
882
 
class TestSmartServerRepositoryStreamRevisionsChunked(tests.TestCaseWithTransport):
 
1002
class TestSmartServerRepositoryStreamRevisionsChunked(tests.TestCaseWithMemoryTransport):
883
1003
 
884
1004
    def test_fetch_revisions(self):
885
1005
        backing = self.get_transport()
894
1014
        tree.commit('2nd commit', rev_id=rev_id2_utf8)
895
1015
        tree.unlock()
896
1016
 
897
 
        response = request.execute(backing.local_abspath(''))
 
1017
        response = request.execute('')
898
1018
        self.assertEqual(None, response)
899
1019
        response = request.do_body("%s\n%s\n1" % (rev_id2_utf8, rev_id1_utf8))
900
1020
        self.assertEqual(('ok',), response.args)
901
 
        from cStringIO import StringIO
902
1021
        parser = pack.ContainerPushParser()
903
1022
        names = []
904
1023
        for stream_bytes in response.body_stream:
916
1035
            backing)
917
1036
        repo = self.make_repository('.')
918
1037
        rev_id1_utf8 = u'\xc8'.encode('utf-8')
919
 
        response = request.execute(backing.local_abspath(''))
 
1038
        response = request.execute('')
920
1039
        self.assertEqual(None, response)
921
1040
        response = request.do_body("%s\n\n1" % (rev_id1_utf8,))
922
1041
        self.assertEqual(
924
1043
            response)
925
1044
 
926
1045
 
927
 
class TestSmartServerIsReadonly(tests.TestCaseWithTransport):
 
1046
class TestSmartServerIsReadonly(tests.TestCaseWithMemoryTransport):
928
1047
 
929
1048
    def test_is_readonly_no(self):
930
1049
        backing = self.get_transport()
962
1081
            smart.request.request_handlers.get('Branch.set_last_revision'),
963
1082
            smart.branch.SmartServerBranchRequestSetLastRevision)
964
1083
        self.assertEqual(
 
1084
            smart.request.request_handlers.get('Branch.set_last_revision_info'),
 
1085
            smart.branch.SmartServerBranchRequestSetLastRevisionInfo)
 
1086
        self.assertEqual(
965
1087
            smart.request.request_handlers.get('Branch.unlock'),
966
1088
            smart.branch.SmartServerBranchRequestUnlock)
967
1089
        self.assertEqual(