test_web.py 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209
  1. # test_web.py -- Compatibility tests for the git web server.
  2. # Copyright (C) 2010 Google, Inc.
  3. #
  4. # Dulwich is dual-licensed under the Apache License, Version 2.0 and the GNU
  5. # General Public License as public by the Free Software Foundation; version 2.0
  6. # or (at your option) any later version. You can redistribute it and/or
  7. # modify it under the terms of either of these two licenses.
  8. #
  9. # Unless required by applicable law or agreed to in writing, software
  10. # distributed under the License is distributed on an "AS IS" BASIS,
  11. # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. # See the License for the specific language governing permissions and
  13. # limitations under the License.
  14. #
  15. # You should have received a copy of the licenses; if not, see
  16. # <http://www.gnu.org/licenses/> for a copy of the GNU General Public License
  17. # and <http://www.apache.org/licenses/LICENSE-2.0> for a copy of the Apache
  18. # License, Version 2.0.
  19. #
  20. """Compatibility tests between Dulwich and the cgit HTTP server.
  21. warning: these tests should be fairly stable, but when writing/debugging new
  22. tests, deadlocks may freeze the test process such that it cannot be
  23. Ctrl-C'ed. On POSIX systems, you can kill the tests with Ctrl-Z, "kill %".
  24. """
  25. import threading
  26. from wsgiref import simple_server
  27. import sys
  28. from typing import Tuple
  29. from dulwich.server import (
  30. DictBackend,
  31. UploadPackHandler,
  32. ReceivePackHandler,
  33. )
  34. from dulwich.tests import (
  35. SkipTest,
  36. skipIf,
  37. )
  38. from dulwich.web import (
  39. make_wsgi_chain,
  40. HTTPGitApplication,
  41. WSGIRequestHandlerLogger,
  42. WSGIServerLogger,
  43. )
  44. from dulwich.tests.compat.server_utils import (
  45. ServerTests,
  46. NoSideBand64kReceivePackHandler,
  47. )
  48. from dulwich.tests.compat.utils import (
  49. CompatTestCase,
  50. )
  51. @skipIf(sys.platform == "win32", "Broken on windows, with very long fail time.")
  52. class WebTests(ServerTests):
  53. """Base tests for web server tests.
  54. Contains utility and setUp/tearDown methods, but does non inherit from
  55. TestCase so tests are not automatically run.
  56. """
  57. protocol = "http"
  58. def _start_server(self, repo):
  59. backend = DictBackend({"/": repo})
  60. app = self._make_app(backend)
  61. dul_server = simple_server.make_server(
  62. "localhost",
  63. 0,
  64. app,
  65. server_class=WSGIServerLogger,
  66. handler_class=WSGIRequestHandlerLogger,
  67. )
  68. self.addCleanup(dul_server.shutdown)
  69. self.addCleanup(dul_server.server_close)
  70. threading.Thread(target=dul_server.serve_forever).start()
  71. self._server = dul_server
  72. _, port = dul_server.socket.getsockname()
  73. return port
  74. @skipIf(sys.platform == "win32", "Broken on windows, with very long fail time.")
  75. class SmartWebTestCase(WebTests, CompatTestCase):
  76. """Test cases for smart HTTP server.
  77. This server test case does not use side-band-64k in git-receive-pack.
  78. """
  79. min_git_version: Tuple[int, ...] = (1, 6, 6)
  80. def _handlers(self):
  81. return {b"git-receive-pack": NoSideBand64kReceivePackHandler}
  82. def _check_app(self, app):
  83. receive_pack_handler_cls = app.handlers[b"git-receive-pack"]
  84. caps = receive_pack_handler_cls.capabilities()
  85. self.assertNotIn(b"side-band-64k", caps)
  86. def _make_app(self, backend):
  87. app = make_wsgi_chain(backend, handlers=self._handlers())
  88. to_check = app
  89. # peel back layers until we're at the base application
  90. while not issubclass(to_check.__class__, HTTPGitApplication):
  91. to_check = to_check.app
  92. self._check_app(to_check)
  93. return app
  94. def patch_capabilities(handler, caps_removed):
  95. # Patch a handler's capabilities by specifying a list of them to be
  96. # removed, and return the original classmethod for restoration.
  97. original_capabilities = handler.capabilities
  98. filtered_capabilities = [
  99. i for i in original_capabilities() if i not in caps_removed
  100. ]
  101. def capabilities(cls):
  102. return filtered_capabilities
  103. handler.capabilities = classmethod(capabilities)
  104. return original_capabilities
  105. @skipIf(sys.platform == "win32", "Broken on windows, with very long fail time.")
  106. class SmartWebSideBand64kTestCase(SmartWebTestCase):
  107. """Test cases for smart HTTP server with side-band-64k support."""
  108. # side-band-64k in git-receive-pack was introduced in git 1.7.0.2
  109. min_git_version = (1, 7, 0, 2)
  110. def setUp(self):
  111. self.o_uph_cap = patch_capabilities(UploadPackHandler, (b"no-done",))
  112. self.o_rph_cap = patch_capabilities(ReceivePackHandler, (b"no-done",))
  113. super(SmartWebSideBand64kTestCase, self).setUp()
  114. def tearDown(self):
  115. super(SmartWebSideBand64kTestCase, self).tearDown()
  116. UploadPackHandler.capabilities = self.o_uph_cap
  117. ReceivePackHandler.capabilities = self.o_rph_cap
  118. def _handlers(self):
  119. return None # default handlers include side-band-64k
  120. def _check_app(self, app):
  121. receive_pack_handler_cls = app.handlers[b"git-receive-pack"]
  122. caps = receive_pack_handler_cls.capabilities()
  123. self.assertIn(b"side-band-64k", caps)
  124. self.assertNotIn(b"no-done", caps)
  125. class SmartWebSideBand64kNoDoneTestCase(SmartWebTestCase):
  126. """Test cases for smart HTTP server with side-band-64k and no-done
  127. support.
  128. """
  129. # no-done was introduced in git 1.7.4
  130. min_git_version = (1, 7, 4)
  131. def _handlers(self):
  132. return None # default handlers include side-band-64k
  133. def _check_app(self, app):
  134. receive_pack_handler_cls = app.handlers[b"git-receive-pack"]
  135. caps = receive_pack_handler_cls.capabilities()
  136. self.assertIn(b"side-band-64k", caps)
  137. self.assertIn(b"no-done", caps)
  138. @skipIf(sys.platform == "win32", "Broken on windows, with very long fail time.")
  139. class DumbWebTestCase(WebTests, CompatTestCase):
  140. """Test cases for dumb HTTP server."""
  141. def _make_app(self, backend):
  142. return make_wsgi_chain(backend, dumb=True)
  143. def test_push_to_dulwich(self):
  144. # Note: remove this if dulwich implements dumb web pushing.
  145. raise SkipTest("Dumb web pushing not supported.")
  146. def test_push_to_dulwich_remove_branch(self):
  147. # Note: remove this if dumb pushing is supported
  148. raise SkipTest("Dumb web pushing not supported.")
  149. def test_new_shallow_clone_from_dulwich(self):
  150. # Note: remove this if C git and dulwich implement dumb web shallow
  151. # clones.
  152. raise SkipTest("Dumb web shallow cloning not supported.")
  153. def test_shallow_clone_from_git_is_identical(self):
  154. # Note: remove this if C git and dulwich implement dumb web shallow
  155. # clones.
  156. raise SkipTest("Dumb web shallow cloning not supported.")
  157. def test_fetch_same_depth_into_shallow_clone_from_dulwich(self):
  158. # Note: remove this if C git and dulwich implement dumb web shallow
  159. # clones.
  160. raise SkipTest("Dumb web shallow cloning not supported.")
  161. def test_fetch_full_depth_into_shallow_clone_from_dulwich(self):
  162. # Note: remove this if C git and dulwich implement dumb web shallow
  163. # clones.
  164. raise SkipTest("Dumb web shallow cloning not supported.")
  165. def test_push_to_dulwich_issue_88_standard(self):
  166. raise SkipTest("Dumb web pushing not supported.")