server_utils.py 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368
  1. # server_utils.py -- Git server compatibility utilities
  2. # Copyright (C) 2010 Google, Inc.
  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. """Utilities for testing git server compatibility."""
  22. import errno
  23. import os
  24. import shutil
  25. import socket
  26. import tempfile
  27. from dulwich.objects import hex_to_sha
  28. from dulwich.protocol import CAPABILITY_SIDE_BAND_64K
  29. from dulwich.repo import Repo
  30. from dulwich.server import ReceivePackHandler
  31. from dulwich.tests.utils import tear_down_repo
  32. from .utils import require_git_version, run_git_or_fail
  33. class _StubRepo:
  34. """A stub repo that just contains a path to tear down."""
  35. def __init__(self, name) -> None:
  36. temp_dir = tempfile.mkdtemp()
  37. self.path = os.path.join(temp_dir, name)
  38. os.mkdir(self.path)
  39. def close(self) -> None:
  40. pass
  41. def _get_shallow(repo):
  42. shallow_file = repo.get_named_file("shallow")
  43. if not shallow_file:
  44. return []
  45. shallows = []
  46. with shallow_file:
  47. for line in shallow_file:
  48. sha = line.strip()
  49. if not sha:
  50. continue
  51. hex_to_sha(sha)
  52. shallows.append(sha)
  53. return shallows
  54. class ServerTests:
  55. """Base tests for testing servers.
  56. Does not inherit from TestCase so tests are not automatically run.
  57. """
  58. min_single_branch_version = (
  59. 1,
  60. 7,
  61. 10,
  62. )
  63. def import_repos(self) -> None:
  64. self._old_repo = self.import_repo("server_old.export")
  65. self._new_repo = self.import_repo("server_new.export")
  66. def url(self, port) -> str:
  67. return f"{self.protocol}://localhost:{port}/"
  68. def branch_args(self, branches=None):
  69. if branches is None:
  70. branches = ["master", "branch"]
  71. return [f"{b}:{b}" for b in branches]
  72. def test_push_to_dulwich(self) -> None:
  73. self.import_repos()
  74. self.assertReposNotEqual(self._old_repo, self._new_repo)
  75. port = self._start_server(self._old_repo)
  76. run_git_or_fail(
  77. ["push", self.url(port), *self.branch_args()],
  78. cwd=self._new_repo.path,
  79. )
  80. self.assertReposEqual(self._old_repo, self._new_repo)
  81. def test_push_to_dulwich_no_op(self) -> None:
  82. self._old_repo = self.import_repo("server_old.export")
  83. self._new_repo = self.import_repo("server_old.export")
  84. self.assertReposEqual(self._old_repo, self._new_repo)
  85. port = self._start_server(self._old_repo)
  86. run_git_or_fail(
  87. ["push", self.url(port), *self.branch_args()],
  88. cwd=self._new_repo.path,
  89. )
  90. self.assertReposEqual(self._old_repo, self._new_repo)
  91. def test_push_to_dulwich_remove_branch(self) -> None:
  92. self._old_repo = self.import_repo("server_old.export")
  93. self._new_repo = self.import_repo("server_old.export")
  94. self.assertReposEqual(self._old_repo, self._new_repo)
  95. port = self._start_server(self._old_repo)
  96. run_git_or_fail(["push", self.url(port), ":master"], cwd=self._new_repo.path)
  97. self.assertEqual(list(self._old_repo.get_refs().keys()), [b"refs/heads/branch"])
  98. def test_fetch_from_dulwich(self) -> None:
  99. self.import_repos()
  100. self.assertReposNotEqual(self._old_repo, self._new_repo)
  101. port = self._start_server(self._new_repo)
  102. run_git_or_fail(
  103. ["fetch", self.url(port), *self.branch_args()],
  104. cwd=self._old_repo.path,
  105. )
  106. # flush the pack cache so any new packs are picked up
  107. self._old_repo.object_store._pack_cache_time = 0
  108. self.assertReposEqual(self._old_repo, self._new_repo)
  109. def test_fetch_from_dulwich_no_op(self) -> None:
  110. self._old_repo = self.import_repo("server_old.export")
  111. self._new_repo = self.import_repo("server_old.export")
  112. self.assertReposEqual(self._old_repo, self._new_repo)
  113. port = self._start_server(self._new_repo)
  114. run_git_or_fail(
  115. ["fetch", self.url(port), *self.branch_args()],
  116. cwd=self._old_repo.path,
  117. )
  118. # flush the pack cache so any new packs are picked up
  119. self._old_repo.object_store._pack_cache_time = 0
  120. self.assertReposEqual(self._old_repo, self._new_repo)
  121. def test_clone_from_dulwich_empty(self) -> None:
  122. old_repo_dir = tempfile.mkdtemp()
  123. self.addCleanup(shutil.rmtree, old_repo_dir)
  124. self._old_repo = Repo.init_bare(old_repo_dir)
  125. port = self._start_server(self._old_repo)
  126. new_repo_base_dir = tempfile.mkdtemp()
  127. self.addCleanup(shutil.rmtree, new_repo_base_dir)
  128. new_repo_dir = os.path.join(new_repo_base_dir, "empty_new")
  129. run_git_or_fail(["clone", self.url(port), new_repo_dir], cwd=new_repo_base_dir)
  130. new_repo = Repo(new_repo_dir)
  131. self.addCleanup(new_repo.close)
  132. self.assertReposEqual(self._old_repo, new_repo)
  133. def test_lsremote_from_dulwich(self) -> None:
  134. self._repo = self.import_repo("server_old.export")
  135. port = self._start_server(self._repo)
  136. o = run_git_or_fail(["ls-remote", self.url(port)])
  137. self.assertEqual(len(o.split(b"\n")), 4)
  138. def test_new_shallow_clone_from_dulwich(self) -> None:
  139. require_git_version(self.min_single_branch_version)
  140. self._source_repo = self.import_repo("server_new.export")
  141. self._stub_repo = _StubRepo("shallow")
  142. self.addCleanup(tear_down_repo, self._stub_repo)
  143. port = self._start_server(self._source_repo)
  144. # Fetch at depth 1
  145. run_git_or_fail(
  146. [
  147. "clone",
  148. "--mirror",
  149. "--depth=1",
  150. "--no-single-branch",
  151. self.url(port),
  152. self._stub_repo.path,
  153. ]
  154. )
  155. self._stub_repo.close()
  156. clone = self._stub_repo = Repo(self._stub_repo.path)
  157. self.addCleanup(clone.close)
  158. expected_shallow = [
  159. b"35e0b59e187dd72a0af294aedffc213eaa4d03ff",
  160. b"514dc6d3fbfe77361bcaef320c4d21b72bc10be9",
  161. ]
  162. self.assertEqual(expected_shallow, _get_shallow(clone))
  163. self.assertReposNotEqual(clone, self._source_repo)
  164. def test_shallow_clone_from_git_is_identical(self) -> None:
  165. require_git_version(self.min_single_branch_version)
  166. self._source_repo = self.import_repo("server_new.export")
  167. self._stub_repo_git = _StubRepo("shallow-git")
  168. self.addCleanup(tear_down_repo, self._stub_repo_git)
  169. self._stub_repo_dw = _StubRepo("shallow-dw")
  170. self.addCleanup(tear_down_repo, self._stub_repo_dw)
  171. # shallow clone using stock git, then using dulwich
  172. run_git_or_fail(
  173. [
  174. "clone",
  175. "--mirror",
  176. "--depth=1",
  177. "--no-single-branch",
  178. "file://" + self._source_repo.path,
  179. self._stub_repo_git.path,
  180. ]
  181. )
  182. port = self._start_server(self._source_repo)
  183. run_git_or_fail(
  184. [
  185. "clone",
  186. "--mirror",
  187. "--depth=1",
  188. "--no-single-branch",
  189. self.url(port),
  190. self._stub_repo_dw.path,
  191. ]
  192. )
  193. # compare the two clones; they should be equal
  194. self.assertReposEqual(
  195. Repo(self._stub_repo_git.path), Repo(self._stub_repo_dw.path)
  196. )
  197. def test_fetch_same_depth_into_shallow_clone_from_dulwich(self) -> None:
  198. require_git_version(self.min_single_branch_version)
  199. self._source_repo = self.import_repo("server_new.export")
  200. self._stub_repo = _StubRepo("shallow")
  201. self.addCleanup(tear_down_repo, self._stub_repo)
  202. port = self._start_server(self._source_repo)
  203. # Fetch at depth 2
  204. run_git_or_fail(
  205. [
  206. "clone",
  207. "--mirror",
  208. "--depth=2",
  209. "--no-single-branch",
  210. self.url(port),
  211. self._stub_repo.path,
  212. ]
  213. )
  214. self._stub_repo.close()
  215. clone = self._stub_repo = Repo(self._stub_repo.path)
  216. self.addCleanup(clone.close)
  217. # Fetching at the same depth is a no-op.
  218. run_git_or_fail(
  219. ["fetch", "--depth=2", self.url(port), *self.branch_args()],
  220. cwd=self._stub_repo.path,
  221. )
  222. expected_shallow = [
  223. b"94de09a530df27ac3bb613aaecdd539e0a0655e1",
  224. b"da5cd81e1883c62a25bb37c4d1f8ad965b29bf8d",
  225. ]
  226. self.assertEqual(expected_shallow, _get_shallow(clone))
  227. self.assertReposNotEqual(clone, self._source_repo)
  228. def test_fetch_full_depth_into_shallow_clone_from_dulwich(self) -> None:
  229. require_git_version(self.min_single_branch_version)
  230. self._source_repo = self.import_repo("server_new.export")
  231. self._stub_repo = _StubRepo("shallow")
  232. self.addCleanup(tear_down_repo, self._stub_repo)
  233. port = self._start_server(self._source_repo)
  234. # Fetch at depth 2
  235. run_git_or_fail(
  236. [
  237. "clone",
  238. "--mirror",
  239. "--depth=2",
  240. "--no-single-branch",
  241. self.url(port),
  242. self._stub_repo.path,
  243. ]
  244. )
  245. self._stub_repo.close()
  246. clone = self._stub_repo = Repo(self._stub_repo.path)
  247. self.addCleanup(clone.close)
  248. # Fetching at the same depth is a no-op.
  249. run_git_or_fail(
  250. ["fetch", "--depth=2", self.url(port), *self.branch_args()],
  251. cwd=self._stub_repo.path,
  252. )
  253. # The whole repo only has depth 4, so it should equal server_new.
  254. run_git_or_fail(
  255. ["fetch", "--depth=4", self.url(port), *self.branch_args()],
  256. cwd=self._stub_repo.path,
  257. )
  258. self.assertEqual([], _get_shallow(clone))
  259. self.assertReposEqual(clone, self._source_repo)
  260. def test_fetch_from_dulwich_issue_88_standard(self) -> None:
  261. # Basically an integration test to see that the ACK/NAK
  262. # generation works on repos with common head.
  263. self._source_repo = self.import_repo("issue88_expect_ack_nak_server.export")
  264. self._client_repo = self.import_repo("issue88_expect_ack_nak_client.export")
  265. port = self._start_server(self._source_repo)
  266. run_git_or_fail(["fetch", self.url(port), "master"], cwd=self._client_repo.path)
  267. self.assertObjectStoreEqual(
  268. self._source_repo.object_store, self._client_repo.object_store
  269. )
  270. def test_fetch_from_dulwich_issue_88_alternative(self) -> None:
  271. # likewise, but the case where the two repos have no common parent
  272. self._source_repo = self.import_repo("issue88_expect_ack_nak_other.export")
  273. self._client_repo = self.import_repo("issue88_expect_ack_nak_client.export")
  274. port = self._start_server(self._source_repo)
  275. self.assertRaises(
  276. KeyError,
  277. self._client_repo.get_object,
  278. b"02a14da1fc1fc13389bbf32f0af7d8899f2b2323",
  279. )
  280. run_git_or_fail(["fetch", self.url(port), "master"], cwd=self._client_repo.path)
  281. self.assertEqual(
  282. b"commit",
  283. self._client_repo.get_object(
  284. b"02a14da1fc1fc13389bbf32f0af7d8899f2b2323"
  285. ).type_name,
  286. )
  287. def test_push_to_dulwich_issue_88_standard(self) -> None:
  288. # Same thing, but we reverse the role of the server/client
  289. # and do a push instead.
  290. self._source_repo = self.import_repo("issue88_expect_ack_nak_client.export")
  291. self._client_repo = self.import_repo("issue88_expect_ack_nak_server.export")
  292. port = self._start_server(self._source_repo)
  293. run_git_or_fail(["push", self.url(port), "master"], cwd=self._client_repo.path)
  294. self.assertReposEqual(self._source_repo, self._client_repo)
  295. # TODO(dborowitz): Come up with a better way of testing various permutations of
  296. # capabilities. The only reason it is the way it is now is that side-band-64k
  297. # was only recently introduced into git-receive-pack.
  298. class NoSideBand64kReceivePackHandler(ReceivePackHandler):
  299. """ReceivePackHandler that does not support side-band-64k."""
  300. @classmethod
  301. def capabilities(cls):
  302. return [
  303. c
  304. for c in ReceivePackHandler.capabilities()
  305. if c != CAPABILITY_SIDE_BAND_64K
  306. ]
  307. def ignore_error(error):
  308. """Check whether this error is safe to ignore."""
  309. (e_type, e_value, e_tb) = error
  310. return issubclass(e_type, socket.error) and e_value[0] in (
  311. errno.ECONNRESET,
  312. errno.EPIPE,
  313. )