~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/tests/test_smart.py

  • Committer: Aaron Bentley
  • Date: 2009-08-21 17:28:25 UTC
  • mto: This revision was merged to the branch mainline in revision 4641.
  • Revision ID: aaron@aaronbentley.com-20090821172825-jwlyw2a8d4ck4an8
Add expected failures for shelving root changes.

Show diffs side-by-side

added added

removed removed

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