~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/tests/test_smart.py

  • Committer: John Arbash Meinel
  • Date: 2010-02-10 17:52:08 UTC
  • mfrom: (5021 +trunk)
  • mto: This revision was merged to the branch mainline in revision 5023.
  • Revision ID: john@arbash-meinel.com-20100210175208-bubuwav4uqigu291
Merge bzr.dev 5021 to resolve NEWS

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
# Copyright (C) 2006, 2007 Canonical Ltd
 
1
# Copyright (C) 2006-2010 Canonical Ltd
2
2
#
3
3
# This program is free software; you can redistribute it and/or modify
4
4
# it under the terms of the GNU General Public License as published by
30
30
 
31
31
from bzrlib import (
32
32
    bencode,
 
33
    branch as _mod_branch,
33
34
    bzrdir,
34
35
    errors,
35
36
    pack,
36
 
    smart,
37
37
    tests,
 
38
    transport,
38
39
    urlutils,
39
40
    versionedfile,
40
41
    )
41
 
from bzrlib.branch import Branch, BranchReferenceFormat
42
 
import bzrlib.smart.branch
43
 
import bzrlib.smart.bzrdir, bzrlib.smart.bzrdir as smart_dir
44
 
import bzrlib.smart.packrepository
45
 
import bzrlib.smart.repository
46
 
import bzrlib.smart.vfs
47
 
from bzrlib.smart.request import (
48
 
    FailedSmartServerResponse,
49
 
    SmartServerRequest,
50
 
    SmartServerResponse,
51
 
    SuccessfulSmartServerResponse,
52
 
    )
53
 
from bzrlib.tests import (
54
 
    split_suite_by_re,
55
 
    )
56
 
from bzrlib.transport import chroot, get_transport, local, memory
 
42
from bzrlib.smart import (
 
43
    branch as smart_branch,
 
44
    bzrdir as smart_dir,
 
45
    repository as smart_repo,
 
46
    packrepository as smart_packrepo,
 
47
    request as smart_req,
 
48
    server,
 
49
    vfs,
 
50
    )
 
51
from bzrlib.transport import (
 
52
    chroot,
 
53
    local,
 
54
    memory,
 
55
    )
57
56
 
58
57
 
59
58
def load_tests(standard_tests, module, loader):
60
59
    """Multiply tests version and protocol consistency."""
61
60
    # FindRepository tests.
62
 
    bzrdir_mod = bzrlib.smart.bzrdir
63
61
    scenarios = [
64
62
        ("find_repository", {
65
 
            "_request_class":bzrdir_mod.SmartServerRequestFindRepositoryV1}),
 
63
            "_request_class": smart_dir.SmartServerRequestFindRepositoryV1}),
66
64
        ("find_repositoryV2", {
67
 
            "_request_class":bzrdir_mod.SmartServerRequestFindRepositoryV2}),
 
65
            "_request_class": smart_dir.SmartServerRequestFindRepositoryV2}),
68
66
        ("find_repositoryV3", {
69
 
            "_request_class":bzrdir_mod.SmartServerRequestFindRepositoryV3}),
 
67
            "_request_class": smart_dir.SmartServerRequestFindRepositoryV3}),
70
68
        ]
71
 
    to_adapt, result = split_suite_by_re(standard_tests,
 
69
    to_adapt, result = tests.split_suite_by_re(standard_tests,
72
70
        "TestSmartServerRequestFindRepository")
73
 
    v2_only, v1_and_2 = split_suite_by_re(to_adapt,
 
71
    v2_only, v1_and_2 = tests.split_suite_by_re(to_adapt,
74
72
        "_v2")
75
73
    tests.multiply_tests(v1_and_2, scenarios, result)
76
74
    # The first scenario is only applicable to v1 protocols, it is deleted
91
89
            backing_transport = tests.TestCaseWithTransport.get_transport(self)
92
90
            self._chroot_server = chroot.ChrootServer(backing_transport)
93
91
            self.start_server(self._chroot_server)
94
 
        t = get_transport(self._chroot_server.get_url())
 
92
        t = transport.get_transport(self._chroot_server.get_url())
95
93
        if relpath is not None:
96
94
            t = t.clone(relpath)
97
95
        return t
108
106
        self.transport_server = self.make_transport_server
109
107
 
110
108
    def make_transport_server(self):
111
 
        return smart.server.SmartTCPServer_for_testing('-' + self.id())
 
109
        return server.SmartTCPServer_for_testing('-' + self.id())
112
110
 
113
111
    def get_smart_medium(self):
114
112
        """Get a smart medium to use in tests."""
123
121
            None, 'foo')]),('text', [
124
122
            versionedfile.FulltextContentFactory(('k2',), None, None, 'bar')])]
125
123
        fmt = bzrdir.format_registry.get('pack-0.92')().repository_format
126
 
        bytes = smart.repository._stream_to_byte_stream(stream, fmt)
 
124
        bytes = smart_repo._stream_to_byte_stream(stream, fmt)
127
125
        streams = []
128
126
        # Iterate the resulting iterable; checking that we get only one stream
129
127
        # out.
130
 
        fmt, stream = smart.repository._byte_stream_to_stream(bytes)
 
128
        fmt, stream = smart_repo._byte_stream_to_stream(bytes)
131
129
        for kind, substream in stream:
132
130
            streams.append((kind, list(substream)))
133
131
        self.assertLength(1, streams)
137
135
class TestSmartServerResponse(tests.TestCase):
138
136
 
139
137
    def test__eq__(self):
140
 
        self.assertEqual(SmartServerResponse(('ok', )),
141
 
            SmartServerResponse(('ok', )))
142
 
        self.assertEqual(SmartServerResponse(('ok', ), 'body'),
143
 
            SmartServerResponse(('ok', ), 'body'))
144
 
        self.assertNotEqual(SmartServerResponse(('ok', )),
145
 
            SmartServerResponse(('notok', )))
146
 
        self.assertNotEqual(SmartServerResponse(('ok', ), 'body'),
147
 
            SmartServerResponse(('ok', )))
 
138
        self.assertEqual(smart_req.SmartServerResponse(('ok', )),
 
139
            smart_req.SmartServerResponse(('ok', )))
 
140
        self.assertEqual(smart_req.SmartServerResponse(('ok', ), 'body'),
 
141
            smart_req.SmartServerResponse(('ok', ), 'body'))
 
142
        self.assertNotEqual(smart_req.SmartServerResponse(('ok', )),
 
143
            smart_req.SmartServerResponse(('notok', )))
 
144
        self.assertNotEqual(smart_req.SmartServerResponse(('ok', ), 'body'),
 
145
            smart_req.SmartServerResponse(('ok', )))
148
146
        self.assertNotEqual(None,
149
 
            SmartServerResponse(('ok', )))
 
147
            smart_req.SmartServerResponse(('ok', )))
150
148
 
151
149
    def test__str__(self):
152
150
        """SmartServerResponses can be stringified."""
153
151
        self.assertEqual(
154
152
            "<SuccessfulSmartServerResponse args=('args',) body='body'>",
155
 
            str(SuccessfulSmartServerResponse(('args',), 'body')))
 
153
            str(smart_req.SuccessfulSmartServerResponse(('args',), 'body')))
156
154
        self.assertEqual(
157
155
            "<FailedSmartServerResponse args=('args',) body='body'>",
158
 
            str(FailedSmartServerResponse(('args',), 'body')))
 
156
            str(smart_req.FailedSmartServerResponse(('args',), 'body')))
159
157
 
160
158
 
161
159
class TestSmartServerRequest(tests.TestCaseWithMemoryTransport):
162
160
 
163
161
    def test_translate_client_path(self):
164
162
        transport = self.get_transport()
165
 
        request = SmartServerRequest(transport, 'foo/')
 
163
        request = smart_req.SmartServerRequest(transport, 'foo/')
166
164
        self.assertEqual('./', request.translate_client_path('foo/'))
167
165
        self.assertRaises(
168
166
            errors.InvalidURLJoin, request.translate_client_path, 'foo/..')
178
176
    def test_translate_client_path_vfs(self):
179
177
        """VfsRequests receive escaped paths rather than raw UTF-8."""
180
178
        transport = self.get_transport()
181
 
        request = smart.vfs.VfsRequest(transport, 'foo/')
 
179
        request = vfs.VfsRequest(transport, 'foo/')
182
180
        e_acute = u'\N{LATIN SMALL LETTER E WITH ACUTE}'.encode('utf-8')
183
181
        escaped = urlutils.escape('foo/' + e_acute)
