/brz/remove-bazaar

To get this branch, use:
bzr branch http://gegoxaren.bato24.eu/bzr/brz/remove-bazaar
2018.6.1 by Robert Collins
Implement a BzrDir.open_branch smart server method for opening a branch without VFS.
1
# Copyright (C) 2006 Canonical Ltd
2
#
3
# This program is free software; you can redistribute it and/or modify
4
# it under the terms of the GNU General Public License as published by
5
# the Free Software Foundation; either version 2 of the License, or
6
# (at your option) any later version.
7
#
8
# This program is distributed in the hope that it will be useful,
9
# but WITHOUT ANY WARRANTY; without even the implied warranty of
10
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
11
# GNU General Public License for more details.
12
#
13
# You should have received a copy of the GNU General Public License
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
2018.5.42 by Robert Collins
Various hopefully improvements, but wsgi is broken, handing over to spiv :).
19
from bzrlib import bzrdir, errors, smart, tests
2018.6.1 by Robert Collins
Implement a BzrDir.open_branch smart server method for opening a branch without VFS.
20
from bzrlib.smart.request import SmartServerResponse
21
import bzrlib.smart.bzrdir
2018.5.38 by Robert Collins
Implement RemoteBranch.revision_history().
22
import bzrlib.smart.branch
2018.5.40 by Robert Collins
Implement a remote Repository.has_revision method.
23
import bzrlib.smart.repository
2018.6.1 by Robert Collins
Implement a BzrDir.open_branch smart server method for opening a branch without VFS.
24
25
2018.5.59 by Robert Collins
Get BranchConfig working somewhat on RemoteBranches (Robert Collins, Vincent Ladeuil).
26
class TestCaseWithSmartMedium(tests.TestCaseWithTransport):
27
28
    def setUp(self):
29
        super(TestCaseWithSmartMedium, self).setUp()
30
        # We're allowed to set  the transport class here, so that we don't use
31
        # the default or a parameterized class, but rather use the
32
        # TestCaseWithTransport infrastructure to set up a smart server and
33
        # transport.
34
        self.transport_server = smart.server.SmartTCPServer_for_testing
35
36
    def get_smart_medium(self):
37
        """Get a smart medium to use in tests."""
38
        return self.get_transport().get_smart_medium()
39
40
2018.6.1 by Robert Collins
Implement a BzrDir.open_branch smart server method for opening a branch without VFS.
41
class TestSmartServerResponse(tests.TestCase):
42
43
    def test__eq__(self):
44
        self.assertEqual(SmartServerResponse(('ok', )),
45
            SmartServerResponse(('ok', )))
46
        self.assertEqual(SmartServerResponse(('ok', ), 'body'),
47
            SmartServerResponse(('ok', ), 'body'))
48
        self.assertNotEqual(SmartServerResponse(('ok', )),
49
            SmartServerResponse(('notok', )))
50
        self.assertNotEqual(SmartServerResponse(('ok', ), 'body'),
51
            SmartServerResponse(('ok', )))
2018.5.41 by Robert Collins
Fix SmartServerResponse.__eq__ to handle None.
52
        self.assertNotEqual(None,
53
            SmartServerResponse(('ok', )))
2018.6.1 by Robert Collins
Implement a BzrDir.open_branch smart server method for opening a branch without VFS.
54
55
2018.5.34 by Robert Collins
Get test_remote.BasicRemoteObjectTests.test_open_remote_branch passing by implementing a remote method BzrDir.find_repository.
56
class TestSmartServerRequestFindRepository(tests.TestCaseWithTransport):
57
58
    def test_no_repository(self):
59
        """When there is no repository to be found, ('norepository', ) is returned."""
60
        backing = self.get_transport()
61
        request = smart.bzrdir.SmartServerRequestFindRepository(backing)
62
        self.make_bzrdir('.')
63
        self.assertEqual(SmartServerResponse(('norepository', )),
64
            request.execute(backing.local_abspath('')))
65
66
    def test_nonshared_repository(self):
67
        # nonshared repositorys only allow 'find' to return a handle when the 
68
        # path the repository is being searched on is the same as that that 
69
        # the repository is at.
70
        backing = self.get_transport()
71
        request = smart.bzrdir.SmartServerRequestFindRepository(backing)
72
        self.make_repository('.')
73
        self.assertEqual(SmartServerResponse(('ok', '')),
74
            request.execute(backing.local_abspath('')))
75
        self.make_bzrdir('subdir')
76
        self.assertEqual(SmartServerResponse(('norepository', )),
77
            request.execute(backing.local_abspath('subdir')))
78
79
    def test_shared_repository(self):
80
        """When there is a shared repository, we get 'ok', 'relpath-to-repo'."""
81
        backing = self.get_transport()
82
        request = smart.bzrdir.SmartServerRequestFindRepository(backing)
83
        self.make_repository('.', shared=True)
84
        self.assertEqual(SmartServerResponse(('ok', '')),
85
            request.execute(backing.local_abspath('')))
86
        self.make_bzrdir('subdir')
87
        self.assertEqual(SmartServerResponse(('ok', '..')),
88
            request.execute(backing.local_abspath('subdir')))
89
        self.make_bzrdir('subdir/deeper')
90
        self.assertEqual(SmartServerResponse(('ok', '../..')),
91
            request.execute(backing.local_abspath('subdir/deeper')))
92
93
2018.5.42 by Robert Collins
Various hopefully improvements, but wsgi is broken, handing over to spiv :).
94
class TestSmartServerRequestInitializeBzrDir(tests.TestCaseWithTransport):
95
96
    def test_empty_dir(self):
97
        """Initializing an empty dir should succeed and do it."""
98
        backing = self.get_transport()
