~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/tests/test_smart.py

Merge and cleanup pre-external-reference-repository tests

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