184
182
        self.assertEqual('./' + urlutils.escape(e_acute),
186
184
 
187
185
    def test_transport_from_client_path(self):
188
186
        transport = self.get_transport()
189
 
        request = SmartServerRequest(transport, 'foo/')
 
187
        request = smart_req.SmartServerRequest(transport, 'foo/')
190
188
        self.assertEqual(
191
189
            transport.base,
192
190
            request.transport_from_client_path('foo/').base)
203
201
        local_result = dir.cloning_metadir()
204
202
        request_class = smart_dir.SmartServerBzrDirRequestCloningMetaDir
205
203
        request = request_class(backing)
206
 
        expected = SuccessfulSmartServerResponse(
 
204
        expected = smart_req.SuccessfulSmartServerResponse(
207
205
            (local_result.network_name(),
208
206
            local_result.repository_format.network_name(),
209
207
            ('branch', local_result.get_branch_format().network_name())))
215
213
        referenced_branch = self.make_branch('referenced')
216
214
        dir = self.make_bzrdir('.')
217
215
        local_result = dir.cloning_metadir()
218
 
        reference = BranchReferenceFormat().initialize(dir, referenced_branch)
219
 
        reference_url = BranchReferenceFormat().get_reference(dir)
 
216
        reference = _mod_branch.BranchReferenceFormat().initialize(
 
217
            dir, referenced_branch)
 
218
        reference_url = _mod_branch.BranchReferenceFormat().get_reference(dir)
220
219
        # The server shouldn't try to follow the branch reference, so it's fine
221
220
        # if the referenced branch isn't reachable.
222
221
        backing.rename('referenced', 'moved')
223
222
        request_class = smart_dir.SmartServerBzrDirRequestCloningMetaDir
224
223
        request = request_class(backing)
225
 
        expected = FailedSmartServerResponse(('BranchReference',))
 
224
        expected = smart_req.FailedSmartServerResponse(('BranchReference',))
226
225
        self.assertEqual(expected, request.execute('', 'False'))
227
226
 
228
227
 
233
232
        """When there is a bzrdir present, the call succeeds."""
234
233
        backing = self.get_transport()
235
234
        self.make_bzrdir('.')
236
 
        request_class = bzrlib.smart.bzrdir.SmartServerRequestCreateRepository
 
235
        request_class = smart_dir.SmartServerRequestCreateRepository
237
236
        request = request_class(backing)
238
237
        reference_bzrdir_format = bzrdir.format_registry.get('pack-0.92')()
239
238
        reference_format = reference_bzrdir_format.repository_format
240
239
        network_name = reference_format.network_name()
241
 
        expected = SuccessfulSmartServerResponse(
 
240
        expected = smart_req.SuccessfulSmartServerResponse(
242
241
            ('ok', 'no', 'no', 'no', network_name))
243
242
        self.assertEqual(expected, request.execute('', network_name, 'True'))
244
243
 
251
250
        backing = self.get_transport()
252
251
        request = self._request_class(backing)
253
252
        self.make_bzrdir('.')
254
 
        self.assertEqual(SmartServerResponse(('norepository', )),
 
253
        self.assertEqual(smart_req.SmartServerResponse(('norepository', )),
255
254
            request.execute(''))
256
255
 
257
256
    def test_nonshared_repository(self):
263
262
        result = self._make_repository_and_result()
264
263
        self.assertEqual(result, request.execute(''))
265
264
        self.make_bzrdir('subdir')
266
 
        self.assertEqual(SmartServerResponse(('norepository', )),
 
265
        self.assertEqual(smart_req.SmartServerResponse(('norepository', )),
267
266
            request.execute('subdir'))
268
267
 
269
268
    def _make_repository_and_result(self, shared=False, format=None):
284
283
            external = 'yes'
285
284
        else:
286
285
            external = 'no'
287
 
        if (smart.bzrdir.SmartServerRequestFindRepositoryV3 ==
 
286
        if (smart_dir.SmartServerRequestFindRepositoryV3 ==
288
287
            self._request_class):
289
 
            return SuccessfulSmartServerResponse(
 
288
            return smart_req.SuccessfulSmartServerResponse(
290
289
                ('ok', '', rich_root, subtrees, external,
291
290
                 repo._format.network_name()))
292
 
        elif (smart.bzrdir.SmartServerRequestFindRepositoryV2 ==
 
291
        elif (smart_dir.SmartServerRequestFindRepositoryV2 ==
293
292
            self._request_class):
294
293
            # All tests so far are on formats, and for non-external
295
294
            # repositories.
296
 
            return SuccessfulSmartServerResponse(
 
295
            return smart_req.SuccessfulSmartServerResponse(
297
296
                ('ok', '', rich_root, subtrees, external))
298
297
        else:
299
 
            return SuccessfulSmartServerResponse(('ok', '', rich_root, subtrees))
 
298
            return smart_req.SuccessfulSmartServerResponse(
 
299
                ('ok', '', rich_root, subtrees))
300
300
 
301
301
    def test_shared_repository(self):
302
302
        """When there is a shared repository, we get 'ok', 'relpath-to-repo'."""
305
305
        result = self._make_repository_and_result(shared=True)
306
306
        self.assertEqual(result, request.execute(''))
307
307
        self.make_bzrdir('subdir')
308
 
        result2 = SmartServerResponse(result.args[0:1] + ('..', ) + result.args[2:])
 
308
        result2 = smart_req.SmartServerResponse(
 
309
            result.args[0:1] + ('..', ) + result.args[2:])
309
310
        self.assertEqual(result2,
310
311
            request.execute('subdir'))
311
312
        self.make_bzrdir('subdir/deeper')
312
 
        result3 = SmartServerResponse(result.args[0:1] + ('../..', ) + result.args[2:])
 
313
        result3 = smart_req.SmartServerResponse(
 
314
            result.args[0:1] + ('../..', ) + result.args[2:])
313
315
        self.assertEqual(result3,
314
316
            request.execute('subdir/deeper'))
315
317
 
317
319
        """Test for the format attributes for rich root and subtree support."""
318
320
        backing = self.get_transport()
319
321
        request = self._request_class(backing)
320
 
        result = self._make_repository_and_result(format='dirstate-with-subtree')
 
322
        result = self._make_repository_and_result(
 
323
            format='dirstate-with-subtree')
321
324
        # check the test will be valid
322
325
        self.assertEqual('yes', result.args[2])
323
326
        self.assertEqual('yes', result.args[3])
327
330
        """Test for the supports_external_lookups attribute."""
328
331
        backing = self.get_transport()
329
332
        request = self._request_class(backing)
330
 
        result = self._make_repository_and_result(format='dirstate-with-subtree')
 
333
        result = self._make_repository_and_result(
 
334
            format='dirstate-with-subtree')
331
335
        # check the test will be valid
332
336
        self.assertEqual('no', result.args[4])
333
337
        self.assertEqual(result, request.execute(''))
344
348
        local_result = dir._get_config()._get_config_file().read()
345
349
        request_class = smart_dir.SmartServerBzrDirRequestConfigFile
346
350
        request = request_class(backing)
347
 
        expected = SuccessfulSmartServerResponse((), local_result)
 
351
        expected = smart_req.SuccessfulSmartServerResponse((), local_result)
348
352
        self.assertEqual(expected, request.execute(''))
349
353
 
350
354
    def test_missing(self):
352
356
        dir = self.make_bzrdir('.')
353
357
        request_class = smart_dir.SmartServerBzrDirRequestConfigFile
354
358
        request = request_class(backing)
355
 
        expected = SuccessfulSmartServerResponse((), '')
 
359
        expected = smart_req.SuccessfulSmartServerResponse((), '')
356
360
        self.assertEqual(expected, request.execute(''))
357
361
 
358
362
 
361
365
    def test_empty_dir(self):
362
366
        """Initializing an empty dir should succeed and do it."""
363
367
        backing = self.get_transport()
364
 
        request = smart.bzrdir.SmartServerRequestInitializeBzrDir(backing)
365
 
        self.assertEqual(SmartServerResponse(('ok', )),
 
368
        request = smart_dir.SmartServerRequestInitializeBzrDir(backing)
 
369
        self.assertEqual(smart_req.SmartServerResponse(('ok', )),
366
370
            request.execute(''))
367
371
        made_dir = bzrdir.BzrDir.open_from_transport(backing)
368
372
        # no branch, tree or repository is expected with the current
374
378
    def test_missing_dir(self):
375
379
        """Initializing a missing directory should fail like the bzrdir api."""
376
380
        backing = self.get_transport()
377
 
        request = smart.bzrdir.SmartServerRequestInitializeBzrDir(backing)
 
381
        request = smart_dir.SmartServerRequestInitializeBzrDir(backing)
378
382
        self.assertRaises(errors.NoSuchFile,
379
383
            request.execute, 'subdir')
380
384
 
381
385
    def test_initialized_dir(self):
382
386
        """Initializing an extant bzrdir should fail like the bzrdir api."""
383
387
        backing = self.get_transport()
384
 
        request = smart.bzrdir.SmartServerRequestInitializeBzrDir(backing)
 
388
        request = smart_dir.SmartServerRequestInitializeBzrDir(backing)
385
389
        self.make_bzrdir('subdir')
386
390
        self.assertRaises(errors.FileExists,
387
391
            request.execute, 'subdir')
388
392
 
389
393
 
390
 
class TestSmartServerRequestBzrDirInitializeEx(tests.TestCaseWithMemoryTransport):
 
394
class TestSmartServerRequestBzrDirInitializeEx(
 
395
    tests.TestCaseWithMemoryTransport):
391
396
    """Basic tests for BzrDir.initialize_ex_1.16 in the smart server.
392
397
 
393
398
    The main unit tests in test_bzrdir exercise the API comprehensively.
397
402
        """Initializing an empty dir should succeed and do it."""
398
403
        backing = self.get_transport()
399
404
        name = self.make_bzrdir('reference')._format.network_name()
400
 
        request = smart.bzrdir.SmartServerRequestBzrDirInitializeEx(backing)
401
 
        self.assertEqual(SmartServerResponse(('', '', '', '', '', '', name,
402
 
            'False', '', '', '')),
 
405
        request = smart_dir.SmartServerRequestBzrDirInitializeEx(backing)
 
406
        self.assertEqual(
 
407
            smart_req.SmartServerResponse(('', '', '', '', '', '', name,
 
408
                                           'False', '', '', '')),
403
409
            request.execute(name, '', 'True', 'False', 'False', '', '', '', '',
404
 
            'False'))
 
410
                            'False'))
405
411
        made_dir = bzrdir.BzrDir.open_from_transport(backing)
406
412
        # no branch, tree or repository is expected with the current
407
413
        # default format.
413
419
        """Initializing a missing directory should fail like the bzrdir api."""
414
420
        backing = self.get_transport()
415
421
        name = self.make_bzrdir('reference')._format.network_name()
416
 
        request = smart.bzrdir.SmartServerRequestBzrDirInitializeEx(backing)
 
422
        request = smart_dir.SmartServerRequestBzrDirInitializeEx(backing)
417
423
        self.assertRaises(errors.NoSuchFile, request.execute, name,
418
424
            'subdir/dir', 'False', 'False', 'False', '', '', '', '', 'False')
419
425
 
421
427
        """Initializing an extant directory should fail like the bzrdir api."""
422
428
        backing = self.get_transport()
423
429
        name = self.make_bzrdir('reference')._format.network_name()
424
 
        request = smart.bzrdir.SmartServerRequestBzrDirInitializeEx(backing)
 
430
        request = smart_dir.SmartServerRequestBzrDirInitializeEx(backing)
425
431
        self.make_bzrdir('subdir')
426
432
        self.assertRaises(errors.FileExists, request.execute, name, 'subdir',
427
433
            'False', 'False', 'False', '', '', '', '', 'False')
428
434
 
429
435
 
430
436
class TestSmartServerRequestOpenBzrDir(tests.TestCaseWithMemoryTransport):
431
 
    
 
437
 
432
438
    def test_no_directory(self):
433
439
        backing = self.get_transport()
434
 
        request = smart.bzrdir.SmartServerRequestOpenBzrDir(backing)
435
 
        self.assertEqual(SmartServerResponse(('no', )),
 
440
        request = smart_dir.SmartServerRequestOpenBzrDir(backing)
 
441
        self.assertEqual(smart_req.SmartServerResponse(('no', )),
436
442
            request.execute('does-not-exist'))
437
443
 
438
444
    def test_empty_directory(self):
439
445
        backing = self.get_transport()
440
446
        backing.mkdir('empty')
441
 
        request = smart.bzrdir.SmartServerRequestOpenBzrDir(backing)
442
 
        self.assertEqual(SmartServerResponse(('no', )),
 
447
        request = smart_dir.SmartServerRequestOpenBzrDir(backing)
 
448
        self.assertEqual(smart_req.SmartServerResponse(('no', )),
443
449
            request.execute('empty'))
444
450
 
445
451
    def test_outside_root_client_path(self):
446
452
        backing = self.get_transport()
447
 
        request = smart.bzrdir.SmartServerRequestOpenBzrDir(backing,
 
453
        request = smart_dir.SmartServerRequestOpenBzrDir(backing,
448
454
            root_client_path='root')
449
 
        self.assertEqual(SmartServerResponse(('no', )),
 
455
        self.assertEqual(smart_req.SmartServerResponse(('no', )),
450
456
            request.execute('not-root'))
451
457
 
452
 
    
 
458
 
453
459
class TestSmartServerRequestOpenBzrDir_2_1(tests.TestCaseWithMemoryTransport):
454
 
    
 
460
 
455
461
    def test_no_directory(self):
456
462
        backing = self.get_transport()
457
 
        request = smart.bzrdir.SmartServerRequestOpenBzrDir_2_1(backing)
458
 
        self.assertEqual(SmartServerResponse(('no', )),
 
463
        request = smart_dir.SmartServerRequestOpenBzrDir_2_1(backing)
 
464
        self.assertEqual(smart_req.SmartServerResponse(('no', )),
459
465
            request.execute('does-not-exist'))
460
466
 
461
467
    def test_empty_directory(self):
462
468
        backing = self.get_transport()
463
469
        backing.mkdir('empty')
464
 
        request = smart.bzrdir.SmartServerRequestOpenBzrDir_2_1(backing)
465
 
        self.assertEqual(SmartServerResponse(('no', )),
 
470
        request = smart_dir.SmartServerRequestOpenBzrDir_2_1(backing)
 
471
        self.assertEqual(smart_req.SmartServerResponse(('no', )),
466
472
            request.execute('empty'))
467
473
 
468
474
    def test_present_without_workingtree(self):
469
475
        backing = self.get_transport()
470
 
        request = smart.bzrdir.SmartServerRequestOpenBzrDir_2_1(backing)
 
476
        request = smart_dir.SmartServerRequestOpenBzrDir_2_1(backing)
471
477
        self.make_bzrdir('.')
472
 
        self.assertEqual(SmartServerResponse(('yes', 'no')),
 
478
        self.assertEqual(smart_req.SmartServerResponse(('yes', 'no')),
473
479
            request.execute(''))
474
480
 
475
481
    def test_outside_root_client_path(self):
476
482
        backing = self.get_transport()
477
 
        request = smart.bzrdir.SmartServerRequestOpenBzrDir_2_1(backing,
 
483
        request = smart_dir.SmartServerRequestOpenBzrDir_2_1(backing,
478
484
            root_client_path='root')
479
 
        self.assertEqual(SmartServerResponse(('no',)),
 
485
        self.assertEqual(smart_req.SmartServerResponse(('no',)),
480
486
            request.execute('not-root'))
481
487
 
482
 
    
 
488
 
483
489
class TestSmartServerRequestOpenBzrDir_2_1_disk(TestCaseWithChrootedTransport):
484
490
 
485
491
    def test_present_with_workingtree(self):
486
492
        self.vfs_transport_factory = local.LocalURLServer
487
493
        backing = self.get_transport()
488
 
        request = smart.bzrdir.SmartServerRequestOpenBzrDir_2_1(backing)
 
494
        request = smart_dir.SmartServerRequestOpenBzrDir_2_1(backing)
489
495
        bd = self.make_bzrdir('.')
490
496
        bd.create_repository()
491
497
        bd.create_branch()
492
498
        bd.create_workingtree()
493
 
        self.assertEqual(SmartServerResponse(('yes', 'yes')),
 
499
        self.assertEqual(smart_req.SmartServerResponse(('yes', 'yes')),
494
500
            request.execute(''))
495
501
 
496
502
 
499
505
    def test_no_branch(self):
500
506
        """When there is no branch, ('nobranch', ) is returned."""
501
507
        backing = self.get_transport()
502
 
        request = smart.bzrdir.SmartServerRequestOpenBranch(backing)
 
508
        request = smart_dir.SmartServerRequestOpenBranch(backing)
503
509
        self.make_bzrdir('.')
504
 
        self.assertEqual(SmartServerResponse(('nobranch', )),
 
510
        self.assertEqual(smart_req.SmartServerResponse(('nobranch', )),
505
511
            request.execute(''))
506
512
 
507
513
    def test_branch(self):
508
514
        """When there is a branch, 'ok' is returned."""
509
515
        backing = self.get_transport()
510
 
        request = smart.bzrdir.SmartServerRequestOpenBranch(backing)
 
516
        request = smart_dir.SmartServerRequestOpenBranch(backing)
511
517
        self.make_branch('.')
512
 
        self.assertEqual(SmartServerResponse(('ok', '')),
 
518
        self.assertEqual(smart_req.SmartServerResponse(('ok', '')),
513
519
            request.execute(''))
514
520
 
515
521
    def test_branch_reference(self):
516
522
        """When there is a branch reference, the reference URL is returned."""
517
523
        self.vfs_transport_factory = local.LocalURLServer
518
524
        backing = self.get_transport()
519
 
        request = smart.bzrdir.SmartServerRequestOpenBranch(backing)
 
525
        request = smart_dir.SmartServerRequestOpenBranch(backing)
520
526
        branch = self.make_branch('branch')
521
527
        checkout = branch.create_checkout('reference',lightweight=True)
522
 
        reference_url = BranchReferenceFormat().get_reference(checkout.bzrdir)
 
528
        reference_url = _mod_branch.BranchReferenceFormat().get_reference(
 
529
            checkout.bzrdir)
523
530
        self.assertFileEqual(reference_url, 'reference/.bzr/branch/location')
524
 
        self.assertEqual(SmartServerResponse(('ok', reference_url)),
 
531
        self.assertEqual(smart_req.SmartServerResponse(('ok', reference_url)),
525
532
            request.execute('reference'))
526
533
 
527
534
    def test_notification_on_branch_from_repository(self):
528
535
        """When there is a repository, the error should return details."""
529
536
        backing = self.get_transport()
530
 
        request = smart.bzrdir.SmartServerRequestOpenBranch(backing)
 
537
        request = smart_dir.SmartServerRequestOpenBranch(backing)
531
538
        repo = self.make_repository('.')
532
 
        self.assertEqual(SmartServerResponse(('nobranch',)),
 
539
        self.assertEqual(smart_req.SmartServerResponse(('nobranch',)),
533
540
            request.execute(''))
534
541
 
535
542
 
539
546
        """When there is no branch, ('nobranch', ) is returned."""
540
547
        backing = self.get_transport()
541
548
        self.make_bzrdir('.')
542
 
        request = smart.bzrdir.SmartServerRequestOpenBranchV2(backing)
543
 
        self.assertEqual(SmartServerResponse(('nobranch', )),
 
549
        request = smart_dir.SmartServerRequestOpenBranchV2(backing)
 
550
        self.assertEqual(smart_req.SmartServerResponse(('nobranch', )),
544
551
            request.execute(''))
545
552
 
546
553
    def test_branch(self):
547
554
        """When there is a branch, 'ok' is returned."""
548
555
        backing = self.get_transport()
549
556
        expected = self.make_branch('.')._format.network_name()
550
 
        request = smart.bzrdir.SmartServerRequestOpenBranchV2(backing)
551
 
        self.assertEqual(SuccessfulSmartServerResponse(('branch', expected)),
552
 
            request.execute(''))
 
557
        request = smart_dir.SmartServerRequestOpenBranchV2(backing)
 
558
        self.assertEqual(smart_req.SuccessfulSmartServerResponse(
 
559
                ('branch', expected)),
 
560
                         request.execute(''))
553
561
 
554
562
    def test_branch_reference(self):
555
563
        """When there is a branch reference, the reference URL is returned."""
556
564
        self.vfs_transport_factory = local.LocalURLServer
557
565
        backing = self.get_transport()
558
 
        request = smart.bzrdir.SmartServerRequestOpenBranchV2(backing)
 
566
        request = smart_dir.SmartServerRequestOpenBranchV2(backing)
559
567
        branch = self.make_branch('branch')
560
568
        checkout = branch.create_checkout('reference',lightweight=True)
561
 
        reference_url = BranchReferenceFormat().get_reference(checkout.bzrdir)
 
569
        reference_url = _mod_branch.BranchReferenceFormat().get_reference(
 
570
            checkout.bzrdir)
562
571
        self.assertFileEqual(reference_url, 'reference/.bzr/branch/location')
563
 
        self.assertEqual(SuccessfulSmartServerResponse(('ref', reference_url)),
564
 
            request.execute('reference'))
 
572
        self.assertEqual(smart_req.SuccessfulSmartServerResponse(
 
573
                ('ref', reference_url)),
 
574
                         request.execute('reference'))
565
575
 
566
576
    def test_stacked_branch(self):
567
577
        """Opening a stacked branch does not open the stacked-on branch."""
569
579
        feature = self.make_branch('feature')
570
580
        feature.set_stacked_on_url(trunk.base)
571
581
        opened_branches = []
572
 
        Branch.hooks.install_named_hook('open', opened_branches.append, None)
 
582
        _mod_branch.Branch.hooks.install_named_hook(
 
583
            'open', opened_branches.append, None)
573
584
        backing = self.get_transport()
574
 
        request = smart.bzrdir.SmartServerRequestOpenBranchV2(backing)
 
585
        request = smart_dir.SmartServerRequestOpenBranchV2(backing)
575
586
        request.setup_jail()
576
587
        try:
577
588
            response = request.execute('feature')
578
589
        finally:
579
590
            request.teardown_jail()
580
591
        expected_format = feature._format.network_name()
581
 
        self.assertEqual(
582
 
            SuccessfulSmartServerResponse(('branch', expected_format)),
583
 
            response)
 
592
        self.assertEqual(smart_req.SuccessfulSmartServerResponse(
 
593
                ('branch', expected_format)),
 
594
                         response)
584
595
        self.assertLength(1, opened_branches)
585
596
 
586
597
    def test_notification_on_branch_from_repository(self):
587
598
        """When there is a repository, the error should return details."""
588
599
        backing = self.get_transport()
589
 
        request = smart.bzrdir.SmartServerRequestOpenBranchV2(backing)
 
600
        request = smart_dir.SmartServerRequestOpenBranchV2(backing)
590
601
        repo = self.make_repository('.')
591
 
        self.assertEqual(SmartServerResponse(('nobranch',)),
 
602
        self.assertEqual(smart_req.SmartServerResponse(('nobranch',)),
592
603
            request.execute(''))
593
604
 
594
605
 
598
609
        """When there is no branch, ('nobranch', ) is returned."""
599
610
        backing = self.get_transport()
600
611
        self.make_bzrdir('.')
601
 
        request = smart.bzrdir.SmartServerRequestOpenBranchV3(backing)
602
 
        self.assertEqual(SmartServerResponse(('nobranch',)),
 
612
        request = smart_dir.SmartServerRequestOpenBranchV3(backing)
 
613
        self.assertEqual(smart_req.SmartServerResponse(('nobranch',)),
603
614
            request.execute(''))
604
615
 
605
616
    def test_branch(self):
606
617
        """When there is a branch, 'ok' is returned."""
607
618
        backing = self.get_transport()
608
619
        expected = self.make_branch('.')._format.network_name()
609
 
        request = smart.bzrdir.SmartServerRequestOpenBranchV3(backing)
610
 
        self.assertEqual(SuccessfulSmartServerResponse(('branch', expected)),
611
 
            request.execute(''))
 
620
        request = smart_dir.SmartServerRequestOpenBranchV3(backing)
 
621
        self.assertEqual(smart_req.SuccessfulSmartServerResponse(
 
622
                ('branch', expected)),
 
623
                         request.execute(''))
612
624
 
613
625
    def test_branch_reference(self):
614
626
        """When there is a branch reference, the reference URL is returned."""
615
627
        self.vfs_transport_factory = local.LocalURLServer
616
628
        backing = self.get_transport()
617
 
        request = smart.bzrdir.SmartServerRequestOpenBranchV3(backing)
 
629
        request = smart_dir.SmartServerRequestOpenBranchV3(backing)
618
630
        branch = self.make_branch('branch')
619
631
        checkout = branch.create_checkout('reference',lightweight=True)
620
 
        reference_url = BranchReferenceFormat().get_reference(checkout.bzrdir)
 
632
        reference_url = _mod_branch.BranchReferenceFormat().get_reference(
 
633
            checkout.bzrdir)
621
634
        self.assertFileEqual(reference_url, 'reference/.bzr/branch/location')
622
 
        self.assertEqual(SuccessfulSmartServerResponse(('ref', reference_url)),
623
 
            request.execute('reference'))
 
635
        self.assertEqual(smart_req.SuccessfulSmartServerResponse(
 
636
                ('ref', reference_url)),
 
637
                         request.execute('reference'))
624
638
 
625
639
    def test_stacked_branch(self):
626
640
        """Opening a stacked branch does not open the stacked-on branch."""
628
642
        feature = self.make_branch('feature')
629
643
        feature.set_stacked_on_url(trunk.base)
630
644
        opened_branches = []
631
 
        Branch.hooks.install_named_hook('open', opened_branches.append, None)
 
645
        _mod_branch.Branch.hooks.install_named_hook(
 
646
            'open', opened_branches.append, None)
632
647
        backing = self.get_transport()
633
 
        request = smart.bzrdir.SmartServerRequestOpenBranchV3(backing)
 
648
        request = smart_dir.SmartServerRequestOpenBranchV3(backing)
634
649
        request.setup_jail()
635
650
        try:
636
651
            response = request.execute('feature')
637
652
        finally:
638
653
            request.teardown_jail()
639
654
        expected_format = feature._format.network_name()
640
 
        self.assertEqual(
641
 
            SuccessfulSmartServerResponse(('branch', expected_format)),
642
 
            response)
 
655
        self.assertEqual(smart_req.SuccessfulSmartServerResponse(
 
656
                ('branch', expected_format)),
 
657
                         response)
643
658
        self.assertLength(1, opened_branches)
644
659
 
645
660
    def test_notification_on_branch_from_repository(self):
646
661
        """When there is a repository, the error should return details."""
647
662
        backing = self.get_transport()
648
 
        request = smart.bzrdir.SmartServerRequestOpenBranchV3(backing)
 
663
        request = smart_dir.SmartServerRequestOpenBranchV3(backing)
649
664
        repo = self.make_repository('.')
650
 
        self.assertEqual(
651
 
            SmartServerResponse(('nobranch', 'location is a repository')),
652
 
            request.execute(''))
 
665
        self.assertEqual(smart_req.SmartServerResponse(
 
666
                ('nobranch', 'location is a repository')),
 
667
                         request.execute(''))
653
668
 
654
669
 
655
670
class TestSmartServerRequestRevisionHistory(tests.TestCaseWithMemoryTransport):
657
672
    def test_empty(self):
658
673
        """For an empty branch, the body is empty."""
659
674
        backing = self.get_transport()
660
 
        request = smart.branch.SmartServerRequestRevisionHistory(backing)
 
675
        request = smart_branch.SmartServerRequestRevisionHistory(backing)
661
676
        self.make_branch('.')
662
 
        self.assertEqual(SmartServerResponse(('ok', ), ''),
 
677
        self.assertEqual(smart_req.SmartServerResponse(('ok', ), ''),
663
678
            request.execute(''))
664
679
 
665
680
    def test_not_empty(self):
666
681
        """For a non-empty branch, the body is empty."""
667
682
        backing = self.get_transport()
668
 
        request = smart.branch.SmartServerRequestRevisionHistory(backing)
 
683
        request = smart_branch.SmartServerRequestRevisionHistory(backing)
669
684
        tree = self.make_branch_and_memory_tree('.')
670
685
        tree.lock_write()
671
686
        tree.add('')
673
688
        r2 = tree.commit('2nd commit', rev_id=u'\xc8'.encode('utf-8'))
674
689
        tree.unlock()
675
690
        self.assertEqual(
676
 
            SmartServerResponse(('ok', ), ('\x00'.join([r1, r2]))),
 
691
            smart_req.SmartServerResponse(('ok', ), ('\x00'.join([r1, r2]))),
677
692
            request.execute(''))
678
693
 
679
694
 
682
697
    def test_no_branch(self):
683
698
        """When there is a bzrdir and no branch, NotBranchError is raised."""
684
699
        backing = self.get_transport()
685
 
        request = smart.branch.SmartServerBranchRequest(backing)
 
700
        request = smart_branch.SmartServerBranchRequest(backing)
686
701
        self.make_bzrdir('.')
687
702
        self.assertRaises(errors.NotBranchError,
688
703
            request.execute, '')
690
705
    def test_branch_reference(self):
691
706
        """When there is a branch reference, NotBranchError is raised."""
692
707
        backing = self.get_transport()
693
 
        request = smart.branch.SmartServerBranchRequest(backing)
 
708
        request = smart_branch.SmartServerBranchRequest(backing)
694
709
        branch = self.make_branch('branch')
695
710
        checkout = branch.create_checkout('reference',lightweight=True)
696
711
        self.assertRaises(errors.NotBranchError,
697
712
            request.execute, 'checkout')
698
713
 
699
714
 
700
 
class TestSmartServerBranchRequestLastRevisionInfo(tests.TestCaseWithMemoryTransport):
 
715
class TestSmartServerBranchRequestLastRevisionInfo(
 
716
    tests.TestCaseWithMemoryTransport):
701
717
 
702
718
    def test_empty(self):
703
719
        """For an empty branch, the result is ('ok', '0', 'null:')."""
704
720
        backing = self.get_transport()
705
 
        request = smart.branch.SmartServerBranchRequestLastRevisionInfo(backing)
 
721
        request = smart_branch.SmartServerBranchRequestLastRevisionInfo(backing)
706
722
        self.make_branch('.')
707
 
        self.assertEqual(SmartServerResponse(('ok', '0', 'null:')),
 
723
        self.assertEqual(smart_req.SmartServerResponse(('ok', '0', 'null:')),
708
724
            request.execute(''))
709
725
 
710
726
    def test_not_empty(self):
711
727
        """For a non-empty branch, the result is ('ok', 'revno', 'revid')."""
712
728
        backing = self.get_transport()
713
 
        request = smart.branch.SmartServerBranchRequestLastRevisionInfo(backing)
 
729
        request = smart_branch.SmartServerBranchRequestLastRevisionInfo(backing)
714
730
        tree = self.make_branch_and_memory_tree('.')
715
731
        tree.lock_write()
716
732
        tree.add('')
719
735
        r2 = tree.commit('2nd commit', rev_id=rev_id_utf8)
720
736
        tree.unlock()
721
737
        self.assertEqual(
722
 
            SmartServerResponse(('ok', '2', rev_id_utf8)),
 
738
            smart_req.SmartServerResponse(('ok', '2', rev_id_utf8)),
723
739
            request.execute(''))
724
740
 
725
741
 
726
 
class TestSmartServerBranchRequestGetConfigFile(tests.TestCaseWithMemoryTransport):
 
742
class TestSmartServerBranchRequestGetConfigFile(
 
743
    tests.TestCaseWithMemoryTransport):
727
744
 
728
745
    def test_default(self):
729
746
        """With no file, we get empty content."""
730
747
        backing = self.get_transport()
731
 
        request = smart.branch.SmartServerBranchGetConfigFile(backing)
 
748
        request = smart_branch.SmartServerBranchGetConfigFile(backing)
732
749
        branch = self.make_branch('.')
733
750
        # there should be no file by default
734
751
        content = ''
735
 
        self.assertEqual(SmartServerResponse(('ok', ), content),
 
752
        self.assertEqual(smart_req.SmartServerResponse(('ok', ), content),
736
753
            request.execute(''))
737
754
 
738
755
    def test_with_content(self):
740
757
        # branch.control_files.get('branch.conf') for now - in the future it may
741
758
        # perform more complex processing.
742
759
        backing = self.get_transport()
743
 
        request = smart.branch.SmartServerBranchGetConfigFile(backing)
 
760
        request = smart_branch.SmartServerBranchGetConfigFile(backing)
744
761
        branch = self.make_branch('.')
745
762
        branch._transport.put_bytes('branch.conf', 'foo bar baz')
746
 
        self.assertEqual(SmartServerResponse(('ok', ), 'foo bar baz'),
 
763
        self.assertEqual(smart_req.SmartServerResponse(('ok', ), 'foo bar baz'),
747
764
            request.execute(''))
748
765
 
749
766
 
760
777
 
761
778
    def test_value_name(self):
762
779
        branch = self.make_branch('.')
763
 
        request = smart.branch.SmartServerBranchRequestSetConfigOption(
 
780
        request = smart_branch.SmartServerBranchRequestSetConfigOption(
764
781
            branch.bzrdir.root_transport)
765
782
        branch_token, repo_token = self.get_lock_tokens(branch)
766
783
        config = branch._get_config()
767
784
        result = request.execute('', branch_token, repo_token, 'bar', 'foo',
768
785
            '')
769
 
        self.assertEqual(SuccessfulSmartServerResponse(()), result)
 
786
        self.assertEqual(smart_req.SuccessfulSmartServerResponse(()), result)
770
787
        self.assertEqual('bar', config.get_option('foo'))
771
788
        # Cleanup
772
789
        branch.unlock()
773
790
 
774
791
    def test_value_name_section(self):
775
792
        branch = self.make_branch('.')
776
 
        request = smart.branch.SmartServerBranchRequestSetConfigOption(
 
793
        request = smart_branch.SmartServerBranchRequestSetConfigOption(
777
794
            branch.bzrdir.root_transport)
778
795
        branch_token, repo_token = self.get_lock_tokens(branch)
779
796
        config = branch._get_config()
780
797
        result = request.execute('', branch_token, repo_token, 'bar', 'foo',
781
798
            'gam')
782
 
        self.assertEqual(SuccessfulSmartServerResponse(()), result)
 
799
        self.assertEqual(smart_req.SuccessfulSmartServerResponse(()), result)
783
800
        self.assertEqual('bar', config.get_option('foo', 'gam'))
784
801
        # Cleanup
785
802
        branch.unlock()
794
811
        tag_bytes = base_branch._get_tags_bytes()
795
812
        # get_lock_tokens takes out a lock.
796
813
        branch_token, repo_token = self.get_lock_tokens(base_branch)
797
 
        request = smart.branch.SmartServerBranchSetTagsBytes(
 
814
        request = smart_branch.SmartServerBranchSetTagsBytes(
798
815
            self.get_transport())
799
816
        response = request.execute('base', branch_token, repo_token)
800
817
        self.assertEqual(None, response)
802
819
        self.assertEqual(None, response)
803
820
        response = request.do_end()
804
821
        self.assertEquals(
805
 
            SuccessfulSmartServerResponse(()), response)
 
822
            smart_req.SuccessfulSmartServerResponse(()), response)
806
823
        base_branch.unlock()
807
824
 
808
825
    def test_lock_failed(self):
809
826
        base_branch = self.make_branch('base')
810
827
        base_branch.lock_write()
811
828
        tag_bytes = base_branch._get_tags_bytes()
812
 
        request = smart.branch.SmartServerBranchSetTagsBytes(
 
829
        request = smart_branch.SmartServerBranchSetTagsBytes(
813
830
            self.get_transport())
814
831
        self.assertRaises(errors.TokenMismatch, request.execute,
815
832
            'base', 'wrong token', 'wrong token')
846
863
 
847
864
    def assertRequestSucceeds(self, revision_id, revno):
848
865
        response = self.set_last_revision(revision_id, revno)
849
 
        self.assertEqual(SuccessfulSmartServerResponse(('ok',)), response)
 
866
        self.assertEqual(smart_req.SuccessfulSmartServerResponse(('ok',)),
 
867
                         response)
850
868
 
851
869
 
852
870
class TestSetLastRevisionVerbMixin(object):
860
878
        """If the revision_id is not present, the verb returns NoSuchRevision.
861
879
        """
862
880
        revision_id = 'non-existent revision'
863
 
        self.assertEqual(
864
 
            FailedSmartServerResponse(('NoSuchRevision', revision_id)),
865
 
            self.set_last_revision(revision_id, 1))
 
881
        self.assertEqual(smart_req.FailedSmartServerResponse(('NoSuchRevision',
 
882
                                                              revision_id)),
 
883
                         self.set_last_revision(revision_id, 1))
866
884
 
867
885
    def make_tree_with_two_commits(self):
868
886
        self.tree.lock_write()
908
926
        rejection_message = u'rejection message\N{INTERROBANG}'
909
927
        def hook_that_rejects(params):
910
928
            raise errors.TipChangeRejected(rejection_message)
911
 
        Branch.hooks.install_named_hook(
 
929
        _mod_branch.Branch.hooks.install_named_hook(
912
930
            'pre_change_branch_tip', hook_that_rejects, None)
913
931
        self.assertEqual(
914
 
            FailedSmartServerResponse(
 
932
            smart_req.FailedSmartServerResponse(
915
933
                ('TipChangeRejected', rejection_message.encode('utf-8'))),
916
934
            self.set_last_revision('null:', 0))
917
935
 
920
938
        SetLastRevisionTestBase, TestSetLastRevisionVerbMixin):
921
939
    """Tests for Branch.set_last_revision verb."""
922
940
 
923
 
    request_class = smart.branch.SmartServerBranchRequestSetLastRevision
 
941
    request_class = smart_branch.SmartServerBranchRequestSetLastRevision
924
942
 
925
943
    def _set_last_revision(self, revision_id, revno, branch_token, repo_token):
926
944
        return self.request.execute(
931
949
        SetLastRevisionTestBase, TestSetLastRevisionVerbMixin):
932
950
    """Tests for Branch.set_last_revision_info verb."""
933
951
 
934
 
    request_class = smart.branch.SmartServerBranchRequestSetLastRevisionInfo
 
952
    request_class = smart_branch.SmartServerBranchRequestSetLastRevisionInfo
935
953
 
936
954
    def _set_last_revision(self, revision_id, revno, branch_token, repo_token):
937
955
        return self.request.execute(
948
966
        SetLastRevisionTestBase, TestSetLastRevisionVerbMixin):
949
967
    """Tests for Branch.set_last_revision_ex verb."""
950
968
 
951
 
    request_class = smart.branch.SmartServerBranchRequestSetLastRevisionEx
 
969
    request_class = smart_branch.SmartServerBranchRequestSetLastRevisionEx
952
970
 
953
971
    def _set_last_revision(self, revision_id, revno, branch_token, repo_token):
954
972
        return self.request.execute(
957
975
    def assertRequestSucceeds(self, revision_id, revno):
958
976
        response = self.set_last_revision(revision_id, revno)
959
977
        self.assertEqual(
960
 
            SuccessfulSmartServerResponse(('ok', revno, revision_id)),
 
978
            smart_req.SuccessfulSmartServerResponse(('ok', revno, revision_id)),
961
979
            response)
962
980
 
963
981
    def test_branch_last_revision_info_rewind(self):
974
992
        response = self.request.execute(
975
993
            '', branch_token, repo_token, rev_id_utf8, 0, 0)
976
994
        self.assertEqual(
977
 
            SuccessfulSmartServerResponse(('ok', 2, 'rev-2')),
 
995
            smart_req.SuccessfulSmartServerResponse(('ok', 2, 'rev-2')),
978
996
            response)
979
997
        self.assertEqual(
980
998
            (2, 'rev-2'), self.tree.branch.last_revision_info())
984
1002
        response = self.request.execute(
985
1003
            '', branch_token, repo_token, rev_id_utf8, 0, 1)
986
1004
        self.assertEqual(
987
 
            SuccessfulSmartServerResponse(('ok', 1, rev_id_utf8)),
 
1005
            smart_req.SuccessfulSmartServerResponse(('ok', 1, rev_id_utf8)),
988
1006
            response)
989
1007
        self.unlock_branch()
990
1008
        self.assertEqual(
1015
1033
        """
1016
1034
        self.make_branch_with_divergent_history()
1017
1035
        self.assertEqual(
1018
 
            FailedSmartServerResponse(('Diverged',)),
 
1036
            smart_req.FailedSmartServerResponse(('Diverged',)),
1019
1037
            self.set_last_revision('child-1', 2))
1020
1038
        # The branch tip was not changed.
1021
1039
        self.assertEqual('child-2', self.tree.branch.last_revision())
1029
1047
        response = self.request.execute(
1030
1048
            '', branch_token, repo_token, 'child-1', 1, 0)
1031
1049
        self.assertEqual(
1032
 
            SuccessfulSmartServerResponse(('ok', 2, 'child-1')),
 
1050
            smart_req.SuccessfulSmartServerResponse(('ok', 2, 'child-1')),
1033
1051
            response)
1034
1052
        self.unlock_branch()
1035
1053
        # The branch tip was changed.
1040
1058
 
1041
1059
    def test_get_parent_none(self):
1042
1060
        base_branch = self.make_branch('base')
1043
 
        request = smart.branch.SmartServerBranchGetParent(self.get_transport())
 
1061
        request = smart_branch.SmartServerBranchGetParent(self.get_transport())
1044
1062
        response = request.execute('base')
1045
1063
        self.assertEquals(
1046
 
            SuccessfulSmartServerResponse(('',)), response)
 
1064
            smart_req.SuccessfulSmartServerResponse(('',)), response)
1047
1065
 
1048
1066
    def test_get_parent_something(self):
1049
1067
        base_branch = self.make_branch('base')
1050
1068
        base_branch.set_parent(self.get_url('foo'))
1051
 
        request = smart.branch.SmartServerBranchGetParent(self.get_transport())
 
1069
        request = smart_branch.SmartServerBranchGetParent(self.get_transport())
1052
1070
        response = request.execute('base')
1053
1071
        self.assertEquals(
1054
 
            SuccessfulSmartServerResponse(("../foo",)),
 
1072
            smart_req.SuccessfulSmartServerResponse(("../foo",)),
1055
1073
            response)
1056
1074
 
1057
1075
 
1062
1080
        branch.lock_write()
1063
1081
        branch._set_parent_location('foo')
1064
1082
        branch.unlock()
1065
 
        request = smart.branch.SmartServerBranchRequestSetParentLocation(
 
1083
        request = smart_branch.SmartServerBranchRequestSetParentLocation(
1066
1084
            self.get_transport())
1067
1085
        branch_token = branch.lock_write()
1068
1086
        repo_token = branch.repository.lock_write()
1071
1089
        finally:
1072
1090
            branch.repository.unlock()
1073
1091
            branch.unlock()
1074
 
        self.assertEqual(SuccessfulSmartServerResponse(()), response)
 
1092
        self.assertEqual(smart_req.SuccessfulSmartServerResponse(()), response)
1075
1093
        self.assertEqual(None, branch.get_parent())
1076
1094
 
1077
1095
    def test_set_parent_something(self):
1078
1096
        branch = self.make_branch('base', format="1.9")
1079
 
        request = smart.branch.SmartServerBranchRequestSetParentLocation(
 
1097
        request = smart_branch.SmartServerBranchRequestSetParentLocation(
1080
1098
            self.get_transport())
1081
1099
        branch_token = branch.lock_write()
1082
1100
        repo_token = branch.repository.lock_write()
1086
1104
        finally:
1087
1105
            branch.repository.unlock()
1088
1106
            branch.unlock()
1089
 
        self.assertEqual(SuccessfulSmartServerResponse(()), response)
 
1107
        self.assertEqual(smart_req.SuccessfulSmartServerResponse(()), response)
1090
1108
        self.assertEqual('http://bar/', branch.get_parent())
1091
1109
 
1092
1110
 
1093
 
class TestSmartServerBranchRequestGetTagsBytes(tests.TestCaseWithMemoryTransport):
 
1111
class TestSmartServerBranchRequestGetTagsBytes(
 
1112
    tests.TestCaseWithMemoryTransport):
1094
1113
    # Only called when the branch format and tags match [yay factory
1095
1114
    # methods] so only need to test straight forward cases.
1096
1115
 
1097
1116
    def test_get_bytes(self):
1098
1117
        base_branch = self.make_branch('base')
1099
 
        request = smart.branch.SmartServerBranchGetTagsBytes(
 
1118
        request = smart_branch.SmartServerBranchGetTagsBytes(
1100
1119
            self.get_transport())
1101
1120
        response = request.execute('base')
1102
1121
        self.assertEquals(
1103
 
            SuccessfulSmartServerResponse(('',)), response)
 
1122
            smart_req.SuccessfulSmartServerResponse(('',)), response)
1104
1123
 
1105
1124
 
1106
1125
class TestSmartServerBranchRequestGetStackedOnURL(tests.TestCaseWithMemoryTransport):
1110
1129
        stacked_branch = self.make_branch('stacked', format='1.6')
1111
1130
        # typically should be relative
1112
1131
        stacked_branch.set_stacked_on_url('../base')
1113
 
        request = smart.branch.SmartServerBranchRequestGetStackedOnURL(
 
1132
        request = smart_branch.SmartServerBranchRequestGetStackedOnURL(
1114
1133
            self.get_transport())
1115
1134
        response = request.execute('stacked')
1116
1135
        self.assertEquals(
1117
 
            SmartServerResponse(('ok', '../base')),
 
1136
            smart_req.SmartServerResponse(('ok', '../base')),
1118
1137
            response)
1119
1138
 
1120
1139
 
1125
1144
 
1126
1145
    def test_lock_write_on_unlocked_branch(self):
1127
1146
        backing = self.get_transport()
1128
 
        request = smart.branch.SmartServerBranchRequestLockWrite(backing)
 
1147
        request = smart_branch.SmartServerBranchRequestLockWrite(backing)
1129
1148
        branch = self.make_branch('.', format='knit')
1130
1149
        repository = branch.repository
1131
1150
        response = request.execute('')
1132
1151
        branch_nonce = branch.control_files._lock.peek().get('nonce')
1133
1152
        repository_nonce = repository.control_files._lock.peek().get('nonce')
1134
 
        self.assertEqual(
1135
 
            SmartServerResponse(('ok', branch_nonce, repository_nonce)),
1136
 
            response)
 
1153
        self.assertEqual(smart_req.SmartServerResponse(
 
1154
                ('ok', branch_nonce, repository_nonce)),
 
1155
                         response)
1137
1156
        # The branch (and associated repository) is now locked.  Verify that
1138
1157
        # with a new branch object.
1139
1158
        new_branch = repository.bzrdir.open_branch()
1140
1159
        self.assertRaises(errors.LockContention, new_branch.lock_write)
1141
1160
        # Cleanup
1142
 
        request = smart.branch.SmartServerBranchRequestUnlock(backing)
 
1161
        request = smart_branch.SmartServerBranchRequestUnlock(backing)
1143
1162
        response = request.execute('', branch_nonce, repository_nonce)
1144
1163
 
1145
1164
    def test_lock_write_on_locked_branch(self):
1146
1165
        backing = self.get_transport()
1147
 
        request = smart.branch.SmartServerBranchRequestLockWrite(backing)
 
1166
        request = smart_branch.SmartServerBranchRequestLockWrite(backing)
1148
1167
        branch = self.make_branch('.')
1149
1168
        branch_token = branch.lock_write()
1150
1169
        branch.leave_lock_in_place()
1151
1170
        branch.unlock()
1152
1171
        response = request.execute('')
1153
1172
        self.assertEqual(
1154
 
            SmartServerResponse(('LockContention',)), response)
 
1173
            smart_req.SmartServerResponse(('LockContention',)), response)
1155
1174
        # Cleanup
1156
1175
        branch.lock_write(branch_token)
1157
1176
        branch.dont_leave_lock_in_place()
1159
1178
 
1160
1179
    def test_lock_write_with_tokens_on_locked_branch(self):
1161
1180
        backing = self.get_transport()
1162
 
        request = smart.branch.SmartServerBranchRequestLockWrite(backing)
 
1181
        request = smart_branch.SmartServerBranchRequestLockWrite(backing)
1163
1182
        branch = self.make_branch('.', format='knit')
1164
1183
        branch_token = branch.lock_write()
1165
1184
        repo_token = branch.repository.lock_write()
1170
1189
        response = request.execute('',
1171
1190
                                   branch_token, repo_token)
1172
1191
        self.assertEqual(
1173
 
            SmartServerResponse(('ok', branch_token, repo_token)), response)
 
1192
            smart_req.SmartServerResponse(('ok', branch_token, repo_token)),
 
1193
            response)
1174
1194
        # Cleanup
1175
1195
        branch.repository.lock_write(repo_token)
1176
1196
        branch.repository.dont_leave_lock_in_place()
1181
1201
 
1182
1202
    def test_lock_write_with_mismatched_tokens_on_locked_branch(self):
1183
1203
        backing = self.get_transport()
1184
 
        request = smart.branch.SmartServerBranchRequestLockWrite(backing)
 
1204
        request = smart_branch.SmartServerBranchRequestLockWrite(backing)
1185
1205
        branch = self.make_branch('.', format='knit')
1186
1206
        branch_token = branch.lock_write()
1187
1207
        repo_token = branch.repository.lock_write()
1192
1212
        response = request.execute('',
1193
1213
                                   branch_token+'xxx', repo_token)
1194
1214
        self.assertEqual(
1195
 
            SmartServerResponse(('TokenMismatch',)), response)
 
1215
            smart_req.SmartServerResponse(('TokenMismatch',)), response)
1196
1216
        # Cleanup
1197
1217
        branch.repository.lock_write(repo_token)
1198
1218
        branch.repository.dont_leave_lock_in_place()
1203
1223
 
1204
1224
    def test_lock_write_on_locked_repo(self):
1205
1225
        backing = self.get_transport()
1206
 
        request = smart.branch.SmartServerBranchRequestLockWrite(backing)
 
1226
        request = smart_branch.SmartServerBranchRequestLockWrite(backing)
1207
1227
        branch = self.make_branch('.', format='knit')
1208
1228
        repo = branch.repository
1209
1229
        repo_token = repo.lock_write()
1211
1231
        repo.unlock()
1212
1232
        response = request.execute('')
1213
1233
        self.assertEqual(
1214
 
            SmartServerResponse(('LockContention',)), response)
 
1234
            smart_req.SmartServerResponse(('LockContention',)), response)
1215
1235
        # Cleanup
1216
1236
        repo.lock_write(repo_token)
1217
1237
        repo.dont_leave_lock_in_place()
1219
1239
 
1220
1240
    def test_lock_write_on_readonly_transport(self):
1221
1241
        backing = self.get_readonly_transport()
1222
 
        request = smart.branch.SmartServerBranchRequestLockWrite(backing)
 
1242
        request = smart_branch.SmartServerBranchRequestLockWrite(backing)
1223
1243
        branch = self.make_branch('.')
1224
1244
        root = self.get_transport().clone('/')
1225
1245
        path = urlutils.relative_url(root.base, self.get_transport().base)
1236
1256
 
1237
1257
    def test_unlock_on_locked_branch_and_repo(self):
1238
1258
        backing = self.get_transport()
1239
 
        request = smart.branch.SmartServerBranchRequestUnlock(backing)
 
1259
        request = smart_branch.SmartServerBranchRequestUnlock(backing)
1240
1260
        branch = self.make_branch('.', format='knit')
1241
1261
        # Lock the branch
1242
1262
        branch_token = branch.lock_write()
1250
1270
        response = request.execute('',
1251
1271
                                   branch_token, repo_token)
1252
1272
        self.assertEqual(
1253
 
            SmartServerResponse(('ok',)), response)
 
1273
            smart_req.SmartServerResponse(('ok',)), response)
1254
1274
        # The branch is now unlocked.  Verify that with a new branch
1255
1275
        # object.
1256
1276
        new_branch = branch.bzrdir.open_branch()
1259
1279
 
1260
1280
    def test_unlock_on_unlocked_branch_unlocked_repo(self):
1261
1281
        backing = self.get_transport()
1262
 
        request = smart.branch.SmartServerBranchRequestUnlock(backing)
 
1282
        request = smart_branch.SmartServerBranchRequestUnlock(backing)
1263
1283
        branch = self.make_branch('.', format='knit')
1264
1284
        response = request.execute(
1265
1285
            '', 'branch token', 'repo token')
1266
1286
        self.assertEqual(
1267
 
            SmartServerResponse(('TokenMismatch',)), response)
 
1287
            smart_req.SmartServerResponse(('TokenMismatch',)), response)
1268
1288
 
1269
1289
    def test_unlock_on_unlocked_branch_locked_repo(self):
1270
1290
        backing = self.get_transport()
1271
 
        request = smart.branch.SmartServerBranchRequestUnlock(backing)
 
1291
        request = smart_branch.SmartServerBranchRequestUnlock(backing)
1272
1292
        branch = self.make_branch('.', format='knit')
1273
1293
        # Lock the repository.
1274
1294
        repo_token = branch.repository.lock_write()
1279
1299
        response = request.execute(
1280
1300
            '', 'branch token', repo_token)
1281
1301
        self.assertEqual(
1282
 
            SmartServerResponse(('TokenMismatch',)), response)
 
1302
            smart_req.SmartServerResponse(('TokenMismatch',)), response)
1283
1303
        # Cleanup
1284
1304
        branch.repository.lock_write(repo_token)
1285
1305
        branch.repository.dont_leave_lock_in_place()
1295
1315
        # its the exact path being looked at and the server is not
1296
1316
        # searching.
1297
1317
        backing = self.get_transport()
1298
 
        request = smart.repository.SmartServerRepositoryRequest(backing)
 
1318
        request = smart_repo.SmartServerRepositoryRequest(backing)
1299
1319
        self.make_repository('.', shared=True)
1300
1320
        self.make_bzrdir('subdir')
1301
1321
        self.assertRaises(errors.NoRepositoryPresent,
1307
1327
    def test_trivial_bzipped(self):
1308
1328
        # This tests that the wire encoding is actually bzipped
1309
1329
        backing = self.get_transport()
1310
 
        request = smart.repository.SmartServerRepositoryGetParentMap(backing)
 
1330
        request = smart_repo.SmartServerRepositoryGetParentMap(backing)
1311
1331
        tree = self.make_branch_and_memory_tree('.')
1312
1332
 
1313
1333
        self.assertEqual(None,
1314
1334
            request.execute('', 'missing-id'))
1315
1335
        # Note that it returns a body that is bzipped.
1316
1336
        self.assertEqual(
1317
 
            SuccessfulSmartServerResponse(('ok', ), bz2.compress('')),
 
1337
            smart_req.SuccessfulSmartServerResponse(('ok', ), bz2.compress('')),
1318
1338
            request.do_body('\n\n0\n'))
1319
1339
 
1320
1340
    def test_trivial_include_missing(self):
1321
1341
        backing = self.get_transport()
1322
 
        request = smart.repository.SmartServerRepositoryGetParentMap(backing)
 
1342
        request = smart_repo.SmartServerRepositoryGetParentMap(backing)
1323
1343
        tree = self.make_branch_and_memory_tree('.')
1324
1344
 
1325
1345
        self.assertEqual(None,
1326
1346
            request.execute('', 'missing-id', 'include-missing:'))
1327
1347
        self.assertEqual(
1328
 
            SuccessfulSmartServerResponse(('ok', ),
 
1348
            smart_req.SuccessfulSmartServerResponse(('ok', ),
1329
1349
                bz2.compress('missing:missing-id')),
1330
1350
            request.do_body('\n\n0\n'))
1331
1351
 
1332
1352
 
1333
 
class TestSmartServerRepositoryGetRevisionGraph(tests.TestCaseWithMemoryTransport):
 
1353
class TestSmartServerRepositoryGetRevisionGraph(
 
1354
    tests.TestCaseWithMemoryTransport):
1334
1355
 
1335
1356
    def test_none_argument(self):
1336
1357
        backing = self.get_transport()
1337
 
        request = smart.repository.SmartServerRepositoryGetRevisionGraph(backing)
 
1358
        request = smart_repo.SmartServerRepositoryGetRevisionGraph(backing)
1338
1359
        tree = self.make_branch_and_memory_tree('.')
1339
1360
        tree.lock_write()
1340
1361
        tree.add('')
1349
1370
        response.body = '\n'.join(sorted(response.body.split('\n')))
1350
1371
 
1351
1372
        self.assertEqual(
1352
 
            SmartServerResponse(('ok', ), '\n'.join(lines)), response)
 
1373
            smart_req.SmartServerResponse(('ok', ), '\n'.join(lines)), response)
1353
1374
 
1354
1375
    def test_specific_revision_argument(self):
1355
1376
        backing = self.get_transport()
1356
 
        request = smart.repository.SmartServerRepositoryGetRevisionGraph(backing)
 
1377
        request = smart_repo.SmartServerRepositoryGetRevisionGraph(backing)
1357
1378
        tree = self.make_branch_and_memory_tree('.')
1358
1379
        tree.lock_write()
1359
1380
        tree.add('')
1362
1383
        r2 = tree.commit('2nd commit', rev_id=u'\xc8'.encode('utf-8'))
1363
1384
        tree.unlock()
1364
1385
 
1365
 
        self.assertEqual(SmartServerResponse(('ok', ), rev_id_utf8),
 
1386
        self.assertEqual(smart_req.SmartServerResponse(('ok', ), rev_id_utf8),
1366
1387
            request.execute('', rev_id_utf8))
1367
1388
 
1368
1389
    def test_no_such_revision(self):
1369
1390
        backing = self.get_transport()
1370
 
        request = smart.repository.SmartServerRepositoryGetRevisionGraph(backing)
 
1391
        request = smart_repo.SmartServerRepositoryGetRevisionGraph(backing)
1371
1392
        tree = self.make_branch_and_memory_tree('.')
1372
1393
        tree.lock_write()
1373
1394
        tree.add('')
1375
1396
        tree.unlock()
1376
1397
 
1377
1398
        # Note that it still returns body (of zero bytes).
1378
 
        self.assertEqual(
1379
 
            SmartServerResponse(('nosuchrevision', 'missingrevision', ), ''),
1380
 
            request.execute('', 'missingrevision'))
1381
 
 
1382
 
 
1383
 
class TestSmartServerRepositoryGetRevIdForRevno(tests.TestCaseWithMemoryTransport):
 
1399
        self.assertEqual(smart_req.SmartServerResponse(
 
1400
                ('nosuchrevision', 'missingrevision', ), ''),
 
1401
                         request.execute('', 'missingrevision'))
 
1402
 
 
1403
 
 
1404
class TestSmartServerRepositoryGetRevIdForRevno(
 
1405
    tests.TestCaseWithMemoryTransport):
1384
1406
 
1385
1407
    def test_revno_found(self):
1386
1408
        backing = self.get_transport()
1387
 
        request = smart.repository.SmartServerRepositoryGetRevIdForRevno(backing)
 
1409
        request = smart_repo.SmartServerRepositoryGetRevIdForRevno(backing)
1388
1410
        tree = self.make_branch_and_memory_tree('.')
1389
1411
        tree.lock_write()
1390
1412
        tree.add('')
1394
1416
        tree.commit('2nd commit', rev_id=rev2_id_utf8)
1395
1417
        tree.unlock()
1396
1418
 
1397
 
        self.assertEqual(SmartServerResponse(('ok', rev1_id_utf8)),
 
1419
        self.assertEqual(smart_req.SmartServerResponse(('ok', rev1_id_utf8)),
1398
1420
            request.execute('', 1, (2, rev2_id_utf8)))
1399
1421
 
1400
1422
    def test_known_revid_missing(self):
1401
1423
        backing = self.get_transport()
1402
 
        request = smart.repository.SmartServerRepositoryGetRevIdForRevno(backing)
 
1424
        request = smart_repo.SmartServerRepositoryGetRevIdForRevno(backing)
1403
1425
        repo = self.make_repository('.')
1404
1426
        self.assertEqual(
1405
 
            FailedSmartServerResponse(('nosuchrevision', 'ghost')),
 
1427
            smart_req.FailedSmartServerResponse(('nosuchrevision', 'ghost')),
1406
1428
            request.execute('', 1, (2, 'ghost')))
1407
1429
 
1408
1430
    def test_history_incomplete(self):
1409
1431
        backing = self.get_transport()
1410
 
        request = smart.repository.SmartServerRepositoryGetRevIdForRevno(backing)
 
1432
        request = smart_repo.SmartServerRepositoryGetRevIdForRevno(backing)
1411
1433
        parent = self.make_branch_and_memory_tree('parent', format='1.9')
1412
1434
        parent.lock_write()
1413
1435
        parent.add([''], ['TREE_ROOT'])
1421
1443
        local.branch.create_clone_on_transport(
1422
1444
            self.get_transport('stacked'), stacked_on=self.get_url('parent'))
1423
1445
        self.assertEqual(
1424
 
            SmartServerResponse(('history-incomplete', 2, r2)),
 
1446
            smart_req.SmartServerResponse(('history-incomplete', 2, r2)),
1425
1447
            request.execute('stacked', 1, (3, r3)))
1426
1448
 
1427
1449
 
1440
1462
    def test_ancestry_of(self):
1441
1463
        """The search argument may be a 'ancestry-of' some heads'."""
1442
1464
        backing = self.get_transport()
1443
 
        request = smart.repository.SmartServerRepositoryGetStream(backing)
 
1465
        request = smart_repo.SmartServerRepositoryGetStream(backing)
1444
1466
        repo, r1, r2 = self.make_two_commit_repo()
1445
1467
        fetch_spec = ['ancestry-of', r2]
1446
1468
        lines = '\n'.join(fetch_spec)
1453
1475
    def test_search(self):
1454
1476
        """The search argument may be a 'search' of some explicit keys."""
1455
1477
        backing = self.get_transport()
1456
 
        request = smart.repository.SmartServerRepositoryGetStream(backing)
 
1478
        request = smart_repo.SmartServerRepositoryGetStream(backing)
1457
1479
        repo, r1, r2 = self.make_two_commit_repo()
1458
1480
        fetch_spec = ['search', '%s %s' % (r1, r2), 'null:', '2']
1459
1481
        lines = '\n'.join(fetch_spec)
1469
1491
    def test_missing_revision(self):
1470
1492
        """For a missing revision, ('no', ) is returned."""
1471
1493
        backing = self.get_transport()
1472
 
        request = smart.repository.SmartServerRequestHasRevision(backing)
 
1494
        request = smart_repo.SmartServerRequestHasRevision(backing)
1473
1495
        self.make_repository('.')
1474
 
        self.assertEqual(SmartServerResponse(('no', )),
 
1496
        self.assertEqual(smart_req.SmartServerResponse(('no', )),
1475
1497
            request.execute('', 'revid'))
1476
1498
 
1477
1499
    def test_present_revision(self):
1478
1500
        """For a present revision, ('yes', ) is returned."""
1479
1501
        backing = self.get_transport()
1480
 
        request = smart.repository.SmartServerRequestHasRevision(backing)
 
1502
        request = smart_repo.SmartServerRequestHasRevision(backing)
1481
1503
        tree = self.make_branch_and_memory_tree('.')
1482
1504
        tree.lock_write()
1483
1505
        tree.add('')
1485
1507
        r1 = tree.commit('a commit', rev_id=rev_id_utf8)
1486
1508
        tree.unlock()
1487
1509
        self.assertTrue(tree.branch.repository.has_revision(rev_id_utf8))
1488
 
        self.assertEqual(SmartServerResponse(('yes', )),
 
1510
        self.assertEqual(smart_req.SmartServerResponse(('yes', )),
1489
1511
            request.execute('', rev_id_utf8))
1490
1512
 
1491
1513
 
1494
1516
    def test_empty_revid(self):
1495
1517
        """With an empty revid, we get only size an number and revisions"""
1496
1518
        backing = self.get_transport()
1497
 
        request = smart.repository.SmartServerRepositoryGatherStats(backing)
 
1519
        request = smart_repo.SmartServerRepositoryGatherStats(backing)
1498
1520
        repository = self.make_repository('.')
1499
1521
        stats = repository.gather_stats()
1500
1522
        expected_body = 'revisions: 0\n'
1501
 
        self.assertEqual(SmartServerResponse(('ok', ), expected_body),
 
1523
        self.assertEqual(smart_req.SmartServerResponse(('ok', ), expected_body),
1502
1524
                         request.execute('', '', 'no'))
1503
1525
 
1504
1526
    def test_revid_with_committers(self):
1505
1527
        """For a revid we get more infos."""
1506
1528
        backing = self.get_transport()
1507
1529
        rev_id_utf8 = u'\xc8abc'.encode('utf-8')
1508
 
        request = smart.repository.SmartServerRepositoryGatherStats(backing)
 
1530
        request = smart_repo.SmartServerRepositoryGatherStats(backing)
1509
1531
        tree = self.make_branch_and_memory_tree('.')
1510
1532
        tree.lock_write()
1511
1533
        tree.add('')
1519
1541
        expected_body = ('firstrev: 123456.200 3600\n'
1520
1542
                         'latestrev: 654321.400 0\n'
1521
1543
                         'revisions: 2\n')
1522
 
        self.assertEqual(SmartServerResponse(('ok', ), expected_body),
 
1544
        self.assertEqual(smart_req.SmartServerResponse(('ok', ), expected_body),
1523
1545
                         request.execute('',
1524
1546
                                         rev_id_utf8, 'no'))
1525
1547
 
1527
1549
        """For a revid and requesting committers we get the whole thing."""
1528
1550
        backing = self.get_transport()
1529
1551
        rev_id_utf8 = u'\xc8abc'.encode('utf-8')
1530
 
        request = smart.repository.SmartServerRepositoryGatherStats(backing)
 
1552
        request = smart_repo.SmartServerRepositoryGatherStats(backing)
1531
1553
        tree = self.make_branch_and_memory_tree('.')
1532
1554
        tree.lock_write()
1533
1555
        tree.add('')
1543
1565
                         'firstrev: 123456.200 3600\n'
1544
1566
                         'latestrev: 654321.400 0\n'
1545
1567
                         'revisions: 2\n')
1546
 
        self.assertEqual(SmartServerResponse(('ok', ), expected_body),
 
1568
        self.assertEqual(smart_req.SmartServerResponse(('ok', ), expected_body),
1547
1569
                         request.execute('',
1548
1570
                                         rev_id_utf8, 'yes'))
1549
1571
 
1553
1575
    def test_is_shared(self):
1554
1576
        """For a shared repository, ('yes', ) is returned."""
1555
1577
        backing = self.get_transport()
1556
 
        request = smart.repository.SmartServerRepositoryIsShared(backing)
 
1578
        request = smart_repo.SmartServerRepositoryIsShared(backing)
1557
1579
        self.make_repository('.', shared=True)
1558
 
        self.assertEqual(SmartServerResponse(('yes', )),
 
1580
        self.assertEqual(smart_req.SmartServerResponse(('yes', )),
1559
1581
            request.execute('', ))
1560
1582
 
1561
1583
    def test_is_not_shared(self):
1562
1584
        """For a shared repository, ('no', ) is returned."""
1563
1585
        backing = self.get_transport()
1564
 
        request = smart.repository.SmartServerRepositoryIsShared(backing)
 
1586
        request = smart_repo.SmartServerRepositoryIsShared(backing)
1565
1587
        self.make_repository('.', shared=False)
1566
 
        self.assertEqual(SmartServerResponse(('no', )),
 
1588
        self.assertEqual(smart_req.SmartServerResponse(('no', )),
1567
1589
            request.execute('', ))
1568
1590
 
1569
1591
 
1571
1593
 
1572
1594
    def test_lock_write_on_unlocked_repo(self):
1573
1595
        backing = self.get_transport()
1574
 
        request = smart.repository.SmartServerRepositoryLockWrite(backing)
 
1596
        request = smart_repo.SmartServerRepositoryLockWrite(backing)
1575
1597
        repository = self.make_repository('.', format='knit')
1576
1598
        response = request.execute('')
1577
1599
        nonce = repository.control_files._lock.peek().get('nonce')
1578
 
        self.assertEqual(SmartServerResponse(('ok', nonce)), response)
 
1600
        self.assertEqual(smart_req.SmartServerResponse(('ok', nonce)), response)
1579
1601
        # The repository is now locked.  Verify that with a new repository
1580
1602
        # object.
1581
1603
        new_repo = repository.bzrdir.open_repository()
1582
1604
        self.assertRaises(errors.LockContention, new_repo.lock_write)
1583
1605
        # Cleanup
1584
 
        request = smart.repository.SmartServerRepositoryUnlock(backing)
 
1606
        request = smart_repo.SmartServerRepositoryUnlock(backing)
1585
1607
        response = request.execute('', nonce)
1586
1608
 
1587
1609
    def test_lock_write_on_locked_repo(self):
1588
1610
        backing = self.get_transport()
1589
 
        request = smart.repository.SmartServerRepositoryLockWrite(backing)
 
1611
        request = smart_repo.SmartServerRepositoryLockWrite(backing)
1590
1612
        repository = self.make_repository('.', format='knit')
1591
1613
        repo_token = repository.lock_write()
1592
1614
        repository.leave_lock_in_place()
1593
1615
        repository.unlock()
1594
1616
        response = request.execute('')
1595
1617
        self.assertEqual(
1596
 
            SmartServerResponse(('LockContention',)), response)
 
1618
            smart_req.SmartServerResponse(('LockContention',)), response)
1597
1619
        # Cleanup
1598
1620
        repository.lock_write(repo_token)
1599
1621
        repository.dont_leave_lock_in_place()
1601
1623
 
1602
1624
    def test_lock_write_on_readonly_transport(self):
1603
1625
        backing = self.get_readonly_transport()
1604
 
        request = smart.repository.SmartServerRepositoryLockWrite(backing)
 
1626
        request = smart_repo.SmartServerRepositoryLockWrite(backing)
1605
1627
        repository = self.make_repository('.', format='knit')
1606
1628
        response = request.execute('')
1607
1629
        self.assertFalse(response.is_successful())
1611
1633
class TestInsertStreamBase(tests.TestCaseWithMemoryTransport):
1612
1634
 
1613
1635
    def make_empty_byte_stream(self, repo):
1614
 
        byte_stream = smart.repository._stream_to_byte_stream([], repo._format)
 
1636
        byte_stream = smart_repo._stream_to_byte_stream([], repo._format)
1615
1637
        return ''.join(byte_stream)
1616
1638
 
1617
1639
 
1619
1641
 
1620
1642
    def test_insert_stream_empty(self):
1621
1643
        backing = self.get_transport()
1622
 
        request = smart.repository.SmartServerRepositoryInsertStream(backing)
 
1644
        request = smart_repo.SmartServerRepositoryInsertStream(backing)
1623
1645
        repository = self.make_repository('.')
1624
1646
        response = request.execute('', '')
1625
1647
        self.assertEqual(None, response)
1626
1648
        response = request.do_chunk(self.make_empty_byte_stream(repository))
1627
1649
        self.assertEqual(None, response)
1628
1650
        response = request.do_end()
1629
 
        self.assertEqual(SmartServerResponse(('ok', )), response)
1630
 
        
 
1651
        self.assertEqual(smart_req.SmartServerResponse(('ok', )), response)
 
1652
 
1631
1653
 
1632
1654
class TestSmartServerRepositoryInsertStreamLocked(TestInsertStreamBase):
1633
1655
 
1634
1656
    def test_insert_stream_empty(self):
1635
1657
        backing = self.get_transport()
1636
 
        request = smart.repository.SmartServerRepositoryInsertStreamLocked(
 
1658
        request = smart_repo.SmartServerRepositoryInsertStreamLocked(
1637
1659
            backing)
1638
1660
        repository = self.make_repository('.', format='knit')
1639
1661
        lock_token = repository.lock_write()
1642
1664
        response = request.do_chunk(self.make_empty_byte_stream(repository))
1643
1665
        self.assertEqual(None, response)
1644
1666
        response = request.do_end()
1645
 
        self.assertEqual(SmartServerResponse(('ok', )), response)
 
1667
        self.assertEqual(smart_req.SmartServerResponse(('ok', )), response)
1646
1668
        repository.unlock()
1647
1669
 
1648
1670
    def test_insert_stream_with_wrong_lock_token(self):
1649
1671
        backing = self.get_transport()
1650
 
        request = smart.repository.SmartServerRepositoryInsertStreamLocked(
 
1672
        request = smart_repo.SmartServerRepositoryInsertStreamLocked(
1651
1673
            backing)
1652
1674
        repository = self.make_repository('.', format='knit')
1653
1675
        lock_token = repository.lock_write()
1663
1685
 
1664
1686
    def test_unlock_on_locked_repo(self):
1665
1687
        backing = self.get_transport()
1666
 
        request = smart.repository.SmartServerRepositoryUnlock(backing)
 
1688
        request = smart_repo.SmartServerRepositoryUnlock(backing)
1667
1689
        repository = self.make_repository('.', format='knit')
1668
1690
        token = repository.lock_write()
1669
1691
        repository.leave_lock_in_place()
1670
1692
        repository.unlock()
1671
1693
        response = request.execute('', token)
1672
1694
        self.assertEqual(
1673
 
            SmartServerResponse(('ok',)), response)
 
1695
            smart_req.SmartServerResponse(('ok',)), response)
1674
1696
        # The repository is now unlocked.  Verify that with a new repository
1675
1697
        # object.
1676
1698
        new_repo = repository.bzrdir.open_repository()
1679
1701
 
1680
1702
    def test_unlock_on_unlocked_repo(self):
1681
1703
        backing = self.get_transport()
1682
 
        request = smart.repository.SmartServerRepositoryUnlock(backing)
 
1704
        request = smart_repo.SmartServerRepositoryUnlock(backing)
1683
1705
        repository = self.make_repository('.', format='knit')
1684
1706
        response = request.execute('', 'some token')
1685
1707
        self.assertEqual(
1686
 
            SmartServerResponse(('TokenMismatch',)), response)
 
1708
            smart_req.SmartServerResponse(('TokenMismatch',)), response)
1687
1709
 
1688
1710
 
1689
1711
class TestSmartServerIsReadonly(tests.TestCaseWithMemoryTransport):
1690
1712
 
1691
1713
    def test_is_readonly_no(self):
1692
1714
        backing = self.get_transport()
1693
 
        request = smart.request.SmartServerIsReadonly(backing)
 
1715
        request = smart_req.SmartServerIsReadonly(backing)
1694
1716
        response = request.execute()
1695
1717
        self.assertEqual(
1696
 
            SmartServerResponse(('no',)), response)
 
1718
            smart_req.SmartServerResponse(('no',)), response)
1697
1719
 
1698
1720
    def test_is_readonly_yes(self):
1699
1721
        backing = self.get_readonly_transport()
1700
 
        request = smart.request.SmartServerIsReadonly(backing)
 
1722
        request = smart_req.SmartServerIsReadonly(backing)
1701
1723
        response = request.execute()
1702
1724
        self.assertEqual(
1703
 
            SmartServerResponse(('yes',)), response)
1704
 
 
1705
 
 
1706
 
class TestSmartServerRepositorySetMakeWorkingTrees(tests.TestCaseWithMemoryTransport):
 
1725
            smart_req.SmartServerResponse(('yes',)), response)
 
1726
 
 
1727
 
 
1728
class TestSmartServerRepositorySetMakeWorkingTrees(
 
1729
    tests.TestCaseWithMemoryTransport):
1707
1730
 
1708
1731
    def test_set_false(self):
1709
1732
        backing = self.get_transport()
1710
1733
        repo = self.make_repository('.', shared=True)
1711
1734
        repo.set_make_working_trees(True)
1712
 
        request_class = smart.repository.SmartServerRepositorySetMakeWorkingTrees
 
1735
        request_class = smart_repo.SmartServerRepositorySetMakeWorkingTrees
1713
1736
        request = request_class(backing)
1714
 
        self.assertEqual(SuccessfulSmartServerResponse(('ok',)),
 
1737
        self.assertEqual(smart_req.SuccessfulSmartServerResponse(('ok',)),
1715
1738
            request.execute('', 'False'))
1716
1739
        repo = repo.bzrdir.open_repository()
1717
1740
        self.assertFalse(repo.make_working_trees())
1720
1743
        backing = self.get_transport()
1721
1744
        repo = self.make_repository('.', shared=True)
1722
1745
        repo.set_make_working_trees(False)
1723
 
        request_class = smart.repository.SmartServerRepositorySetMakeWorkingTrees
 
1746
        request_class = smart_repo.SmartServerRepositorySetMakeWorkingTrees
1724
1747
        request = request_class(backing)
1725
 
        self.assertEqual(SuccessfulSmartServerResponse(('ok',)),
 
1748
        self.assertEqual(smart_req.SuccessfulSmartServerResponse(('ok',)),
1726
1749
            request.execute('', 'True'))
1727
1750
        repo = repo.bzrdir.open_repository()
1728
1751
        self.assertTrue(repo.make_working_trees())
1747
1770
        repo.lock_write()
1748
1771
        self.addCleanup(repo.unlock)
1749
1772
        backing = self.get_transport()
1750
 
        request = smart.packrepository.SmartServerPackRepositoryAutopack(
 
1773
        request = smart_packrepo.SmartServerPackRepositoryAutopack(
1751
1774
            backing)
1752
1775
        response = request.execute('')
1753
 
        self.assertEqual(SmartServerResponse(('ok',)), response)
 
1776
        self.assertEqual(smart_req.SmartServerResponse(('ok',)), response)
1754
1777
        repo._pack_collection.reload_pack_names()
1755
1778
        self.assertEqual(1, len(repo._pack_collection.names()))
1756
1779
 
1762
1785
        for x in range(9):
1763
1786
            tree.commit('commit %s' % x)
1764
1787
        backing = self.get_transport()
1765
 
        request = smart.packrepository.SmartServerPackRepositoryAutopack(
 
1788
        request = smart_packrepo.SmartServerPackRepositoryAutopack(
1766
1789
            backing)
1767
1790
        response = request.execute('')
1768
 
        self.assertEqual(SmartServerResponse(('ok',)), response)
 
1791
        self.assertEqual(smart_req.SmartServerResponse(('ok',)), response)
1769
1792
        repo._pack_collection.reload_pack_names()
1770
1793
        self.assertEqual(9, len(repo._pack_collection.names()))
1771
1794
 
1773
1796
        """A request to autopack a non-pack repo is a no-op."""
1774
1797
        repo = self.make_repository('.', format='knit')
1775
1798
        backing = self.get_transport()
1776
 
        request = smart.packrepository.SmartServerPackRepositoryAutopack(
 
1799
        request = smart_packrepo.SmartServerPackRepositoryAutopack(
1777
1800
            backing)
1778
1801
        response = request.execute('')
1779
 
        self.assertEqual(SmartServerResponse(('ok',)), response)
 
1802
        self.assertEqual(smart_req.SmartServerResponse(('ok',)), response)
1780
1803
 
1781
1804
 
1782
1805
class TestSmartServerVfsGet(tests.TestCaseWithMemoryTransport):
1786
1809
        filename = u'foo\N{INTERROBANG}'
1787
1810
        filename_escaped = urlutils.escape(filename)
1788
1811
        backing = self.get_transport()
1789
 
        request = smart.vfs.GetRequest(backing)
 
1812
        request = vfs.GetRequest(backing)
1790
1813
        backing.put_bytes_non_atomic(filename_escaped, 'contents')
1791
 
        self.assertEqual(SmartServerResponse(('ok', ), 'contents'),
 
1814
        self.assertEqual(smart_req.SmartServerResponse(('ok', ), 'contents'),
1792
1815
            request.execute(filename_escaped))
1793
1816
 
1794
1817
 
1799
1822
        """All registered request_handlers can be found."""
1800
1823
        # If there's a typo in a register_lazy call, this loop will fail with
1801
1824
        # an AttributeError.
1802
 
        for key, item in smart.request.request_handlers.iteritems():
 
1825
        for key, item in smart_req.request_handlers.iteritems():
1803
1826
            pass
1804
1827
 
1805
1828
    def assertHandlerEqual(self, verb, handler):
1806
 
        self.assertEqual(smart.request.request_handlers.get(verb), handler)
 
1829
        self.assertEqual(smart_req.request_handlers.get(verb), handler)
1807
1830
 
1808
1831
    def test_registered_methods(self):
1809
1832
        """Test that known methods are registered to the correct object."""
1810
1833
        self.assertHandlerEqual('Branch.get_config_file',
1811
 
            smart.branch.SmartServerBranchGetConfigFile)
 
1834
            smart_branch.SmartServerBranchGetConfigFile)
1812
1835
        self.assertHandlerEqual('Branch.get_parent',
1813
 
            smart.branch.SmartServerBranchGetParent)
 
1836
            smart_branch.SmartServerBranchGetParent)
1814
1837
        self.assertHandlerEqual('Branch.get_tags_bytes',
1815
 
            smart.branch.SmartServerBranchGetTagsBytes)
 
1838
            smart_branch.SmartServerBranchGetTagsBytes)
1816
1839
        self.assertHandlerEqual('Branch.lock_write',
1817
 
            smart.branch.SmartServerBranchRequestLockWrite)
 
1840
            smart_branch.SmartServerBranchRequestLockWrite)
1818
1841
        self.assertHandlerEqual('Branch.last_revision_info',
1819
 
            smart.branch.SmartServerBranchRequestLastRevisionInfo)
 
1842
            smart_branch.SmartServerBranchRequestLastRevisionInfo)
1820
1843
        self.assertHandlerEqual('Branch.revision_history',
1821
 
            smart.branch.SmartServerRequestRevisionHistory)
 
1844
            smart_branch.SmartServerRequestRevisionHistory)
1822
1845
        self.assertHandlerEqual('Branch.set_config_option',
1823
 
            smart.branch.SmartServerBranchRequestSetConfigOption)
 
1846
            smart_branch.SmartServerBranchRequestSetConfigOption)
1824
1847
        self.assertHandlerEqual('Branch.set_last_revision',
1825
 
            smart.branch.SmartServerBranchRequestSetLastRevision)
 
1848
            smart_branch.SmartServerBranchRequestSetLastRevision)
1826
1849
        self.assertHandlerEqual('Branch.set_last_revision_info',
1827
 
            smart.branch.SmartServerBranchRequestSetLastRevisionInfo)
 
1850
            smart_branch.SmartServerBranchRequestSetLastRevisionInfo)
1828
1851
        self.assertHandlerEqual('Branch.set_last_revision_ex',
1829
 
            smart.branch.SmartServerBranchRequestSetLastRevisionEx)
 
1852
            smart_branch.SmartServerBranchRequestSetLastRevisionEx)
1830
1853
        self.assertHandlerEqual('Branch.set_parent_location',
1831
 
            smart.branch.SmartServerBranchRequestSetParentLocation)
 
1854
            smart_branch.SmartServerBranchRequestSetParentLocation)
1832
1855
        self.assertHandlerEqual('Branch.unlock',
1833
 
            smart.branch.SmartServerBranchRequestUnlock)
 
1856
            smart_branch.SmartServerBranchRequestUnlock)
1834
1857
        self.assertHandlerEqual('BzrDir.find_repository',
1835
 
            smart.bzrdir.SmartServerRequestFindRepositoryV1)
 
1858
            smart_dir.SmartServerRequestFindRepositoryV1)
1836
1859
        self.assertHandlerEqual('BzrDir.find_repositoryV2',
1837
 
            smart.bzrdir.SmartServerRequestFindRepositoryV2)
 
1860
            smart_dir.SmartServerRequestFindRepositoryV2)
1838
1861
        self.assertHandlerEqual('BzrDirFormat.initialize',
1839
 
            smart.bzrdir.SmartServerRequestInitializeBzrDir)
 
1862
            smart_dir.SmartServerRequestInitializeBzrDir)
1840
1863
        self.assertHandlerEqual('BzrDirFormat.initialize_ex_1.16',
1841
 
            smart.bzrdir.SmartServerRequestBzrDirInitializeEx)
 
1864
            smart_dir.SmartServerRequestBzrDirInitializeEx)
1842
1865
        self.assertHandlerEqual('BzrDir.cloning_metadir',
1843
 
            smart.bzrdir.SmartServerBzrDirRequestCloningMetaDir)
 
1866
            smart_dir.SmartServerBzrDirRequestCloningMetaDir)