99
        request = smart.bzrdir.SmartServerRequestInitializeBzrDir(backing)
100
        self.assertEqual(SmartServerResponse(('ok', )),
101
            request.execute(backing.local_abspath('.')))
102
        made_dir = bzrdir.BzrDir.open_from_transport(backing)
103
        # no branch, tree or repository is expected with the current 
104
        # default formart.
105
        self.assertRaises(errors.NoWorkingTree, made_dir.open_workingtree)
106
        self.assertRaises(errors.NotBranchError, made_dir.open_branch)
107
        self.assertRaises(errors.NoRepositoryPresent, made_dir.open_repository)
108
109
    def test_missing_dir(self):
110
        """Initializing a missing directory should fail like the bzrdir api."""
111
        backing = self.get_transport()
112
        request = smart.bzrdir.SmartServerRequestInitializeBzrDir(backing)
113
        self.assertRaises(errors.NoSuchFile,
114
            request.execute, backing.local_abspath('subdir'))
115
116
    def test_initialized_dir(self):
117
        """Initializing an extant bzrdir should fail like the bzrdir api."""
118
        backing = self.get_transport()
119
        request = smart.bzrdir.SmartServerRequestInitializeBzrDir(backing)
120
        self.make_bzrdir('subdir')
121
        self.assertRaises(errors.FileExists,
122
            request.execute, backing.local_abspath('subdir'))
123
124
2018.6.1 by Robert Collins
Implement a BzrDir.open_branch smart server method for opening a branch without VFS.
125
class TestSmartServerRequestOpenBranch(tests.TestCaseWithTransport):
126
127
    def test_no_branch(self):
128
        """When there is no branch, ('nobranch', ) is returned."""
129
        backing = self.get_transport()
130
        request = smart.bzrdir.SmartServerRequestOpenBranch(backing)
131
        self.make_bzrdir('.')
132
        self.assertEqual(SmartServerResponse(('nobranch', )),
133
            request.execute(backing.local_abspath('')))
134
135
    def test_branch(self):
136
        """When there is a branch, 'ok' is returned."""
137
        backing = self.get_transport()
138
        request = smart.bzrdir.SmartServerRequestOpenBranch(backing)
139
        self.make_branch('.')
140
        self.assertEqual(SmartServerResponse(('ok', '')),
141
            request.execute(backing.local_abspath('')))
142
143
    def test_branch_reference(self):
144
        """When there is a branch reference, the reference URL is returned."""
145
        backing = self.get_transport()
146
        request = smart.bzrdir.SmartServerRequestOpenBranch(backing)
147
        branch = self.make_branch('branch')
148
        checkout = branch.create_checkout('reference',lightweight=True)
149
        # TODO: once we have an API to probe for references of any sort, we
150
        # can use it here.
151
        reference_url = backing.abspath('branch') + '/'
152
        self.assertFileEqual(reference_url, 'reference/.bzr/branch/location')
153
        self.assertEqual(SmartServerResponse(('ok', reference_url)),
154
            request.execute(backing.local_abspath('reference')))
155
2018.5.38 by Robert Collins
Implement RemoteBranch.revision_history().
156
157
class TestSmartServerRequestRevisionHistory(tests.TestCaseWithTransport):
158
159
    def test_empty(self):
160
        """For an empty branch, the body is empty."""
161
        backing = self.get_transport()
162
        request = smart.branch.SmartServerRequestRevisionHistory(backing)
163
        self.make_branch('.')
164
        self.assertEqual(SmartServerResponse(('ok', ), ''),
165
            request.execute(backing.local_abspath('')))
166
167
    def test_not_empty(self):
168
        """For a non-empty branch, the body is empty."""
169
        backing = self.get_transport()
170
        request = smart.branch.SmartServerRequestRevisionHistory(backing)
171
        tree = self.make_branch_and_memory_tree('.')
172
        tree.lock_write()
173
        tree.add('')
174
        r1 = tree.commit('1st commit')
175
        r2 = tree.commit('2nd commit', rev_id=u'\xc8')
176
        tree.unlock()
2018.5.83 by Andrew Bennetts
Fix some test failures caused by the switch from unicode to UTF-8-encoded strs for revision IDs.
177
        self.assertEqual(
178
            SmartServerResponse(('ok', ), ('\x00'.join([r1, r2]))),
2018.5.38 by Robert Collins
Implement RemoteBranch.revision_history().
179
            request.execute(backing.local_abspath('')))
180
2018.5.49 by Wouter van Heyst
Refactor SmartServerBranchRequest out from SmartServerRequestRevisionHistory to
181
182
class TestSmartServerBranchRequest(tests.TestCaseWithTransport):
183
184
    def test_no_branch(self):
185
        """When there is a bzrdir and no branch, NotBranchError is raised."""
186
        backing = self.get_transport()
187
        request = smart.branch.SmartServerBranchRequest(backing)
188
        self.make_bzrdir('.')
189
        self.assertRaises(errors.NotBranchError,
190
            request.execute, backing.local_abspath(''))
191
2018.5.38 by Robert Collins
Implement RemoteBranch.revision_history().
192
    def test_branch_reference(self):
193
        """When there is a branch reference, NotBranchError is raised."""
194
        backing = self.get_transport()
2018.5.49 by Wouter van Heyst
Refactor SmartServerBranchRequest out from SmartServerRequestRevisionHistory to
195
        request = smart.branch.SmartServerBranchRequest(backing)
2018.5.38 by Robert Collins
Implement RemoteBranch.revision_history().
196
        branch = self.make_branch('branch')
197
        checkout = branch.create_checkout('reference',lightweight=True)
198
        self.assertRaises(errors.NotBranchError,
199
            request.execute, backing.local_abspath('checkout'))
