~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/tests/test_remote.py

  • Committer: Canonical.com Patch Queue Manager
  • Date: 2008-01-15 00:34:05 UTC
  • mfrom: (3104.4.7 bzr-http-client)
  • Revision ID: pqm@pqm.ubuntu.com-20080115003405-jfuumkpctmvl2e4r
(andrew) Calculate remote path relative to the shared medium in
        _SmartClient.

Show diffs side-by-side

added added

removed removed

Lines of Context:
130
130
class FakeClient(_SmartClient):
131
131
    """Lookalike for _SmartClient allowing testing."""
132
132
    
133
 
    def __init__(self, responses):
134
 
        # We don't call the super init because there is no medium.
 
133
    def __init__(self, responses, fake_medium_base='fake base'):
135
134
        """Create a FakeClient.
136
135
 
137
 
        :param respones: A list of response-tuple, body-data pairs to be sent
 
136
        :param responses: A list of response-tuple, body-data pairs to be sent
138
137
            back to callers.
139
138
        """
140
139
        self.responses = responses
141
140
        self._calls = []
142
141
        self.expecting_body = False
 
142
        _SmartClient.__init__(self, FakeMedium(fake_medium_base))
143
143
 
144
144
    def call(self, method, *args):
145
145
        self._calls.append(('call', method, args))
152
152
        return result[0], FakeProtocol(result[1], self)
153
153
 
154
154
 
 
155
class FakeMedium(object):
 
156
 
 
157
    def __init__(self, base):
 
158
        self.base = base
 
159
 
 
160
 
155
161
class TestBzrDirOpenBranch(tests.TestCase):
156
162
 
157
163
    def test_branch_present(self):
158
 
        client = FakeClient([(('ok', ''), ), (('ok', '', 'no', 'no'), )])
159
164
        transport = MemoryTransport()
160
165
        transport.mkdir('quack')
161
166
        transport = transport.clone('quack')
 
167
        client = FakeClient([(('ok', ''), ), (('ok', '', 'no', 'no'), )],
 
168
                            transport.base)
162
169
        bzrdir = RemoteBzrDir(transport, _client=client)
163
170
        result = bzrdir.open_branch()
164
171
        self.assertEqual(
165
 
            [('call', 'BzrDir.open_branch', ('///quack/',)),
166
 
             ('call', 'BzrDir.find_repository', ('///quack/',))],
 
172
            [('call', 'BzrDir.open_branch', ('quack/',)),
 
173
             ('call', 'BzrDir.find_repository', ('quack/',))],
167
174
            client._calls)
168
175
        self.assertIsInstance(result, RemoteBranch)
169
176
        self.assertEqual(bzrdir, result.bzrdir)
170
177
 
171
178
    def test_branch_missing(self):
172
 
        client = FakeClient([(('nobranch',), )])
173
179
        transport = MemoryTransport()
174
180
        transport.mkdir('quack')
175
181
        transport = transport.clone('quack')
 
182
        client = FakeClient([(('nobranch',), )], transport.base)
176
183
        bzrdir = RemoteBzrDir(transport, _client=client)
177
184
        self.assertRaises(errors.NotBranchError, bzrdir.open_branch)
178
185
        self.assertEqual(
179
 
            [('call', 'BzrDir.open_branch', ('///quack/',))],
 
186
            [('call', 'BzrDir.open_branch', ('quack/',))],
180
187
            client._calls)
181
188
 
182
189
    def check_open_repository(self, rich_root, subtrees):
 
190
        transport = MemoryTransport()
 
191
        transport.mkdir('quack')
 
192
        transport = transport.clone('quack')
183
193
        if rich_root:
184
194
            rich_response = 'yes'
185
195
        else:
188
198
            subtree_response = 'yes'
189
199
        else:
190
200
            subtree_response = 'no'
191
 
        client = FakeClient([(('ok', '', rich_response, subtree_response), ),])
192
 
        transport = MemoryTransport()
193
 
        transport.mkdir('quack')
194
 
        transport = transport.clone('quack')
 
201
        client = FakeClient([(('ok', '', rich_response, subtree_response), ),],
 
202
                            transport.base)
