~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/tests/test_smart.py

  • Committer: Robert Collins
  • Date: 2009-07-07 04:32:13 UTC
  • mto: This revision was merged to the branch mainline in revision 4524.
  • Revision ID: robertc@robertcollins.net-20090707043213-4hjjhgr40iq7gk2d
More informative assertions in xml serialisation.

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 protococl."""
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('default')()
 
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 (smart.bzrdir.SmartServerRequestFindRepositoryV3 ==
 
251
            self._request_class):
 
252
            return SuccessfulSmartServerResponse(
 
253
                ('ok', '', rich_root, subtrees, 'no',
 
254
                 repo._format.network_name()))
 
255
        elif (smart.bzrdir.SmartServerRequestFindRepositoryV2 ==
 
256
            self._request_class):
 
257
            # All tests so far are on formats, and for non-external
 
258
            # repositories.
 
259
            return SuccessfulSmartServerResponse(
 
260
                ('ok', '', rich_root, subtrees, 'no'))
 
261
        else:
 
262
            return SuccessfulSmartServerResponse(('ok', '', rich_root, subtrees))
98
263
 
99
264
    def test_shared_repository(self):
100
265
        """When there is a shared repository, we get 'ok', 'relpath-to-repo'."""
101
266
        backing = self.get_transport()
102
 
        request = smart.bzrdir.SmartServerRequestFindRepository(backing)
 
267
        request = self._request_class(backing)
103
268
        result = self._make_repository_and_result(shared=True)
104
 
        self.assertEqual(result, request.execute(backing.local_abspath('')))
 
269
        self.assertEqual(result, request.execute(''))
105
270
        self.make_bzrdir('subdir')
106
271
        result2 = SmartServerResponse(result.args[0:1] + ('..', ) + result.args[2:])
107
272
        self.assertEqual(result2,
108
 
            request.execute(backing.local_abspath('subdir')))
 
273
            request.execute('subdir'))
109
274
        self.make_bzrdir('subdir/deeper')
110
275
        result3 = SmartServerResponse(result.args[0:1] + ('../..', ) + result.args[2:])
111
276
        self.assertEqual(result3,
112
 
            request.execute(backing.local_abspath('subdir/deeper')))
 
277
            request.execute('subdir/deeper'))
113
278
 
114
279
    def test_rich_root_and_subtree_encoding(self):
115
280
        """Test for the format attributes for rich root and subtree support."""
116
281
        backing = self.get_transport()
117
 
        request = smart.bzrdir.SmartServerRequestFindRepository(backing)
 
282
        request = self._request_class(backing)
118
283
        result = self._make_repository_and_result(format='dirstate-with-subtree')
119
284
        # check the test will be valid
120
285
        self.assertEqual('yes', result.args[2])
121
286
        self.assertEqual('yes', result.args[3])
122
 
        self.assertEqual(result, request.execute(backing.local_abspath('')))
123
 
 
124
 
 
125
 
class TestSmartServerRequestInitializeBzrDir(tests.TestCaseWithTransport):
 
287
        self.assertEqual(result, request.execute(''))
 
288
 
 
289
    def test_supports_external_lookups_no_v2(self):
 
290
        """Test for the supports_external_lookups attribute."""
 
291
        backing = self.get_transport()
 
292
        request = self._request_class(backing)
 
293
        result = self._make_repository_and_result(format='dirstate-with-subtree')
 
294
        # check the test will be valid
 
295
        self.assertEqual('no', result.args[4])
 
296
        self.assertEqual(result, request.execute(''))
 
297
 
 
298
 
 
299
class TestSmartServerBzrDirRequestGetConfigFile(
 
300
    tests.TestCaseWithMemoryTransport):
 
301
    """Tests for BzrDir.get_config_file."""
 
302
 
 
303
    def test_present(self):
 
304
        backing = self.get_transport()
 
305
        dir = self.make_bzrdir('.')
 
306
        dir.get_config().set_default_stack_on("/")
 
307
        local_result = dir._get_config()._get_config_file().read()
 
308
        request_class = smart_dir.SmartServerBzrDirRequestConfigFile
 
309
        request = request_class(backing)
 
310
        expected = SuccessfulSmartServerResponse((), local_result)
 
311
        self.assertEqual(expected, request.execute(''))
 
312
 
 
313
    def test_missing(self):
 
314
        backing = self.get_transport()
 
315
        dir = self.make_bzrdir('.')
 
316
        request_class = smart_dir.SmartServerBzrDirRequestConfigFile
 
317
        request = request_class(backing)
 
318
        expected = SuccessfulSmartServerResponse((), '')
 
319
        self.assertEqual(expected, request.execute(''))
 
320
 
 
321
 
 
322
class TestSmartServerRequestInitializeBzrDir(tests.TestCaseWithMemoryTransport):
126
323
 
127
324
    def test_empty_dir(self):
128
325
        """Initializing an empty dir should succeed and do it."""
129
326
        backing = self.get_transport()
130
327
        request = smart.bzrdir.SmartServerRequestInitializeBzrDir(backing)
131
328
        self.assertEqual(SmartServerResponse(('ok', )),
132
 
            request.execute(backing.local_abspath('.')))
 
329
            request.execute(''))
133
330
        made_dir = bzrdir.BzrDir.open_from_transport(backing)
134
 
        # no branch, tree or repository is expected with the current 
 
331
        # no branch, tree or repository is expected with the current
135
332
        # default formart.
136
333
        self.assertRaises(errors.NoWorkingTree, made_dir.open_workingtree)
137
334
        self.assertRaises(errors.NotBranchError, made_dir.open_branch)
142
339
        backing = self.get_transport()
143
340
        request = smart.bzrdir.SmartServerRequestInitializeBzrDir(backing)
144
341
        self.assertRaises(errors.NoSuchFile,
145
 
            request.execute, backing.local_abspath('subdir'))
 
342
            request.execute, 'subdir')
146
343
 
147
344
    def test_initialized_dir(self):
148
345
        """Initializing an extant bzrdir should fail like the bzrdir api."""
150
347
        request = smart.bzrdir.SmartServerRequestInitializeBzrDir(backing)
151
348
        self.make_bzrdir('subdir')
152
349
        self.assertRaises(errors.FileExists,
153
 
            request.execute, backing.local_abspath('subdir'))
154
 
 
155
 
 
156
 
class TestSmartServerRequestOpenBranch(tests.TestCaseWithTransport):
 
350
            request.execute, 'subdir')
 
351
 
 
352
 
 
353
class TestSmartServerRequestBzrDirInitializeEx(tests.TestCaseWithMemoryTransport):
 
354
    """Basic tests for BzrDir.initialize_ex_1.16 in the smart server.
 
355
 
 
356
    The main unit tests in test_bzrdir exercise the API comprehensively.
 
