test_web.py 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201
  1. # test_web.py -- Compatibility tests for the git web 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. """Compatibility tests between Dulwich and the cgit HTTP server.
  20. warning: these tests should be fairly stable, but when writing/debugging new
  21. tests, deadlocks may freeze the test process such that it cannot be
  22. Ctrl-C'ed. On POSIX systems, you can kill the tests with Ctrl-Z, "kill %".
  23. """
  24. import threading
  25. from wsgiref import simple_server
  26. import sys
  27. from dulwich.server import (
  28. DictBackend,
  29. UploadPackHandler,
  30. ReceivePackHandler,
  31. )
  32. from dulwich.tests import (
  33. SkipTest,
  34. skipIf,
  35. )
  36. from dulwich.web import (
  37. make_wsgi_chain,
  38. HTTPGitApplication,
  39. WSGIRequestHandlerLogger,
  40. WSGIServerLogger,
  41. )
  42. from dulwich.tests.compat.server_utils import (
  43. ServerTests,
  44. NoSideBand64kReceivePackHandler,
  45. )
  46. from dulwich.tests.compat.utils import (
  47. CompatTestCase,
  48. )
  49. @skipIf(sys.platform == 'win32', 'Broken on windows, with very long fail time.')
  50. class WebTests(ServerTests):
  51. """Base tests for web server tests.
  52. Contains utility and setUp/tearDown methods, but does non inherit from
  53. TestCase so tests are not automatically run.
  54. """
  55. protocol = 'http'
  56. def _start_server(self, repo):
  57. backend = DictBackend({'/': repo})
  58. app = self._make_app(backend)
  59. dul_server = simple_server.make_server(
  60. 'localhost', 0, app, server_class=WSGIServerLogger,
  61. handler_class=WSGIRequestHandlerLogger)
  62. self.addCleanup(dul_server.shutdown)
  63. self.addCleanup(dul_server.server_close)
  64. threading.Thread(target=dul_server.serve_forever).start()
  65. self._server = dul_server
  66. _, port = dul_server.socket.getsockname()
  67. return port
  68. @skipIf(sys.platform == 'win32', 'Broken on windows, with very long fail time.')
  69. class SmartWebTestCase(WebTests, CompatTestCase):
  70. """Test cases for smart HTTP server.
  71. This server test case does not use side-band-64k in git-receive-pack.
  72. """
  73. min_git_version = (1, 6, 6)
  74. def _handlers(self):
  75. return {b'git-receive-pack': NoSideBand64kReceivePackHandler}
  76. def _check_app(self, app):
  77. receive_pack_handler_cls = app.handlers[b'git-receive-pack']
  78. caps = receive_pack_handler_cls.capabilities()
  79. self.assertNotIn(b'side-band-64k', caps)
  80. def _make_app(self, backend):
  81. app = make_wsgi_chain(backend, handlers=self._handlers())
  82. to_check = app
  83. # peel back layers until we're at the base application
  84. while not issubclass(to_check.__class__, HTTPGitApplication):
  85. to_check = to_check.app
  86. self._check_app(to_check)
  87. return app
  88. def patch_capabilities(handler, caps_removed):
  89. # Patch a handler's capabilities by specifying a list of them to be
  90. # removed, and return the original classmethod for restoration.
  91. original_capabilities = handler.capabilities
  92. filtered_capabilities = tuple(
  93. i for i in original_capabilities() if i not in caps_removed)
  94. def capabilities(cls):
  95. return filtered_capabilities
  96. handler.capabilities = classmethod(capabilities)
  97. return original_capabilities
  98. @skipIf(sys.platform == 'win32', 'Broken on windows, with very long fail time.')
  99. class SmartWebSideBand64kTestCase(SmartWebTestCase):
  100. """Test cases for smart HTTP server with side-band-64k support."""
  101. # side-band-64k in git-receive-pack was introduced in git 1.7.0.2
  102. min_git_version = (1, 7, 0, 2)
  103. def setUp(self):
  104. self.o_uph_cap = patch_capabilities(UploadPackHandler, (b"no-done",))
  105. self.o_rph_cap = patch_capabilities(ReceivePackHandler, (b"no-done",))
  106. super(SmartWebSideBand64kTestCase, self).setUp()
  107. def tearDown(self):
  108. super(SmartWebSideBand64kTestCase, self).tearDown()
  109. UploadPackHandler.capabilities = self.o_uph_cap
  110. ReceivePackHandler.capabilities = self.o_rph_cap
  111. def _handlers(self):
  112. return None # default handlers include side-band-64k
  113. def _check_app(self, app):
  114. receive_pack_handler_cls = app.handlers[b'git-receive-pack']
  115. caps = receive_pack_handler_cls.capabilities()
  116. self.assertIn(b'side-band-64k', caps)
  117. self.assertNotIn(b'no-done', caps)
  118. class SmartWebSideBand64kNoDoneTestCase(SmartWebTestCase):
  119. """Test cases for smart HTTP server with side-band-64k and no-done
  120. support.
  121. """
  122. # no-done was introduced in git 1.7.4
  123. min_git_version = (1, 7, 4)
  124. def _handlers(self):
  125. return None # default handlers include side-band-64k
  126. def _check_app(self, app):
  127. receive_pack_handler_cls = app.handlers[b'git-receive-pack']
  128. caps = receive_pack_handler_cls.capabilities()
  129. self.assertIn(b'side-band-64k', caps)
  130. self.assertIn(b'no-done', caps)
  131. @skipIf(sys.platform == 'win32', 'Broken on windows, with very long fail time.')
  132. class DumbWebTestCase(WebTests, CompatTestCase):
  133. """Test cases for dumb HTTP server."""
  134. def _make_app(self, backend):
  135. return make_wsgi_chain(backend, dumb=True)
  136. def test_push_to_dulwich(self):
  137. # Note: remove this if dulwich implements dumb web pushing.
  138. raise SkipTest('Dumb web pushing not supported.')
  139. def test_push_to_dulwich_remove_branch(self):
  140. # Note: remove this if dumb pushing is supported
  141. raise SkipTest('Dumb web pushing not supported.')
  142. def test_new_shallow_clone_from_dulwich(self):
  143. # Note: remove this if C git and dulwich implement dumb web shallow
  144. # clones.
  145. raise SkipTest('Dumb web shallow cloning not supported.')
  146. def test_shallow_clone_from_git_is_identical(self):
  147. # Note: remove this if C git and dulwich implement dumb web shallow
  148. # clones.
  149. raise SkipTest('Dumb web shallow cloning not supported.')
  150. def test_fetch_same_depth_into_shallow_clone_from_dulwich(self):
  151. # Note: remove this if C git and dulwich implement dumb web shallow
  152. # clones.
  153. raise SkipTest('Dumb web shallow cloning not supported.')
  154. def test_fetch_full_depth_into_shallow_clone_from_dulwich(self):
  155. # Note: remove this if C git and dulwich implement dumb web shallow
  156. # clones.
  157. raise SkipTest('Dumb web shallow cloning not supported.')
  158. def test_push_to_dulwich_issue_88_standard(self):
  159. raise SkipTest('Dumb web pushing not supported.')