1844
1867
        self.assertHandlerEqual('BzrDir.get_config_file',
1845
 
            smart.bzrdir.SmartServerBzrDirRequestConfigFile)
 
1868
            smart_dir.SmartServerBzrDirRequestConfigFile)
1846
1869
        self.assertHandlerEqual('BzrDir.open_branch',
1847
 
            smart.bzrdir.SmartServerRequestOpenBranch)
 
1870
            smart_dir.SmartServerRequestOpenBranch)
1848
1871
        self.assertHandlerEqual('BzrDir.open_branchV2',
1849
 
            smart.bzrdir.SmartServerRequestOpenBranchV2)
 
1872
            smart_dir.SmartServerRequestOpenBranchV2)
1850
1873
        self.assertHandlerEqual('BzrDir.open_branchV3',
1851
 
            smart.bzrdir.SmartServerRequestOpenBranchV3)
 
1874
            smart_dir.SmartServerRequestOpenBranchV3)
1852
1875
        self.assertHandlerEqual('PackRepository.autopack',
1853
 
            smart.packrepository.SmartServerPackRepositoryAutopack)
 
1876
            smart_packrepo.SmartServerPackRepositoryAutopack)
1854
1877
        self.assertHandlerEqual('Repository.gather_stats',
1855
 
            smart.repository.SmartServerRepositoryGatherStats)
 
