Add basic Index object based on libgit2.
[jelmer/dulwich-libgit2.git] / dulwich / tests / compat / test_server.py
1 # test_server.py -- Compatibility tests for 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 # of the License or (at your option) any later version of
8 # the License.
9 #
10 # This program is distributed in the hope that it will be useful,
11 # but WITHOUT ANY WARRANTY; without even the implied warranty of
12 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13 # GNU General Public License for more details.
14 #
15 # You should have received a copy of the GNU General Public License
16 # along with this program; if not, write to the Free Software
17 # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
18 # MA  02110-1301, USA.
19
20 """Compatibility tests between Dulwich and the cgit server.
21
22 Warning: these tests should be fairly stable, but when writing/debugging new
23 tests, deadlocks may freeze the test process such that it cannot be Ctrl-C'ed.
24 On *nix, you can kill the tests with Ctrl-Z, "kill %".
25 """
26
27 import threading
28
29 from dulwich.server import (
30     DictBackend,
31     TCPGitServer,
32     ReceivePackHandler,
33     )
34 from server_utils import (
35     ServerTests,
36     ShutdownServerMixIn,
37     NoSideBand64kReceivePackHandler,
38     )
39 from utils import (
40     CompatTestCase,
41     )
42
43
44 if not getattr(TCPGitServer, 'shutdown', None):
45     _TCPGitServer = TCPGitServer
46
47     class TCPGitServer(ShutdownServerMixIn, TCPGitServer):
48         """Subclass of TCPGitServer that can be shut down."""
49
50         def __init__(self, *args, **kwargs):
51             # BaseServer is old-style so we have to call both __init__s
52             ShutdownServerMixIn.__init__(self)
53             _TCPGitServer.__init__(self, *args, **kwargs)
54
55         serve = ShutdownServerMixIn.serve_forever
56
57
58 class GitServerTestCase(ServerTests, CompatTestCase):
59     """Tests for client/server compatibility.
60
61     This server test case does not use side-band-64k in git-receive-pack.
62     """
63
64     protocol = 'git'
65
66     def setUp(self):
67         ServerTests.setUp(self)
68         CompatTestCase.setUp(self)
69
70     def tearDown(self):
71         ServerTests.tearDown(self)
72         CompatTestCase.tearDown(self)
73
74     def _handlers(self):
75         return {'git-receive-pack': NoSideBand64kReceivePackHandler}
76
77     def _check_server(self, dul_server):
78         receive_pack_handler_cls = dul_server.handlers['git-receive-pack']
79         caps = receive_pack_handler_cls.capabilities()
80         self.assertFalse('side-band-64k' in caps)
81
82     def _start_server(self, repo):
83         backend = DictBackend({'/': repo})
84         dul_server = TCPGitServer(backend, 'localhost', 0,
85                                   handlers=self._handlers())
86         self._check_server(dul_server)
87         threading.Thread(target=dul_server.serve).start()
88         self._server = dul_server
89         _, port = self._server.socket.getsockname()
90         return port
91
92
93 class GitServerSideBand64kTestCase(GitServerTestCase):
94     """Tests for client/server compatibility with side-band-64k support."""
95
96     # side-band-64k in git-receive-pack was introduced in git 1.7.0.2
97     min_git_version = (1, 7, 0, 2)
98
99     def _handlers(self):
100         return None  # default handlers include side-band-64k
101
102     def _check_server(self, server):
103         receive_pack_handler_cls = server.handlers['git-receive-pack']
104         caps = receive_pack_handler_cls.capabilities()
105         self.assertTrue('side-band-64k' in caps)