357
    """
 
358
 
 
359
    def test_empty_dir(self):
 
360
        """Initializing an empty dir should succeed and do it."""
 
361
        backing = self.get_transport()
 
362
        name = self.make_bzrdir('reference')._format.network_name()
 
363
        request = smart.bzrdir.SmartServerRequestBzrDirInitializeEx(backing)
 
364
        self.assertEqual(SmartServerResponse(('', '', '', '', '', '', name,
 
365
            'False', '', '', '')),
 
366
            request.execute(name, '', 'True', 'False', 'False', '', '', '', '',
 
367
            'False'))
 
368
        made_dir = bzrdir.BzrDir.open_from_transport(backing)
 
369
        # no branch, tree or repository is expected with the current
 
370
        # default format.
 
371
        self.assertRaises(errors.NoWorkingTree, made_dir.open_workingtree)
 
372
        self.assertRaises(errors.NotBranchError, made_dir.open_branch)
 
373
        self.assertRaises(errors.NoRepositoryPresent, made_dir.open_repository)
 
374
 
 
375
    def test_missing_dir(self):
 
376
        """Initializing a missing directory should fail like the bzrdir api."""
 
377
        backing = self.get_transport()
 
378
        name = self.make_bzrdir('reference')._format.network_name()
 
379
        request = smart.bzrdir.SmartServerRequestBzrDirInitializeEx(backing)
 
380
        self.assertRaises(errors.NoSuchFile, request.execute, name,
 
381
            'subdir/dir', 'False', 'False', 'False', '', '', '', '', 'False')
 
382
 
 
383
    def test_initialized_dir(self):
 
384
        """Initializing an extant directory should fail like the bzrdir api."""
 
385
        backing = self.get_transport()
 
386
        name = self.make_bzrdir('reference')._format.network_name()
 
387
        request = smart.bzrdir.SmartServerRequestBzrDirInitializeEx(backing)
 
388
        self.make_bzrdir('subdir')
 
389
        self.assertRaises(errors.FileExists, request.execute, name, 'subdir',
 
390
            'False', 'False', 'False', '', '', '', '', 'False')
 
391
 
 
392
 
 
393
class TestSmartServerRequestOpenBranch(TestCaseWithChrootedTransport):
157
394
 
158
395
    def test_no_branch(self):
159
396
        """When there is no branch, ('nobranch', ) is returned."""
161
398
        request = smart.bzrdir.SmartServerRequestOpenBranch(backing)
162
399
        self.make_bzrdir('.')
163
400
        self.assertEqual(SmartServerResponse(('nobranch', )),
164
 
            request.execute(backing.local_abspath('')))
 
401
            request.execute(''))
165
402
 
166
403
    def test_branch(self):
167
404
        """When there is a branch, 'ok' is returned."""
169
406
        request = smart.bzrdir.SmartServerRequestOpenBranch(backing)
170
407
        self.make_branch('.')
171
408
        self.assertEqual(SmartServerResponse(('ok', '')),
172
 
            request.execute(backing.local_abspath('')))
 
409
            request.execute(''))
173
410
 
174
411
    def test_branch_reference(self):
175
412
        """When there is a branch reference, the reference URL is returned."""
177
414
        request = smart.bzrdir.SmartServerRequestOpenBranch(backing)
178
415
        branch = self.make_branch('branch')
179
416
        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') + '/'
 
417
        reference_url = BranchReferenceFormat().get_reference(checkout.bzrdir)
183
418
        self.assertFileEqual(reference_url, 'reference/.bzr/branch/location')
184
419
        self.assertEqual(SmartServerResponse(('ok', reference_url)),
185
 
            request.execute(backing.local_abspath('reference')))
186
 
 
187
 
 
188
 
class TestSmartServerRequestRevisionHistory(tests.TestCaseWithTransport):
 
420
            request.execute('reference'))
 
421
 
 
422
 
 
423
class TestSmartServerRequestOpenBranchV2(TestCaseWithChrootedTransport):
 
424
 
 
425
    def test_no_branch(self):
 
426
        """When there is no branch, ('nobranch', ) is returned."""
 
427
        backing = self.get_transport()
 
428
        self.make_bzrdir('.')
 
429
        request = smart.bzrdir.SmartServerRequestOpenBranchV2(backing)
 
430
        self.assertEqual(SmartServerResponse(('nobranch', )),
 
431
            request.execute(''))
 
432
 
 
433
    def test_branch(self):
 
434
        """When there is a branch, 'ok' is returned."""
 
435
        backing = self.get_transport()
 
436
        expected = self.make_branch('.')._format.network_name()
 
437
        request = smart.bzrdir.SmartServerRequestOpenBranchV2(backing)
 
438
        self.assertEqual(SuccessfulSmartServerResponse(('branch', expected)),
 
439
            request.execute(''))
 
440
 
 
441
    def test_branch_reference(self):
 
442
        """When there is a branch reference, the reference URL is returned."""
 
443
        backing = self.get_transport()
 
444
        request = smart.bzrdir.SmartServerRequestOpenBranchV2(backing)
 
445
        branch = self.make_branch('branch')
 
446
        checkout = branch.create_checkout('reference',lightweight=True)
 
447
        reference_url = BranchReferenceFormat().get_reference(checkout.bzrdir)
 
448
        self.assertFileEqual(reference_url, 'reference/.bzr/branch/location')
 
449
        self.assertEqual(SuccessfulSmartServerResponse(('ref', reference_url)),
 
450
            request.execute('reference'))
 
451
 
 
452
    def test_stacked_branch(self):
 
453
        """Opening a stacked branch does not open the stacked-on branch."""
 
454
        trunk = self.make_branch('trunk')
 
455
        feature = self.make_branch('feature', format='1.9')
 
456
        feature.set_stacked_on_url(trunk.base)
 
457
        opened_branches = []
 
458
        Branch.hooks.install_named_hook('open', opened_branches.append, None)
 
459
        backing = self.get_transport()
 
460
        request = smart.bzrdir.SmartServerRequestOpenBranchV2(backing)
 
461
        request.setup_jail()
 
462
        try:
 
463
            response = request.execute('feature')
 
464
        finally:
 
465
            request.teardown_jail()
 
466
        expected_format = feature._format.network_name()
 
467
        self.assertEqual(
 
468
            SuccessfulSmartServerResponse(('branch', expected_format)),
 
469
            response)
 
470
        self.assertLength(1, opened_branches)
 
471
 
 
472
 
 
473
class TestSmartServerRequestRevisionHistory(tests.TestCaseWithMemoryTransport):
189
474
 
190
475
    def test_empty(self):
191
476
        """For an empty branch, the body is empty."""
193
478
        request = smart.branch.SmartServerRequestRevisionHistory(backing)
194
479
        self.make_branch('.')
195
480
        self.assertEqual(SmartServerResponse(('ok', ), ''),
196
 
            request.execute(backing.local_abspath('')))
 
481
            request.execute(''))
197
482
 
198
483
    def test_not_empty(self):
199
484
        """For a non-empty branch, the body is empty."""
207
492
        tree.unlock()
208
493
        self.assertEqual(
209
494
            SmartServerResponse(('ok', ), ('\x00'.join([r1, r2]))),
210
 
            request.execute(backing.local_abspath('')))
211
 
 
212
 
 
213
 
class TestSmartServerBranchRequest(tests.TestCaseWithTransport):
 
495
            request.execute(''))
 
496
 
 
497
 
 
498
class TestSmartServerBranchRequest(tests.TestCaseWithMemoryTransport):
214
499
 
215
500
    def test_no_branch(self):
216
501
        """When there is a bzrdir and no branch, NotBranchError is raised."""
218
503
        request = smart.branch.SmartServerBranchRequest(backing)
219
504
        self.make_bzrdir('.')
220
505
        self.assertRaises(errors.NotBranchError,
221
 
            request.execute, backing.local_abspath(''))
 
506
            request.execute, '')
222
507
 
223
508
    def test_branch_reference(self):
224
509
        """When there is a branch reference, NotBranchError is raised."""
227
512
        branch = self.make_branch('branch')
228
513
        checkout = branch.create_checkout('reference',lightweight=True)
229
514
        self.assertRaises(errors.NotBranchError,
230
 
            request.execute, backing.local_abspath('checkout'))
231
 
 
232
 
 
233
 
class TestSmartServerBranchRequestLastRevisionInfo(tests.TestCaseWithTransport):
 
515
            request.execute, 'checkout')
 
516
 
 
517
 
 
518
class TestSmartServerBranchRequestLastRevisionInfo(tests.TestCaseWithMemoryTransport):
234
519
 
235
520
    def test_empty(self):
236
521
        """For an empty branch, the result is ('ok', '0', 'null:')."""
238
523
        request = smart.branch.SmartServerBranchRequestLastRevisionInfo(backing)
239
524
        self.make_branch('.')
240
525
        self.assertEqual(SmartServerResponse(('ok', '0', 'null:')),
241
 
            request.execute(backing.local_abspath('')))
 
526
            request.execute(''))
242
527
 
243
528
    def test_not_empty(self):
244
529
        """For a non-empty branch, the result is ('ok', 'revno', 'revid')."""
253
538
        tree.unlock()
254
539
        self.assertEqual(
255
540
            SmartServerResponse(('ok', '2', rev_id_utf8)),
256
 
            request.execute(backing.local_abspath('')))
257
 
 
258
 
 
259
 
class TestSmartServerBranchRequestGetConfigFile(tests.TestCaseWithTransport):
 
541
            request.execute(''))
 
542
 
 
543
 
 
544
class TestSmartServerBranchRequestGetConfigFile(tests.TestCaseWithMemoryTransport):
260
545
 
261
546
    def test_default(self):
262
547
        """With no file, we get empty content."""
266
551
        # there should be no file by default
267
552
        content = ''
268
553
        self.assertEqual(SmartServerResponse(('ok', ), content),
269
 
            request.execute(backing.local_abspath('')))
 
554
            request.execute(''))
270
555
 
271
556
    def test_with_content(self):
272
557
        # SmartServerBranchGetConfigFile should return the content from
273
558
        # branch.control_files.get('branch.conf') for now - in the future it may
274
 
        # perform more complex processing. 
 
559
        # perform more complex processing.
275
560
        backing = self.get_transport()
276
561
        request = smart.branch.SmartServerBranchGetConfigFile(backing)
277
562
        branch = self.make_branch('.')
278
 
        branch.control_files.put_utf8('branch.conf', 'foo bar baz')
 
563
        branch._transport.put_bytes('branch.conf', 'foo bar baz')
279
564
        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()
 
565
            request.execute(''))
 
566
 
 
567
 
 
568
class TestLockedBranch(tests.TestCaseWithMemoryTransport):
 
569
 
 
570
    def get_lock_tokens(self, branch):
 
571
        branch_token = branch.lock_write()
 
572
        repo_token = branch.repository.lock_write()
 
573
        branch.repository.unlock()
 
574
        return branch_token, repo_token
 
575
 
 
576
 
 
577
class TestSmartServerBranchRequestSetConfigOption(TestLockedBranch):
 
578
 
 
579
    def test_value_name(self):
 
580
        branch = self.make_branch('.')
 
581
        request = smart.branch.SmartServerBranchRequestSetConfigOption(
 
582
            branch.bzrdir.root_transport)
 
583
        branch_token, repo_token = self.get_lock_tokens(branch)
 
584
        config = branch._get_config()
 
585
        result = request.execute('', branch_token, repo_token, 'bar', 'foo',
 
586
            '')
 
587
        self.assertEqual(SuccessfulSmartServerResponse(()), result)
 
588
        self.assertEqual('bar', config.get_option('foo'))
 
589
        # Cleanup
 
590
        branch.unlock()
 
591
 
 
592
    def test_value_name_section(self):
 
593
        branch = self.make_branch('.')
 
594
        request = smart.branch.SmartServerBranchRequestSetConfigOption(
 
595
            branch.bzrdir.root_transport)
 
596
        branch_token, repo_token = self.get_lock_tokens(branch)
 
597
        config = branch._get_config()
 
598
        result = request.execute('', branch_token, repo_token, 'bar', 'foo',
 
599
            'gam')
 
600
        self.assertEqual(SuccessfulSmartServerResponse(()), result)
 
601
        self.assertEqual('bar', config.get_option('foo', 'gam'))
 
602
        # Cleanup
 
603
        branch.unlock()
 
604
 
 
605
 
 
606
class SetLastRevisionTestBase(TestLockedBranch):
 
607
    """Base test case for verbs that implement set_last_revision."""
 
608
 
 
609
    def setUp(self):
 
610
        tests.TestCaseWithMemoryTransport.setUp(self)
 
611
        backing_transport = self.get_transport()
 
612
        self.request = self.request_class(backing_transport)
 
613
        self.tree = self.make_branch_and_memory_tree('.')
 
614
 
 
615
    def lock_branch(self):
 
616
        return self.get_lock_tokens(self.tree.branch)
 
617
 
 
618
    def unlock_branch(self):
 
619
        self.tree.branch.unlock()
 
620
 
 
621
    def set_last_revision(self, revision_id, revno):
 
622
        branch_token, repo_token = self.lock_branch()
 
623
        response = self._set_last_revision(
 
624
            revision_id, revno, branch_token, repo_token)
 
625
        self.unlock_branch()
 
626
        return response
 
627
 
 
628
    def assertRequestSucceeds(self, revision_id, revno):
 
629
        response = self.set_last_revision(revision_id, revno)
 
630
        self.assertEqual(SuccessfulSmartServerResponse(('ok',)), response)
 
631
 
 
632
 
 
633
class TestSetLastRevisionVerbMixin(object):
 
634
    """Mixin test case for verbs that implement set_last_revision."""
 
635
 
 
636
    def test_set_null_to_null(self):
 
637
        """An empty branch can have its last revision set to 'null:'."""
 
638
        self.assertRequestSucceeds('null:', 0)
 
639
 
 
640
    def test_NoSuchRevision(self):
 
641
        """If the revision_id is not present, the verb returns NoSuchRevision.
 