195
203
        bzrdir = RemoteBzrDir(transport, _client=client)
196
204
        result = bzrdir.open_repository()
197
205
        self.assertEqual(
198
 
            [('call', 'BzrDir.find_repository', ('///quack/',))],
 
206
            [('call', 'BzrDir.find_repository', ('quack/',))],
199
207
            client._calls)
200
208
        self.assertIsInstance(result, RemoteRepository)
201
209
        self.assertEqual(bzrdir, result.bzrdir)
245
253
 
246
254
    def test_empty_branch(self):
247
255
        # in an empty branch we decode the response properly
248
 
        client = FakeClient([(('ok', '0', 'null:'), )])
249
256
        transport = MemoryTransport()
 
257
        client = FakeClient([(('ok', '0', 'null:'), )], transport.base)
250
258
        transport.mkdir('quack')
251
259
        transport = transport.clone('quack')
252
260
        # we do not want bzrdir to make any remote calls
255
263
        result = branch.last_revision_info()
256
264
 
257
265
        self.assertEqual(
258
 
            [('call', 'Branch.last_revision_info', ('///quack/',))],
 
266
            [('call', 'Branch.last_revision_info', ('quack/',))],
259
267
            client._calls)
260
268
        self.assertEqual((0, NULL_REVISION), result)
261
269
 
262
270
    def test_non_empty_branch(self):
263
271
        # in a non-empty branch we also decode the response properly
264
272
        revid = u'\xc8'.encode('utf8')
265
 
        client = FakeClient([(('ok', '2', revid), )])
266
273
        transport = MemoryTransport()
 
274
        client = FakeClient([(('ok', '2', revid), )], transport.base)
267
275
        transport.mkdir('kwaak')
268
276
        transport = transport.clone('kwaak')
269
277
        # we do not want bzrdir to make any remote calls
272
280
        result = branch.last_revision_info()
273
281
 
274
282
        self.assertEqual(
275
 
            [('call', 'Branch.last_revision_info', ('///kwaak/',))],
 
283
            [('call', 'Branch.last_revision_info', ('kwaak/',))],
276
284
            client._calls)
277
285
        self.assertEqual((2, revid), result)
278
286
 
282
290
    def test_set_empty(self):
283
291
        # set_revision_history([]) is translated to calling
284
292
        # Branch.set_last_revision(path, '') on the wire.
 
293
        transport = MemoryTransport()
 
294
        transport.mkdir('branch')
 
295
        transport = transport.clone('branch')
 
296
 
285
297
        client = FakeClient([
286
298
            # lock_write
287
299
            (('ok', 'branch token', 'repo token'), ),
288
300
            # set_last_revision
289
301
            (('ok',), ),
290
302
            # unlock
291
 
            (('ok',), )])
292
 
        transport = MemoryTransport()
293
 
        transport.mkdir('branch')
294
 
        transport = transport.clone('branch')
295
 
 
 
303
            (('ok',), )],
 
304
            transport.base)
296
305
        bzrdir = RemoteBzrDir(transport, _client=False)
297
306
        branch = RemoteBranch(bzrdir, None, _client=client)
298
307
        # This is a hack to work around the problem that RemoteBranch currently
303
312
        result = branch.set_revision_history([])
304
313
        self.assertEqual(
305
314
            [('call', 'Branch.set_last_revision',
306
 
                ('///branch/', 'branch token', 'repo token', 'null:'))],
 
315
                ('branch/', 'branch token', 'repo token', 'null:'))],
307
316
            client._calls)
308
317
        branch.unlock()
309
318
        self.assertEqual(None, result)
311
320
    def test_set_nonempty(self):
312
321
        # set_revision_history([rev-id1, ..., rev-idN]) is translated to calling
313
322
        # Branch.set_last_revision(path, rev-idN) on the wire.
 
323
        transport = MemoryTransport()
 
324
        transport.mkdir('branch')
 
325
        transport = transport.clone('branch')
 
326
 
314
327
        client = FakeClient([
315
328
            # lock_write
316
329
            (('ok', 'branch token', 'repo token'), ),
317
330
            # set_last_revision
318
331
            (('ok',), ),
319
332
            # unlock
320
 
            (('ok',), )])