200
201
2018.5.50 by Wouter van Heyst
Add SmartServerBranchRequestLastRevisionInfo method.
202
class TestSmartServerBranchRequestLastRevisionInfo(tests.TestCaseWithTransport):
203
204
    def test_empty(self):
205
        """For an empty branch, the result is ('ok', '0', '')."""
206
        backing = self.get_transport()
207
        request = smart.branch.SmartServerBranchRequestLastRevisionInfo(backing)
208
        self.make_branch('.')
209
        self.assertEqual(SmartServerResponse(('ok', '0', '')),
210
            request.execute(backing.local_abspath('')))
211
212
    def test_not_empty(self):
213
        """For a non-empty branch, the result is ('ok', 'revno', 'revid')."""
214
        backing = self.get_transport()
215
        request = smart.branch.SmartServerBranchRequestLastRevisionInfo(backing)
216
        tree = self.make_branch_and_memory_tree('.')
217
        tree.lock_write()
218
        tree.add('')
2018.5.83 by Andrew Bennetts
Fix some test failures caused by the switch from unicode to UTF-8-encoded strs for revision IDs.
219
        rev_id = u'\xc8'
220
        rev_id_utf8 = rev_id.encode('utf-8')
2018.5.50 by Wouter van Heyst
Add SmartServerBranchRequestLastRevisionInfo method.
221
        r1 = tree.commit('1st commit')
2018.5.83 by Andrew Bennetts
Fix some test failures caused by the switch from unicode to UTF-8-encoded strs for revision IDs.
222
        r2 = tree.commit('2nd commit', rev_id=rev_id)
2018.5.50 by Wouter van Heyst
Add SmartServerBranchRequestLastRevisionInfo method.
223
        tree.unlock()
224
        self.assertEqual(
2018.5.83 by Andrew Bennetts
Fix some test failures caused by the switch from unicode to UTF-8-encoded strs for revision IDs.
225
            SmartServerResponse(('ok', '2', rev_id_utf8)),
2018.5.50 by Wouter van Heyst
Add SmartServerBranchRequestLastRevisionInfo method.
226
            request.execute(backing.local_abspath('')))
227
228
2018.12.3 by Andrew Bennetts
Add a Branch.set_last_revision smart method, and make RemoteBranch.set_revision_history use it.
229
class TestSmartServerBranchRequestGetConfigFile(tests.TestCaseWithTransport):
2018.5.59 by Robert Collins
Get BranchConfig working somewhat on RemoteBranches (Robert Collins, Vincent Ladeuil).
230
231
    def test_default(self):
232
        """With no file, we get empty content."""
233
        backing = self.get_transport()
234
        request = smart.branch.SmartServerBranchGetConfigFile(backing)
235
        branch = self.make_branch('.')
236
        # there should be no file by default
237
        content = ''
238
        self.assertEqual(SmartServerResponse(('ok', ), content),
239
            request.execute(backing.local_abspath('')))
240
241
    def test_with_content(self):
242
        # SmartServerBranchGetConfigFile should return the content from
243
        # branch.control_files.get('branch.conf') for now - in the future it may
244
        # perform more complex processing. 
245
        backing = self.get_transport()
246
        request = smart.branch.SmartServerBranchGetConfigFile(backing)
247
        branch = self.make_branch('.')
248
        branch.control_files.put_utf8('branch.conf', 'foo bar baz')
249
        self.assertEqual(SmartServerResponse(('ok', ), 'foo bar baz'),
250
            request.execute(backing.local_abspath('')))
251
252
2018.12.3 by Andrew Bennetts
Add a Branch.set_last_revision smart method, and make RemoteBranch.set_revision_history use it.
253
class TestSmartServerBranchRequestSetLastRevision(tests.TestCaseWithTransport):
254
255
    def test_empty(self):
256
        backing = self.get_transport()
257
        request = smart.branch.SmartServerBranchRequestSetLastRevision(backing)
2018.14.1 by Andrew Bennetts
Update to current hpss branch? Fix lots of test failures.
258
        b = self.make_branch('.')
259
        branch_token, repo_token = b.lock_write()
260
        try:
261
            self.assertEqual(SmartServerResponse(('ok',)),
262
                request.execute(
263
                    backing.local_abspath(''), branch_token, repo_token, ''))
264
        finally:
265
            b.unlock()
2018.12.3 by Andrew Bennetts
Add a Branch.set_last_revision smart method, and make RemoteBranch.set_revision_history use it.
266
267
    def test_not_present_revision_id(self):
268
        backing = self.get_transport()
269
        request = smart.branch.SmartServerBranchRequestSetLastRevision(backing)
2018.14.1 by Andrew Bennetts
Update to current hpss branch? Fix lots of test failures.
270
        b = self.make_branch('.')
271
        branch_token, repo_token = b.lock_write()
272
        try:
273
            revision_id = 'non-existent revision'
274
            self.assertEqual(
275
                SmartServerResponse(('NoSuchRevision', revision_id)),
276
                request.execute(
277
                    backing.local_abspath(''), branch_token, repo_token,
278
                    revision_id))
279
        finally:
280
            b.unlock()
2018.12.3 by Andrew Bennetts
Add a Branch.set_last_revision smart method, and make RemoteBranch.set_revision_history use it.
281
282
    def test_revision_id_present(self):
283
        backing = self.get_transport()
284
        request = smart.branch.SmartServerBranchRequestSetLastRevision(backing)
285
        tree = self.make_branch_and_memory_tree('.')
286
        tree.lock_write()
287
        tree.add('')
2018.5.83 by Andrew Bennetts
Fix some test failures caused by the switch from unicode to UTF-8-encoded strs for revision IDs.
288
        rev_id = u'\xc8'