642
        """
 
643
        revision_id = 'non-existent revision'
 
644
        self.assertEqual(
 
645
            FailedSmartServerResponse(('NoSuchRevision', revision_id)),
 
646
            self.set_last_revision(revision_id, 1))
 
647
 
 
648
    def make_tree_with_two_commits(self):
 
649
        self.tree.lock_write()
 
650
        self.tree.add('')
 
651
        rev_id_utf8 = u'\xc8'.encode('utf-8')
 
652
        r1 = self.tree.commit('1st commit', rev_id=rev_id_utf8)
 
653
        r2 = self.tree.commit('2nd commit', rev_id='rev-2')
 
654
        self.tree.unlock()
 
655
 
 
656
    def test_branch_last_revision_info_is_updated(self):
 
657
        """A branch's tip can be set to a revision that is present in its
 
658
        repository.
 
659
        """
 
660
        # Make a branch with an empty revision history, but two revisions in
 
661
        # its repository.
 
662
        self.make_tree_with_two_commits()
 
663
        rev_id_utf8 = u'\xc8'.encode('utf-8')
 
664
        self.tree.branch.set_revision_history([])
 
665
        self.assertEqual(
 
666
            (0, 'null:'), self.tree.branch.last_revision_info())
 
667
        # We can update the branch to a revision that is present in the
 
668
        # repository.
 
669
        self.assertRequestSucceeds(rev_id_utf8, 1)
 
670
        self.assertEqual(
 
671
            (1, rev_id_utf8), self.tree.branch.last_revision_info())
 
672
 
 
673
    def test_branch_last_revision_info_rewind(self):
 
674
        """A branch's tip can be set to a revision that is an ancestor of the
 
675
        current tip.
 
676
        """
 
677
        self.make_tree_with_two_commits()
 
678
        rev_id_utf8 = u'\xc8'.encode('utf-8')
 
679
        self.assertEqual(
 
680
            (2, 'rev-2'), self.tree.branch.last_revision_info())
 
681
        self.assertRequestSucceeds(rev_id_utf8, 1)
 
682
        self.assertEqual(
 
683
            (1, rev_id_utf8), self.tree.branch.last_revision_info())
 
684
 
 
685
    def test_TipChangeRejected(self):
 
686
        """If a pre_change_branch_tip hook raises TipChangeRejected, the verb
 
687
        returns TipChangeRejected.
 
688
        """
 
689
        rejection_message = u'rejection message\N{INTERROBANG}'
 
690
        def hook_that_rejects(params):
 
691
            raise errors.TipChangeRejected(rejection_message)
 
692
        Branch.hooks.install_named_hook(
 
693
            'pre_change_branch_tip', hook_that_rejects, None)
 
694
        self.assertEqual(
 
695
            FailedSmartServerResponse(
 
696
                ('TipChangeRejected', rejection_message.encode('utf-8'))),
 
697
            self.set_last_revision('null:', 0))
 
698
 
 
699
 
 
700
class TestSmartServerBranchRequestSetLastRevision(
 
701
        SetLastRevisionTestBase, TestSetLastRevisionVerbMixin):
 
702
    """Tests for Branch.set_last_revision verb."""
 
703
 
 
704
    request_class = smart.branch.SmartServerBranchRequestSetLastRevision
 
705
 
 
706
    def _set_last_revision(self, revision_id, revno, branch_token, repo_token):
 
707
        return self.request.execute(
 
708
            '', branch_token, repo_token, revision_id)
 
709
 
 
710
 
 
711
class TestSmartServerBranchRequestSetLastRevisionInfo(
 
712
        SetLastRevisionTestBase, TestSetLastRevisionVerbMixin):
 
713
    """Tests for Branch.set_last_revision_info verb."""
 
714
 
 
715
    request_class = smart.branch.SmartServerBranchRequestSetLastRevisionInfo
 
716
 
 
717
    def _set_last_revision(self, revision_id, revno, branch_token, repo_token):
 
718
        return self.request.execute(
 
719
            '', branch_token, repo_token, revno, revision_id)
 
720
 
 
721
    def test_NoSuchRevision(self):
 
722
        """Branch.set_last_revision_info does not have to return
 
723
        NoSuchRevision if the revision_id is absent.
 
724
        """
 
725
        raise tests.TestNotApplicable()
 
726
 
 
727
 
 
728
class TestSmartServerBranchRequestSetLastRevisionEx(
 
729
        SetLastRevisionTestBase, TestSetLastRevisionVerbMixin):
 
730
    """Tests for Branch.set_last_revision_ex verb."""
 
731
 
 
732
    request_class = smart.branch.SmartServerBranchRequestSetLastRevisionEx
 
733
 
 
734
    def _set_last_revision(self, revision_id, revno, branch_token, repo_token):
 
735
        return self.request.execute(
 
736
            '', branch_token, repo_token, revision_id, 0, 0)
 
737
 
 
738
    def assertRequestSucceeds(self, revision_id, revno):
 
739
        response = self.set_last_revision(revision_id, revno)
 
740
        self.assertEqual(
 
741
            SuccessfulSmartServerResponse(('ok', revno, revision_id)),
 
742
            response)
 
743
 
 
744
    def test_branch_last_revision_info_rewind(self):
 
745
        """A branch's tip can be set to a revision that is an ancestor of the
 
746
        current tip, but only if allow_overwrite_descendant is passed.
 
747
        """
 
748
        self.make_tree_with_two_commits()
 
749
        rev_id_utf8 = u'\xc8'.encode('utf-8')
 
750
        self.assertEqual(
 
751
            (2, 'rev-2'), self.tree.branch.last_revision_info())
 
752
        # If allow_overwrite_descendant flag is 0, then trying to set the tip
 
753
        # to an older revision ID has no effect.
 
754
        branch_token, repo_token = self.lock_branch()
 
755
        response = self.request.execute(
 
756
            '', branch_token, repo_token, rev_id_utf8, 0, 0)
 
757
        self.assertEqual(
 
758
            SuccessfulSmartServerResponse(('ok', 2, 'rev-2')),
 
759
            response)
 
760
        self.assertEqual(
 
761
            (2, 'rev-2'), self.tree.branch.last_revision_info())
 
762
 
 
763
        # If allow_overwrite_descendant flag is 1, then setting the tip to an
 
764
        # ancestor works.
 
765
        response = self.request.execute(
 
766
            '', branch_token, repo_token, rev_id_utf8, 0, 1)
 
767
        self.assertEqual(
 
768
            SuccessfulSmartServerResponse(('ok', 1, rev_id_utf8)),
 
769
            response)
 
770
        self.unlock_branch()
 
771
        self.assertEqual(
 
772
            (1, rev_id_utf8), self.tree.branch.last_revision_info())
 
773
 
 
774
    def make_branch_with_divergent_history(self):
 
775
        """Make a branch with divergent history in its repo.
 
