server_utils.py 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214
  1. # server_utils.py -- Git server compatibility utilities
  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. """Utilities for testing git server compatibility."""
  20. import os
  21. import select
  22. import shutil
  23. import socket
  24. import tempfile
  25. import threading
  26. from dulwich.repo import Repo
  27. from dulwich.server import (
  28. ReceivePackHandler,
  29. )
  30. from dulwich.tests.utils import (
  31. tear_down_repo,
  32. )
  33. from dulwich.tests.compat.utils import (
  34. import_repo,
  35. run_git_or_fail,
  36. )
  37. class ServerTests(object):
  38. """Base tests for testing servers.
  39. Does not inherit from TestCase so tests are not automatically run.
  40. """
  41. def setUp(self):
  42. self._old_repo = None
  43. self._new_repo = None
  44. self._server = None
  45. def tearDown(self):
  46. if self._server is not None:
  47. self._server.shutdown()
  48. self._server = None
  49. if self._old_repo is not None:
  50. tear_down_repo(self._old_repo)
  51. if self._new_repo is not None:
  52. tear_down_repo(self._new_repo)
  53. def import_repos(self):
  54. self._old_repo = import_repo('server_old.export')
  55. self._new_repo = import_repo('server_new.export')
  56. def url(self, port):
  57. return '%s://localhost:%s/' % (self.protocol, port)
  58. def branch_args(self, branches=None):
  59. if branches is None:
  60. branches = ['master', 'branch']
  61. return ['%s:%s' % (b, b) for b in branches]
  62. def test_push_to_dulwich(self):
  63. self.import_repos()
  64. self.assertReposNotEqual(self._old_repo, self._new_repo)
  65. port = self._start_server(self._old_repo)
  66. run_git_or_fail(['push', self.url(port)] + self.branch_args(),
  67. cwd=self._new_repo.path)
  68. self.assertReposEqual(self._old_repo, self._new_repo)
  69. def test_fetch_from_dulwich(self):
  70. self.import_repos()
  71. self.assertReposNotEqual(self._old_repo, self._new_repo)
  72. port = self._start_server(self._new_repo)
  73. run_git_or_fail(['fetch', self.url(port)] + self.branch_args(),
  74. cwd=self._old_repo.path)
  75. # flush the pack cache so any new packs are picked up
  76. self._old_repo.object_store._pack_cache = None
  77. self.assertReposEqual(self._old_repo, self._new_repo)
  78. def test_fetch_from_dulwich_no_op(self):
  79. self._old_repo = import_repo('server_old.export')
  80. self._new_repo = import_repo('server_old.export')
  81. self.assertReposEqual(self._old_repo, self._new_repo)
  82. port = self._start_server(self._new_repo)
  83. run_git_or_fail(['fetch', self.url(port)] + self.branch_args(),
  84. cwd=self._old_repo.path)
  85. # flush the pack cache so any new packs are picked up
  86. self._old_repo.object_store._pack_cache = None
  87. self.assertReposEqual(self._old_repo, self._new_repo)
  88. def test_clone_from_dulwich_empty(self):
  89. old_repo_dir = os.path.join(tempfile.mkdtemp(), 'empty_old')
  90. run_git_or_fail(['init', '--quiet', '--bare', old_repo_dir])
  91. self._old_repo = Repo(old_repo_dir)
  92. port = self._start_server(self._old_repo)
  93. new_repo_base_dir = tempfile.mkdtemp()
  94. try:
  95. new_repo_dir = os.path.join(new_repo_base_dir, 'empty_new')
  96. run_git_or_fail(['clone', self.url(port), new_repo_dir],
  97. cwd=new_repo_base_dir)
  98. new_repo = Repo(new_repo_dir)
  99. self.assertReposEqual(self._old_repo, new_repo)
  100. finally:
  101. # We don't create a Repo from new_repo_dir until after some errors
  102. # may have occurred, so don't depend on tearDown to clean it up.
  103. shutil.rmtree(new_repo_base_dir)
  104. class ShutdownServerMixIn:
  105. """Mixin that allows serve_forever to be shut down.
  106. The methods in this mixin are backported from SocketServer.py in the Python
  107. 2.6.4 standard library. The mixin is unnecessary in 2.6 and later, when
  108. BaseServer supports the shutdown method directly.
  109. """
  110. def __init__(self):
  111. self.__is_shut_down = threading.Event()
  112. self.__serving = False
  113. def serve_forever(self, poll_interval=0.5):
  114. """Handle one request at a time until shutdown.
  115. Polls for shutdown every poll_interval seconds. Ignores
  116. self.timeout. If you need to do periodic tasks, do them in
  117. another thread.
  118. """
  119. self.__serving = True
  120. self.__is_shut_down.clear()
  121. while self.__serving:
  122. # XXX: Consider using another file descriptor or
  123. # connecting to the socket to wake this up instead of
  124. # polling. Polling reduces our responsiveness to a
  125. # shutdown request and wastes cpu at all other times.
  126. r, w, e = select.select([self], [], [], poll_interval)
  127. if r:
  128. self._handle_request_noblock()
  129. self.__is_shut_down.set()
  130. serve = serve_forever # override alias from TCPGitServer
  131. def shutdown(self):
  132. """Stops the serve_forever loop.
  133. Blocks until the loop has finished. This must be called while
  134. serve_forever() is running in another thread, or it will deadlock.
  135. """
  136. self.__serving = False
  137. self.__is_shut_down.wait()
  138. def handle_request(self):
  139. """Handle one request, possibly blocking.
  140. Respects self.timeout.
  141. """
  142. # Support people who used socket.settimeout() to escape
  143. # handle_request before self.timeout was available.
  144. timeout = self.socket.gettimeout()
  145. if timeout is None:
  146. timeout = self.timeout
  147. elif self.timeout is not None:
  148. timeout = min(timeout, self.timeout)
  149. fd_sets = select.select([self], [], [], timeout)
  150. if not fd_sets[0]:
  151. self.handle_timeout()
  152. return
  153. self._handle_request_noblock()
  154. def _handle_request_noblock(self):
  155. """Handle one request, without blocking.
  156. I assume that select.select has returned that the socket is
  157. readable before this function was called, so there should be
  158. no risk of blocking in get_request().
  159. """
  160. try:
  161. request, client_address = self.get_request()
  162. except socket.error:
  163. return
  164. if self.verify_request(request, client_address):
  165. try:
  166. self.process_request(request, client_address)
  167. except:
  168. self.handle_error(request, client_address)
  169. self.close_request(request)
  170. # TODO(dborowitz): Come up with a better way of testing various permutations of
  171. # capabilities. The only reason it is the way it is now is that side-band-64k
  172. # was only recently introduced into git-receive-pack.
  173. class NoSideBand64kReceivePackHandler(ReceivePackHandler):
  174. """ReceivePackHandler that does not support side-band-64k."""
  175. @classmethod
  176. def capabilities(cls):
  177. return tuple(c for c in ReceivePackHandler.capabilities()
  178. if c != 'side-band-64k')