289
        rev_id_utf8 = rev_id.encode('utf-8')
290
        r1 = tree.commit('1st commit', rev_id=rev_id)
2018.12.3 by Andrew Bennetts
Add a Branch.set_last_revision smart method, and make RemoteBranch.set_revision_history use it.
291
        r2 = tree.commit('2nd commit')
292
        tree.unlock()
2018.14.1 by Andrew Bennetts
Update to current hpss branch? Fix lots of test failures.
293
        branch_token, repo_token = tree.branch.lock_write()
294
        try:
295
            self.assertEqual(
296
                SmartServerResponse(('ok',)),
297
                request.execute(
298
                    backing.local_abspath(''), branch_token, repo_token,
2018.5.83 by Andrew Bennetts
Fix some test failures caused by the switch from unicode to UTF-8-encoded strs for revision IDs.
299
                    rev_id_utf8))
300
            self.assertEqual([rev_id_utf8], tree.branch.revision_history())
2018.14.1 by Andrew Bennetts
Update to current hpss branch? Fix lots of test failures.
301
        finally:
302
            tree.branch.unlock()
2018.12.3 by Andrew Bennetts
Add a Branch.set_last_revision smart method, and make RemoteBranch.set_revision_history use it.
303
304
    def test_revision_id_present2(self):
305
        backing = self.get_transport()
306
        request = smart.branch.SmartServerBranchRequestSetLastRevision(backing)
307
        tree = self.make_branch_and_memory_tree('.')
308
        tree.lock_write()
309
        tree.add('')
2018.5.83 by Andrew Bennetts
Fix some test failures caused by the switch from unicode to UTF-8-encoded strs for revision IDs.
310
        rev_id = u'\xc8'
311
        rev_id_utf8 = rev_id.encode('utf-8')
312
        r1 = tree.commit('1st commit', rev_id=rev_id)
2018.12.3 by Andrew Bennetts
Add a Branch.set_last_revision smart method, and make RemoteBranch.set_revision_history use it.
313
        r2 = tree.commit('2nd commit')
314
        tree.unlock()
315
        tree.branch.set_revision_history([])
2018.14.1 by Andrew Bennetts
Update to current hpss branch? Fix lots of test failures.
316
        branch_token, repo_token = tree.branch.lock_write()
317
        try:
318
            self.assertEqual(
319
                SmartServerResponse(('ok',)),
320
                request.execute(
321
                    backing.local_abspath(''), branch_token, repo_token,
2018.5.83 by Andrew Bennetts
Fix some test failures caused by the switch from unicode to UTF-8-encoded strs for revision IDs.
322
                    rev_id_utf8))
323
            self.assertEqual([rev_id_utf8], tree.branch.revision_history())
2018.14.1 by Andrew Bennetts
Update to current hpss branch? Fix lots of test failures.
324
        finally:
325
            tree.branch.unlock()
2018.12.3 by Andrew Bennetts
Add a Branch.set_last_revision smart method, and make RemoteBranch.set_revision_history use it.
326
327
2018.5.79 by Andrew Bennetts
Implement RemoteBranch.lock_write/unlock as smart operations.
328
class TestSmartServerBranchRequestLockWrite(tests.TestCaseWithTransport):
329
330
    def setUp(self):
331
        tests.TestCaseWithTransport.setUp(self)
332
        self.reduceLockdirTimeout()
333
334
    def test_lock_write_on_unlocked_branch(self):
335
        backing = self.get_transport()
336
        request = smart.branch.SmartServerBranchRequestLockWrite(backing)
337
        branch = self.make_branch('.')
338
        repository = branch.repository
339
        response = request.execute(backing.local_abspath(''))
340
        branch_nonce = branch.control_files._lock.peek().get('nonce')
341
        repository_nonce = repository.control_files._lock.peek().get('nonce')
342
        self.assertEqual(
343
            SmartServerResponse(('ok', branch_nonce, repository_nonce)),
344
            response)
345
        # The branch (and associated repository) is now locked.  Verify that
346
        # with a new branch object.
347
        new_branch = repository.bzrdir.open_branch()
348
        self.assertRaises(errors.LockContention, new_branch.lock_write)
349
350
    def test_lock_write_on_locked_branch(self):
351
        backing = self.get_transport()
352
        request = smart.branch.SmartServerBranchRequestLockWrite(backing)
353
        branch = self.make_branch('.')
354
        branch.lock_write()
355
        branch.leave_lock_in_place()
356
        branch.unlock()
357
        response = request.execute(backing.local_abspath(''))
358
        self.assertEqual(
359
            SmartServerResponse(('LockContention',)), response)
360
361
    def test_lock_write_with_tokens_on_locked_branch(self):
362
        backing = self.get_transport()
363
        request = smart.branch.SmartServerBranchRequestLockWrite(backing)
364
        branch = self.make_branch('.')
365
        branch_token, repo_token = branch.lock_write()
366
        branch.leave_lock_in_place()
367
        branch.repository.leave_lock_in_place()
368
        branch.unlock()
369
        response = request.execute(backing.local_abspath(''),
370
                                   branch_token, repo_token)
371
        self.assertEqual(
372
            SmartServerResponse(('ok', branch_token, repo_token)), response)
373
374
    def test_lock_write_with_mismatched_tokens_on_locked_branch(self):
375
        backing = self.get_transport()
376
        request = smart.branch.SmartServerBranchRequestLockWrite(backing)
377
        branch = self.make_branch('.')
378
        branch_token, repo_token = branch.lock_write()
379
        branch.leave_lock_in_place()
380
        branch.repository.leave_lock_in_place()
381
        branch.unlock()
