Use property to access commit parents.
[jelmer/dulwich-libgit2.git] / dulwich / tests / test_server.py
1 # test_server.py -- Tests for the git server
2 # Copyright (C) 2010 Google, Inc.
3 #
4 # This program is free software; you can redistribute it and/or
5 # modify it under the terms of the GNU General Public License
6 # as published by the Free Software Foundation; version 2
7 # or (at your option) any later version of the License.
8 #
9 # This program is distributed in the hope that it will be useful,
10 # but WITHOUT ANY WARRANTY; without even the implied warranty of
11 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12 # GNU General Public License for more details.
13 #
14 # You should have received a copy of the GNU General Public License
15 # along with this program; if not, write to the Free Software
16 # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
17 # MA  02110-1301, USA.
18
19
20 """Tests for the smart protocol server."""
21
22
23 from unittest import TestCase
24
25 from dulwich.errors import (
26     GitProtocolError,
27     )
28 from dulwich.server import (
29     Backend,
30     DictBackend,
31     BackendRepo,
32     Handler,
33     MultiAckGraphWalkerImpl,
34     MultiAckDetailedGraphWalkerImpl,
35     ProtocolGraphWalker,
36     SingleAckGraphWalkerImpl,
37     UploadPackHandler,
38     )
39
40
41 ONE = '1' * 40
42 TWO = '2' * 40
43 THREE = '3' * 40
44 FOUR = '4' * 40
45 FIVE = '5' * 40
46 SIX = '6' * 40
47
48 class TestProto(object):
49
50     def __init__(self):
51         self._output = []
52         self._received = {0: [], 1: [], 2: [], 3: []}
53
54     def set_output(self, output_lines):
55         self._output = ['%s\n' % line.rstrip() for line in output_lines]
56
57     def read_pkt_line(self):
58         if self._output:
59             return self._output.pop(0)
60         else:
61             return None
62
63     def write_sideband(self, band, data):
64         self._received[band].append(data)
65
66     def write_pkt_line(self, data):
67         if data is None:
68             data = 'None'
69         self._received[0].append(data)
70
71     def get_received_line(self, band=0):
72         lines = self._received[band]
73         if lines:
74             return lines.pop(0)
75         else:
76             return None
77
78
79 class HandlerTestCase(TestCase):
80
81     def setUp(self):
82         self._handler = Handler(Backend(), None, None)
83         self._handler.capabilities = lambda: ('cap1', 'cap2', 'cap3')
84         self._handler.required_capabilities = lambda: ('cap2',)
85
86     def assertSucceeds(self, func, *args, **kwargs):
87         try:
88             func(*args, **kwargs)
89         except GitProtocolError, e:
90             self.fail(e)
91
92     def test_capability_line(self):
93         self.assertEquals('cap1 cap2 cap3', self._handler.capability_line())
94
95     def test_set_client_capabilities(self):
96         set_caps = self._handler.set_client_capabilities
97         self.assertSucceeds(set_caps, ['cap2'])
98         self.assertSucceeds(set_caps, ['cap1', 'cap2'])
99
100         # different order
101         self.assertSucceeds(set_caps, ['cap3', 'cap1', 'cap2'])
102
103         # error cases
104         self.assertRaises(GitProtocolError, set_caps, ['capxxx', 'cap2'])
105         self.assertRaises(GitProtocolError, set_caps, ['cap1', 'cap3'])
106
107         # ignore innocuous but unknown capabilities
108         self.assertRaises(GitProtocolError, set_caps, ['cap2', 'ignoreme'])
109         self.assertFalse('ignoreme' in self._handler.capabilities())
110         self._handler.innocuous_capabilities = lambda: ('ignoreme',)
111         self.assertSucceeds(set_caps, ['cap2', 'ignoreme'])
112
113     def test_has_capability(self):
114         self.assertRaises(GitProtocolError, self._handler.has_capability, 'cap')
115         caps = self._handler.capabilities()
116         self._handler.set_client_capabilities(caps)
117         for cap in caps:
118             self.assertTrue(self._handler.has_capability(cap))
119         self.assertFalse(self._handler.has_capability('capxxx'))
120
121
122 class UploadPackHandlerTestCase(TestCase):
123
124     def setUp(self):
125         self._backend = DictBackend({"/": BackendRepo()})
126         self._handler = UploadPackHandler(self._backend,
127                 ["/", "host=lolcathost"], None, None)
128         self._handler.proto = TestProto()
129
130     def test_progress(self):
131         caps = self._handler.required_capabilities()
132         self._handler.set_client_capabilities(caps)
133         self._handler.progress('first message')
134         self._handler.progress('second message')
135         self.assertEqual('first message',
136                          self._handler.proto.get_received_line(2))
137         self.assertEqual('second message',
138                          self._handler.proto.get_received_line(2))
139         self.assertEqual(None, self._handler.proto.get_received_line(2))
140
141     def test_no_progress(self):
142         caps = list(self._handler.required_capabilities()) + ['no-progress']
143         self._handler.set_client_capabilities(caps)
144         self._handler.progress('first message')
145         self._handler.progress('second message')
146         self.assertEqual(None, self._handler.proto.get_received_line(2))
147
148     def test_get_tagged(self):
149         refs = {
150             'refs/tags/tag1': ONE,
151             'refs/tags/tag2': TWO,
152             'refs/heads/master': FOUR,  # not a tag, no peeled value
153             }
154         peeled = {
155             'refs/tags/tag1': '1234',
156             'refs/tags/tag2': '5678',
157             }
158
159         class TestRepo(object):
160             def get_peeled(self, ref):
161                 return peeled.get(ref, refs[ref])
162
163         caps = list(self._handler.required_capabilities()) + ['include-tag']
164         self._handler.set_client_capabilities(caps)
165         self.assertEquals({'1234': ONE, '5678': TWO},
166                           self._handler.get_tagged(refs, repo=TestRepo()))
167
168         # non-include-tag case
169         caps = self._handler.required_capabilities()
170         self._handler.set_client_capabilities(caps)
171         self.assertEquals({}, self._handler.get_tagged(refs, repo=TestRepo()))
172
173
174 class TestCommit(object):
175
176     def __init__(self, sha, parents, commit_time):
177         self.id = sha
178         self.parents = parents
179         self.commit_time = commit_time
180         self.type_name = "commit"
181
182     def __repr__(self):
183         return '%s(%s)' % (self.__class__.__name__, self._sha)
184
185
186 class TestRepo(object):
187     def __init__(self):
188         self.peeled = {}
189
190     def get_peeled(self, name):
191         return self.peeled[name]
192
193
194 class TestBackend(object):
195
196     def __init__(self, repo, objects):
197         self.repo = repo
198         self.object_store = objects
199
200
201 class TestUploadPackHandler(Handler):
202
203     def __init__(self, objects, proto):
204         self.backend = TestBackend(TestRepo(), objects)
205         self.proto = proto
206         self.stateless_rpc = False
207         self.advertise_refs = False
208
209     def capabilities(self):
210         return ('multi_ack',)
211
212
213 class ProtocolGraphWalkerTestCase(TestCase):
214
215     def setUp(self):
216         # Create the following commit tree:
217         #   3---5
218         #  /
219         # 1---2---4
220         self._objects = {
221             ONE: TestCommit(ONE, [], 111),
222             TWO: TestCommit(TWO, [ONE], 222),
223             THREE: TestCommit(THREE, [ONE], 333),
224             FOUR: TestCommit(FOUR, [TWO], 444),
225             FIVE: TestCommit(FIVE, [THREE], 555),
226             }
227
228         self._walker = ProtocolGraphWalker(
229             TestUploadPackHandler(self._objects, TestProto()),
230             self._objects, None)
231
232     def test_is_satisfied_no_haves(self):
233         self.assertFalse(self._walker._is_satisfied([], ONE, 0))
234         self.assertFalse(self._walker._is_satisfied([], TWO, 0))
235         self.assertFalse(self._walker._is_satisfied([], THREE, 0))
236
237     def test_is_satisfied_have_root(self):
238         self.assertTrue(self._walker._is_satisfied([ONE], ONE, 0))
239         self.assertTrue(self._walker._is_satisfied([ONE], TWO, 0))
240         self.assertTrue(self._walker._is_satisfied([ONE], THREE, 0))
241
242     def test_is_satisfied_have_branch(self):
243         self.assertTrue(self._walker._is_satisfied([TWO], TWO, 0))
244         # wrong branch
245         self.assertFalse(self._walker._is_satisfied([TWO], THREE, 0))
246
247     def test_all_wants_satisfied(self):
248         self._walker.set_wants([FOUR, FIVE])
249         # trivial case: wants == haves
250         self.assertTrue(self._walker.all_wants_satisfied([FOUR, FIVE]))
251         # cases that require walking the commit tree
252         self.assertTrue(self._walker.all_wants_satisfied([ONE]))
253         self.assertFalse(self._walker.all_wants_satisfied([TWO]))
254         self.assertFalse(self._walker.all_wants_satisfied([THREE]))
255         self.assertTrue(self._walker.all_wants_satisfied([TWO, THREE]))
256
257     def test_read_proto_line(self):
258         self._walker.proto.set_output([
259             'want %s' % ONE,
260             'want %s' % TWO,
261             'have %s' % THREE,
262             'foo %s' % FOUR,
263             'bar',
264             'done',
265             ])
266         self.assertEquals(('want', ONE), self._walker.read_proto_line())
267         self.assertEquals(('want', TWO), self._walker.read_proto_line())
268         self.assertEquals(('have', THREE), self._walker.read_proto_line())
269         self.assertRaises(GitProtocolError, self._walker.read_proto_line)
270         self.assertRaises(GitProtocolError, self._walker.read_proto_line)
271         self.assertEquals(('done', None), self._walker.read_proto_line())
272         self.assertEquals((None, None), self._walker.read_proto_line())
273
274     def test_determine_wants(self):
275         self.assertRaises(GitProtocolError, self._walker.determine_wants, {})
276
277         self._walker.proto.set_output([
278             'want %s multi_ack' % ONE,
279             'want %s' % TWO,
280             ])
281         heads = {'ref1': ONE, 'ref2': TWO, 'ref3': THREE}
282         self._walker.get_peeled = heads.get
283         self.assertEquals([ONE, TWO], self._walker.determine_wants(heads))
284
285         self._walker.proto.set_output(['want %s multi_ack' % FOUR])
286         self.assertRaises(GitProtocolError, self._walker.determine_wants, heads)
287
288         self._walker.proto.set_output([])
289         self.assertEquals([], self._walker.determine_wants(heads))
290
291         self._walker.proto.set_output(['want %s multi_ack' % ONE, 'foo'])
292         self.assertRaises(GitProtocolError, self._walker.determine_wants, heads)
293
294         self._walker.proto.set_output(['want %s multi_ack' % FOUR])
295         self.assertRaises(GitProtocolError, self._walker.determine_wants, heads)
296
297     def test_determine_wants_advertisement(self):
298         self._walker.proto.set_output([])
299         # advertise branch tips plus tag
300         heads = {'ref4': FOUR, 'ref5': FIVE, 'tag6': SIX}
301         peeled = {'ref4': FOUR, 'ref5': FIVE, 'tag6': FIVE}
302         self._walker.get_peeled = peeled.get
303         self._walker.determine_wants(heads)
304         lines = []
305         while True:
306             line = self._walker.proto.get_received_line()
307             if line == 'None':
308                 break
309             # strip capabilities list if present
310             if '\x00' in line:
311                 line = line[:line.index('\x00')]
312             lines.append(line.rstrip())
313
314         self.assertEquals([
315             '%s ref4' % FOUR,
316             '%s ref5' % FIVE,
317             '%s tag6^{}' % FIVE,
318             '%s tag6' % SIX,
319             ], sorted(lines))
320
321         # ensure peeled tag was advertised immediately following tag
322         for i, line in enumerate(lines):
323             if line.endswith(' tag6'):
324                 self.assertEquals('%s tag6^{}' % FIVE, lines[i+1])
325
326     # TODO: test commit time cutoff
327
328
329 class TestProtocolGraphWalker(object):
330
331     def __init__(self):
332         self.acks = []
333         self.lines = []
334         self.done = False
335         self.stateless_rpc = False
336         self.advertise_refs = False
337
338     def read_proto_line(self):
339         return self.lines.pop(0)
340
341     def send_ack(self, sha, ack_type=''):
342         self.acks.append((sha, ack_type))
343
344     def send_nak(self):
345         self.acks.append((None, 'nak'))
346
347     def all_wants_satisfied(self, haves):
348         return self.done
349
350     def pop_ack(self):
351         if not self.acks:
352             return None
353         return self.acks.pop(0)
354
355
356 class AckGraphWalkerImplTestCase(TestCase):
357     """Base setup and asserts for AckGraphWalker tests."""
358
359     def setUp(self):
360         self._walker = TestProtocolGraphWalker()
361         self._walker.lines = [
362             ('have', TWO),
363             ('have', ONE),
364             ('have', THREE),
365             ('done', None),
366             ]
367         self._impl = self.impl_cls(self._walker)
368
369     def assertNoAck(self):
370         self.assertEquals(None, self._walker.pop_ack())
371
372     def assertAcks(self, acks):
373         for sha, ack_type in acks:
374             self.assertEquals((sha, ack_type), self._walker.pop_ack())
375         self.assertNoAck()
376
377     def assertAck(self, sha, ack_type=''):
378         self.assertAcks([(sha, ack_type)])
379
380     def assertNak(self):
381         self.assertAck(None, 'nak')
382
383     def assertNextEquals(self, sha):
384         self.assertEquals(sha, self._impl.next())
385
386
387 class SingleAckGraphWalkerImplTestCase(AckGraphWalkerImplTestCase):
388
389     impl_cls = SingleAckGraphWalkerImpl
390
391     def test_single_ack(self):
392         self.assertNextEquals(TWO)
393         self.assertNoAck()
394
395         self.assertNextEquals(ONE)
396         self._walker.done = True
397         self._impl.ack(ONE)
398         self.assertAck(ONE)
399
400         self.assertNextEquals(THREE)
401         self._impl.ack(THREE)
402         self.assertNoAck()
403
404         self.assertNextEquals(None)
405         self.assertNoAck()
406
407     def test_single_ack_flush(self):
408         # same as ack test but ends with a flush-pkt instead of done
409         self._walker.lines[-1] = (None, None)
410
411         self.assertNextEquals(TWO)
412         self.assertNoAck()
413
414         self.assertNextEquals(ONE)
415         self._walker.done = True
416         self._impl.ack(ONE)
417         self.assertAck(ONE)
418
419         self.assertNextEquals(THREE)
420         self.assertNoAck()
421
422         self.assertNextEquals(None)
423         self.assertNoAck()
424
425     def test_single_ack_nak(self):
426         self.assertNextEquals(TWO)
427         self.assertNoAck()
428
429         self.assertNextEquals(ONE)
430         self.assertNoAck()
431
432         self.assertNextEquals(THREE)
433         self.assertNoAck()
434
435         self.assertNextEquals(None)
436         self.assertNak()
437
438     def test_single_ack_nak_flush(self):
439         # same as nak test but ends with a flush-pkt instead of done
440         self._walker.lines[-1] = (None, None)
441
442         self.assertNextEquals(TWO)
443         self.assertNoAck()
444
445         self.assertNextEquals(ONE)
446         self.assertNoAck()
447
448         self.assertNextEquals(THREE)
449         self.assertNoAck()
450
451         self.assertNextEquals(None)
452         self.assertNak()
453
454 class MultiAckGraphWalkerImplTestCase(AckGraphWalkerImplTestCase):
455
456     impl_cls = MultiAckGraphWalkerImpl
457
458     def test_multi_ack(self):
459         self.assertNextEquals(TWO)
460         self.assertNoAck()
461
462         self.assertNextEquals(ONE)
463         self._walker.done = True
464         self._impl.ack(ONE)
465         self.assertAck(ONE, 'continue')
466
467         self.assertNextEquals(THREE)
468         self._impl.ack(THREE)
469         self.assertAck(THREE, 'continue')
470
471         self.assertNextEquals(None)
472         self.assertAck(THREE)
473
474     def test_multi_ack_partial(self):
475         self.assertNextEquals(TWO)
476         self.assertNoAck()
477
478         self.assertNextEquals(ONE)
479         self._impl.ack(ONE)
480         self.assertAck(ONE, 'continue')
481
482         self.assertNextEquals(THREE)
483         self.assertNoAck()
484
485         self.assertNextEquals(None)
486         # done, re-send ack of last common
487         self.assertAck(ONE)
488
489     def test_multi_ack_flush(self):
490         self._walker.lines = [
491             ('have', TWO),
492             (None, None),
493             ('have', ONE),
494             ('have', THREE),
495             ('done', None),
496             ]
497         self.assertNextEquals(TWO)
498         self.assertNoAck()
499
500         self.assertNextEquals(ONE)
501         self.assertNak() # nak the flush-pkt
502
503         self._walker.done = True
504         self._impl.ack(ONE)
505         self.assertAck(ONE, 'continue')
506
507         self.assertNextEquals(THREE)
508         self._impl.ack(THREE)
509         self.assertAck(THREE, 'continue')
510
511         self.assertNextEquals(None)
512         self.assertAck(THREE)
513
514     def test_multi_ack_nak(self):
515         self.assertNextEquals(TWO)
516         self.assertNoAck()
517
518         self.assertNextEquals(ONE)
519         self.assertNoAck()
520
521         self.assertNextEquals(THREE)
522         self.assertNoAck()
523
524         self.assertNextEquals(None)
525         self.assertNak()
526
527
528 class MultiAckDetailedGraphWalkerImplTestCase(AckGraphWalkerImplTestCase):
529
530     impl_cls = MultiAckDetailedGraphWalkerImpl
531
532     def test_multi_ack(self):
533         self.assertNextEquals(TWO)
534         self.assertNoAck()
535
536         self.assertNextEquals(ONE)
537         self._walker.done = True
538         self._impl.ack(ONE)
539         self.assertAcks([(ONE, 'common'), (ONE, 'ready')])
540
541         self.assertNextEquals(THREE)
542         self._impl.ack(THREE)
543         self.assertAck(THREE, 'ready')
544
545         self.assertNextEquals(None)
546         self.assertAck(THREE)
547
548     def test_multi_ack_partial(self):
549         self.assertNextEquals(TWO)
550         self.assertNoAck()
551
552         self.assertNextEquals(ONE)
553         self._impl.ack(ONE)
554         self.assertAck(ONE, 'common')
555
556         self.assertNextEquals(THREE)
557         self.assertNoAck()
558
559         self.assertNextEquals(None)
560         # done, re-send ack of last common
561         self.assertAck(ONE)
562
563     def test_multi_ack_flush(self):
564         # same as ack test but contains a flush-pkt in the middle
565         self._walker.lines = [
566             ('have', TWO),
567             (None, None),
568             ('have', ONE),
569             ('have', THREE),
570             ('done', None),
571             ]
572         self.assertNextEquals(TWO)
573         self.assertNoAck()
574
575         self.assertNextEquals(ONE)
576         self.assertNak() # nak the flush-pkt
577
578         self._walker.done = True
579         self._impl.ack(ONE)
580         self.assertAcks([(ONE, 'common'), (ONE, 'ready')])
581
582         self.assertNextEquals(THREE)
583         self._impl.ack(THREE)
584         self.assertAck(THREE, 'ready')
585
586         self.assertNextEquals(None)
587         self.assertAck(THREE)
588
589     def test_multi_ack_nak(self):
590         self.assertNextEquals(TWO)
591         self.assertNoAck()
592
593         self.assertNextEquals(ONE)
594         self.assertNoAck()
595
596         self.assertNextEquals(THREE)
597         self.assertNoAck()
598
599         self.assertNextEquals(None)
600         self.assertNak()
601
602     def test_multi_ack_nak_flush(self):
603         # same as nak test but contains a flush-pkt in the middle
604         self._walker.lines = [
605             ('have', TWO),
606             (None, None),
607             ('have', ONE),
608             ('have', THREE),
609             ('done', None),
610             ]
611         self.assertNextEquals(TWO)
612         self.assertNoAck()
613
614         self.assertNextEquals(ONE)
615         self.assertNak()
616
617         self.assertNextEquals(THREE)
618         self.assertNoAck()
619
620         self.assertNextEquals(None)
621         self.assertNak()
622
623     def test_multi_ack_stateless(self):
624         # transmission ends with a flush-pkt
625         self._walker.lines[-1] = (None, None)
626         self._walker.stateless_rpc = True
627
628         self.assertNextEquals(TWO)
629         self.assertNoAck()
630
631         self.assertNextEquals(ONE)
632         self.assertNoAck()
633
634         self.assertNextEquals(THREE)
635         self.assertNoAck()
636
637         self.assertNextEquals(None)
638         self.assertNak()