776
 
 
777
        The branch's tip will be 'child-2', and the repo will also contain
 
778
        'child-1', which diverges from a common base revision.
 
779
        """
 
780
        self.tree.lock_write()
 
781
        self.tree.add('')
 
782
        r1 = self.tree.commit('1st commit')
 
783
        revno_1, revid_1 = self.tree.branch.last_revision_info()
 
784
        r2 = self.tree.commit('2nd commit', rev_id='child-1')
 
785
        # Undo the second commit
 
786
        self.tree.branch.set_last_revision_info(revno_1, revid_1)
 
787
        self.tree.set_parent_ids([revid_1])
 
788
        # Make a new second commit, child-2.  child-2 has diverged from
 
789
        # child-1.
 
790
        new_r2 = self.tree.commit('2nd commit', rev_id='child-2')
 
791
        self.tree.unlock()
 
792
 
 
793
    def test_not_allow_diverged(self):
 
794
        """If allow_diverged is not passed, then setting a divergent history
 
795
        returns a Diverged error.
 
796
        """
 
797
        self.make_branch_with_divergent_history()
 
798
        self.assertEqual(
 
799
            FailedSmartServerResponse(('Diverged',)),
 
800
            self.set_last_revision('child-1', 2))
 
801
        # The branch tip was not changed.
 
802
        self.assertEqual('child-2', self.tree.branch.last_revision())
 
803
 
 
804
    def test_allow_diverged(self):
 
805
        """If allow_diverged is passed, then setting a divergent history
 
806
        succeeds.
 
