test_aiohttp.py 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216
  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 runner.cleanup()
  72. future = asyncio.run_coroutine_threadsafe(stop(), loop)
  73. future.result(timeout=5)
  74. loop.call_soon_threadsafe(loop.stop)
  75. thread.join(timeout=1.0)
  76. self.addCleanup(cleanup)
  77. self._server = runner
  78. return port
  79. @skipIf(aiohttp_missing, "aiohttp not available")
  80. @skipIf(sys.platform == "win32", "Broken on windows, with very long fail time.")
  81. class SmartAiohttpTestCase(AiohttpServerTests, CompatTestCase):
  82. """Test cases for smart HTTP server using aiohttp.
  83. This server test case does not use side-band-64k in git-receive-pack.
  84. """
  85. min_git_version: tuple[int, ...] = (1, 6, 6)
  86. def _handlers(self):
  87. return {b"git-receive-pack": NoSideBand64kReceivePackHandler}
  88. def _make_app(self, repo):
  89. from dulwich.server import DEFAULT_HANDLERS
  90. handlers = dict(DEFAULT_HANDLERS)
  91. handlers.update(self._handlers())
  92. return create_repo_app(repo, handlers=handlers)
  93. def patch_capabilities(handler, caps_removed):
  94. # Patch a handler's capabilities by specifying a list of them to be
  95. # removed, and return the original method for restoration.
  96. original_capabilities = handler.capabilities
  97. def capabilities(self):
  98. # Call original to get base capabilities (including object-format)
  99. base_caps = original_capabilities(self)
  100. # Filter out the capabilities we want to remove
  101. return [i for i in base_caps if i not in caps_removed]
  102. handler.capabilities = capabilities
  103. return original_capabilities
  104. @skipIf(aiohttp_missing, "aiohttp not available")
  105. @skipIf(sys.platform == "win32", "Broken on windows, with very long fail time.")
  106. class SmartAiohttpSideBand64kTestCase(SmartAiohttpTestCase):
  107. """Test cases for smart HTTP server with side-band-64k support using aiohttp."""
  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) -> None:
  111. from dulwich.server import UploadPackHandler
  112. self.o_uph_cap = patch_capabilities(UploadPackHandler, (b"no-done",))
  113. self.o_rph_cap = patch_capabilities(ReceivePackHandler, (b"no-done",))
  114. super().setUp()
  115. def tearDown(self) -> None:
  116. from dulwich.server import UploadPackHandler
  117. super().tearDown()
  118. UploadPackHandler.capabilities = self.o_uph_cap
  119. ReceivePackHandler.capabilities = self.o_rph_cap
  120. def _make_app(self, repo):
  121. return create_repo_app(repo)
  122. @skipIf(aiohttp_missing, "aiohttp not available")
  123. @skipIf(sys.platform == "win32", "Broken on windows, with very long fail time.")
  124. class SmartAiohttpSideBand64kNoDoneTestCase(SmartAiohttpTestCase):
  125. """Test cases for smart HTTP server with side-band-64k and no-done
  126. support using aiohttp.
  127. """
  128. # no-done was introduced in git 1.7.4
  129. min_git_version = (1, 7, 4)
  130. def _make_app(self, repo):
  131. return create_repo_app(repo)
  132. @skipIf(aiohttp_missing, "aiohttp not available")
  133. @skipIf(sys.platform == "win32", "Broken on windows, with very long fail time.")
  134. class DumbAiohttpTestCase(AiohttpServerTests, CompatTestCase):
  135. """Test cases for dumb HTTP server using aiohttp."""
  136. def _make_app(self, repo):
  137. return create_repo_app(repo, dumb=True)
  138. def test_push_to_dulwich(self) -> NoReturn:
  139. # Note: remove this if dulwich implements dumb web pushing.
  140. raise SkipTest("Dumb web pushing not supported.")
  141. def test_push_to_dulwich_remove_branch(self) -> NoReturn:
  142. # Note: remove this if dumb pushing is supported
  143. raise SkipTest("Dumb web pushing not supported.")
  144. def test_new_shallow_clone_from_dulwich(self) -> NoReturn:
  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_shallow_clone_from_git_is_identical(self) -> NoReturn:
  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_fetch_same_depth_into_shallow_clone_from_dulwich(self) -> NoReturn:
  153. # Note: remove this if C git and dulwich implement dumb web shallow
  154. # clones.
  155. raise SkipTest("Dumb web shallow cloning not supported.")
  156. def test_fetch_full_depth_into_shallow_clone_from_dulwich(self) -> NoReturn:
  157. # Note: remove this if C git and dulwich implement dumb web shallow
  158. # clones.
  159. raise SkipTest("Dumb web shallow cloning not supported.")
  160. def test_push_to_dulwich_issue_88_standard(self) -> NoReturn:
  161. raise SkipTest("Dumb web pushing not supported.")