382
        response = request.execute(backing.local_abspath(''),
383
                                   branch_token+'xxx', repo_token)
384
        self.assertEqual(
385
            SmartServerResponse(('TokenMismatch',)), response)
386
387
    def test_lock_write_on_locked_repo(self):
388
        backing = self.get_transport()
389
        request = smart.branch.SmartServerBranchRequestLockWrite(backing)
390
        branch = self.make_branch('.')
391
        branch.repository.lock_write()
392
        branch.repository.leave_lock_in_place()
393
        branch.repository.unlock()
394
        response = request.execute(backing.local_abspath(''))
395
        self.assertEqual(
396
            SmartServerResponse(('LockContention',)), response)
397
2018.5.95 by Andrew Bennetts
Add a Transport.is_readonly remote call, let {Branch,Repository}.lock_write remote call return UnlockableTransport, and miscellaneous test fixes.
398
    def test_lock_write_on_readonly_transport(self):
399
        backing = self.get_readonly_transport()
400
        request = smart.branch.SmartServerBranchRequestLockWrite(backing)
401
        branch = self.make_branch('.')
402
        response = request.execute('')
403
        self.assertEqual(
404
            SmartServerResponse(('UnlockableTransport',)), response)
405
2018.5.79 by Andrew Bennetts
Implement RemoteBranch.lock_write/unlock as smart operations.
406
407
class TestSmartServerBranchRequestUnlock(tests.TestCaseWithTransport):
408
409
    def setUp(self):
410
        tests.TestCaseWithTransport.setUp(self)
411
        self.reduceLockdirTimeout()
412
413
    def test_unlock_on_locked_branch_and_repo(self):
414
        backing = self.get_transport()
415
        request = smart.branch.SmartServerBranchRequestUnlock(backing)
416
        branch = self.make_branch('.')
417
        # Lock the branch
418
        branch_token, repo_token = branch.lock_write()
419
        # Unlock the branch (and repo) object, leaving the physical locks
420
        # in place.
421
        branch.leave_lock_in_place()
422
        branch.repository.leave_lock_in_place()
423
        branch.unlock()
424
        response = request.execute(backing.local_abspath(''),
425
                                   branch_token, repo_token)
426
        self.assertEqual(
427
            SmartServerResponse(('ok',)), response)
428
        # The branch is now unlocked.  Verify that with a new branch
429
        # object.
430
        new_branch = branch.bzrdir.open_branch()
431
        new_branch.lock_write()
432
        new_branch.unlock()
433
434
    def test_unlock_on_unlocked_branch_unlocked_repo(self):
435
        backing = self.get_transport()
436
        request = smart.branch.SmartServerBranchRequestUnlock(backing)
437
        branch = self.make_branch('.')
438
        response = request.execute(
439
            backing.local_abspath(''), 'branch token', 'repo token')
440
        self.assertEqual(
441
            SmartServerResponse(('TokenMismatch',)), response)
442
443
    def test_unlock_on_unlocked_branch_locked_repo(self):
444
        backing = self.get_transport()
445
        request = smart.branch.SmartServerBranchRequestUnlock(backing)
446
        branch = self.make_branch('.')
447
        # Lock the repository.
448
        repo_token = branch.repository.lock_write()
449
        branch.repository.leave_lock_in_place()
450
        branch.repository.unlock()
451
        # Issue branch lock_write request on the unlocked branch (with locked
452
        # repo).
453
        response = request.execute(
454
            backing.local_abspath(''), 'branch token', repo_token)
455
        self.assertEqual(
456
            SmartServerResponse(('TokenMismatch',)), response)
457
458
2018.5.56 by Robert Collins
Factor out code we expect to be common in SmartServerRequestHasRevision to SmartServerRepositoryRequest (Robert Collins, Vincent Ladeuil).
459
class TestSmartServerRepositoryRequest(tests.TestCaseWithTransport):
460
461
    def test_no_repository(self):
462
        """Raise NoRepositoryPresent when there is a bzrdir and no repo."""
463
        # we test this using a shared repository above the named path,
464
        # thus checking the right search logic is used - that is, that
465
        # its the exact path being looked at and the server is not
466
        # searching.
467
        backing = self.get_transport()
2018.5.58 by Wouter van Heyst
Small test fixes to reflect naming and documentation
468
        request = smart.repository.SmartServerRepositoryRequest(backing)
2018.5.56 by Robert Collins
Factor out code we expect to be common in SmartServerRequestHasRevision to SmartServerRepositoryRequest (Robert Collins, Vincent Ladeuil).
469
        self.make_repository('.', shared=True)
470
        self.make_bzrdir('subdir')
471
        self.assertRaises(errors.NoRepositoryPresent,
2018.5.58 by Wouter van Heyst
Small test fixes to reflect naming and documentation
472
            request.execute, backing.local_abspath('subdir'))
2018.5.56 by Robert Collins
Factor out code we expect to be common in SmartServerRequestHasRevision to SmartServerRepositoryRequest (Robert Collins, Vincent Ladeuil).
473
474
2018.5.67 by Wouter van Heyst
Implement RemoteRepository.get_revision_graph (Wouter van Heyst, Robert Collins)
475
class TestSmartServerRepositoryGetRevisionGraph(tests.TestCaseWithTransport):
476
477
    def test_none_argument(self):
478
        backing = self.get_transport()
479
        request = smart.repository.SmartServerRepositoryGetRevisionGraph(backing)
480
        tree = self.make_branch_and_memory_tree('.')
481
        tree.lock_write()
482
        tree.add('')
483
        r1 = tree.commit('1st commit')
484
        r2 = tree.commit('2nd commit', rev_id=u'\xc8')
485
        tree.unlock()
486
487
        # the lines of revision_id->revision_parent_list has no guaranteed