321
 
        transport = MemoryTransport()
322
 
        transport.mkdir('branch')
323
 
        transport = transport.clone('branch')
324
 
 
 
333
            (('ok',), )],
 
334
            transport.base)
325
335
        bzrdir = RemoteBzrDir(transport, _client=False)
326
336
        branch = RemoteBranch(bzrdir, None, _client=client)
327
337
        # This is a hack to work around the problem that RemoteBranch currently
334
344
        result = branch.set_revision_history(['rev-id1', 'rev-id2'])
335
345
        self.assertEqual(
336
346
            [('call', 'Branch.set_last_revision',
337
 
                ('///branch/', 'branch token', 'repo token', 'rev-id2'))],
 
347
                ('branch/', 'branch token', 'repo token', 'rev-id2'))],
338
348
            client._calls)
339
349
        branch.unlock()
340
350
        self.assertEqual(None, result)
374
384
 
375
385
    def test_get_branch_conf(self):
376
386
        # in an empty branch we decode the response properly
377
 
        client = FakeClient([(('ok', ), 'config file body')])
 
387
        client = FakeClient([(('ok', ), 'config file body')], self.get_url())
378
388
        # we need to make a real branch because the remote_branch.control_files
379
389
        # will trigger _ensure_real.
380
390
        branch = self.make_branch('quack')
384
394
        branch = RemoteBranch(bzrdir, None, _client=client)
385
395
        result = branch.control_files.get('branch.conf')
386
396
        self.assertEqual(
387
 
            [('call_expecting_body', 'Branch.get_config_file', ('///quack/',))],
 
397
            [('call_expecting_body', 'Branch.get_config_file', ('quack/',))],
388
398
            client._calls)
389
399
        self.assertEqual('config file body', result.read())
390
400
 
392
402
class TestBranchLockWrite(tests.TestCase):
393
403
 
394
404
    def test_lock_write_unlockable(self):
395
 
        client = FakeClient([(('UnlockableTransport', ), '')])
396
405
        transport = MemoryTransport()
 
406
        client = FakeClient([(('UnlockableTransport', ), '')], transport.base)
397
407
        transport.mkdir('quack')
398
408
        transport = transport.clone('quack')
399
409
        # we do not want bzrdir to make any remote calls
401
411
        branch = RemoteBranch(bzrdir, None, _client=client)
402
412
        self.assertRaises(errors.UnlockableTransport, branch.lock_write)
403
413
        self.assertEqual(
404
 
            [('call', 'Branch.lock_write', ('///quack/', '', ''))],
 
414
            [('call', 'Branch.lock_write', ('quack/', '', ''))],
405
415
            client._calls)
406
416
 
407
417
 
474
484
        :param transport_path: Path below the root of the MemoryTransport
475
485
            where the repository will be created.