807
        """
 
808
        self.make_branch_with_divergent_history()
 
809
        branch_token, repo_token = self.lock_branch()
 
810
        response = self.request.execute(
 
811
            '', branch_token, repo_token, 'child-1', 1, 0)
 
812
        self.assertEqual(
 
813
            SuccessfulSmartServerResponse(('ok', 2, 'child-1')),
 
814
            response)
 
815
        self.unlock_branch()
 
816
        # The branch tip was changed.
 
817
        self.assertEqual('child-1', self.tree.branch.last_revision())
 
818
 
 
819
 
 
820
class TestSmartServerBranchRequestGetParent(tests.TestCaseWithMemoryTransport):
 
821
 
 
822
    def test_get_parent_none(self):
 
823
        base_branch = self.make_branch('base')
 
824
        request = smart.branch.SmartServerBranchGetParent(self.get_transport())
 
825
        response = request.execute('base')
 
826
        self.assertEquals(
 
827
            SuccessfulSmartServerResponse(('',)), response)
 
828
 
 
829
    def test_get_parent_something(self):
 
830
        base_branch = self.make_branch('base')
 
831
        base_branch.set_parent(self.get_url('foo'))
 
832
        request = smart.branch.SmartServerBranchGetParent(self.get_transport())
 
833
        response = request.execute('base')
 
834
        self.assertEquals(
 
835
            SuccessfulSmartServerResponse(("../foo",)),
 
836
            response)
 
837
 
 
838
 
 
839
class TestSmartServerBranchRequestSetParent(tests.TestCaseWithMemoryTransport):
 
840
 
 
841
    def test_set_parent_none(self):
 
842
        branch = self.make_branch('base', format="1.9")
 
843
        branch.lock_write()
 
844
        branch._set_parent_location('foo')
 
845
        branch.unlock()
 
846
        request = smart.branch.SmartServerBranchRequestSetParentLocation(
 
847
            self.get_transport())
 
848
        branch_token = branch.lock_write()
 
849
        repo_token = branch.repository.lock_write()
 
850
        try:
 
851
            response = request.execute('base', branch_token, repo_token, '')
 
852
        finally:
 
853
            branch.repository.unlock()
 
854
            branch.unlock()
 
855
        self.assertEqual(SuccessfulSmartServerResponse(()), response)
 
856
        self.assertEqual(None, branch.get_parent())
 
857
 
 
858
    def test_set_parent_something(self):
 
859
        branch = self.make_branch('base', format="1.9")
 
860
        request = smart.branch.SmartServerBranchRequestSetParentLocation(
 
861
            self.get_transport())
 
862
        branch_token = branch.lock_write()
 
863
        repo_token = branch.repository.lock_write()
 
864
        try:
 
865
            response = request.execute('base', branch_token, repo_token,
 
866
            'http://bar/')
 
867
        finally:
 
868
            branch.repository.unlock()
 
869
            branch.unlock()
 
870
        self.assertEqual(SuccessfulSmartServerResponse(()), response)
 
871
        self.assertEqual('http://bar/', branch.get_parent())
 
872
 
 
873
 
 
874
class TestSmartServerBranchRequestGetTagsBytes(tests.TestCaseWithMemoryTransport):
 
875
# Only called when the branch format and tags match [yay factory
 
876
# methods] so only need to test straight forward cases.
 
877
 
 
878
    def test_get_bytes(self):
 
879
        base_branch = self.make_branch('base')
 
880
        request = smart.branch.SmartServerBranchGetTagsBytes(
 
881
            self.get_transport())
 
882
        response = request.execute('base')
 
883
        self.assertEquals(
 
884
            SuccessfulSmartServerResponse(('',)), response)
 
885
 
 
886
 
 
887
class TestSmartServerBranchRequestGetStackedOnURL(tests.TestCaseWithMemoryTransport):
 
888
 
 
889
    def test_get_stacked_on_url(self):
 
890
        base_branch = self.make_branch('base', format='1.6')
 
891
        stacked_branch = self.make_branch('stacked', format='1.6')
 
892
        # typically should be relative
 
893
        stacked_branch.set_stacked_on_url('../base')
 
894
        request = smart.branch.SmartServerBranchRequestGetStackedOnURL(
 
895
            self.get_transport())
 
896
        response = request.execute('stacked')
 
897
        self.assertEquals(
 
898
            SmartServerResponse(('ok', '../base')),
 
899
            response)
 
900
 
 
901
 
 
902
class TestSmartServerBranchRequestLockWrite(tests.TestCaseWithMemoryTransport):
 
903
 
 
904
    def setUp(self):
 
905
        tests.TestCaseWithMemoryTransport.setUp(self)
370
906
 
371
907
    def test_lock_write_on_unlocked_branch(self):
372
908
        backing = self.get_transport()
373
909
        request = smart.branch.SmartServerBranchRequestLockWrite(backing)
374
 
        branch = self.make_branch('.')
 
910
        branch = self.make_branch('.', format='knit')
375
911
        repository = branch.repository
376
 
        response = request.execute(backing.local_abspath(''))
 
912
        response = request.execute('')
377
913
        branch_nonce = branch.control_files._lock.peek().get('nonce')
378
914
        repository_nonce = repository.control_files._lock.peek().get('nonce')
379
915
        self.assertEqual(
383
919
        # with a new branch object.
384
920
        new_branch = repository.bzrdir.open_branch()
385
921
        self.assertRaises(errors.LockContention, new_branch.lock_write)
 
922
        # Cleanup
 
923
        request = smart.branch.SmartServerBranchRequestUnlock(backing)
 
924
        response = request.execute('', branch_nonce, repository_nonce)
386
925
 
387
926
    def test_lock_write_on_locked_branch(self):
388
927
        backing = self.get_transport()
389
928
        request = smart.branch.SmartServerBranchRequestLockWrite(backing)
390
929
        branch = self.make_branch('.')
391
 
        branch.lock_write()
 
930
        branch_token = branch.lock_write()
392
931
        branch.leave_lock_in_place()
393
932
        branch.unlock()
394
 
        response = request.execute(backing.local_abspath(''))
 
933
        response = request.execute('')
395
934
        self.assertEqual(
396
935
            SmartServerResponse(('LockContention',)), response)
 
936
        # Cleanup
 
937
        branch.lock_write(branch_token)
 
938
        branch.dont_leave_lock_in_place()
 
939
        branch.unlock()
397
940
 
398
941
    def test_lock_write_with_tokens_on_locked_branch(self):
399
942
        backing = self.get_transport()
400
943
        request = smart.branch.SmartServerBranchRequestLockWrite(backing)
401
 
        branch = self.make_branch('.')
 
944
        branch = self.make_branch('.', format='knit')
402
945
        branch_token = branch.lock_write()
403
946
        repo_token = branch.repository.lock_write()
404
947
        branch.repository.unlock()
405
948
        branch.leave_lock_in_place()
406
949
        branch.repository.leave_lock_in_place()
407
950
        branch.unlock()
408
 
        response = request.execute(backing.local_abspath(''),
 
951
        response = request.execute('',
409
952
                                   branch_token, repo_token)
410
953
        self.assertEqual(
411
954
            SmartServerResponse(('ok', branch_token, repo_token)), response)
 
955
        # Cleanup
 
956
        branch.repository.lock_write(repo_token)
 
957
        branch.repository.dont_leave_lock_in_place()
 
958
        branch.repository.unlock()
 
959
        branch.lock_write(branch_token)
 
960
        branch.dont_leave_lock_in_place()
 
961
        branch.unlock()
412
962
 
413
963
    def test_lock_write_with_mismatched_tokens_on_locked_branch(self):
414
964
        backing = self.get_transport()
415
965
        request = smart.branch.SmartServerBranchRequestLockWrite(backing)
416
 
        branch = self.make_branch('.')
 
966
        branch = self.make_branch('.', format='knit')
417
967
        branch_token = branch.lock_write()
418
968
        repo_token = branch.repository.lock_write()
419
969
        branch.repository.unlock()
420
970
        branch.leave_lock_in_place()
421
971
        branch.repository.leave_lock_in_place()
422
972
        branch.unlock()
423
 
        response = request.execute(backing.local_abspath(''),
 
973
        response = request.execute('',
424
974
                                   branch_token+'xxx', repo_token)
425
975
        self.assertEqual(
426
976
            SmartServerResponse(('TokenMismatch',)), response)
 
977
        # Cleanup
 
978
        branch.repository.lock_write(repo_token)
 
979
        branch.repository.dont_leave_lock_in_place()
 
980
        branch.repository.unlock()
 
981
        branch.lock_write(branch_token)
 
982
        branch.dont_leave_lock_in_place()
 
983
        branch.unlock()
427
984
 
428
985
    def test_lock_write_on_locked_repo(self):
429
986
        backing = self.get_transport()
430
987
        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(''))
 
988
        branch = self.make_branch('.', format='knit')
 
989
        repo = branch.repository
 
990
        repo_token = repo.lock_write()
 
991
        repo.leave_lock_in_place()
 
992
        repo.unlock()
 
993
        response = request.execute('')
436
994
        self.assertEqual(
437
995
            SmartServerResponse(('LockContention',)), response)
 
996
        # Cleanup
 
997
        repo.lock_write(repo_token)
 
998
        repo.dont_leave_lock_in_place()
 
999
        repo.unlock()
438
1000
 
439
1001
    def test_lock_write_on_readonly_transport(self):
440
1002
        backing = self.get_readonly_transport()
441
1003
        request = smart.branch.SmartServerBranchRequestLockWrite(backing)
442
1004
        branch = self.make_branch('.')
443
 
        response = request.execute('')
444
 
        self.assertEqual(
445
 
            SmartServerResponse(('UnlockableTransport',)), response)
446
 
 
447
 
 
448
 
class TestSmartServerBranchRequestUnlock(tests.TestCaseWithTransport):
 
1005
        root = self.get_transport().clone('/')
 
1006
        path = urlutils.relative_url(root.base, self.get_transport().base)
 
1007
        response = request.execute(path)
 
1008
        error_name, lock_str, why_str = response.args
 
1009
        self.assertFalse(response.is_successful())
 
1010
        self.assertEqual('LockFailed', error_name)
 
1011
 
 
1012
 
 
1013
class TestSmartServerBranchRequestUnlock(tests.TestCaseWithMemoryTransport):
449
1014
 
450
1015
    def setUp(self):
451
 
        tests.TestCaseWithTransport.setUp(self)
452
 
        self.reduceLockdirTimeout()
 
1016
        tests.TestCaseWithMemoryTransport.setUp(self)
453
1017
 
454
1018
    def test_unlock_on_locked_branch_and_repo(self):
455
1019
        backing = self.get_transport()
456
1020
        request = smart.branch.SmartServerBranchRequestUnlock(backing)
457
 
        branch = self.make_branch('.')
 
1021
        branch = self.make_branch('.', format='knit')
458
1022
        # Lock the branch
459
1023
        branch_token = branch.lock_write()
460
1024
        repo_token = branch.repository.lock_write()
464
1028
        branch.leave_lock_in_place()
465
1029
        branch.repository.leave_lock_in_place()
466
1030
        branch.unlock()
467
 
        response = request.execute(backing.local_abspath(''),
 
1031
        response = request.execute('',
468
1032
                                   branch_token, repo_token)
469
1033
        self.assertEqual(
470
1034
            SmartServerResponse(('ok',)), response)
477
1041
    def test_unlock_on_unlocked_branch_unlocked_repo(self):
478
1042
        backing = self.get_transport()
479
1043
        request = smart.branch.SmartServerBranchRequestUnlock(backing)
480
 
        branch = self.make_branch('.')
 
1044
        branch = self.make_branch('.', format='knit')
481
1045
        response = request.execute(
482
 
            backing.local_abspath(''), 'branch token', 'repo token')
 
1046
            '', 'branch token', 'repo token')
483
1047
        self.assertEqual(
484
1048
            SmartServerResponse(('TokenMismatch',)), response)
485
1049
 
486
1050
    def test_unlock_on_unlocked_branch_locked_repo(self):
487
1051
        backing = self.get_transport()
488
1052
        request = smart.branch.SmartServerBranchRequestUnlock(backing)
489
 
        branch = self.make_branch('.')
 
1053
        branch = self.make_branch('.', format='knit')
490
1054
        # Lock the repository.
491
1055
        repo_token = branch.repository.lock_write()
492
1056
        branch.repository.leave_lock_in_place()
494
1058
        # Issue branch lock_write request on the unlocked branch (with locked
495
1059
        # repo).
496
1060
        response = request.execute(
497
 
            backing.local_abspath(''), 'branch token', repo_token)
 
1061
            '', 'branch token', repo_token)
498
1062
        self.assertEqual(
499
1063
            SmartServerResponse(('TokenMismatch',)), response)
500
 
 
501
 
 
502
 
class TestSmartServerRepositoryRequest(tests.TestCaseWithTransport):
 
1064
        # Cleanup
 
1065
        branch.repository.lock_write(repo_token)
 
1066
        branch.repository.dont_leave_lock_in_place()
 
1067
        branch.repository.unlock()
 
1068
 
 
1069
 
 
1070
class TestSmartServerRepositoryRequest(tests.TestCaseWithMemoryTransport):
503
1071
 
504
1072
    def test_no_repository(self):
505
1073
        """Raise NoRepositoryPresent when there is a bzrdir and no repo."""
512
1080
        self.make_repository('.', shared=True)
513
1081
        self.make_bzrdir('subdir')
514
1082
        self.assertRaises(errors.NoRepositoryPresent,
515
 
            request.execute, backing.local_abspath('subdir'))
516
 
 
517
 
 
518
 
class TestSmartServerRepositoryGetRevisionGraph(tests.TestCaseWithTransport):
 
1083
            request.execute, 'subdir')
 
1084
 
 
1085
 
 
1086
class TestSmartServerRepositoryGetParentMap(tests.TestCaseWithMemoryTransport):
 
1087
 
 
1088
    def test_trivial_bzipped(self):
 
1089
        # This tests that the wire encoding is actually bzipped
 
1090
        backing = self.get_transport()
 
1091
        request = smart.repository.SmartServerRepositoryGetParentMap(backing)
 
1092
        tree = self.make_branch_and_memory_tree('.')
 
1093
 
 
1094
        self.assertEqual(None,
 
1095
            request.execute('', 'missing-id'))
 
1096
        # Note that it returns a body that is bzipped.
 
1097
        self.assertEqual(
 
1098
            SuccessfulSmartServerResponse(('ok', ), bz2.compress('')),
 
1099
            request.do_body('\n\n0\n'))
 
1100
 
 
1101
    def test_trivial_include_missing(self):
 
1102
        backing = self.get_transport()
 
1103
        request = smart.repository.SmartServerRepositoryGetParentMap(backing)
 
1104
        tree = self.make_branch_and_memory_tree('.')
 
1105
 
 
1106
        self.assertEqual(None,
 
1107
            request.execute('', 'missing-id', 'include-missing:'))
 
1108
        self.assertEqual(
 
1109
            SuccessfulSmartServerResponse(('ok', ),
 
1110
                bz2.compress('missing:missing-id')),
 
1111
            request.do_body('\n\n0\n'))
 
1112
 
 
1113
 
 
1114
class TestSmartServerRepositoryGetRevisionGraph(tests.TestCaseWithMemoryTransport):
519
1115
 
520
1116
    def test_none_argument(self):
521
1117
        backing = self.get_transport()
530
1126
        # the lines of revision_id->revision_parent_list has no guaranteed
531
1127
        # order coming out of a dict, so sort both our test and response
532
1128
        lines = sorted([' '.join([r2, r1]), r1])
533
 
        response = request.execute(backing.local_abspath(''), '')
 
1129
        response = request.execute('', '')
534
1130
        response.body = '\n'.join(sorted(response.body.split('\n')))
535
1131
 
536
1132
        self.assertEqual(
548
1144
        tree.unlock()
549
1145
 
550
1146
        self.assertEqual(SmartServerResponse(('ok', ), rev_id_utf8),
551
 
            request.execute(backing.local_abspath(''), rev_id_utf8))
552
 
    
 
1147
            request.execute('', rev_id_utf8))
 
1148
 
553
1149
    def test_no_such_revision(self):
554
1150
        backing = self.get_transport()
555
1151
        request = smart.repository.SmartServerRepositoryGetRevisionGraph(backing)
562
1158
        # Note that it still returns body (of zero bytes).
563
1159
        self.assertEqual(
564
1160
            SmartServerResponse(('nosuchrevision', 'missingrevision', ), ''),
565
 
            request.execute(backing.local_abspath(''), 'missingrevision'))
566
 
 
567
 
 
568
 
class TestSmartServerRequestHasRevision(tests.TestCaseWithTransport):
 
1161
            request.execute('', 'missingrevision'))
 
1162
 
 
1163
 
 
1164
class TestSmartServerRepositoryGetRevIdForRevno(tests.TestCaseWithMemoryTransport):
 
1165
 
 
1166
    def test_revno_found(self):
 
1167
        backing = self.get_transport()
 
1168
        request = smart.repository.SmartServerRepositoryGetRevIdForRevno(backing)
 
1169
        tree = self.make_branch_and_memory_tree('.')
 
1170
        tree.lock_write()
 
1171
        tree.add('')
 
1172
        rev1_id_utf8 = u'\xc8'.encode('utf-8')
 
1173
        rev2_id_utf8 = u'\xc9'.encode('utf-8')
 
1174
        tree.commit('1st commit', rev_id=rev1_id_utf8)
 
1175
        tree.commit('2nd commit', rev_id=rev2_id_utf8)
 
1176
        tree.unlock()
 
1177
 
 
1178
        self.assertEqual(SmartServerResponse(('ok', rev1_id_utf8)),
 
1179
            request.execute('', 1, (2, rev2_id_utf8)))
 
1180
 
 
1181
    def test_known_revid_missing(self):
 
1182
        backing = self.get_transport()
 
1183
        request = smart.repository.SmartServerRepositoryGetRevIdForRevno(backing)
 
1184
        repo = self.make_repository('.')
 
1185
        self.assertEqual(
 
1186
            FailedSmartServerResponse(('nosuchrevision', 'ghost')),
 
1187
            request.execute('', 1, (2, 'ghost')))
 
1188
 
 
1189
    def test_history_incomplete(self):
 
1190
        backing = self.get_transport()
 
1191
        request = smart.repository.SmartServerRepositoryGetRevIdForRevno(backing)
 
1192
        parent = self.make_branch_and_memory_tree('parent', format='1.9')
 
1193
        r1 = parent.commit(message='first commit')
 
1194
        r2 = parent.commit(message='second commit')
 
1195
        local = self.make_branch_and_memory_tree('local', format='1.9')
 
1196
        local.branch.pull(parent.branch)
 
1197
        local.set_parent_ids([r2])
 
1198
        r3 = local.commit(message='local commit')
 
1199
        local.branch.create_clone_on_transport(
 
1200
            self.get_transport('stacked'), stacked_on=self.get_url('parent'))
 
1201
        self.assertEqual(
 
1202
            SmartServerResponse(('history-incomplete', 2, r2)),
 
1203
            request.execute('stacked', 1, (3, r3)))
 
1204
 
 
1205
class TestSmartServerRepositoryGetStream(tests.TestCaseWithMemoryTransport):
 
1206
 
 
1207
    def make_two_commit_repo(self):
 
1208
        tree = self.make_branch_and_memory_tree('.')
 
1209
        tree.lock_write()
 
1210
        tree.add('')
 
1211
        r1 = tree.commit('1st commit')
 
1212
        r2 = tree.commit('2nd commit', rev_id=u'\xc8'.encode('utf-8'))
 
1213
        tree.unlock()
 
1214
        repo = tree.branch.repository
 
1215
        return repo, r1, r2
 
1216
 
 
1217
    def test_ancestry_of(self):
 
1218
        """The search argument may be a 'ancestry-of' some heads'."""
 