488
        # order coming out of a dict, so sort both our test and response
489
        lines = sorted([' '.join([r2, r1]), r1])
490
        response = request.execute(backing.local_abspath(''), '')
491
        response.body = '\n'.join(sorted(response.body.split('\n')))
492
2018.5.83 by Andrew Bennetts
Fix some test failures caused by the switch from unicode to UTF-8-encoded strs for revision IDs.
493
        self.assertEqual(
494
            SmartServerResponse(('ok', ), '\n'.join(lines)), response)
2018.5.67 by Wouter van Heyst
Implement RemoteRepository.get_revision_graph (Wouter van Heyst, Robert Collins)
495
496
    def test_specific_revision_argument(self):
497
        backing = self.get_transport()
498
        request = smart.repository.SmartServerRepositoryGetRevisionGraph(backing)
499
        tree = self.make_branch_and_memory_tree('.')
500
        tree.lock_write()
501
        tree.add('')
502
        r1 = tree.commit('1st commit', rev_id=u'\xc9')
503
        r2 = tree.commit('2nd commit', rev_id=u'\xc8')
504
        tree.unlock()
505
506
        self.assertEqual(SmartServerResponse(('ok', ),
507
            u'\xc9'.encode('utf8')),
508
            request.execute(backing.local_abspath(''), u'\xc9'.encode('utf8')))
509
    
510
    def test_no_such_revision(self):
511
        backing = self.get_transport()
512
        request = smart.repository.SmartServerRepositoryGetRevisionGraph(backing)
513
        tree = self.make_branch_and_memory_tree('.')
514
        tree.lock_write()
515
        tree.add('')
516
        r1 = tree.commit('1st commit')
517
        tree.unlock()
518
2018.14.1 by Andrew Bennetts
Update to current hpss branch? Fix lots of test failures.
519
        # Note that it still returns body (of zero bytes).
520
        self.assertEqual(
521
            SmartServerResponse(('nosuchrevision', 'missingrevision', ), ''),
2018.5.67 by Wouter van Heyst
Implement RemoteRepository.get_revision_graph (Wouter van Heyst, Robert Collins)
522
            request.execute(backing.local_abspath(''), 'missingrevision'))
523
2018.5.79 by Andrew Bennetts
Implement RemoteBranch.lock_write/unlock as smart operations.
524
2018.5.56 by Robert Collins
Factor out code we expect to be common in SmartServerRequestHasRevision to SmartServerRepositoryRequest (Robert Collins, Vincent Ladeuil).
525
class TestSmartServerRequestHasRevision(tests.TestCaseWithTransport):
526
527
    def test_missing_revision(self):
528
        """For a missing revision, ('no', ) is returned."""
529
        backing = self.get_transport()
530
        request = smart.repository.SmartServerRequestHasRevision(backing)
531
        self.make_repository('.')
532
        self.assertEqual(SmartServerResponse(('no', )),
533
            request.execute(backing.local_abspath(''), 'revid'))
534
535
    def test_present_revision(self):
536
        """For a present revision, ('ok', ) is returned."""
537
        backing = self.get_transport()
538
        request = smart.repository.SmartServerRequestHasRevision(backing)
539
        tree = self.make_branch_and_memory_tree('.')
540
        tree.lock_write()
541
        tree.add('')
542
        r1 = tree.commit('a commit', rev_id=u'\xc8abc')
543
        tree.unlock()
544
        self.assertTrue(tree.branch.repository.has_revision(u'\xc8abc'))
545
        self.assertEqual(SmartServerResponse(('ok', )),
546
            request.execute(backing.local_abspath(''),
547
                u'\xc8abc'.encode('utf8')))
548
549
2018.10.2 by v.ladeuil+lp at free
gather_stats server side and request registration
550
class TestSmartServerRepositoryGatherStats(tests.TestCaseWithTransport):
551
552
    def test_empty_revid(self):
553
        """With an empty revid, we get only size an number and revisions"""
554
        backing = self.get_transport()
555
        request = smart.repository.SmartServerRepositoryGatherStats(backing)
556
        repository = self.make_repository('.')
557
        stats = repository.gather_stats()
558
        size = stats['size']
559
        expected_body = 'revisions: 0\nsize: %d\n' % size
560
        self.assertEqual(SmartServerResponse(('ok', ), expected_body),
561
                         request.execute(backing.local_abspath(''), '', 'no'))
562
563
    def test_revid_with_committers(self):
564
        """For a revid we get more infos."""
565
        backing = self.get_transport()
566
        rev_id = u'\xc8abc'
567
        request = smart.repository.SmartServerRepositoryGatherStats(backing)
568
        tree = self.make_branch_and_memory_tree('.')
569
        tree.lock_write()
570
        tree.add('')
571
        # Let's build a predictable result
572
        tree.commit('a commit', timestamp=123456.2, timezone=3600)
573
        tree.commit('a commit', timestamp=654321.4, timezone=0, rev_id=rev_id)
574
        tree.unlock()
575
576
        stats = tree.branch.repository.gather_stats()
577
        size = stats['size']
578
        expected_body = ('firstrev: 123456.200 3600\n'
579
                         'latestrev: 654321.400 0\n'
580
                         'revisions: 2\n'
581
                         'size: %d\n' % size)
582
        self.assertEqual(SmartServerResponse(('ok', ), expected_body),
583
                         request.execute(backing.local_abspath(''),
584
                                         rev_id.encode('utf8'), 'no'))
585
586
    def test_not_empty_repository_with_committers(self):
587
        """For a revid and requesting committers we get the whole thing."""
588
        backing = self.get_transport()
589
        rev_id = u'\xc8abc'
