test_web.py 7.0 KB

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