1219
        backing = self.get_transport()
 
1220
        request = smart.repository.SmartServerRepositoryGetStream(backing)
 
1221
        repo, r1, r2 = self.make_two_commit_repo()
 
1222
        fetch_spec = ['ancestry-of', r2]
 
1223
        lines = '\n'.join(fetch_spec)
 
1224
        request.execute('', repo._format.network_name())
 
1225
        response = request.do_body(lines)
 
1226
        self.assertEqual(('ok',), response.args)
 
1227
        stream_bytes = ''.join(response.body_stream)
 
1228
        self.assertStartsWith(stream_bytes, 'Bazaar pack format 1')
 
1229
 
 
1230
    def test_search(self):
 
1231
        """The search argument may be a 'search' of some explicit keys."""
 
1232
        backing = self.get_transport()
 
1233
        request = smart.repository.SmartServerRepositoryGetStream(backing)
 
1234
        repo, r1, r2 = self.make_two_commit_repo()
 
1235
        fetch_spec = ['search', '%s %s' % (r1, r2), 'null:', '2']
 
1236
        lines = '\n'.join(fetch_spec)
 
1237
        request.execute('', repo._format.network_name())
 
1238
        response = request.do_body(lines)
 
1239
        self.assertEqual(('ok',), response.args)
 
1240
        stream_bytes = ''.join(response.body_stream)
 
1241
        self.assertStartsWith(stream_bytes, 'Bazaar pack format 1')
 
1242
 
 
1243
 
 
1244
class TestSmartServerRequestHasRevision(tests.TestCaseWithMemoryTransport):
569
1245
 
570
1246
    def test_missing_revision(self):
571
1247
        """For a missing revision, ('no', ) is returned."""
573
1249
        request = smart.repository.SmartServerRequestHasRevision(backing)
574
1250
        self.make_repository('.')
575
1251
        self.assertEqual(SmartServerResponse(('no', )),
576
 
            request.execute(backing.local_abspath(''), 'revid'))
 
1252
            request.execute('', 'revid'))
577
1253
 
578
1254
    def test_present_revision(self):
579
1255
        """For a present revision, ('yes', ) is returned."""
587
1263
        tree.unlock()
588
1264
        self.assertTrue(tree.branch.repository.has_revision(rev_id_utf8))
589
1265
        self.assertEqual(SmartServerResponse(('yes', )),
590
 
            request.execute(backing.local_abspath(''), rev_id_utf8))
591
 
 
592
 
 
593
 
class TestSmartServerRepositoryGatherStats(tests.TestCaseWithTransport):
 
1266
            request.execute('', rev_id_utf8))
 
1267
 
 
1268
 
 
1269
class TestSmartServerRepositoryGatherStats(tests.TestCaseWithMemoryTransport):
594
1270
 
595
1271
    def test_empty_revid(self):
596
1272
        """With an empty revid, we get only size an number and revisions"""
598
1274
        request = smart.repository.SmartServerRepositoryGatherStats(backing)
599
1275
        repository = self.make_repository('.')
600
1276
        stats = repository.gather_stats()
601
 
        size = stats['size']
602
 
        expected_body = 'revisions: 0\nsize: %d\n' % size
 
1277
        expected_body = 'revisions: 0\n'
603
1278
        self.assertEqual(SmartServerResponse(('ok', ), expected_body),
604
 
                         request.execute(backing.local_abspath(''), '', 'no'))
 
1279
                         request.execute('', '', 'no'))
605
1280
 
606
1281
    def test_revid_with_committers(self):
607
1282
        """For a revid we get more infos."""
618
1293
        tree.unlock()
619
1294
 
620
1295
        stats = tree.branch.repository.gather_stats()
621
 
        size = stats['size']
622
1296
        expected_body = ('firstrev: 123456.200 3600\n'
623
1297
                         'latestrev: 654321.400 0\n'
624
 
                         'revisions: 2\n'
625
 
                         'size: %d\n' % size)
 
1298
                         'revisions: 2\n')
626
1299
        self.assertEqual(SmartServerResponse(('ok', ), expected_body),
627
 
                         request.execute(backing.local_abspath(''),
 
1300
                         request.execute('',
628
1301
                                         rev_id_utf8, 'no'))
629
1302
 
630
1303
    def test_not_empty_repository_with_committers(self):
643
1316
        tree.unlock()
644
1317
        stats = tree.branch.repository.gather_stats()
645
1318
 
646
 
        size = stats['size']
647
1319
        expected_body = ('committers: 2\n'
648
1320
                         'firstrev: 123456.200 3600\n'
649
1321
                         'latestrev: 654321.400 0\n'
650
 
                         'revisions: 2\n'
651
 
                         'size: %d\n' % size)
 
1322
                         'revisions: 2\n')
652
1323
        self.assertEqual(SmartServerResponse(('ok', ), expected_body),
653
 
                         request.execute(backing.local_abspath(''),
 
1324
                         request.execute('',
654
1325
                                         rev_id_utf8, 'yes'))
655
1326
 
656
1327
 
657
 
class TestSmartServerRepositoryIsShared(tests.TestCaseWithTransport):
 
1328
class TestSmartServerRepositoryIsShared(tests.TestCaseWithMemoryTransport):
658
1329
 
659
1330
    def test_is_shared(self):
660
1331
        """For a shared repository, ('yes', ) is returned."""
662
1333
        request = smart.repository.SmartServerRepositoryIsShared(backing)
663
1334
        self.make_repository('.', shared=True)
664
1335
        self.assertEqual(SmartServerResponse(('yes', )),
665
 
            request.execute(backing.local_abspath(''), ))
 
1336
            request.execute('', ))
666
1337
 
667
1338
    def test_is_not_shared(self):
668
1339
        """For a shared repository, ('no', ) is returned."""
670
1341
        request = smart.repository.SmartServerRepositoryIsShared(backing)