476
486
        """
477
 
        client = FakeClient(responses)
478
487
        transport = MemoryTransport()
479
488
        transport.mkdir(transport_path)
 
489
        client = FakeClient(responses, transport.base)
480
490
        transport = transport.clone(transport_path)
481
491
        # we do not want bzrdir to make any remote calls
482
492
        bzrdir = RemoteBzrDir(transport, _client=False)
495
505
        result = repo.gather_stats(None)
496
506
        self.assertEqual(
497
507
            [('call_expecting_body', 'Repository.gather_stats',
498
 
             ('///quack/','','no'))],
 
508
             ('quack/','','no'))],
499
509
            client._calls)
500
510
        self.assertEqual({'revisions': 2, 'size': 18}, result)
501
511
 
513
523
        result = repo.gather_stats(revid)
514
524
        self.assertEqual(
515
525
            [('call_expecting_body', 'Repository.gather_stats',
516
 
              ('///quick/', revid, 'no'))],
 
526
              ('quick/', revid, 'no'))],
517
527
            client._calls)
518
528
        self.assertEqual({'revisions': 2, 'size': 18,
519
529
                          'firstrev': (123456.300, 3600),
535
545
        result = repo.gather_stats(revid, True)
536
546
        self.assertEqual(
537
547
            [('call_expecting_body', 'Repository.gather_stats',
538
 
              ('///buick/', revid, 'yes'))],
 
548
              ('buick/', revid, 'yes'))],
539
549
            client._calls)
540
550
        self.assertEqual({'revisions': 2, 'size': 18,
541
551
                          'committers': 128,
571
581
        result = repo.get_revision_graph()
572
582
        self.assertEqual(
573
583
            [('call_expecting_body', 'Repository.get_revision_graph',
574
 
             ('///sinhala/', ''))],
 
584
             ('sinhala/', ''))],
575
585
            client._calls)
576
586
        self.assertEqual({r1: (), r2: (r1, )}, result)
577
587
 
591
601
        result = repo.get_revision_graph(r2)
592
602
        self.assertEqual(
593
603
            [('call_expecting_body', 'Repository.get_revision_graph',
594
 
             ('///sinhala/', r2))],
 
604
             ('sinhala/', r2))],
595
605
            client._calls)
596
606
        self.assertEqual({r11: (), r12: (), r2: (r11, r12), }, result)
597
607
 
606
616
            repo.get_revision_graph, revid)
607
617
        self.assertEqual(
608
618
            [('call_expecting_body', 'Repository.get_revision_graph',
609
 
             ('///sinhala/', revid))],
 
619
             ('sinhala/', revid))],
610
620
            client._calls)
611
621
 
612
622
        
620
630
            responses, transport_path)
621
631
        result = repo.is_shared()
622
632
        self.assertEqual(
623
 
            [('call', 'Repository.is_shared', ('///quack/',))],
 
633
            [('call', 'Repository.is_shared', ('quack/',))],
624
634
            client._calls)
625
635
        self.assertEqual(True, result)
626
636
 
632
642
            responses, transport_path)
633
643
        result = repo.is_shared()
634
644
        self.assertEqual(
635
 
            [('call', 'Repository.is_shared', ('///qwack/',))],
 
645
            [('call', 'Repository.is_shared', ('qwack/',))],
636
646
            client._calls)
637
647
        self.assertEqual(False, result)
638
648
 
646
656
            responses, transport_path)
647
657
        result = repo.lock_write()
648
658
        self.assertEqual(
649
 
            [('call', 'Repository.lock_write', ('///quack/', ''))],
 
659
            [('call', 'Repository.lock_write', ('quack/', ''))],
650
660
            client._calls)
651
661
        self.assertEqual('a token', result)
652
662
 
657
667
            responses, transport_path)
658
668
        self.assertRaises(errors.LockContention, repo.lock_write)
659
669
        self.assertEqual(
660
 
            [('call', 'Repository.lock_write', ('///quack/', ''))],
 
670
            [('call', 'Repository.lock_write', ('quack/', ''))],
661
671
            client._calls)
662
672
 
663
673
    def test_lock_write_unlockable(self):
667
677
            responses, transport_path)
668
678
        self.assertRaises(errors.UnlockableTransport, repo.lock_write)
669
679
        self.assertEqual(
670
 
            [('call', 'Repository.lock_write', ('///quack/', ''))],
 
680
            [('call', 'Repository.lock_write', ('quack/', ''))],
671
681
            client._calls)
672
682
 
673
683
 
682
692
        repo.lock_write()
683
693
        repo.unlock()
684
694
        self.assertEqual(
685
 
            [('call', 'Repository.lock_write', ('///quack/', '')),
686
 
             ('call', 'Repository.unlock', ('///quack/', 'a token'))],
 
695
            [('call', 'Repository.lock_write', ('quack/', '')),
 
696
             ('call', 'Repository.unlock', ('quack/', 'a token'))],
687
697
            client._calls)
688
698
 
689
699
    def test_unlock_wrong_token(self):
737
747
        expected_responses = [(('ok',), self.tarball_content),
738
748
            ]
739
749
        expected_calls = [('call_expecting_body', 'Repository.tarball',
740
 
                           ('///repo/', 'bz2',),),
 
750
                           ('repo/', 'bz2',),),
741
751
            ]
742
752
        remote_repo, client = self.setup_fake_client_and_repository(
743
753
            expected_responses, transport_path)