590
        request = smart.repository.SmartServerRepositoryGatherStats(backing)
591
        tree = self.make_branch_and_memory_tree('.')
592
        tree.lock_write()
593
        tree.add('')
594
        # Let's build a predictable result
595
        tree.commit('a commit', timestamp=123456.2, timezone=3600,
596
                    committer='foo')
597
        tree.commit('a commit', timestamp=654321.4, timezone=0,
598
                    committer='bar', rev_id=rev_id)
599
        tree.unlock()
600
        stats = tree.branch.repository.gather_stats()
601
602
        size = stats['size']
603
        expected_body = ('committers: 2\n'
604
                         'firstrev: 123456.200 3600\n'
605
                         'latestrev: 654321.400 0\n'
606
                         'revisions: 2\n'
607
                         'size: %d\n' % size)
608
        self.assertEqual(SmartServerResponse(('ok', ), expected_body),
609
                         request.execute(backing.local_abspath(''),
610
                                         rev_id.encode('utf8'), 'yes'))
611
612
2018.5.57 by Robert Collins
Implement RemoteRepository.is_shared (Robert Collins, Vincent Ladeuil).
613
class TestSmartServerRepositoryIsShared(tests.TestCaseWithTransport):
614
615
    def test_is_shared(self):
616
        """For a shared repository, ('yes', ) is returned."""
617
        backing = self.get_transport()
618
        request = smart.repository.SmartServerRepositoryIsShared(backing)
619
        self.make_repository('.', shared=True)
620
        self.assertEqual(SmartServerResponse(('yes', )),
621
            request.execute(backing.local_abspath(''), ))
622
623
    def test_is_not_shared(self):
2018.5.58 by Wouter van Heyst
Small test fixes to reflect naming and documentation
624
        """For a shared repository, ('no', ) is returned."""
2018.5.57 by Robert Collins
Implement RemoteRepository.is_shared (Robert Collins, Vincent Ladeuil).
625
        backing = self.get_transport()
626
        request = smart.repository.SmartServerRepositoryIsShared(backing)
627
        self.make_repository('.', shared=False)
628
        self.assertEqual(SmartServerResponse(('no', )),
629
            request.execute(backing.local_abspath(''), ))
630
2018.5.56 by Robert Collins
Factor out code we expect to be common in SmartServerRequestHasRevision to SmartServerRepositoryRequest (Robert Collins, Vincent Ladeuil).
631
2018.5.78 by Andrew Bennetts
Implement RemoteRepository.lock_write/unlock to expect and send tokens over the
632
class TestSmartServerRepositoryLockWrite(tests.TestCaseWithTransport):
633
634
    def setUp(self):
635
        tests.TestCaseWithTransport.setUp(self)
636
        self.reduceLockdirTimeout()
637
638
    def test_lock_write_on_unlocked_repo(self):
639
        backing = self.get_transport()
640
        request = smart.repository.SmartServerRepositoryLockWrite(backing)
641
        repository = self.make_repository('.')
642
        response = request.execute(backing.local_abspath(''))
643
        nonce = repository.control_files._lock.peek().get('nonce')
644
        self.assertEqual(SmartServerResponse(('ok', nonce)), response)
645
        # The repository is now locked.  Verify that with a new repository
646
        # object.
647
        new_repo = repository.bzrdir.open_repository()
648
        self.assertRaises(errors.LockContention, new_repo.lock_write)
649
650
    def test_lock_write_on_locked_repo(self):
651
        backing = self.get_transport()
652
        request = smart.repository.SmartServerRepositoryLockWrite(backing)
653
        repository = self.make_repository('.')
654
        repository.lock_write()
655
        repository.leave_lock_in_place()
656
        repository.unlock()
657
        response = request.execute(backing.local_abspath(''))
658
        self.assertEqual(
659
            SmartServerResponse(('LockContention',)), response)
660
2018.5.95 by Andrew Bennetts
Add a Transport.is_readonly remote call, let {Branch,Repository}.lock_write remote call return UnlockableTransport, and miscellaneous test fixes.
661
    def test_lock_write_on_readonly_transport(self):
662
        backing = self.get_readonly_transport()
663
        request = smart.repository.SmartServerRepositoryLockWrite(backing)
664
        repository = self.make_repository('.')
665
        response = request.execute('')
666
        self.assertEqual(
667
            SmartServerResponse(('UnlockableTransport',)), response)
668
2018.5.78 by Andrew Bennetts
Implement RemoteRepository.lock_write/unlock to expect and send tokens over the
669
670
class TestSmartServerRepositoryUnlock(tests.TestCaseWithTransport):
671
672
    def setUp(self):
673
        tests.TestCaseWithTransport.setUp(self)
674
        self.reduceLockdirTimeout()
675
676
    def test_unlock_on_locked_repo(self):
677
        backing = self.get_transport()
678
        request = smart.repository.SmartServerRepositoryUnlock(backing)
679
        repository = self.make_repository('.')
680
        token = repository.lock_write()
681
        repository.leave_lock_in_place()
682
        repository.unlock()
683
        response = request.execute(backing.local_abspath(''), token)
684
        self.assertEqual(
685
            SmartServerResponse(('ok',)), response)
686
        # The repository is now unlocked.  Verify that with a new repository
687
        # object.
688
        new_repo = repository.bzrdir.open_repository()
689
        new_repo.lock_write()
690
        new_repo.unlock()
691
692
    def test_unlock_on_unlocked_repo(self):
693
        backing = self.get_transport()
694
        request = smart.repository.SmartServerRepositoryUnlock(backing)
695
        repository = self.make_repository('.')
696
        response = request.execute(backing.local_abspath(''), 'some token')
697
        self.assertEqual(
698
            SmartServerResponse(('TokenMismatch',)), response)