1878
            smart_repo.SmartServerRepositoryGatherStats)
1856
1879
        self.assertHandlerEqual('Repository.get_parent_map',
1857
 
            smart.repository.SmartServerRepositoryGetParentMap)
 
1880
            smart_repo.SmartServerRepositoryGetParentMap)
1858
1881
        self.assertHandlerEqual('Repository.get_rev_id_for_revno',
1859
 
            smart.repository.SmartServerRepositoryGetRevIdForRevno)
 
1882
            smart_repo.SmartServerRepositoryGetRevIdForRevno)
1860
1883
        self.assertHandlerEqual('Repository.get_revision_graph',
1861
 
            smart.repository.SmartServerRepositoryGetRevisionGraph)
 
1884
            smart_repo.SmartServerRepositoryGetRevisionGraph)
1862
1885
        self.assertHandlerEqual('Repository.get_stream',
1863
 
            smart.repository.SmartServerRepositoryGetStream)
 
1886
            smart_repo.SmartServerRepositoryGetStream)
1864
1887
        self.assertHandlerEqual('Repository.has_revision',
1865
 
            smart.repository.SmartServerRequestHasRevision)
 
1888
            smart_repo.SmartServerRequestHasRevision)
1866
1889
        self.assertHandlerEqual('Repository.insert_stream',
1867
 
            smart.repository.SmartServerRepositoryInsertStream)
 