671
1342
        self.make_repository('.', shared=False)
672
1343
        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()
 
1344
            request.execute('', ))
 
1345
 
 
1346
 
 
1347
class TestSmartServerRepositoryLockWrite(tests.TestCaseWithMemoryTransport):
681
1348
 
682
1349
    def test_lock_write_on_unlocked_repo(self):
683
1350
        backing = self.get_transport()
684
1351
        request = smart.repository.SmartServerRepositoryLockWrite(backing)
685
 
        repository = self.make_repository('.')
686
 
        response = request.execute(backing.local_abspath(''))
 
1352
        repository = self.make_repository('.', format='knit')
 
1353
        response = request.execute('')
687
1354
        nonce = repository.control_files._lock.peek().get('nonce')
688
1355
        self.assertEqual(SmartServerResponse(('ok', nonce)), response)
689
1356
        # The repository is now locked.  Verify that with a new repository
690
1357
        # object.
691
1358
        new_repo = repository.bzrdir.open_repository()
692
1359
        self.assertRaises(errors.LockContention, new_repo.lock_write)
 
1360
        # Cleanup
 
1361
        request = smart.repository.SmartServerRepositoryUnlock(backing)
 
1362
        response = request.execute('', nonce)
693
1363
 
694
1364
    def test_lock_write_on_locked_repo(self):
695
1365
        backing = self.get_transport()
696
1366
        request = smart.repository.SmartServerRepositoryLockWrite(backing)
697
 
        repository = self.make_repository('.')
698
 
        repository.lock_write()
 
1367
        repository = self.make_repository('.', format='knit')
 
1368
        repo_token = repository.lock_write()
699
1369
        repository.leave_lock_in_place()
700
1370
        repository.unlock()
701
 
        response = request.execute(backing.local_abspath(''))
 
1371
        response = request.execute('')
702
1372
        self.assertEqual(
703
1373
            SmartServerResponse(('LockContention',)), response)
 
1374
        # Cleanup
 
1375
        repository.lock_write(repo_token)
 
1376
        repository.dont_leave_lock_in_place()
 
1377
        repository.unlock()
704
1378
 
705
1379
    def test_lock_write_on_readonly_transport(self):
706
1380
        backing = self.get_readonly_transport()
707
1381
        request = smart.repository.SmartServerRepositoryLockWrite(backing)
 
1382
        repository = self.make_repository('.', format='knit')
 
1383
        response = request.execute('')
 
1384
        self.assertFalse(response.is_successful())
 
1385
        self.assertEqual('LockFailed', response.args[0])
 
1386
 
 
1387
 
 
1388
class TestInsertStreamBase(tests.TestCaseWithMemoryTransport):
 
1389
 
 
1390
    def make_empty_byte_stream(self, repo):
 
1391
        byte_stream = smart.repository._stream_to_byte_stream([], repo._format)
 
1392
        return ''.join(byte_stream)
 
1393
 
 
1394
 
 
1395
class TestSmartServerRepositoryInsertStream(TestInsertStreamBase):
 
1396
 
 
1397
    def test_insert_stream_empty(self):
 
1398
        backing = self.get_transport()
 
1399
        request = smart.repository.SmartServerRepositoryInsertStream(backing)
708
1400
        repository = self.make_repository('.')
709
 
        response = request.execute('')
710
 
        self.assertEqual(
711
 
            SmartServerResponse(('UnlockableTransport',)), response)
712
 
 
713
 
 
714
 
class TestSmartServerRepositoryUnlock(tests.TestCaseWithTransport):
 
1401
        response = request.execute('', '')
 
1402
        self.assertEqual(None, response)
 
1403
        response = request.do_chunk(self.make_empty_byte_stream(repository))
 
1404
        self.assertEqual(None, response)
 
1405
        response = request.do_end()
 
1406
        self.assertEqual(SmartServerResponse(('ok', )), response)
 
1407
        
 
1408
 
 
1409
class TestSmartServerRepositoryInsertStreamLocked(TestInsertStreamBase):
 
1410
 
 
1411
    def test_insert_stream_empty(self):
 
1412
        backing = self.get_transport()
 
1413
        request = smart.repository.SmartServerRepositoryInsertStreamLocked(
 
1414
            backing)
 
1415
        repository = self.make_repository('.', format='knit')
 
1416
        lock_token = repository.lock_write()
 
1417
        response = request.execute('', '', lock_token)
 
1418
        self.assertEqual(None, response)
 
1419
        response = request.do_chunk(self.make_empty_byte_stream(repository))
 
1420
        self.assertEqual(None, response)
 
1421
        response = request.do_end()
 
1422
        self.assertEqual(SmartServerResponse(('ok', )), response)
 
1423
        repository.unlock()
 
1424
 
 
1425
    def test_insert_stream_with_wrong_lock_token(self):
 
1426
        backing = self.get_transport()
 
1427
        request = smart.repository.SmartServerRepositoryInsertStreamLocked(
 
1428
            backing)
 
1429
        repository = self.make_repository('.', format='knit')
 
1430
        lock_token = repository.lock_write()
 
1431
        self.assertRaises(
 
1432
            errors.TokenMismatch, request.execute, '', '', 'wrong-token')
 
1433
        repository.unlock()
 
1434
 
 
1435
 
 
1436
class TestSmartServerRepositoryUnlock(tests.TestCaseWithMemoryTransport):
715
1437
 
716
1438
    def setUp(self):
717
 
        tests.TestCaseWithTransport.setUp(self)
718
 
        self.reduceLockdirTimeout()
 
1439
        tests.TestCaseWithMemoryTransport.setUp(self)
719
1440
 
720
1441
    def test_unlock_on_locked_repo(self):
721
1442
        backing = self.get_transport()
722
1443
        request = smart.repository.SmartServerRepositoryUnlock(backing)
723
 
        repository = self.make_repository('.')
 
1444
        repository = self.make_repository('.', format='knit')
724
1445
        token = repository.lock_write()
725
1446
        repository.leave_lock_in_place()
726
1447
        repository.unlock()
727
 
        response = request.execute(backing.local_abspath(''), token)
 
1448
        response = request.execute('', token)
728
1449
        self.assertEqual(
729
1450
            SmartServerResponse(('ok',)), response)
730
1451
        # The repository is now unlocked.  Verify that with a new repository
736
1457
    def test_unlock_on_unlocked_repo(self):
737
1458
        backing = self.get_transport()
738
1459
        request = smart.repository.SmartServerRepositoryUnlock(backing)
739
 
        repository = self.make_repository('.')
740
 
        response = request.execute(backing.local_abspath(''), 'some token')
 
1460
        repository = self.make_repository('.', format='knit')
 
1461
        response = request.execute('', 'some token')
741
1462
        self.assertEqual(
742
1463
            SmartServerResponse(('TokenMismatch',)), response)
743
1464
 
744
1465
 
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):
 
1466
class TestSmartServerIsReadonly(tests.TestCaseWithMemoryTransport):
770
1467
 
771
1468
    def test_is_readonly_no(self):
772
1469
        backing = self.get_transport()
783
1480
            SmartServerResponse(('yes',)), response)
784
1481
 
785
1482
 
 
1483
class TestSmartServerRepositorySetMakeWorkingTrees(tests.TestCaseWithMemoryTransport):
 
1484
 
 
1485
    def test_set_false(self):
 
1486
        backing = self.get_transport()
 
1487
        repo = self.make_repository('.', shared=True)
 
1488
        repo.set_make_working_trees(True)
 
1489
        request_class = smart.repository.SmartServerRepositorySetMakeWorkingTrees
 
1490
        request = request_class(backing)
 
1491
        self.assertEqual(SuccessfulSmartServerResponse(('ok',)),
 
1492
            request.execute('', 'False'))
 
1493
        repo = repo.bzrdir.open_repository()
 
1494
        self.assertFalse(repo.make_working_trees())
 
1495
 
 
1496
    def test_set_true(self):
 
1497
        backing = self.get_transport()
 
1498
        repo = self.make_repository('.', shared=True)
 
1499
        repo.set_make_working_trees(False)
 
1500
        request_class = smart.repository.SmartServerRepositorySetMakeWorkingTrees
 
1501
        request = request_class(backing)
 
1502
        self.assertEqual(SuccessfulSmartServerResponse(('ok',)),
 
1503
            request.execute('', 'True'))
 
1504
        repo = repo.bzrdir.open_repository()
 
1505
        self.assertTrue(repo.make_working_trees())
 
1506
 
 
1507
 
 
1508
class TestSmartServerPackRepositoryAutopack(tests.TestCaseWithTransport):
 
1509
 
 
1510
    def make_repo_needing_autopacking(self, path='.'):
 
1511
        # Make a repo in need of autopacking.
 
1512
        tree = self.make_branch_and_tree('.', format='pack-0.92')
 
1513
        repo = tree.branch.repository
 
1514
        # monkey-patch the pack collection to disable autopacking
 
1515
        repo._pack_collection._max_pack_count = lambda count: count
 
1516
        for x in range(10):
 
1517
            tree.commit('commit %s' % x)
 
1518
        self.assertEqual(10, len(repo._pack_collection.names()))
 
