~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/tests/test_smart.py

  • Committer: John Arbash Meinel
  • Date: 2009-08-25 18:45:40 UTC
  • mto: (4634.6.15 2.0)
  • mto: This revision was merged to the branch mainline in revision 4667.
  • Revision ID: john@arbash-meinel.com-20090825184540-6dn3xjq62xhgj2gq
Add support for skipping ghost nodes.

Show diffs side-by-side

added added

removed removed

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