server_utils.py 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365
  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. repo_git = Repo(self._stub_repo_git.path)
  195. self.addCleanup(repo_git.close)
  196. repo_dw = Repo(self._stub_repo_dw.path)
  197. self.addCleanup(repo_dw.close)
  198. self.assertReposEqual(repo_git, repo_dw)
  199. def test_fetch_same_depth_into_shallow_clone_from_dulwich(self) -> None:
  200. require_git_version(self.min_single_branch_version)
  201. self._source_repo = self.import_repo("server_new.export")
  202. self._stub_repo = _StubRepo("shallow")
  203. self.addCleanup(tear_down_repo, self._stub_repo)
  204. port = self._start_server(self._source_repo)
  205. # Fetch at depth 2
  206. run_git_or_fail(
  207. [
  208. "clone",
  209. "--mirror",
  210. "--depth=2",
  211. "--no-single-branch",
  212. self.url(port),
  213. self._stub_repo.path,
  214. ]
  215. )
  216. self._stub_repo.close()
  217. clone = self._stub_repo = Repo(self._stub_repo.path)
  218. self.addCleanup(clone.close)
  219. # Fetching at the same depth is a no-op.
  220. run_git_or_fail(
  221. ["fetch", "--depth=2", self.url(port), *self.branch_args()],
  222. cwd=self._stub_repo.path,
  223. )
  224. expected_shallow = [
  225. b"94de09a530df27ac3bb613aaecdd539e0a0655e1",
  226. b"da5cd81e1883c62a25bb37c4d1f8ad965b29bf8d",
  227. ]
  228. self.assertEqual(expected_shallow, _get_shallow(clone))
  229. self.assertReposNotEqual(clone, self._source_repo)
  230. def test_fetch_full_depth_into_shallow_clone_from_dulwich(self) -> None:
  231. require_git_version(self.min_single_branch_version)
  232. self._source_repo = self.import_repo("server_new.export")
  233. self._stub_repo = _StubRepo("shallow")
  234. self.addCleanup(tear_down_repo, self._stub_repo)
  235. port = self._start_server(self._source_repo)
  236. # Fetch at depth 2
  237. run_git_or_fail(
  238. [
  239. "clone",
  240. "--mirror",
  241. "--depth=2",
  242. "--no-single-branch",
  243. self.url(port),
  244. self._stub_repo.path,
  245. ]
  246. )
  247. self._stub_repo.close()
  248. clone = self._stub_repo = Repo(self._stub_repo.path)
  249. self.addCleanup(clone.close)
  250. # Fetching at the same depth is a no-op.
  251. run_git_or_fail(
  252. ["fetch", "--depth=2", self.url(port), *self.branch_args()],
  253. cwd=self._stub_repo.path,
  254. )
  255. # The whole repo only has depth 4, so it should equal server_new.
  256. run_git_or_fail(
  257. ["fetch", "--depth=4", self.url(port), *self.branch_args()],
  258. cwd=self._stub_repo.path,
  259. )
  260. self.assertEqual([], _get_shallow(clone))
  261. self.assertReposEqual(clone, self._source_repo)
  262. def test_fetch_from_dulwich_issue_88_standard(self) -> None:
  263. # Basically an integration test to see that the ACK/NAK
  264. # generation works on repos with common head.
  265. self._source_repo = self.import_repo("issue88_expect_ack_nak_server.export")
  266. self._client_repo = self.import_repo("issue88_expect_ack_nak_client.export")
  267. port = self._start_server(self._source_repo)
  268. run_git_or_fail(["fetch", self.url(port), "master"], cwd=self._client_repo.path)
  269. self.assertObjectStoreEqual(
  270. self._source_repo.object_store, self._client_repo.object_store
  271. )
  272. def test_fetch_from_dulwich_issue_88_alternative(self) -> None:
  273. # likewise, but the case where the two repos have no common parent
  274. self._source_repo = self.import_repo("issue88_expect_ack_nak_other.export")
  275. self._client_repo = self.import_repo("issue88_expect_ack_nak_client.export")
  276. port = self._start_server(self._source_repo)
  277. self.assertRaises(
  278. KeyError,
  279. self._client_repo.get_object,
  280. b"02a14da1fc1fc13389bbf32f0af7d8899f2b2323",
  281. )
  282. run_git_or_fail(["fetch", self.url(port), "master"], cwd=self._client_repo.path)
  283. self.assertEqual(
  284. b"commit",
  285. self._client_repo.get_object(
  286. b"02a14da1fc1fc13389bbf32f0af7d8899f2b2323"
  287. ).type_name,
  288. )
  289. def test_push_to_dulwich_issue_88_standard(self) -> None:
  290. # Same thing, but we reverse the role of the server/client
  291. # and do a push instead.
  292. self._source_repo = self.import_repo("issue88_expect_ack_nak_client.export")
  293. self._client_repo = self.import_repo("issue88_expect_ack_nak_server.export")
  294. port = self._start_server(self._source_repo)
  295. run_git_or_fail(["push", self.url(port), "master"], cwd=self._client_repo.path)
  296. self.assertReposEqual(self._source_repo, self._client_repo)
  297. # TODO(dborowitz): Come up with a better way of testing various permutations of
  298. # capabilities. The only reason it is the way it is now is that side-band-64k
  299. # was only recently introduced into git-receive-pack.
  300. class NoSideBand64kReceivePackHandler(ReceivePackHandler):
  301. """ReceivePackHandler that does not support side-band-64k."""
  302. def capabilities(self):
  303. return [c for c in super().capabilities() if c != CAPABILITY_SIDE_BAND_64K]
  304. def ignore_error(error):
  305. """Check whether this error is safe to ignore."""
  306. (e_type, e_value, _e_tb) = error
  307. return issubclass(e_type, socket.error) and e_value[0] in (
  308. errno.ECONNRESET,
  309. errno.EPIPE,
  310. )