server_utils.py 7.3 KB

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