699
700
2018.5.95 by Andrew Bennetts
Add a Transport.is_readonly remote call, let {Branch,Repository}.lock_write remote call return UnlockableTransport, and miscellaneous test fixes.
701
class TestSmartServerIsReadonly(tests.TestCaseWithTransport):
702
703
    def test_is_readonly_no(self):
704
        backing = self.get_transport()
705
        request = smart.request.SmartServerIsReadonly(backing)
706
        response = request.execute()
707
        self.assertEqual(
708
            SmartServerResponse(('no',)), response)
709
710
    def test_is_readonly_yes(self):
711
        backing = self.get_readonly_transport()
712
        request = smart.request.SmartServerIsReadonly(backing)
713
        response = request.execute()
714
        self.assertEqual(
715
            SmartServerResponse(('yes',)), response)
716
717
2018.6.1 by Robert Collins
Implement a BzrDir.open_branch smart server method for opening a branch without VFS.
718
class TestHandlers(tests.TestCase):
719
    """Tests for the request.request_handlers object."""
720
721
    def test_registered_methods(self):
722
        """Test that known methods are registered to the correct object."""
723
        self.assertEqual(
2018.5.59 by Robert Collins
Get BranchConfig working somewhat on RemoteBranches (Robert Collins, Vincent Ladeuil).
724
            smart.request.request_handlers.get('Branch.get_config_file'),
725
            smart.branch.SmartServerBranchGetConfigFile)
726
        self.assertEqual(
2018.5.79 by Andrew Bennetts
Implement RemoteBranch.lock_write/unlock as smart operations.
727
            smart.request.request_handlers.get('Branch.lock_write'),
728
            smart.branch.SmartServerBranchRequestLockWrite)
729
        self.assertEqual(
2018.5.51 by Wouter van Heyst
Test and implement RemoteBranch.last_revision_info()
730
            smart.request.request_handlers.get('Branch.last_revision_info'),
731
            smart.branch.SmartServerBranchRequestLastRevisionInfo)
732
        self.assertEqual(
2018.5.38 by Robert Collins
Implement RemoteBranch.revision_history().
733
            smart.request.request_handlers.get('Branch.revision_history'),
734
            smart.branch.SmartServerRequestRevisionHistory)
735
        self.assertEqual(
2018.5.77 by Wouter van Heyst
Fix typo in request_handlers registration of Branch.set_last_revision, and test that registration
736
            smart.request.request_handlers.get('Branch.set_last_revision'),
737
            smart.branch.SmartServerBranchRequestSetLastRevision)
738
        self.assertEqual(
2018.5.79 by Andrew Bennetts
Implement RemoteBranch.lock_write/unlock as smart operations.
739
            smart.request.request_handlers.get('Branch.unlock'),
740
            smart.branch.SmartServerBranchRequestUnlock)
741
        self.assertEqual(
2018.5.34 by Robert Collins
Get test_remote.BasicRemoteObjectTests.test_open_remote_branch passing by implementing a remote method BzrDir.find_repository.
742
            smart.request.request_handlers.get('BzrDir.find_repository'),
743
            smart.bzrdir.SmartServerRequestFindRepository)
744
        self.assertEqual(
2018.5.42 by Robert Collins
Various hopefully improvements, but wsgi is broken, handing over to spiv :).
745
            smart.request.request_handlers.get('BzrDirFormat.initialize'),
746
            smart.bzrdir.SmartServerRequestInitializeBzrDir)
747
        self.assertEqual(
2018.6.1 by Robert Collins
Implement a BzrDir.open_branch smart server method for opening a branch without VFS.
748
            smart.request.request_handlers.get('BzrDir.open_branch'),
749
            smart.bzrdir.SmartServerRequestOpenBranch)
2018.5.40 by Robert Collins
Implement a remote Repository.has_revision method.
750
        self.assertEqual(
2018.10.2 by v.ladeuil+lp at free
gather_stats server side and request registration
751
            smart.request.request_handlers.get('Repository.gather_stats'),
752
            smart.repository.SmartServerRepositoryGatherStats)
753
        self.assertEqual(
2018.5.67 by Wouter van Heyst
Implement RemoteRepository.get_revision_graph (Wouter van Heyst, Robert Collins)
754
            smart.request.request_handlers.get('Repository.get_revision_graph'),
755
            smart.repository.SmartServerRepositoryGetRevisionGraph)
756
        self.assertEqual(
2018.5.40 by Robert Collins
Implement a remote Repository.has_revision method.
757
            smart.request.request_handlers.get('Repository.has_revision'),
758
            smart.repository.SmartServerRequestHasRevision)
2018.5.57 by Robert Collins
Implement RemoteRepository.is_shared (Robert Collins, Vincent Ladeuil).
759
        self.assertEqual(
760
            smart.request.request_handlers.get('Repository.is_shared'),
761
            smart.repository.SmartServerRepositoryIsShared)
2018.5.78 by Andrew Bennetts
Implement RemoteRepository.lock_write/unlock to expect and send tokens over the
762
        self.assertEqual(
763
            smart.request.request_handlers.get('Repository.lock_write'),
764
            smart.repository.SmartServerRepositoryLockWrite)
765
        self.assertEqual(
766
            smart.request.request_handlers.get('Repository.unlock'),
767
            smart.repository.SmartServerRepositoryUnlock)
2018.5.95 by Andrew Bennetts
Add a Transport.is_readonly remote call, let {Branch,Repository}.lock_write remote call return UnlockableTransport, and miscellaneous test fixes.
768
        self.assertEqual(
769
            smart.request.request_handlers.get('Transport.is_readonly'),
770
            smart.request.SmartServerIsReadonly)