1890
            smart_repo.SmartServerRepositoryInsertStream)
1868
1891
        self.assertHandlerEqual('Repository.insert_stream_locked',
1869
 
            smart.repository.SmartServerRepositoryInsertStreamLocked)
 
1892
            smart_repo.SmartServerRepositoryInsertStreamLocked)
1870
1893
        self.assertHandlerEqual('Repository.is_shared',
1871
 
            smart.repository.SmartServerRepositoryIsShared)
 
1894
            smart_repo.SmartServerRepositoryIsShared)
1872
1895
        self.assertHandlerEqual('Repository.lock_write',
1873
 
            smart.repository.SmartServerRepositoryLockWrite)
 
1896
            smart_repo.SmartServerRepositoryLockWrite)
1874
1897
        self.assertHandlerEqual('Repository.tarball',
1875
 
            smart.repository.SmartServerRepositoryTarball)
 
1898
            smart_repo.SmartServerRepositoryTarball)
1876
1899
        self.assertHandlerEqual('Repository.unlock',
1877
 
            smart.repository.SmartServerRepositoryUnlock)
 
1900
            smart_repo.SmartServerRepositoryUnlock)
1878
1901
        self.assertHandlerEqual('Transport.is_readonly',
1879
 
            smart.request.SmartServerIsReadonly)
 
1902
            smart_req.SmartServerIsReadonly)