1519
        del repo._pack_collection._max_pack_count
 
1520
        return repo
 
1521
 
 
1522
    def test_autopack_needed(self):
 
1523
        repo = self.make_repo_needing_autopacking()
 
1524
        repo.lock_write()
 
1525
        self.addCleanup(repo.unlock)
 
1526
        backing = self.get_transport()
 
1527
        request = smart.packrepository.SmartServerPackRepositoryAutopack(
 
1528
            backing)
 
1529
        response = request.execute('')
 
1530
        self.assertEqual(SmartServerResponse(('ok',)), response)
 
1531
        repo._pack_collection.reload_pack_names()
 
1532
        self.assertEqual(1, len(repo._pack_collection.names()))
 
1533
 
 
1534
    def test_autopack_not_needed(self):
 
1535
        tree = self.make_branch_and_tree('.', format='pack-0.92')
 
1536
        repo = tree.branch.repository
 
1537
        repo.lock_write()
 
1538
        self.addCleanup(repo.unlock)
 
1539
        for x in range(9):
 
1540
            tree.commit('commit %s' % x)
 
1541
        backing = self.get_transport()
 
1542
        request = smart.packrepository.SmartServerPackRepositoryAutopack(
 
1543
            backing)
 
1544
        response = request.execute('')
 
1545
        self.assertEqual(SmartServerResponse(('ok',)), response)
 
1546
        repo._pack_collection.reload_pack_names()
 
1547
        self.assertEqual(9, len(repo._pack_collection.names()))
 
1548
 
 
1549
    def test_autopack_on_nonpack_format(self):
 
1550
        """A request to autopack a non-pack repo is a no-op."""
 
1551
        repo = self.make_repository('.', format='knit')
 
1552
        backing = self.get_transport()
 
1553
        request = smart.packrepository.SmartServerPackRepositoryAutopack(
 
1554
            backing)
 
1555
        response = request.execute('')
 
1556
        self.assertEqual(SmartServerResponse(('ok',)), response)
 
1557
 
 
1558
 
786
1559
class TestHandlers(tests.TestCase):
787
1560
    """Tests for the request.request_handlers object."""
788
1561
 
 
1562
    def test_all_registrations_exist(self):
 
1563
        """All registered request_handlers can be found."""
 
1564
        # If there's a typo in a register_lazy call, this loop will fail with
 
1565
        # an AttributeError.
 
1566
        for key, item in smart.request.request_handlers.iteritems():
 
1567
            pass
 
1568
 
 
1569
    def assertHandlerEqual(self, verb, handler):
 
1570
        self.assertEqual(smart.request.request_handlers.get(verb), handler)
 
1571
 
789
1572
    def test_registered_methods(self):
790
1573
        """Test that known methods are registered to the correct object."""
791
 
        self.assertEqual(
792
 
            smart.request.request_handlers.get('Branch.get_config_file'),
 
1574
        self.assertHandlerEqual('Branch.get_config_file',
793
1575
            smart.branch.SmartServerBranchGetConfigFile)
794
 
        self.assertEqual(
795
 
            smart.request.request_handlers.get('Branch.lock_write'),
 
1576
        self.assertHandlerEqual('Branch.get_parent',
 
1577
            smart.branch.SmartServerBranchGetParent)
 
1578
        self.assertHandlerEqual('Branch.get_tags_bytes',
 
1579
            smart.branch.SmartServerBranchGetTagsBytes)
 
1580
        self.assertHandlerEqual('Branch.lock_write',
796
1581
            smart.branch.SmartServerBranchRequestLockWrite)
797
 
        self.assertEqual(
798
 
            smart.request.request_handlers.get('Branch.last_revision_info'),
 
1582
        self.assertHandlerEqual('Branch.last_revision_info',
799
1583
            smart.branch.SmartServerBranchRequestLastRevisionInfo)
800
 
        self.assertEqual(
801
 
            smart.request.request_handlers.get('Branch.revision_history'),
 
1584
        self.assertHandlerEqual('Branch.revision_history',
802
1585
            smart.branch.SmartServerRequestRevisionHistory)
803
 
        self.assertEqual(
804
 
            smart.request.request_handlers.get('Branch.set_last_revision'),
 
1586
        self.assertHandlerEqual('Branch.set_config_option',
 
1587
            smart.branch.SmartServerBranchRequestSetConfigOption)
 
1588
        self.assertHandlerEqual('Branch.set_last_revision',
805
1589
            smart.branch.SmartServerBranchRequestSetLastRevision)
806
 
        self.assertEqual(
807
 
            smart.request.request_handlers.get('Branch.unlock'),
 
1590
        self.assertHandlerEqual('Branch.set_last_revision_info',
 
1591
            smart.branch.SmartServerBranchRequestSetLastRevisionInfo)
 
1592
        self.assertHandlerEqual('Branch.set_last_revision_ex',
 
1593
            smart.branch.SmartServerBranchRequestSetLastRevisionEx)
 
1594
        self.assertHandlerEqual('Branch.set_parent_location',
 
1595
            smart.branch.SmartServerBranchRequestSetParentLocation)
 
1596
        self.assertHandlerEqual('Branch.unlock',
808
1597
            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'),
 
1598
        self.assertHandlerEqual('BzrDir.find_repository',
 
1599
            smart.bzrdir.SmartServerRequestFindRepositoryV1)
 
1600
        self.assertHandlerEqual('BzrDir.find_repositoryV2',
 
1601
            smart.bzrdir.SmartServerRequestFindRepositoryV2)
 
1602
        self.assertHandlerEqual('BzrDirFormat.initialize',
814
1603
            smart.bzrdir.SmartServerRequestInitializeBzrDir)
815
 
        self.assertEqual(
816
 
            smart.request.request_handlers.get('BzrDir.open_branch'),
 
1604
        self.assertHandlerEqual('BzrDirFormat.initialize_ex_1.16',
 
1605
            smart.bzrdir.SmartServerRequestBzrDirInitializeEx)
 
1606
        self.assertHandlerEqual('BzrDir.cloning_metadir',
 
1607
            smart.bzrdir.SmartServerBzrDirRequestCloningMetaDir)
 
1608
        self.assertHandlerEqual('BzrDir.get_config_file',
 
1609
            smart.bzrdir.SmartServerBzrDirRequestConfigFile)
 
1610
        self.assertHandlerEqual('BzrDir.open_branch',
817
1611
            smart.bzrdir.SmartServerRequestOpenBranch)
818
 
        self.assertEqual(
819
 
            smart.request.request_handlers.get('Repository.gather_stats'),
 
1612
        self.assertHandlerEqual('BzrDir.open_branchV2',
 
1613
            smart.bzrdir.SmartServerRequestOpenBranchV2)
 
1614
        self.assertHandlerEqual('PackRepository.autopack',
 
1615
            smart.packrepository.SmartServerPackRepositoryAutopack)
 
1616
        self.assertHandlerEqual('Repository.gather_stats',
820
1617
            smart.repository.SmartServerRepositoryGatherStats)
821
 
        self.assertEqual(
822
 
            smart.request.request_handlers.get('Repository.get_revision_graph'),
 
1618
        self.assertHandlerEqual('Repository.get_parent_map',
 
1619
            smart.repository.SmartServerRepositoryGetParentMap)
 
1620
        self.assertHandlerEqual('Repository.get_rev_id_for_revno',
 
1621
            smart.repository.SmartServerRepositoryGetRevIdForRevno)
 
1622
        self.assertHandlerEqual('Repository.get_revision_graph',
823
1623
            smart.repository.SmartServerRepositoryGetRevisionGraph)
824
 
        self.assertEqual(
825
 
            smart.request.request_handlers.get('Repository.has_revision'),
 
1624
        self.assertHandlerEqual('Repository.get_stream',
 
1625
            smart.repository.SmartServerRepositoryGetStream)
 
1626
        self.assertHandlerEqual('Repository.has_revision',
826
1627
            smart.repository.SmartServerRequestHasRevision)
827
 
        self.assertEqual(
828
 
            smart.request.request_handlers.get('Repository.is_shared'),
 
1628
        self.assertHandlerEqual('Repository.insert_stream',
 
1629
            smart.repository.SmartServerRepositoryInsertStream)
 
1630
        self.assertHandlerEqual('Repository.insert_stream_locked',
 
1631
            smart.repository.SmartServerRepositoryInsertStreamLocked)
 
1632
        self.assertHandlerEqual('Repository.is_shared',
829
1633
            smart.repository.SmartServerRepositoryIsShared)
830
 
        self.assertEqual(
831
 
            smart.request.request_handlers.get('Repository.lock_write'),
 
1634
        self.assertHandlerEqual('Repository.lock_write',
832
1635
            smart.repository.SmartServerRepositoryLockWrite)
833
 
        self.assertEqual(
834
 
            smart.request.request_handlers.get('Repository.unlock'),
 
1636
        self.assertHandlerEqual('Repository.tarball',
 
1637
            smart.repository.SmartServerRepositoryTarball)
 
1638
        self.assertHandlerEqual('Repository.unlock',
835
1639
            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'),
 
1640
        self.assertHandlerEqual('Transport.is_readonly',
841
1641
            smart.request.SmartServerIsReadonly)