test_web.py 7.0 KB

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