test_aiohttp.py 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218
  1. # test_aiohttp.py -- Compatibility tests for the aiohttp HTTP server.
  2. # Copyright (C) 2025 Jelmer Vernooij <jelmer@jelmer.uk>
  3. #
  4. # SPDX-License-Identifier: Apache-2.0 OR GPL-2.0-or-later
  5. # Dulwich is dual-licensed under the Apache License, Version 2.0 and the GNU
  6. # General Public License as published by the Free Software Foundation; version 2.0
  7. # or (at your option) any later version. You can redistribute it and/or
  8. # modify it under the terms of either of these two licenses.
  9. #
  10. # Unless required by applicable law or agreed to in writing, software
  11. # distributed under the License is distributed on an "AS IS" BASIS,
  12. # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. # See the License for the specific language governing permissions and
  14. # limitations under the License.
  15. #
  16. # You should have received a copy of the licenses; if not, see
  17. # <http://www.gnu.org/licenses/> for a copy of the GNU General Public License
  18. # and <http://www.apache.org/licenses/LICENSE-2.0> for a copy of the Apache
  19. # License, Version 2.0.
  20. #
  21. """Compatibility tests between Dulwich and the cgit HTTP client using aiohttp server.
  22. warning: these tests should be fairly stable, but when writing/debugging new
  23. tests, deadlocks may freeze the test process such that it cannot be
  24. Ctrl-C'ed. On POSIX systems, you can kill the tests with Ctrl-Z, "kill %".
  25. """
  26. import asyncio
  27. import sys
  28. import threading
  29. from typing import NoReturn
  30. try:
  31. from aiohttp import web
  32. aiohttp_missing = False
  33. except ImportError:
  34. web = None # type: ignore
  35. aiohttp_missing = True
  36. from dulwich.server import ReceivePackHandler
  37. from .. import SkipTest, skipIf
  38. from .server_utils import NoSideBand64kReceivePackHandler, ServerTests
  39. from .utils import CompatTestCase
  40. if not aiohttp_missing:
  41. from dulwich.aiohttp.server import create_repo_app
  42. @skipIf(aiohttp_missing, "aiohttp not available")
  43. @skipIf(sys.platform == "win32", "Broken on windows, with very long fail time.")
  44. class AiohttpServerTests(ServerTests):
  45. """Base tests for aiohttp server tests.
  46. Contains utility and setUp/tearDown methods, but does not inherit from
  47. TestCase so tests are not automatically run.
  48. """
  49. protocol = "http"
  50. def _start_server(self, repo):
  51. app = self._make_app(repo)
  52. runner = web.AppRunner(app)
  53. loop = asyncio.new_event_loop()
  54. async def start():
  55. await runner.setup()
  56. site = web.TCPSite(runner, "localhost", 0)
  57. await site.start()
  58. return site
  59. site = loop.run_until_complete(start())
  60. # Get the actual port
  61. port = site._server.sockets[0].getsockname()[1]
  62. # Run the event loop in a separate thread
  63. def run_loop():
  64. asyncio.set_event_loop(loop)
  65. loop.run_forever()
  66. thread = threading.Thread(target=run_loop, daemon=True)
  67. thread.start()
  68. # Cleanup function
  69. def cleanup():
  70. async def stop():
  71. await site.stop()
  72. await runner.cleanup()
  73. future = asyncio.run_coroutine_threadsafe(stop(), loop)
  74. future.result(timeout=5)
  75. loop.call_soon_threadsafe(loop.stop)
  76. thread.join()
  77. loop.close()
  78. self.addCleanup(cleanup)
  79. self._server = runner
  80. return port
  81. @skipIf(aiohttp_missing, "aiohttp not available")
  82. @skipIf(sys.platform == "win32", "Broken on windows, with very long fail time.")
  83. class SmartAiohttpTestCase(AiohttpServerTests, CompatTestCase):
  84. """Test cases for smart HTTP server using aiohttp.
  85. This server test case does not use side-band-64k in git-receive-pack.
  86. """
  87. min_git_version: tuple[int, ...] = (1, 6, 6)
  88. def _handlers(self):
  89. return {b"git-receive-pack": NoSideBand64kReceivePackHandler}
  90. def _make_app(self, repo):
  91. from dulwich.server import DEFAULT_HANDLERS
  92. handlers = dict(DEFAULT_HANDLERS)
  93. handlers.update(self._handlers())
  94. return create_repo_app(repo, handlers=handlers)
  95. def patch_capabilities(handler, caps_removed):
  96. # Patch a handler's capabilities by specifying a list of them to be
  97. # removed, and return the original method for restoration.
  98. original_capabilities = handler.capabilities
  99. def capabilities(self):
  100. # Call original to get base capabilities (including object-format)
  101. base_caps = original_capabilities(self)
  102. # Filter out the capabilities we want to remove
  103. return [i for i in base_caps if i not in caps_removed]
  104. handler.capabilities = capabilities
  105. return original_capabilities
  106. @skipIf(aiohttp_missing, "aiohttp not available")
  107. @skipIf(sys.platform == "win32", "Broken on windows, with very long fail time.")
  108. class SmartAiohttpSideBand64kTestCase(SmartAiohttpTestCase):
  109. """Test cases for smart HTTP server with side-band-64k support using aiohttp."""
  110. # side-band-64k in git-receive-pack was introduced in git 1.7.0.2
  111. min_git_version = (1, 7, 0, 2)
  112. def setUp(self) -> None:
  113. from dulwich.server import UploadPackHandler
  114. self.o_uph_cap = patch_capabilities(UploadPackHandler, (b"no-done",))
  115. self.o_rph_cap = patch_capabilities(ReceivePackHandler, (b"no-done",))
  116. super().setUp()
  117. def tearDown(self) -> None:
  118. from dulwich.server import UploadPackHandler
  119. super().tearDown()
  120. UploadPackHandler.capabilities = self.o_uph_cap
  121. ReceivePackHandler.capabilities = self.o_rph_cap
  122. def _make_app(self, repo):
  123. return create_repo_app(repo)
  124. @skipIf(aiohttp_missing, "aiohttp not available")
  125. @skipIf(sys.platform == "win32", "Broken on windows, with very long fail time.")
  126. class SmartAiohttpSideBand64kNoDoneTestCase(SmartAiohttpTestCase):
  127. """Test cases for smart HTTP server with side-band-64k and no-done
  128. support using aiohttp.
  129. """
  130. # no-done was introduced in git 1.7.4
  131. min_git_version = (1, 7, 4)
  132. def _make_app(self, repo):
  133. return create_repo_app(repo)
  134. @skipIf(aiohttp_missing, "aiohttp not available")
  135. @skipIf(sys.platform == "win32", "Broken on windows, with very long fail time.")
  136. class DumbAiohttpTestCase(AiohttpServerTests, CompatTestCase):
  137. """Test cases for dumb HTTP server using aiohttp."""
  138. def _make_app(self, repo):
  139. return create_repo_app(repo, dumb=True)
  140. def test_push_to_dulwich(self) -> NoReturn:
  141. # Note: remove this if dulwich implements dumb web pushing.
  142. raise SkipTest("Dumb web pushing not supported.")
  143. def test_push_to_dulwich_remove_branch(self) -> NoReturn:
  144. # Note: remove this if dumb pushing is supported
  145. raise SkipTest("Dumb web pushing not supported.")
  146. def test_new_shallow_clone_from_dulwich(self) -> NoReturn:
  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_shallow_clone_from_git_is_identical(self) -> NoReturn:
  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_same_depth_into_shallow_clone_from_dulwich(self) -> NoReturn:
  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_fetch_full_depth_into_shallow_clone_from_dulwich(self) -> NoReturn:
  159. # Note: remove this if C git and dulwich implement dumb web shallow
  160. # clones.
  161. raise SkipTest("Dumb web shallow cloning not supported.")
  162. def test_push_to_dulwich_issue_88_standard(self) -> NoReturn:
  163. raise SkipTest("Dumb web pushing not supported.")